osSemaphore.c 14.7 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;
}

wafwerar's avatar
wafwerar 已提交
78
int32_t tsem_timewait(tsem_t* sem, int64_t nanosecs) {
wafwerar's avatar
wafwerar 已提交
79
  struct timespec ts, rel;
L
Liu Jicong 已提交
80 81
  FILETIME        ft_before, ft_after;
  int             rc;
wafwerar's avatar
wafwerar 已提交
82 83 84 85 86

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

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

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

S
Shengliang Guan 已提交
107 108 109 110 111 112
#elif defined(_TD_DARWIN_64)

/*
 * darwin implementation
 */

F
freemine 已提交
113 114
#include <libproc.h>

115 116
// #define SEM_USE_PTHREAD
// #define SEM_USE_POSIX
wafwerar's avatar
wafwerar 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
// #define SEM_USE_SEM

// #ifdef SEM_USE_SEM
// #include <mach/mach_error.h>
// #include <mach/mach_init.h>
// #include <mach/semaphore.h>
// #include <mach/task.h>

// static TdThread     sem_thread;
// static TdThreadOnce sem_once;
// static task_t       sem_port;
// static volatile int sem_inited = 0;
// static semaphore_t  sem_exit;

// static void *sem_thread_routine(void *arg) {
//   (void)arg;
//   setThreadName("sem_thrd");

//   sem_port = mach_task_self();
//   kern_return_t ret = semaphore_create(sem_port, &sem_exit, SYNC_POLICY_FIFO, 0);
//   if (ret != KERN_SUCCESS) {
//     fprintf(stderr, "==%s[%d]%s()==failed to create sem_exit\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__);
//     sem_inited = -1;
//     return NULL;
//   }
//   sem_inited = 1;
//   semaphore_wait(sem_exit);
//   return NULL;
// }

// static void once_init(void) {
//   int r = 0;
//   r = taosThreadCreate(&sem_thread, NULL, sem_thread_routine, NULL);
//   if (r) {
//     fprintf(stderr, "==%s[%d]%s()==failed to create thread\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__);
//     return;
//   }
//   while (sem_inited == 0) {
//     ;
//   }
// }
// #endif

// struct tsem_s {
// #ifdef SEM_USE_PTHREAD
//   TdThreadMutex    lock;
//   TdThreadCond     cond;
//   volatile int64_t val;
// #elif defined(SEM_USE_POSIX)
//   size_t        id;
//   sem_t        *sem;
// #elif defined(SEM_USE_SEM)
//   semaphore_t sem;
// #else   // SEM_USE_PTHREAD
//   dispatch_semaphore_t sem;
// #endif  // SEM_USE_PTHREAD

//   volatile unsigned int valid : 1;
// };

// int tsem_init(tsem_t *sem, int pshared, unsigned int value) {
//   // fprintf(stderr, "==%s[%d]%s():[%p]==creating\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem);
//   if (*sem) {
//     fprintf(stderr, "==%s[%d]%s():[%p]==already initialized\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__,
//             sem);
//     abort();
//   }
//   struct tsem_s *p = (struct tsem_s *)taosMemoryCalloc(1, sizeof(*p));
//   if (!p) {
//     fprintf(stderr, "==%s[%d]%s():[%p]==out of memory\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem);
//     abort();
//   }

// #ifdef SEM_USE_PTHREAD
//   int r = taosThreadMutexInit(&p->lock, NULL);
//   do {
//     if (r) break;
//     r = taosThreadCondInit(&p->cond, NULL);
//     if (r) {
//       taosThreadMutexDestroy(&p->lock);
//       break;
//     }
//     p->val = value;
//   } while (0);
//   if (r) {
//     fprintf(stderr, "==%s[%d]%s():[%p]==not created\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem);
//     abort();
//   }
// #elif defined(SEM_USE_POSIX)
//   static size_t tick = 0;
//   do {
//     size_t id = atomic_add_fetch_64(&tick, 1);
//     if (id == SEM_VALUE_MAX) {
//       atomic_store_64(&tick, 0);
//       id = 0;
//     }
//     char name[NAME_MAX - 4];
S
Shengliang Guan 已提交
214
//     snprintf(name, sizeof(name), "/t" PRId64, id);
wafwerar's avatar
wafwerar 已提交
215 216 217 218 219 220
//     p->sem = sem_open(name, O_CREAT | O_EXCL, pshared, value);
//     p->id = id;
//     if (p->sem != SEM_FAILED) break;
//     int e = errno;
//     if (e == EEXIST) continue;
//     if (e == EINTR) continue;
L
Liu Jicong 已提交
221 222
//     fprintf(stderr, "==%s[%d]%s():[%p]==not created[%d]%s\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__,
//     sem,
wafwerar's avatar
wafwerar 已提交
223 224 225 226 227 228 229 230 231 232 233 234 235
//             e, strerror(e));
//     abort();
//   } while (p->sem == SEM_FAILED);
// #elif defined(SEM_USE_SEM)
//   taosThreadOnce(&sem_once, once_init);
//   if (sem_inited != 1) {
//     fprintf(stderr, "==%s[%d]%s():[%p]==internal resource init failed\n", taosDirEntryBaseName(__FILE__), __LINE__,
//             __func__, sem);
//     errno = ENOMEM;
//     return -1;
//   }
//   kern_return_t ret = semaphore_create(sem_port, &p->sem, SYNC_POLICY_FIFO, value);
//   if (ret != KERN_SUCCESS) {
L
Liu Jicong 已提交
236 237
//     fprintf(stderr, "==%s[%d]%s():[%p]==semophore_create failed\n", taosDirEntryBaseName(__FILE__), __LINE__,
//     __func__,
wafwerar's avatar
wafwerar 已提交
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
//             sem);
//     // we fail-fast here, because we have less-doc about semaphore_create for the moment
//     abort();
//   }
// #else   // SEM_USE_PTHREAD
//   p->sem = dispatch_semaphore_create(value);
//   if (p->sem == NULL) {
//     fprintf(stderr, "==%s[%d]%s():[%p]==not created\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem);
//     abort();
//   }
// #endif  // SEM_USE_PTHREAD

//   p->valid = 1;

//   *sem = p;

//   return 0;
// }

// int tsem_wait(tsem_t *sem) {
//   if (!*sem) {
//     fprintf(stderr, "==%s[%d]%s():[%p]==not initialized\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem);
//     abort();
//   }
//   struct tsem_s *p = *sem;
//   if (!p->valid) {
L
Liu Jicong 已提交
264 265
//     fprintf(stderr, "==%s[%d]%s():[%p]==already destroyed\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__,
//     sem); abort();
wafwerar's avatar
wafwerar 已提交
266 267 268 269 270 271 272 273 274 275
//   }
// #ifdef SEM_USE_PTHREAD
//   if (taosThreadMutexLock(&p->lock)) {
//     fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__,
//             sem);
//     abort();
//   }
//   p->val -= 1;
//   if (p->val < 0) {
//     if (taosThreadCondWait(&p->cond, &p->lock)) {
L
Liu Jicong 已提交
276 277
//       fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__,
//       __func__,
wafwerar's avatar
wafwerar 已提交
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
//               sem);
//       abort();
//     }
//   }
//   if (taosThreadMutexUnlock(&p->lock)) {
//     fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__,
//             sem);
//     abort();
//   }
//   return 0;
// #elif defined(SEM_USE_POSIX)
//   return sem_wait(p->sem);
// #elif defined(SEM_USE_SEM)
//   return semaphore_wait(p->sem);
// #else   // SEM_USE_PTHREAD
//   return dispatch_semaphore_wait(p->sem, DISPATCH_TIME_FOREVER);
// #endif  // SEM_USE_PTHREAD
// }

// int tsem_post(tsem_t *sem) {
//   if (!*sem) {
//     fprintf(stderr, "==%s[%d]%s():[%p]==not initialized\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem);
//     abort();
//   }
//   struct tsem_s *p = *sem;
//   if (!p->valid) {
L
Liu Jicong 已提交
304 305
//     fprintf(stderr, "==%s[%d]%s():[%p]==already destroyed\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__,
//     sem); abort();
wafwerar's avatar
wafwerar 已提交
306 307 308 309 310 311 312 313 314 315
//   }
// #ifdef SEM_USE_PTHREAD
//   if (taosThreadMutexLock(&p->lock)) {
//     fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__,
//             sem);
//     abort();
//   }
//   p->val += 1;
//   if (p->val <= 0) {
//     if (taosThreadCondSignal(&p->cond)) {
L
Liu Jicong 已提交
316 317
//       fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__,
//       __func__,
wafwerar's avatar
wafwerar 已提交
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
//               sem);
//       abort();
//     }
//   }
//   if (taosThreadMutexUnlock(&p->lock)) {
//     fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__,
//             sem);
//     abort();
//   }
//   return 0;
// #elif defined(SEM_USE_POSIX)
//   return sem_post(p->sem);
// #elif defined(SEM_USE_SEM)
//   return semaphore_signal(p->sem);
// #else   // SEM_USE_PTHREAD
//   return dispatch_semaphore_signal(p->sem);
// #endif  // SEM_USE_PTHREAD
// }

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

//   p->valid = 0;
//   taosMemoryFree(p);

//   *sem = NULL;
//   return 0;
// }
L
Liu Jicong 已提交
395 396

int tsem_init(tsem_t *psem, int flags, unsigned int count) {
wafwerar's avatar
wafwerar 已提交
397 398
  *psem = dispatch_semaphore_create(count);
  if (*psem == NULL) return -1;
L
Liu Jicong 已提交
399
  return 0;
400 401
}

L
Liu Jicong 已提交
402 403
int tsem_destroy(tsem_t *psem) {
  return 0;
404 405
}

L
Liu Jicong 已提交
406
int tsem_post(tsem_t *psem) {
wafwerar's avatar
wafwerar 已提交
407 408
  if (psem == NULL || *psem == NULL) return -1;
  dispatch_semaphore_signal(*psem);
L
Liu Jicong 已提交
409
  return 0;
wafwerar's avatar
wafwerar 已提交
410
}
F
freemine 已提交
411

L
Liu Jicong 已提交
412
int tsem_wait(tsem_t *psem) {
wafwerar's avatar
wafwerar 已提交
413 414
  if (psem == NULL || *psem == NULL) return -1;
  dispatch_semaphore_wait(*psem, DISPATCH_TIME_FOREVER);
L
Liu Jicong 已提交
415
  return 0;
S
slguan 已提交
416
}
F
freemine 已提交
417

L
Liu Jicong 已提交
418
int tsem_timewait(tsem_t *psem, int64_t nanosecs) {
wafwerar's avatar
wafwerar 已提交
419 420
  if (psem == NULL || *psem == NULL) return -1;
  dispatch_semaphore_wait(*psem, nanosecs);
L
Liu Jicong 已提交
421
  return 0;
F
freemine 已提交
422 423
}

L
Liu Jicong 已提交
424
bool taosCheckPthreadValid(TdThread thread) {
wafwerar's avatar
wafwerar 已提交
425 426 427 428 429
  int32_t ret = taosThreadKill(thread, 0);
  if (ret == ESRCH) return false;
  if (ret == EINVAL) return false;
  // alive
  return true;
L
Liu Jicong 已提交
430
}
wafwerar's avatar
wafwerar 已提交
431

F
freemine 已提交
432
int64_t taosGetSelfPthreadId() {
wafwerar's avatar
wafwerar 已提交
433 434
  TdThread thread = taosThreadSelf();
  return (int64_t)thread;
F
freemine 已提交
435 436
}

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

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

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

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

445
int32_t taosGetAppName(char *name, int32_t *len) {
S
Shengliang Guan 已提交
446
  char buf[PATH_MAX + 1];
F
freemine 已提交
447
  buf[0] = '\0';
S
Shengliang Guan 已提交
448
  proc_name(getpid(), buf, sizeof(buf) - 1);
F
freemine 已提交
449 450 451
  buf[PATH_MAX] = '\0';
  size_t n = strlen(buf);
  if (len) *len = n;
wafwerar's avatar
wafwerar 已提交
452
  if (name) tstrncpy(name, buf, TSDB_APP_NAME_LEN);
F
freemine 已提交
453 454 455
  return 0;
}

S
Shengliang Guan 已提交
456 457 458 459 460 461 462
#else

/*
 * linux implementation
 */

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

wafwerar's avatar
wafwerar 已提交
465
bool taosCheckPthreadValid(TdThread thread) { return thread != 0; }
S
Shengliang Guan 已提交
466 467 468 469 470 471 472 473

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

wafwerar's avatar
wafwerar 已提交
474 475 476
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 已提交
477 478

int32_t taosGetPId() {
L
Liu Jicong 已提交
479
  static int32_t pid;
L
Liu Jicong 已提交
480 481 482 483
  if (pid != 0) return pid;
  pid = getpid();
  return pid;
}
S
Shengliang Guan 已提交
484

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

S
Shengliang Guan 已提交
489 490 491 492 493 494 495 496 497 498 499 500
  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 已提交
501
  tstrncpy(name, end, TSDB_APP_NAME_LEN);
S
Shengliang Guan 已提交
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517

  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 已提交
518 519 520 521 522 523 524 525 526 527 528 529 530
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 已提交
531
#endif