osSemaphore.c 17.2 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
  struct timespec ts, rel;
L
Liu Jicong 已提交
79 80
  FILETIME        ft_before, ft_after;
  int             rc;
wafwerar's avatar
wafwerar 已提交
81 82 83 84 85

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

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

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

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

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

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

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

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

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

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

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

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

L
Liu Jicong 已提交
481 482 483 484 485 486 487 488 489 490 491 492 493 494
  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 已提交
495
}
F
freemine 已提交
496

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

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

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

L
Liu Jicong 已提交
512 513 514 515 516 517
  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 已提交
518
    }
L
Liu Jicong 已提交
519 520 521 522 523 524 525 526 527 528
  }
  result = pthread_mutex_unlock(&pxsem->count_lock);
  if (result) {
    return result;
  }
  if (xresult) {
    errno = xresult;
    return -1;
  }
  return 0;
S
slguan 已提交
529
}
F
freemine 已提交
530

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

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

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

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

L
Liu Jicong 已提交
551 552 553 554 555 556
  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 已提交
557
    }
L
Liu Jicong 已提交
558 559 560 561 562 563 564 565 566 567
  }
  result = pthread_mutex_unlock(&pxsem->count_lock);
  if (result) {
    return result;
  }
  if (xresult) {
    errno = xresult;
    return -1;
  }
  return 0;
F
freemine 已提交
568 569
}

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

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

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

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

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

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

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

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

/*
 * linux implementation
 */

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

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

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

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

int32_t taosGetPId() {
  static __thread int32_t pid = 0;
  if (pid != 0) return pid;
  pid = getpid();
  return pid;
}
S
Shengliang Guan 已提交
630

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

S
Shengliang Guan 已提交
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
  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 已提交
664 665 666 667 668 669 670 671 672 673 674 675 676
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 已提交
677
#endif