osSemaphore.c 17.3 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 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
typedef struct {
  pthread_mutex_t count_lock;
  pthread_cond_t  count_bump;
  unsigned int    count;
} bosal_sem_t;

int tsem_init(tsem_t *psem, int flags, unsigned int count) {
  bosal_sem_t *pnewsem;
  int          result;

  pnewsem = (bosal_sem_t *)malloc(sizeof(bosal_sem_t));
  if (!pnewsem) {
    return -1;
  }
  result = pthread_mutex_init(&pnewsem->count_lock, NULL);
  if (result) {
    free(pnewsem);
    return result;
  }
  result = pthread_cond_init(&pnewsem->count_bump, NULL);
  if (result) {
    pthread_mutex_destroy(&pnewsem->count_lock);
    free(pnewsem);
    return result;
  }
  pnewsem->count = count;
  *psem = (tsem_t)pnewsem;
  return 0;
423 424
}

L
Liu Jicong 已提交
425 426
int tsem_destroy(tsem_t *psem) {
  bosal_sem_t *poldsem;
wafwerar's avatar
wafwerar 已提交
427

L
Liu Jicong 已提交
428 429 430 431
  if (!psem) {
    return EINVAL;
  }
  poldsem = (bosal_sem_t *)*psem;
wafwerar's avatar
wafwerar 已提交
432

L
Liu Jicong 已提交
433 434 435 436
  pthread_mutex_destroy(&poldsem->count_lock);
  pthread_cond_destroy(&poldsem->count_bump);
  free(poldsem);
  return 0;
437 438
}

L
Liu Jicong 已提交
439 440 441
int tsem_post(tsem_t *psem) {
  bosal_sem_t *pxsem;
  int          result, xresult;
F
freemine 已提交
442

L
Liu Jicong 已提交
443 444 445 446
  if (!psem) {
    return EINVAL;
  }
  pxsem = (bosal_sem_t *)*psem;
447

L
Liu Jicong 已提交
448 449 450 451 452
  result = pthread_mutex_lock(&pxsem->count_lock);
  if (result) {
    return result;
  }
  pxsem->count = pxsem->count + 1;
F
freemine 已提交
453

L
Liu Jicong 已提交
454
  xresult = pthread_cond_signal(&pxsem->count_bump);
S
slguan 已提交
455

L
Liu Jicong 已提交
456 457 458 459 460 461 462 463 464
  result = pthread_mutex_unlock(&pxsem->count_lock);
  if (result) {
    return result;
  }
  if (xresult) {
    errno = xresult;
    return -1;
  }
  return 0;
S
slguan 已提交
465 466
}

L
Liu Jicong 已提交
467 468 469
int tsem_trywait(tsem_t *psem) {
  bosal_sem_t *pxsem;
  int          result, xresult;
wafwerar's avatar
wafwerar 已提交
470

L
Liu Jicong 已提交
471 472 473 474
  if (!psem) {
    return EINVAL;
  }
  pxsem = (bosal_sem_t *)*psem;
S
slguan 已提交
475

L
Liu Jicong 已提交
476 477 478 479 480
  result = pthread_mutex_lock(&pxsem->count_lock);
  if (result) {
    return result;
  }
  xresult = 0;
F
freemine 已提交
481

L
Liu Jicong 已提交
482 483 484 485 486 487 488 489 490 491 492 493 494 495
  if (pxsem->count > 0) {
    pxsem->count--;
  } else {
    xresult = EAGAIN;
  }
  result = pthread_mutex_unlock(&pxsem->count_lock);
  if (result) {
    return result;
  }
  if (xresult) {
    errno = xresult;
    return -1;
  }
  return 0;
wafwerar's avatar
wafwerar 已提交
496
}
F
freemine 已提交
497

L
Liu Jicong 已提交
498 499 500
int tsem_wait(tsem_t *psem) {
  bosal_sem_t *pxsem;
  int          result, xresult;
F
freemine 已提交
501

L
Liu Jicong 已提交
502 503 504 505
  if (!psem) {
    return EINVAL;
  }
  pxsem = (bosal_sem_t *)*psem;
wafwerar's avatar
wafwerar 已提交
506

L
Liu Jicong 已提交
507 508 509 510 511
  result = pthread_mutex_lock(&pxsem->count_lock);
  if (result) {
    return result;
  }
  xresult = 0;
wafwerar's avatar
wafwerar 已提交
512

L
Liu Jicong 已提交
513 514 515 516 517 518
  if (pxsem->count == 0) {
    xresult = pthread_cond_wait(&pxsem->count_bump, &pxsem->count_lock);
  }
  if (!xresult) {
    if (pxsem->count > 0) {
      pxsem->count--;
wafwerar's avatar
wafwerar 已提交
519
    }
L
Liu Jicong 已提交
520 521 522 523 524 525 526 527 528 529
  }
  result = pthread_mutex_unlock(&pxsem->count_lock);
  if (result) {
    return result;
  }
  if (xresult) {
    errno = xresult;
    return -1;
  }
  return 0;
S
slguan 已提交
530
}
F
freemine 已提交
531

L
Liu Jicong 已提交
532
int tsem_timewait(tsem_t *psem, int64_t nanosecs) {
wafwerar's avatar
wafwerar 已提交
533 534 535 536 537
  struct timespec abstim = {
      .tv_sec = 0,
      .tv_nsec = nanosecs,
  };

L
Liu Jicong 已提交
538 539
  bosal_sem_t *pxsem;
  int          result, xresult;
wafwerar's avatar
wafwerar 已提交
540

L
Liu Jicong 已提交
541 542 543 544
  if (!psem) {
    return EINVAL;
  }
  pxsem = (bosal_sem_t *)*psem;
wafwerar's avatar
wafwerar 已提交
545

L
Liu Jicong 已提交
546 547 548 549 550
  result = pthread_mutex_lock(&pxsem->count_lock);
  if (result) {
    return result;
  }
  xresult = 0;
wafwerar's avatar
wafwerar 已提交
551

L
Liu Jicong 已提交
552 553 554 555 556 557
  if (pxsem->count == 0) {
    xresult = pthread_cond_timedwait(&pxsem->count_bump, &pxsem->count_lock, &abstim);
  }
  if (!xresult) {
    if (pxsem->count > 0) {
      pxsem->count--;
wafwerar's avatar
wafwerar 已提交
558
    }
L
Liu Jicong 已提交
559 560 561 562 563 564 565 566 567 568
  }
  result = pthread_mutex_unlock(&pxsem->count_lock);
  if (result) {
    return result;
  }
  if (xresult) {
    errno = xresult;
    return -1;
  }
  return 0;
F
freemine 已提交
569 570
}

L
Liu Jicong 已提交
571
bool taosCheckPthreadValid(TdThread thread) {
wafwerar's avatar
wafwerar 已提交
572 573 574 575 576
  int32_t ret = taosThreadKill(thread, 0);
  if (ret == ESRCH) return false;
  if (ret == EINVAL) return false;
  // alive
  return true;
L
Liu Jicong 已提交
577
}
wafwerar's avatar
wafwerar 已提交
578

F
freemine 已提交
579
int64_t taosGetSelfPthreadId() {
wafwerar's avatar
wafwerar 已提交
580 581
  TdThread thread = taosThreadSelf();
  return (int64_t)thread;
F
freemine 已提交
582 583
}

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

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

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

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

592
int32_t taosGetAppName(char *name, int32_t *len) {
S
Shengliang Guan 已提交
593
  char buf[PATH_MAX + 1];
F
freemine 已提交
594
  buf[0] = '\0';
S
Shengliang Guan 已提交
595
  proc_name(getpid(), buf, sizeof(buf) - 1);
F
freemine 已提交
596 597 598
  buf[PATH_MAX] = '\0';
  size_t n = strlen(buf);
  if (len) *len = n;
wafwerar's avatar
wafwerar 已提交
599
  if (name) tstrncpy(name, buf, TSDB_APP_NAME_LEN);
F
freemine 已提交
600 601 602
  return 0;
}

S
Shengliang Guan 已提交
603 604 605 606 607 608 609
#else

/*
 * linux implementation
 */

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

wafwerar's avatar
wafwerar 已提交
612
bool taosCheckPthreadValid(TdThread thread) { return thread != 0; }
S
Shengliang Guan 已提交
613 614 615 616 617 618 619 620

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

wafwerar's avatar
wafwerar 已提交
621 622 623
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 已提交
624 625

int32_t taosGetPId() {
L
Liu Jicong 已提交
626
  static int32_t pid;
L
Liu Jicong 已提交
627 628 629 630
  if (pid != 0) return pid;
  pid = getpid();
  return pid;
}
S
Shengliang Guan 已提交
631

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

S
Shengliang Guan 已提交
636 637 638 639 640 641 642 643 644 645 646 647
  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 已提交
648
  tstrncpy(name, end, TSDB_APP_NAME_LEN);
S
Shengliang Guan 已提交
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664

  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 已提交
665 666 667 668 669 670 671 672 673 674 675 676 677
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 已提交
678
#endif