osSemaphore.c 17.5 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"
S
Shengliang Guan 已提交
20

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

/*
 * windows implementation
 */

#include <windows.h>

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

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

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

int64_t taosGetSelfPthreadId() { return GetCurrentThreadId(); }

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

int32_t taosGetPId() { return GetCurrentProcessId(); }

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

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

  strcpy(name, end);
S
Shengliang Guan 已提交
61 62

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

  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 已提交
77
int32_t tsem_timewait(tsem_t* sem, int64_t nanosecs) {
wafwerar's avatar
wafwerar 已提交
78 79 80 81 82 83 84 85
  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 已提交
86 87
  // errno = 0;
  rc = sem_timedwait(sem, pthread_win32_getabstime_np(&ts, &rel));
wafwerar's avatar
wafwerar 已提交
88 89

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

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

/*
 * darwin implementation
 */

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

114 115
// #define SEM_USE_PTHREAD
// #define SEM_USE_POSIX
wafwerar's avatar
wafwerar 已提交
116 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
// #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 已提交
213
//     snprintf(name, sizeof(name), "/t" PRId64, id);
wafwerar's avatar
wafwerar 已提交
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 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 264 265 266 267 268 269 270 271 272 273 274 275 276 277 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 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 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
//     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;
//     fprintf(stderr, "==%s[%d]%s():[%p]==not created[%d]%s\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem,
//             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) {
//     fprintf(stderr, "==%s[%d]%s():[%p]==semophore_create failed\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__,
//             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) {
//     fprintf(stderr, "==%s[%d]%s():[%p]==already destroyed\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem);
//     abort();
//   }
// #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)) {
//       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();
//   }
//   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) {
//     fprintf(stderr, "==%s[%d]%s():[%p]==already destroyed\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem);
//     abort();
//   }
// #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)) {
//       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();
//   }
//   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) {
//     // fprintf(stderr, "==%s[%d]%s():[%p]==not initialized\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem);
//     // 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 已提交
369
//   snprintf(name, sizeof(name), "/t" PRId64, p->id);
wafwerar's avatar
wafwerar 已提交
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 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
//   int r = sem_unlink(name);
//   if (r) {
//     int e = errno;
//     fprintf(stderr, "==%s[%d]%s():[%p]==unlink failed[%d]%s\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem,
//             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;
// }
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;
421 422
}

wafwerar's avatar
wafwerar 已提交
423 424 425 426 427 428 429 430 431 432 433 434 435 436
int tsem_destroy(tsem_t *psem)
{
    bosal_sem_t *poldsem;

    if (! psem)
    {
        return EINVAL;
    }
    poldsem = (bosal_sem_t *)*psem;

    pthread_mutex_destroy(&poldsem->count_lock);
    pthread_cond_destroy(&poldsem->count_bump);
    free(poldsem);
    return 0;
437 438
}

wafwerar's avatar
wafwerar 已提交
439 440 441 442
int tsem_post(tsem_t *psem)
{
     bosal_sem_t *pxsem;
    int result, xresult;
F
freemine 已提交
443

wafwerar's avatar
wafwerar 已提交
444 445 446
    if (! psem)
    {
        return EINVAL;
447
    }
wafwerar's avatar
wafwerar 已提交
448
    pxsem = (bosal_sem_t *)*psem;
449

wafwerar's avatar
wafwerar 已提交
450 451 452 453 454 455
    result = pthread_mutex_lock(&pxsem->count_lock);
    if (result)
    {
        return result;
    }
    pxsem->count = pxsem->count + 1;
F
freemine 已提交
456

wafwerar's avatar
wafwerar 已提交
457
    xresult = pthread_cond_signal(&pxsem->count_bump);
S
slguan 已提交
458

wafwerar's avatar
wafwerar 已提交
459 460 461 462 463 464 465 466 467 468 469
    result = pthread_mutex_unlock(&pxsem->count_lock);
    if (result)
    {
        return result;
    }
    if (xresult)
    {
        errno = xresult;
        return -1;
    }
    return 0;
S
slguan 已提交
470 471
}

wafwerar's avatar
wafwerar 已提交
472 473 474 475 476 477 478 479
int tsem_trywait(tsem_t *psem)
{
    bosal_sem_t *pxsem;
    int result, xresult;

    if (! psem)
    {
        return EINVAL;
480
    }
wafwerar's avatar
wafwerar 已提交
481
    pxsem = (bosal_sem_t *)*psem;
S
slguan 已提交
482

wafwerar's avatar
wafwerar 已提交
483 484 485 486
    result = pthread_mutex_lock(&pxsem->count_lock);
    if (result)
    {
        return result;
487
    }
wafwerar's avatar
wafwerar 已提交
488
    xresult = 0;
F
freemine 已提交
489

wafwerar's avatar
wafwerar 已提交
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
    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;
    }
508
    return 0;
wafwerar's avatar
wafwerar 已提交
509
}
F
freemine 已提交
510

wafwerar's avatar
wafwerar 已提交
511 512 513 514
int tsem_wait(tsem_t *psem)
{
    bosal_sem_t *pxsem;
    int result, xresult;
F
freemine 已提交
515

wafwerar's avatar
wafwerar 已提交
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
    if (! psem)
    {
        return EINVAL;
    }
    pxsem = (bosal_sem_t *)*psem;

    result = pthread_mutex_lock(&pxsem->count_lock);
    if (result)
    {
        return result;
    }
    xresult = 0;

    if (pxsem->count == 0)
    {
        xresult = pthread_cond_wait(&pxsem->count_bump, &pxsem->count_lock);
    }
    if (! xresult)
    {
        if (pxsem->count > 0)
        {
            pxsem->count--;
        }
    }
    result = pthread_mutex_unlock(&pxsem->count_lock);
    if (result)
    {
        return result;
    }
    if (xresult)
    {
        errno = xresult;
        return -1;
    }
    return 0;
S
slguan 已提交
551
}
F
freemine 已提交
552

wafwerar's avatar
wafwerar 已提交
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
int tsem_timewait(tsem_t *psem, int64_t nanosecs)
{
  struct timespec abstim = {
      .tv_sec = 0,
      .tv_nsec = nanosecs,
  };

    bosal_sem_t *pxsem;
    int result, xresult;

    if (! psem)
    {
        return EINVAL;
    }
    pxsem = (bosal_sem_t *)*psem;

    result = pthread_mutex_lock(&pxsem->count_lock);
    if (result)
    {
        return result;
    }
    xresult = 0;

    if (pxsem->count == 0)
    {
        xresult = pthread_cond_timedwait(&pxsem->count_bump, &pxsem->count_lock, &abstim);
    }
    if (! xresult)
    {
        if (pxsem->count > 0)
        {
            pxsem->count--;
        }
    }
    result = pthread_mutex_unlock(&pxsem->count_lock);
    if (result)
    {
        return result;
    }
    if (xresult)
    {
        errno = xresult;
        return -1;
    }
    return 0;
F
freemine 已提交
598 599
}

wafwerar's avatar
wafwerar 已提交
600 601 602 603 604 605 606 607
bool taosCheckPthreadValid(TdThread thread) { 
  int32_t ret = taosThreadKill(thread, 0);
  if (ret == ESRCH) return false;
  if (ret == EINVAL) return false;
  // alive
  return true;
 }

F
freemine 已提交
608
int64_t taosGetSelfPthreadId() {
wafwerar's avatar
wafwerar 已提交
609 610
  TdThread thread = taosThreadSelf();
  return (int64_t)thread;
F
freemine 已提交
611 612
}

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

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

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

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

621
int32_t taosGetAppName(char *name, int32_t *len) {
S
Shengliang Guan 已提交
622
  char buf[PATH_MAX + 1];
F
freemine 已提交
623
  buf[0] = '\0';
S
Shengliang Guan 已提交
624
  proc_name(getpid(), buf, sizeof(buf) - 1);
F
freemine 已提交
625 626 627 628 629 630 631
  buf[PATH_MAX] = '\0';
  size_t n = strlen(buf);
  if (len) *len = n;
  if (name) strcpy(name, buf);
  return 0;
}

S
Shengliang Guan 已提交
632 633 634 635 636 637 638
#else

/*
 * linux implementation
 */

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

wafwerar's avatar
wafwerar 已提交
641
bool taosCheckPthreadValid(TdThread thread) { return thread != 0; }
S
Shengliang Guan 已提交
642 643 644 645 646 647 648 649

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

wafwerar's avatar
wafwerar 已提交
650 651 652
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 已提交
653 654
int32_t taosGetPId() { return getpid(); }

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

S
Shengliang Guan 已提交
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
  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 已提交
688 689 690 691 692 693 694 695 696 697 698 699 700
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 已提交
701
#endif