osSemaphore.c 5.8 KB
Newer Older
S
slguan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

wafwerar's avatar
wafwerar 已提交
16
#define ALLOW_FORBID_FUNC
S
slguan 已提交
17 18
#define _DEFAULT_SOURCE
#include "os.h"
wafwerar's avatar
wafwerar 已提交
19
#include "pthread.h"
L
Liu Jicong 已提交
20
#include "tdef.h"
S
Shengliang Guan 已提交
21

wafwerar's avatar
wafwerar 已提交
22
#ifdef WINDOWS
S
Shengliang Guan 已提交
23 24 25 26 27 28 29

/*
 * windows implementation
 */

#include <windows.h>

wafwerar's avatar
wafwerar 已提交
30
bool taosCheckPthreadValid(TdThread thread) { return thread.p != NULL; }
S
Shengliang Guan 已提交
31

wafwerar's avatar
wafwerar 已提交
32
void taosResetPthread(TdThread* thread) { thread->p = 0; }
S
Shengliang Guan 已提交
33

wafwerar's avatar
wafwerar 已提交
34
int64_t taosGetPthreadId(TdThread thread) {
S
Shengliang Guan 已提交
35 36 37 38 39 40 41 42 43
#ifdef PTW32_VERSION
  return pthread_getw32threadid_np(thread);
#else
  return (int64_t)thread;
#endif
}

int64_t taosGetSelfPthreadId() { return GetCurrentThreadId(); }

wafwerar's avatar
wafwerar 已提交
44
bool taosComparePthread(TdThread first, TdThread second) { return first.p == second.p; }
S
Shengliang Guan 已提交
45 46 47

int32_t taosGetPId() { return GetCurrentProcessId(); }

48
int32_t taosGetAppName(char* name, int32_t* len) {
S
Shengliang Guan 已提交
49 50 51 52 53 54 55
  char filepath[1024] = {0};

  GetModuleFileName(NULL, filepath, MAX_PATH);
  char* sub = strrchr(filepath, '.');
  if (sub != NULL) {
    *sub = '\0';
  }
wafwerar's avatar
wafwerar 已提交
56 57 58 59 60
  char* end = strrchr(filepath, TD_DIRSEP[0]);
  if (end == NULL) {
    end = filepath;
  }

wafwerar's avatar
wafwerar 已提交
61
  tstrncpy(name, end, TSDB_APP_NAME_LEN);
S
Shengliang Guan 已提交
62 63

  if (len != NULL) {
wafwerar's avatar
wafwerar 已提交
64
    *len = (int32_t)strlen(end);
S
Shengliang Guan 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77
  }

  return 0;
}

int32_t tsem_wait(tsem_t* sem) {
  int ret = 0;
  do {
    ret = sem_wait(sem);
  } while (ret != 0 && errno == EINTR);
  return ret;
}

L
Liu Jicong 已提交
78 79 80 81 82 83 84 85 86 87
int32_t tsem_timewait(tsem_t* sem, int64_t milis) {
  return tsem_wait(sem);
#if 0
  struct timespec ts;
  timespec_get(&ts);
  ts.tv_nsec += ms * 1000000;
  ts.tv_sec += ts.tv_nsec / 1000000000;
  ts.tv_nsec %= 1000000000;

  /*GetSystemTimeAsFileTime(&ft_before);*/
wafwerar's avatar
wafwerar 已提交
88
  // errno = 0;
L
Liu Jicong 已提交
89
  rc = sem_timedwait(sem, ts);
wafwerar's avatar
wafwerar 已提交
90 91

  /* This should have timed out */
wafwerar's avatar
wafwerar 已提交
92 93 94 95 96 97
  // assert(errno == ETIMEDOUT);
  // assert(rc != 0);
  // GetSystemTimeAsFileTime(&ft_after);
  // // We specified a non-zero wait. Time must advance.
  // if (ft_before.dwLowDateTime == ft_after.dwLowDateTime && ft_before.dwHighDateTime == ft_after.dwHighDateTime)
  //   {
S
Shengliang Guan 已提交
98
  //     printf("nanoseconds: %d, rc: %d, code:0x%x. before filetime: %d, %d; after filetime: %d, %d\n",
wafwerar's avatar
wafwerar 已提交
99 100 101 102 103 104 105
  //         nanosecs, rc, errno,
  //         (int)ft_before.dwLowDateTime, (int)ft_before.dwHighDateTime,
  //         (int)ft_after.dwLowDateTime, (int)ft_after.dwHighDateTime);
  //     printf("time must advance during sem_timedwait.");
  //     return 1;
  //   }
  return rc;
L
Liu Jicong 已提交
106
#endif
wafwerar's avatar
wafwerar 已提交
107 108
}

S
Shengliang Guan 已提交
109 110
#elif defined(_TD_DARWIN_64)

F
freemine 已提交
111 112
#include <libproc.h>

L
Liu Jicong 已提交
113
int tsem_init(tsem_t *psem, int flags, unsigned int count) {
wafwerar's avatar
wafwerar 已提交
114 115
  *psem = dispatch_semaphore_create(count);
  if (*psem == NULL) return -1;
L
Liu Jicong 已提交
116
  return 0;
117 118
}

L
Liu Jicong 已提交
119
int tsem_destroy(tsem_t *psem) {
wafwerar's avatar
wafwerar 已提交
120
  if (psem == NULL || *psem == NULL) return -1;
wafwerar's avatar
wafwerar 已提交
121 122
  // dispatch_release(*psem);
  // *psem = NULL;
L
Liu Jicong 已提交
123
  return 0;
124 125
}

L
Liu Jicong 已提交
126
int tsem_post(tsem_t *psem) {
wafwerar's avatar
wafwerar 已提交
127 128
  if (psem == NULL || *psem == NULL) return -1;
  dispatch_semaphore_signal(*psem);
L
Liu Jicong 已提交
129
  return 0;
wafwerar's avatar
wafwerar 已提交
130
}
F
freemine 已提交
131

L
Liu Jicong 已提交
132
int tsem_wait(tsem_t *psem) {
wafwerar's avatar
wafwerar 已提交
133 134
  if (psem == NULL || *psem == NULL) return -1;
  dispatch_semaphore_wait(*psem, DISPATCH_TIME_FOREVER);
L
Liu Jicong 已提交
135
  return 0;
S
slguan 已提交
136
}
F
freemine 已提交
137

L
Liu Jicong 已提交
138
int tsem_timewait(tsem_t *psem, int64_t milis) {
wafwerar's avatar
wafwerar 已提交
139
  if (psem == NULL || *psem == NULL) return -1;
L
Liu Jicong 已提交
140
  dispatch_semaphore_wait(*psem, milis * 1000 * 1000);
L
Liu Jicong 已提交
141
  return 0;
F
freemine 已提交
142 143
}

wafwerar's avatar
wafwerar 已提交
144
bool taosCheckPthreadValid(TdThread thread) { return thread != 0; }
wafwerar's avatar
wafwerar 已提交
145

F
freemine 已提交
146
int64_t taosGetSelfPthreadId() {
wafwerar's avatar
wafwerar 已提交
147 148
  TdThread thread = taosThreadSelf();
  return (int64_t)thread;
F
freemine 已提交
149 150
}

wafwerar's avatar
wafwerar 已提交
151
int64_t taosGetPthreadId(TdThread thread) { return (int64_t)thread; }
F
freemine 已提交
152

wafwerar's avatar
wafwerar 已提交
153
void taosResetPthread(TdThread *thread) { *thread = NULL; }
F
freemine 已提交
154

wafwerar's avatar
wafwerar 已提交
155
bool taosComparePthread(TdThread first, TdThread second) { return taosThreadEqual(first, second) ? true : false; }
F
freemine 已提交
156

S
Shengliang Guan 已提交
157
int32_t taosGetPId() { return (int32_t)getpid(); }
F
freemine 已提交
158

159
int32_t taosGetAppName(char *name, int32_t *len) {
S
Shengliang Guan 已提交
160
  char buf[PATH_MAX + 1];
F
freemine 已提交
161
  buf[0] = '\0';
S
Shengliang Guan 已提交
162
  proc_name(getpid(), buf, sizeof(buf) - 1);
F
freemine 已提交
163 164 165
  buf[PATH_MAX] = '\0';
  size_t n = strlen(buf);
  if (len) *len = n;
wafwerar's avatar
wafwerar 已提交
166
  if (name) tstrncpy(name, buf, TSDB_APP_NAME_LEN);
F
freemine 已提交
167 168 169
  return 0;
}

S
Shengliang Guan 已提交
170 171 172 173 174 175 176
#else

/*
 * linux implementation
 */

#include <sys/syscall.h>
S
Shengliang Guan 已提交
177
#include <unistd.h>
S
Shengliang Guan 已提交
178

wafwerar's avatar
wafwerar 已提交
179
bool taosCheckPthreadValid(TdThread thread) { return thread != 0; }
S
Shengliang Guan 已提交
180 181 182 183 184 185 186 187

int64_t taosGetSelfPthreadId() {
  static __thread int id = 0;
  if (id != 0) return id;
  id = syscall(SYS_gettid);
  return id;
}

wafwerar's avatar
wafwerar 已提交
188 189 190
int64_t taosGetPthreadId(TdThread thread) { return (int64_t)thread; }
void    taosResetPthread(TdThread* thread) { *thread = 0; }
bool    taosComparePthread(TdThread first, TdThread second) { return first == second; }
L
Liu Jicong 已提交
191 192

int32_t taosGetPId() {
L
Liu Jicong 已提交
193
  static int32_t pid;
L
Liu Jicong 已提交
194 195 196 197
  if (pid != 0) return pid;
  pid = getpid();
  return pid;
}
S
Shengliang Guan 已提交
198

199
int32_t taosGetAppName(char* name, int32_t* len) {
S
Shengliang Guan 已提交
200 201
  const char* self = "/proc/self/exe";
  char        path[PATH_MAX] = {0};
F
freemine 已提交
202

S
Shengliang Guan 已提交
203 204 205 206 207 208 209 210 211 212 213 214
  if (readlink(self, path, PATH_MAX) <= 0) {
    return -1;
  }

  path[PATH_MAX - 1] = 0;
  char* end = strrchr(path, '/');
  if (end == NULL) {
    return -1;
  }

  ++end;

wafwerar's avatar
wafwerar 已提交
215
  tstrncpy(name, end, TSDB_APP_NAME_LEN);
S
Shengliang Guan 已提交
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231

  if (len != NULL) {
    *len = strlen(name);
  }

  return 0;
}

int32_t tsem_wait(tsem_t* sem) {
  int ret = 0;
  do {
    ret = sem_wait(sem);
  } while (ret != 0 && errno == EINTR);
  return ret;
}

L
Liu Jicong 已提交
232
int32_t tsem_timewait(tsem_t* sem, int64_t ms) {
L
Liu Jicong 已提交
233 234
  int ret = 0;

L
Liu Jicong 已提交
235 236 237 238 239 240 241 242 243
  struct timespec ts = {0};

  if (clock_gettime(CLOCK_REALTIME, &ts) == -1) {
    return -1;
  }

  ts.tv_nsec += ms * 1000000;
  ts.tv_sec += ts.tv_nsec / 1000000000;
  ts.tv_nsec %= 1000000000;
L
Liu Jicong 已提交
244

L
Liu Jicong 已提交
245
  while ((ret = sem_timedwait(sem, &ts)) == -1 && errno == EINTR) continue;
L
Liu Jicong 已提交
246 247 248 249

  return ret;
}

S
Shengliang Guan 已提交
250
#endif