osSemaphore.c 13.0 KB
Newer Older
S
slguan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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/>.
 */

#define _DEFAULT_SOURCE
#include "os.h"
S
Shengliang Guan 已提交
18

wafwerar's avatar
wafwerar 已提交
19
#ifdef WINDOWS
S
Shengliang Guan 已提交
20 21 22 23 24 25 26

/*
 * windows implementation
 */

#include <windows.h>

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

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

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

int64_t taosGetSelfPthreadId() { return GetCurrentThreadId(); }

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

int32_t taosGetPId() { return GetCurrentProcessId(); }

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

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

  strcpy(name, end);
S
Shengliang Guan 已提交
59 60

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

  return 0;
}

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

wafwerar's avatar
wafwerar 已提交
75
int32_t tsem_timewait(tsem_t* sem, int64_t nanosecs) {
wafwerar's avatar
wafwerar 已提交
76 77 78 79 80 81 82 83
  struct timespec ts, rel;
  FILETIME ft_before, ft_after;
  int rc;

  rel.tv_sec = 0;
  rel.tv_nsec = nanosecs;

  GetSystemTimeAsFileTime(&ft_before);
wafwerar's avatar
wafwerar 已提交
84 85
  // errno = 0;
  rc = sem_timedwait(sem, pthread_win32_getabstime_np(&ts, &rel));
wafwerar's avatar
wafwerar 已提交
86 87

  /* This should have timed out */
wafwerar's avatar
wafwerar 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101
  // 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)
  //   {
  //     printf("nanoseconds: %d, rc: %d, errno: %d. before filetime: %d, %d; after filetime: %d, %d\n",
  //         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;
wafwerar's avatar
wafwerar 已提交
102 103
}

S
Shengliang Guan 已提交
104 105 106 107 108 109
#elif defined(_TD_DARWIN_64)

/*
 * darwin implementation
 */

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

112 113 114 115 116 117
// #define SEM_USE_PTHREAD
// #define SEM_USE_POSIX
#define SEM_USE_SEM

#ifdef SEM_USE_SEM
#include <mach/mach_error.h>
S
Shengliang Guan 已提交
118
#include <mach/mach_init.h>
119 120 121
#include <mach/semaphore.h>
#include <mach/task.h>

L
Liu Jicong 已提交
122
static TdThread     sem_thread;
wafwerar's avatar
wafwerar 已提交
123
static TdThreadOnce sem_once;
L
Liu Jicong 已提交
124 125 126
static task_t       sem_port;
static volatile int sem_inited = 0;
static semaphore_t  sem_exit;
127

S
Shengliang Guan 已提交
128
static void *sem_thread_routine(void *arg) {
129
  (void)arg;
130 131
  setThreadName("sem_thrd");

132 133 134
  sem_port = mach_task_self();
  kern_return_t ret = semaphore_create(sem_port, &sem_exit, SYNC_POLICY_FIFO, 0);
  if (ret != KERN_SUCCESS) {
wafwerar's avatar
wafwerar 已提交
135
    fprintf(stderr, "==%s[%d]%s()==failed to create sem_exit\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__);
136 137 138 139 140 141 142 143 144
    sem_inited = -1;
    return NULL;
  }
  sem_inited = 1;
  semaphore_wait(sem_exit);
  return NULL;
}

static void once_init(void) {
S
TD-4088  
Shengliang Guan 已提交
145
  int r = 0;
wafwerar's avatar
wafwerar 已提交
146
  r = taosThreadCreate(&sem_thread, NULL, sem_thread_routine, NULL);
147
  if (r) {
wafwerar's avatar
wafwerar 已提交
148
    fprintf(stderr, "==%s[%d]%s()==failed to create thread\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__);
149 150
    return;
  }
S
Shengliang Guan 已提交
151
  while (sem_inited == 0) {
152 153 154 155 156
    ;
  }
}
#endif

F
freemine 已提交
157
struct tsem_s {
158
#ifdef SEM_USE_PTHREAD
L
Liu Jicong 已提交
159 160
  TdThreadMutex    lock;
  TdThreadCond     cond;
S
Shengliang Guan 已提交
161
  volatile int64_t val;
162
#elif defined(SEM_USE_POSIX)
S
Shengliang Guan 已提交
163
  size_t        id;
L
Liu Jicong 已提交
164
  sem_t        *sem;
165
#elif defined(SEM_USE_SEM)
S
Shengliang Guan 已提交
166 167 168 169
  semaphore_t sem;
#else   // SEM_USE_PTHREAD
  dispatch_semaphore_t sem;
#endif  // SEM_USE_PTHREAD
170

S
Shengliang Guan 已提交
171
  volatile unsigned int valid : 1;
F
freemine 已提交
172 173
};

S
TD-4088  
Shengliang Guan 已提交
174
int tsem_init(tsem_t *sem, int pshared, unsigned int value) {
wafwerar's avatar
wafwerar 已提交
175
  // fprintf(stderr, "==%s[%d]%s():[%p]==creating\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem);
F
freemine 已提交
176
  if (*sem) {
L
Liu Jicong 已提交
177 178
    fprintf(stderr, "==%s[%d]%s():[%p]==already initialized\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__,
            sem);
F
freemine 已提交
179
    abort();
S
slguan 已提交
180
  }
wafwerar's avatar
wafwerar 已提交
181
  struct tsem_s *p = (struct tsem_s *)taosMemoryCalloc(1, sizeof(*p));
F
freemine 已提交
182
  if (!p) {
wafwerar's avatar
wafwerar 已提交
183
    fprintf(stderr, "==%s[%d]%s():[%p]==out of memory\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem);
F
freemine 已提交
184 185 186
    abort();
  }

187
#ifdef SEM_USE_PTHREAD
wafwerar's avatar
wafwerar 已提交
188
  int r = taosThreadMutexInit(&p->lock, NULL);
189 190
  do {
    if (r) break;
wafwerar's avatar
wafwerar 已提交
191
    r = taosThreadCondInit(&p->cond, NULL);
192
    if (r) {
wafwerar's avatar
wafwerar 已提交
193
      taosThreadMutexDestroy(&p->lock);
194 195 196 197 198
      break;
    }
    p->val = value;
  } while (0);
  if (r) {
wafwerar's avatar
wafwerar 已提交
199
    fprintf(stderr, "==%s[%d]%s():[%p]==not created\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem);
200 201 202 203 204 205
    abort();
  }
#elif defined(SEM_USE_POSIX)
  static size_t tick = 0;
  do {
    size_t id = atomic_add_fetch_64(&tick, 1);
S
Shengliang Guan 已提交
206
    if (id == SEM_VALUE_MAX) {
207 208 209
      atomic_store_64(&tick, 0);
      id = 0;
    }
S
Shengliang Guan 已提交
210
    char name[NAME_MAX - 4];
211
    snprintf(name, sizeof(name), "/t%ld", id);
S
Shengliang Guan 已提交
212 213 214
    p->sem = sem_open(name, O_CREAT | O_EXCL, pshared, value);
    p->id = id;
    if (p->sem != SEM_FAILED) break;
S
TD-4088  
Shengliang Guan 已提交
215
    int e = errno;
S
Shengliang Guan 已提交
216 217
    if (e == EEXIST) continue;
    if (e == EINTR) continue;
L
Liu Jicong 已提交
218 219
    fprintf(stderr, "==%s[%d]%s():[%p]==not created[%d]%s\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem,
            e, strerror(e));
220
    abort();
S
Shengliang Guan 已提交
221
  } while (p->sem == SEM_FAILED);
222
#elif defined(SEM_USE_SEM)
wafwerar's avatar
wafwerar 已提交
223
  taosThreadOnce(&sem_once, once_init);
S
Shengliang Guan 已提交
224
  if (sem_inited != 1) {
L
Liu Jicong 已提交
225 226
    fprintf(stderr, "==%s[%d]%s():[%p]==internal resource init failed\n", taosDirEntryBaseName(__FILE__), __LINE__,
            __func__, sem);
227 228 229
    errno = ENOMEM;
    return -1;
  }
F
freemine 已提交
230
  kern_return_t ret = semaphore_create(sem_port, &p->sem, SYNC_POLICY_FIFO, value);
231
  if (ret != KERN_SUCCESS) {
L
Liu Jicong 已提交
232 233
    fprintf(stderr, "==%s[%d]%s():[%p]==semophore_create failed\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__,
            sem);
234 235 236
    // we fail-fast here, because we have less-doc about semaphore_create for the moment
    abort();
  }
S
Shengliang Guan 已提交
237
#else   // SEM_USE_PTHREAD
F
freemine 已提交
238 239
  p->sem = dispatch_semaphore_create(value);
  if (p->sem == NULL) {
wafwerar's avatar
wafwerar 已提交
240
    fprintf(stderr, "==%s[%d]%s():[%p]==not created\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem);
F
freemine 已提交
241 242
    abort();
  }
S
Shengliang Guan 已提交
243
#endif  // SEM_USE_PTHREAD
244

F
freemine 已提交
245 246 247
  p->valid = 1;

  *sem = p;
S
slguan 已提交
248 249 250 251

  return 0;
}

S
TD-4088  
Shengliang Guan 已提交
252
int tsem_wait(tsem_t *sem) {
F
freemine 已提交
253
  if (!*sem) {
wafwerar's avatar
wafwerar 已提交
254
    fprintf(stderr, "==%s[%d]%s():[%p]==not initialized\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem);
F
freemine 已提交
255 256 257 258
    abort();
  }
  struct tsem_s *p = *sem;
  if (!p->valid) {
wafwerar's avatar
wafwerar 已提交
259
    fprintf(stderr, "==%s[%d]%s():[%p]==already destroyed\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem);
F
freemine 已提交
260 261
    abort();
  }
262
#ifdef SEM_USE_PTHREAD
wafwerar's avatar
wafwerar 已提交
263
  if (taosThreadMutexLock(&p->lock)) {
L
Liu Jicong 已提交
264 265
    fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__,
            sem);
266 267 268 269
    abort();
  }
  p->val -= 1;
  if (p->val < 0) {
wafwerar's avatar
wafwerar 已提交
270
    if (taosThreadCondWait(&p->cond, &p->lock)) {
L
Liu Jicong 已提交
271 272
      fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__,
              sem);
273 274 275
      abort();
    }
  }
wafwerar's avatar
wafwerar 已提交
276
  if (taosThreadMutexUnlock(&p->lock)) {
L
Liu Jicong 已提交
277 278
    fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__,
            sem);
279 280 281 282 283 284 285
    abort();
  }
  return 0;
#elif defined(SEM_USE_POSIX)
  return sem_wait(p->sem);
#elif defined(SEM_USE_SEM)
  return semaphore_wait(p->sem);
S
Shengliang Guan 已提交
286
#else   // SEM_USE_PTHREAD
F
freemine 已提交
287
  return dispatch_semaphore_wait(p->sem, DISPATCH_TIME_FOREVER);
S
Shengliang Guan 已提交
288
#endif  // SEM_USE_PTHREAD
S
slguan 已提交
289 290
}

S
TD-4088  
Shengliang Guan 已提交
291
int tsem_post(tsem_t *sem) {
F
freemine 已提交
292
  if (!*sem) {
wafwerar's avatar
wafwerar 已提交
293
    fprintf(stderr, "==%s[%d]%s():[%p]==not initialized\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem);
F
freemine 已提交
294 295 296 297
    abort();
  }
  struct tsem_s *p = *sem;
  if (!p->valid) {
wafwerar's avatar
wafwerar 已提交
298
    fprintf(stderr, "==%s[%d]%s():[%p]==already destroyed\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem);
F
freemine 已提交
299 300
    abort();
  }
301
#ifdef SEM_USE_PTHREAD
wafwerar's avatar
wafwerar 已提交
302
  if (taosThreadMutexLock(&p->lock)) {
L
Liu Jicong 已提交
303 304
    fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__,
            sem);
305 306 307 308
    abort();
  }
  p->val += 1;
  if (p->val <= 0) {
wafwerar's avatar
wafwerar 已提交
309
    if (taosThreadCondSignal(&p->cond)) {
L
Liu Jicong 已提交
310 311
      fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__,
              sem);
312 313 314
      abort();
    }
  }
wafwerar's avatar
wafwerar 已提交
315
  if (taosThreadMutexUnlock(&p->lock)) {
L
Liu Jicong 已提交
316 317
    fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__,
            sem);
318 319 320 321 322 323 324
    abort();
  }
  return 0;
#elif defined(SEM_USE_POSIX)
  return sem_post(p->sem);
#elif defined(SEM_USE_SEM)
  return semaphore_signal(p->sem);
S
Shengliang Guan 已提交
325
#else   // SEM_USE_PTHREAD
F
freemine 已提交
326
  return dispatch_semaphore_signal(p->sem);
S
Shengliang Guan 已提交
327
#endif  // SEM_USE_PTHREAD
F
freemine 已提交
328 329
}

S
TD-4088  
Shengliang Guan 已提交
330
int tsem_destroy(tsem_t *sem) {
wafwerar's avatar
wafwerar 已提交
331
  // fprintf(stderr, "==%s[%d]%s():[%p]==destroying\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem);
F
freemine 已提交
332
  if (!*sem) {
wafwerar's avatar
wafwerar 已提交
333
    // fprintf(stderr, "==%s[%d]%s():[%p]==not initialized\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem);
334
    // abort();
F
freemine 已提交
335 336 337 338
    return 0;
  }
  struct tsem_s *p = *sem;
  if (!p->valid) {
L
Liu Jicong 已提交
339 340
    // fprintf(stderr, "==%s[%d]%s():[%p]==already destroyed\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__,
    // sem); abort();
341 342 343
    return 0;
  }
#ifdef SEM_USE_PTHREAD
wafwerar's avatar
wafwerar 已提交
344
  if (taosThreadMutexLock(&p->lock)) {
L
Liu Jicong 已提交
345 346
    fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__,
            sem);
347 348 349
    abort();
  }
  p->valid = 0;
wafwerar's avatar
wafwerar 已提交
350
  if (taosThreadCondDestroy(&p->cond)) {
L
Liu Jicong 已提交
351 352
    fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__,
            sem);
353 354
    abort();
  }
wafwerar's avatar
wafwerar 已提交
355
  if (taosThreadMutexUnlock(&p->lock)) {
L
Liu Jicong 已提交
356 357
    fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__,
            sem);
358 359
    abort();
  }
wafwerar's avatar
wafwerar 已提交
360
  if (taosThreadMutexDestroy(&p->lock)) {
L
Liu Jicong 已提交
361 362
    fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__,
            sem);
363 364 365
    abort();
  }
#elif defined(SEM_USE_POSIX)
S
Shengliang Guan 已提交
366
  char name[NAME_MAX - 4];
367
  snprintf(name, sizeof(name), "/t%ld", p->id);
S
TD-4088  
Shengliang Guan 已提交
368
  int r = sem_unlink(name);
369
  if (r) {
S
TD-4088  
Shengliang Guan 已提交
370
    int e = errno;
L
Liu Jicong 已提交
371 372
    fprintf(stderr, "==%s[%d]%s():[%p]==unlink failed[%d]%s\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem,
            e, strerror(e));
F
freemine 已提交
373 374
    abort();
  }
375 376
#elif defined(SEM_USE_SEM)
  semaphore_destroy(sem_port, p->sem);
S
Shengliang Guan 已提交
377 378
#else   // SEM_USE_PTHREAD
#endif  // SEM_USE_PTHREAD
F
freemine 已提交
379 380

  p->valid = 0;
wafwerar's avatar
wafwerar 已提交
381
  taosMemoryFree(p);
F
freemine 已提交
382 383

  *sem = NULL;
S
slguan 已提交
384 385
  return 0;
}
F
freemine 已提交
386

wafwerar's avatar
wafwerar 已提交
387
bool taosCheckPthreadValid(TdThread thread) {
F
freemine 已提交
388
  uint64_t id = 0;
wafwerar's avatar
wafwerar 已提交
389
  int      r = TdThreadhreadid_np(thread, &id);
F
freemine 已提交
390 391 392 393
  return r ? false : true;
}

int64_t taosGetSelfPthreadId() {
394
  uint64_t id;
wafwerar's avatar
wafwerar 已提交
395
  TdThreadhreadid_np(0, &id);
S
Shengliang Guan 已提交
396
  return (int64_t)id;
F
freemine 已提交
397 398
}

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

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

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

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

407
int32_t taosGetAppName(char *name, int32_t *len) {
S
Shengliang Guan 已提交
408
  char buf[PATH_MAX + 1];
F
freemine 已提交
409
  buf[0] = '\0';
S
Shengliang Guan 已提交
410
  proc_name(getpid(), buf, sizeof(buf) - 1);
F
freemine 已提交
411 412 413 414 415 416 417
  buf[PATH_MAX] = '\0';
  size_t n = strlen(buf);
  if (len) *len = n;
  if (name) strcpy(name, buf);
  return 0;
}

S
Shengliang Guan 已提交
418 419 420 421 422 423 424
#else

/*
 * linux implementation
 */

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

wafwerar's avatar
wafwerar 已提交
427
bool taosCheckPthreadValid(TdThread thread) { return thread != 0; }
S
Shengliang Guan 已提交
428 429 430 431 432 433 434 435

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

wafwerar's avatar
wafwerar 已提交
436 437 438
int64_t taosGetPthreadId(TdThread thread) { return (int64_t)thread; }
void    taosResetPthread(TdThread* thread) { *thread = 0; }
bool    taosComparePthread(TdThread first, TdThread second) { return first == second; }
S
Shengliang Guan 已提交
439 440
int32_t taosGetPId() { return getpid(); }

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

S
Shengliang Guan 已提交
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
  if (readlink(self, path, PATH_MAX) <= 0) {
    return -1;
  }

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

  ++end;

  strcpy(name, end);

  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 已提交
474 475 476 477 478 479 480 481 482 483 484 485 486
int32_t tsem_timewait(tsem_t* sem, int64_t nanosecs) {
  int ret = 0;

  struct timespec tv = {
      .tv_sec = 0,
      .tv_nsec = nanosecs,
  };

  while ((ret = sem_timedwait(sem, &tv)) == -1 && errno == EINTR) continue;

  return ret;
}

S
Shengliang Guan 已提交
487
#endif