Rizin
unix-like reverse engineering framework and cli tools
thread.c File Reference
#include "uv.h"
#include "internal.h"
#include <pthread.h>
#include <assert.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <unistd.h>
#include <limits.h>

Go to the source code of this file.

Classes

struct  uv_semaphore_s
 

Macros

#define NANOSEC   ((uint64_t) 1e9)
 
#define platform_needs_custom_semaphore   0
 

Typedefs

typedef struct uv_semaphore_s uv_semaphore_t
 

Functions

int uv_barrier_init (uv_barrier_t *barrier, unsigned int count)
 
int uv_barrier_wait (uv_barrier_t *barrier)
 
void uv_barrier_destroy (uv_barrier_t *barrier)
 
static size_t thread_stack_size (void)
 
int uv_thread_create (uv_thread_t *tid, void(*entry)(void *arg), void *arg)
 
int uv_thread_create_ex (uv_thread_t *tid, const uv_thread_options_t *params, void(*entry)(void *arg), void *arg)
 
uv_thread_t uv_thread_self (void)
 
int uv_thread_join (uv_thread_t *tid)
 
int uv_thread_equal (const uv_thread_t *t1, const uv_thread_t *t2)
 
int uv_mutex_init (uv_mutex_t *mutex)
 
int uv_mutex_init_recursive (uv_mutex_t *mutex)
 
void uv_mutex_destroy (uv_mutex_t *mutex)
 
void uv_mutex_lock (uv_mutex_t *mutex)
 
int uv_mutex_trylock (uv_mutex_t *mutex)
 
void uv_mutex_unlock (uv_mutex_t *mutex)
 
int uv_rwlock_init (uv_rwlock_t *rwlock)
 
void uv_rwlock_destroy (uv_rwlock_t *rwlock)
 
void uv_rwlock_rdlock (uv_rwlock_t *rwlock)
 
int uv_rwlock_tryrdlock (uv_rwlock_t *rwlock)
 
void uv_rwlock_rdunlock (uv_rwlock_t *rwlock)
 
void uv_rwlock_wrlock (uv_rwlock_t *rwlock)
 
int uv_rwlock_trywrlock (uv_rwlock_t *rwlock)
 
void uv_rwlock_wrunlock (uv_rwlock_t *rwlock)
 
void uv_once (uv_once_t *guard, void(*callback)(void))
 
static int uv__custom_sem_init (uv_sem_t *sem_, unsigned int value)
 
static void uv__custom_sem_destroy (uv_sem_t *sem_)
 
static void uv__custom_sem_post (uv_sem_t *sem_)
 
static void uv__custom_sem_wait (uv_sem_t *sem_)
 
static int uv__custom_sem_trywait (uv_sem_t *sem_)
 
static int uv__sem_init (uv_sem_t *sem, unsigned int value)
 
static void uv__sem_destroy (uv_sem_t *sem)
 
static void uv__sem_post (uv_sem_t *sem)
 
static void uv__sem_wait (uv_sem_t *sem)
 
static int uv__sem_trywait (uv_sem_t *sem)
 
int uv_sem_init (uv_sem_t *sem, unsigned int value)
 
void uv_sem_destroy (uv_sem_t *sem)
 
void uv_sem_post (uv_sem_t *sem)
 
void uv_sem_wait (uv_sem_t *sem)
 
int uv_sem_trywait (uv_sem_t *sem)
 
int uv_cond_init (uv_cond_t *cond)
 
void uv_cond_destroy (uv_cond_t *cond)
 
void uv_cond_signal (uv_cond_t *cond)
 
void uv_cond_broadcast (uv_cond_t *cond)
 
void uv_cond_wait (uv_cond_t *cond, uv_mutex_t *mutex)
 
int uv_cond_timedwait (uv_cond_t *cond, uv_mutex_t *mutex, uint64_t timeout)
 
int uv_key_create (uv_key_t *key)
 
void uv_key_delete (uv_key_t *key)
 
void * uv_key_get (uv_key_t *key)
 
void uv_key_set (uv_key_t *key, void *value)
 

Macro Definition Documentation

◆ NANOSEC

#define NANOSEC   ((uint64_t) 1e9)

Definition at line 45 of file thread.c.

◆ platform_needs_custom_semaphore

#define platform_needs_custom_semaphore   0

Definition at line 511 of file thread.c.

Typedef Documentation

◆ uv_semaphore_t

Function Documentation

◆ thread_stack_size()

static size_t thread_stack_size ( void  )
static

Definition at line 171 of file thread.c.

171  {
172 #if defined(__APPLE__) || defined(__linux__)
173  struct rlimit lim;
174 
175  /* getrlimit() can fail on some aarch64 systems due to a glibc bug where
176  * the system call wrapper invokes the wrong system call. Don't treat
177  * that as fatal, just use the default stack size instead.
178  */
179  if (0 == getrlimit(RLIMIT_STACK, &lim) && lim.rlim_cur != RLIM_INFINITY) {
180  /* pthread_attr_setstacksize() expects page-aligned values. */
181  lim.rlim_cur -= lim.rlim_cur % (rlim_t) getpagesize();
182 
183  /* Musl's PTHREAD_STACK_MIN is 2 KB on all architectures, which is
184  * too small to safely receive signals on.
185  *
186  * Musl's PTHREAD_STACK_MIN + MINSIGSTKSZ == 8192 on arm64 (which has
187  * the largest MINSIGSTKSZ of the architectures that musl supports) so
188  * let's use that as a lower bound.
189  *
190  * We use a hardcoded value because PTHREAD_STACK_MIN + MINSIGSTKSZ
191  * is between 28 and 133 KB when compiling against glibc, depending
192  * on the architecture.
193  */
194  if (lim.rlim_cur >= 8192)
195  if (lim.rlim_cur >= PTHREAD_STACK_MIN)
196  return lim.rlim_cur;
197  }
198 #endif
199 
200 #if !defined(__linux__)
201  return 0;
202 #elif defined(__PPC__) || defined(__ppc__) || defined(__powerpc__)
203  return 4 << 20; /* glibc default. */
204 #else
205  return 2 << 20; /* glibc default. */
206 #endif
207 }

Referenced by uv_thread_create_ex().

◆ uv__custom_sem_destroy()

static void uv__custom_sem_destroy ( uv_sem_t sem_)
static

Definition at line 551 of file thread.c.

551  {
552  uv_semaphore_t* sem;
553 
554  sem = *(uv_semaphore_t**)sem_;
555  uv_cond_destroy(&sem->cond);
556  uv_mutex_destroy(&sem->mutex);
557  uv__free(sem);
558 }
uv_cond_t cond
Definition: thread.c:517
uv_mutex_t mutex
Definition: thread.c:516
void uv_mutex_destroy(uv_mutex_t *mutex)
Definition: thread.c:324
void uv_cond_destroy(uv_cond_t *cond)
Definition: thread.c:735
void uv__free(void *ptr)
Definition: uv-common.c:81

References uv_semaphore_s::cond, uv_semaphore_s::mutex, uv__free(), uv_cond_destroy(), and uv_mutex_destroy().

Referenced by uv_sem_destroy().

◆ uv__custom_sem_init()

static int uv__custom_sem_init ( uv_sem_t sem_,
unsigned int  value 
)
static

Definition at line 526 of file thread.c.

526  {
527  int err;
528  uv_semaphore_t* sem;
529 
530  sem = uv__malloc(sizeof(*sem));
531  if (sem == NULL)
532  return UV_ENOMEM;
533 
534  if ((err = uv_mutex_init(&sem->mutex)) != 0) {
535  uv__free(sem);
536  return err;
537  }
538 
539  if ((err = uv_cond_init(&sem->cond)) != 0) {
540  uv_mutex_destroy(&sem->mutex);
541  uv__free(sem);
542  return err;
543  }
544 
545  sem->value = value;
546  *(uv_semaphore_t**)sem_ = sem;
547  return 0;
548 }
static bool err
Definition: armass.c:435
static int value
Definition: cmd_api.c:93
#define NULL
Definition: cris-opc.c:27
unsigned int value
Definition: thread.c:518
int uv_cond_init(uv_cond_t *cond)
Definition: thread.c:704
int uv_mutex_init(uv_mutex_t *mutex)
Definition: thread.c:282
void * uv__malloc(size_t size)
Definition: uv-common.c:75

References uv_semaphore_s::cond, err, uv_semaphore_s::mutex, NULL, uv__free(), uv__malloc(), uv_cond_init(), uv_mutex_destroy(), uv_mutex_init(), value, and uv_semaphore_s::value.

Referenced by uv_sem_init().

◆ uv__custom_sem_post()

static void uv__custom_sem_post ( uv_sem_t sem_)
static

Definition at line 561 of file thread.c.

561  {
562  uv_semaphore_t* sem;
563 
564  sem = *(uv_semaphore_t**)sem_;
565  uv_mutex_lock(&sem->mutex);
566  sem->value++;
567  if (sem->value == 1)
568  uv_cond_signal(&sem->cond);
569  uv_mutex_unlock(&sem->mutex);
570 }
void uv_cond_signal(uv_cond_t *cond)
Definition: thread.c:769
void uv_mutex_unlock(uv_mutex_t *mutex)
Definition: thread.c:350
void uv_mutex_lock(uv_mutex_t *mutex)
Definition: thread.c:330

References uv_semaphore_s::cond, uv_semaphore_s::mutex, uv_cond_signal(), uv_mutex_lock(), uv_mutex_unlock(), and uv_semaphore_s::value.

Referenced by uv_sem_post().

◆ uv__custom_sem_trywait()

static int uv__custom_sem_trywait ( uv_sem_t sem_)
static

Definition at line 585 of file thread.c.

585  {
586  uv_semaphore_t* sem;
587 
588  sem = *(uv_semaphore_t**)sem_;
589  if (uv_mutex_trylock(&sem->mutex) != 0)
590  return UV_EAGAIN;
591 
592  if (sem->value == 0) {
593  uv_mutex_unlock(&sem->mutex);
594  return UV_EAGAIN;
595  }
596 
597  sem->value--;
598  uv_mutex_unlock(&sem->mutex);
599 
600  return 0;
601 }
int uv_mutex_trylock(uv_mutex_t *mutex)
Definition: thread.c:336

References uv_semaphore_s::mutex, uv_mutex_trylock(), uv_mutex_unlock(), and uv_semaphore_s::value.

Referenced by uv_sem_trywait().

◆ uv__custom_sem_wait()

static void uv__custom_sem_wait ( uv_sem_t sem_)
static

Definition at line 573 of file thread.c.

573  {
574  uv_semaphore_t* sem;
575 
576  sem = *(uv_semaphore_t**)sem_;
577  uv_mutex_lock(&sem->mutex);
578  while (sem->value == 0)
579  uv_cond_wait(&sem->cond, &sem->mutex);
580  sem->value--;
581  uv_mutex_unlock(&sem->mutex);
582 }
void uv_cond_wait(uv_cond_t *cond, uv_mutex_t *mutex)
Definition: thread.c:779

References uv_semaphore_s::cond, uv_semaphore_s::mutex, uv_cond_wait(), uv_mutex_lock(), uv_mutex_unlock(), and uv_semaphore_s::value.

Referenced by uv_sem_wait().

◆ uv__sem_destroy()

static void uv__sem_destroy ( uv_sem_t sem)
static

Definition at line 610 of file thread.c.

610  {
611  if (sem_destroy(sem))
612  abort();
613 }
int sem_destroy(UV_PLATFORM_SEM_T *semid)

References sem_destroy().

Referenced by uv_sem_destroy().

◆ uv__sem_init()

static int uv__sem_init ( uv_sem_t sem,
unsigned int  value 
)
static

Definition at line 603 of file thread.c.

603  {
604  if (sem_init(sem, 0, value))
605  return UV__ERR(errno);
606  return 0;
607 }
#define UV__ERR(x)
Definition: errno.h:29
int sem_init(UV_PLATFORM_SEM_T *semid, int pshared, unsigned int value)

References sem_init(), UV__ERR, and value.

Referenced by uv_sem_init().

◆ uv__sem_post()

static void uv__sem_post ( uv_sem_t sem)
static

Definition at line 616 of file thread.c.

616  {
617  if (sem_post(sem))
618  abort();
619 }
int sem_post(UV_PLATFORM_SEM_T *semid)

References sem_post().

Referenced by uv_sem_post().

◆ uv__sem_trywait()

static int uv__sem_trywait ( uv_sem_t sem)
static

Definition at line 634 of file thread.c.

634  {
635  int r;
636 
637  do
638  r = sem_trywait(sem);
639  while (r == -1 && errno == EINTR);
640 
641  if (r) {
642  if (errno == EAGAIN)
643  return UV_EAGAIN;
644  abort();
645  }
646 
647  return 0;
648 }
#define r
Definition: crypto_rc6.c:12
int sem_trywait(UV_PLATFORM_SEM_T *semid)
#define EINTR
Definition: sftypes.h:114
#define EAGAIN
Definition: sftypes.h:121

References EAGAIN, EINTR, r, and sem_trywait().

Referenced by uv_sem_trywait().

◆ uv__sem_wait()

static void uv__sem_wait ( uv_sem_t sem)
static

Definition at line 622 of file thread.c.

622  {
623  int r;
624 
625  do
626  r = sem_wait(sem);
627  while (r == -1 && errno == EINTR);
628 
629  if (r)
630  abort();
631 }
int sem_wait(UV_PLATFORM_SEM_T *semid)

References EINTR, r, and sem_wait().

Referenced by uv_sem_wait().

◆ uv_barrier_destroy()

void uv_barrier_destroy ( uv_barrier_t barrier)

Definition at line 118 of file thread.c.

118  {
119  struct _uv_barrier* b;
120 
121  b = barrier->b;
122  uv_mutex_lock(&b->mutex);
123 
124  assert(b->in == 0);
125  assert(b->out == 0);
126 
127  if (b->in != 0 || b->out != 0)
128  abort();
129 
130  uv_mutex_unlock(&b->mutex);
131  uv_mutex_destroy(&b->mutex);
132  uv_cond_destroy(&b->cond);
133 
134  uv__free(barrier->b);
135  barrier->b = NULL;
136 }
assert(limit<=UINT32_MAX/2)
#define b(i)
Definition: sha256.c:42
struct _uv_barrier * b
Definition: unix.h:157

◆ uv_barrier_init()

int uv_barrier_init ( uv_barrier_t barrier,
unsigned int  count 
)

Definition at line 55 of file thread.c.

55  {
56  struct _uv_barrier* b;
57  int rc;
58 
59  if (barrier == NULL || count == 0)
60  return UV_EINVAL;
61 
62  b = uv__malloc(sizeof(*b));
63  if (b == NULL)
64  return UV_ENOMEM;
65 
66  b->in = 0;
67  b->out = 0;
68  b->threshold = count;
69 
70  rc = uv_mutex_init(&b->mutex);
71  if (rc != 0)
72  goto error2;
73 
74  rc = uv_cond_init(&b->cond);
75  if (rc != 0)
76  goto error;
77 
78  barrier->b = b;
79  return 0;
80 
81 error:
82  uv_mutex_destroy(&b->mutex);
83 error2:
84  uv__free(b);
85  return rc;
86 }
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void count
Definition: sflib.h:98
void error(const char *msg)
Definition: untgz.c:593

◆ uv_barrier_wait()

int uv_barrier_wait ( uv_barrier_t barrier)

Definition at line 89 of file thread.c.

89  {
90  struct _uv_barrier* b;
91  int last;
92 
93  if (barrier == NULL || barrier->b == NULL)
94  return UV_EINVAL;
95 
96  b = barrier->b;
97  uv_mutex_lock(&b->mutex);
98 
99  if (++b->in == b->threshold) {
100  b->in = 0;
101  b->out = b->threshold;
102  uv_cond_signal(&b->cond);
103  } else {
104  do
105  uv_cond_wait(&b->cond, &b->mutex);
106  while (b->in != 0);
107  }
108 
109  last = (--b->out == 0);
110  if (!last)
111  uv_cond_signal(&b->cond); /* Not needed for last thread. */
112 
113  uv_mutex_unlock(&b->mutex);
114  return last;
115 }

◆ uv_cond_broadcast()

void uv_cond_broadcast ( uv_cond_t cond)

Definition at line 774 of file thread.c.

774  {
775  if (pthread_cond_broadcast(cond))
776  abort();
777 }
#define cond(bop, top, mask, flags)

◆ uv_cond_destroy()

void uv_cond_destroy ( uv_cond_t cond)

Definition at line 735 of file thread.c.

735  {
736 #if defined(__APPLE__) && defined(__MACH__)
737  /* It has been reported that destroying condition variables that have been
738  * signalled but not waited on can sometimes result in application crashes.
739  * See https://codereview.chromium.org/1323293005.
740  */
741  pthread_mutex_t mutex;
742  struct timespec ts;
743  int err;
744 
745  if (pthread_mutex_init(&mutex, NULL))
746  abort();
747 
748  if (pthread_mutex_lock(&mutex))
749  abort();
750 
751  ts.tv_sec = 0;
752  ts.tv_nsec = 1;
753 
754  err = pthread_cond_timedwait_relative_np(cond, &mutex, &ts);
755  if (err != 0 && err != ETIMEDOUT)
756  abort();
757 
758  if (pthread_mutex_unlock(&mutex))
759  abort();
760 
761  if (pthread_mutex_destroy(&mutex))
762  abort();
763 #endif /* defined(__APPLE__) && defined(__MACH__) */
764 
765  if (pthread_cond_destroy(cond))
766  abort();
767 }
#define ETIMEDOUT
Definition: sftypes.h:170
static uv_mutex_t mutex
Definition: threadpool.c:34

Referenced by uv__custom_sem_destroy(), and uv_barrier_destroy().

◆ uv_cond_init()

int uv_cond_init ( uv_cond_t cond)

Definition at line 704 of file thread.c.

704  {
705  pthread_condattr_t attr;
706  int err;
707 
708  err = pthread_condattr_init(&attr);
709  if (err)
710  return UV__ERR(err);
711 
712  err = pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
713  if (err)
714  goto error2;
715 
716  err = pthread_cond_init(cond, &attr);
717  if (err)
718  goto error2;
719 
720  err = pthread_condattr_destroy(&attr);
721  if (err)
722  goto error;
723 
724  return 0;
725 
726 error:
727  pthread_cond_destroy(cond);
728 error2:
729  pthread_condattr_destroy(&attr);
730  return UV__ERR(err);
731 }

Referenced by uv__custom_sem_init(), and uv_barrier_init().

◆ uv_cond_signal()

void uv_cond_signal ( uv_cond_t cond)

Definition at line 769 of file thread.c.

769  {
770  if (pthread_cond_signal(cond))
771  abort();
772 }

Referenced by uv__custom_sem_post(), and uv_barrier_wait().

◆ uv_cond_timedwait()

int uv_cond_timedwait ( uv_cond_t cond,
uv_mutex_t mutex,
uint64_t  timeout 
)

Definition at line 785 of file thread.c.

785  {
786  int r;
787  struct timespec ts;
788 #if defined(__MVS__)
789  struct timeval tv;
790 #endif
791 
792 #if defined(__APPLE__) && defined(__MACH__)
793  ts.tv_sec = timeout / NANOSEC;
794  ts.tv_nsec = timeout % NANOSEC;
795  r = pthread_cond_timedwait_relative_np(cond, mutex, &ts);
796 #else
797 #if defined(__MVS__)
798  if (gettimeofday(&tv, NULL))
799  abort();
800  timeout += tv.tv_sec * NANOSEC + tv.tv_usec * 1e3;
801 #else
803 #endif
804  ts.tv_sec = timeout / NANOSEC;
805  ts.tv_nsec = timeout % NANOSEC;
806  r = pthread_cond_timedwait(cond, mutex, &ts);
807 #endif
808 
809 
810  if (r == 0)
811  return 0;
812 
813  if (r == ETIMEDOUT)
814  return UV_ETIMEDOUT;
815 
816  abort();
817 #ifndef __SUNPRO_C
818  return UV_EINVAL; /* Satisfy the compiler. */
819 #endif
820 }
uint64_t uv__hrtime(uv_clocktype_t type)
Definition: aix-common.c:43
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval tv
Definition: sflib.h:79
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog gettimeofday
Definition: sflib.h:79
uv_timer_t timeout
Definition: main.c:9
@ UV_CLOCK_PRECISE
Definition: internal.h:147
#define NANOSEC
Definition: thread.c:45

◆ uv_cond_wait()

void uv_cond_wait ( uv_cond_t cond,
uv_mutex_t mutex 
)

Definition at line 779 of file thread.c.

779  {
780  if (pthread_cond_wait(cond, mutex))
781  abort();
782 }

Referenced by uv__custom_sem_wait(), and uv_barrier_wait().

◆ uv_key_create()

int uv_key_create ( uv_key_t key)

Definition at line 823 of file thread.c.

823  {
824  return UV__ERR(pthread_key_create(key, NULL));
825 }
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len key
Definition: sflib.h:118

Referenced by uv__init_current_thread_key().

◆ uv_key_delete()

void uv_key_delete ( uv_key_t key)

Definition at line 828 of file thread.c.

828  {
829  if (pthread_key_delete(*key))
830  abort();
831 }

◆ uv_key_get()

void* uv_key_get ( uv_key_t key)

Definition at line 834 of file thread.c.

834  {
835  return pthread_getspecific(*key);
836 }

Referenced by uv_thread_self().

◆ uv_key_set()

void uv_key_set ( uv_key_t key,
void *  value 
)

Definition at line 839 of file thread.c.

839  {
840  if (pthread_setspecific(*key, value))
841  abort();
842 }

Referenced by uv__thread_start().

◆ uv_mutex_destroy()

void uv_mutex_destroy ( uv_mutex_t mutex)

Definition at line 324 of file thread.c.

324  {
325  if (pthread_mutex_destroy(mutex))
326  abort();
327 }

Referenced by uv__custom_sem_destroy(), uv__custom_sem_init(), uv_barrier_destroy(), and uv_barrier_init().

◆ uv_mutex_init()

int uv_mutex_init ( uv_mutex_t mutex)

Definition at line 282 of file thread.c.

282  {
283 #if defined(NDEBUG) || !defined(PTHREAD_MUTEX_ERRORCHECK)
284  return UV__ERR(pthread_mutex_init(mutex, NULL));
285 #else
286  pthread_mutexattr_t attr;
287  int err;
288 
289  if (pthread_mutexattr_init(&attr))
290  abort();
291 
292  if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK))
293  abort();
294 
295  err = pthread_mutex_init(mutex, &attr);
296 
297  if (pthread_mutexattr_destroy(&attr))
298  abort();
299 
300  return UV__ERR(err);
301 #endif
302 }

Referenced by uv__custom_sem_init(), uv_barrier_init(), and uv_mutex_init_recursive().

◆ uv_mutex_init_recursive()

int uv_mutex_init_recursive ( uv_mutex_t mutex)

Definition at line 305 of file thread.c.

305  {
306  pthread_mutexattr_t attr;
307  int err;
308 
309  if (pthread_mutexattr_init(&attr))
310  abort();
311 
312  if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE))
313  abort();
314 
315  err = pthread_mutex_init(mutex, &attr);
316 
317  if (pthread_mutexattr_destroy(&attr))
318  abort();
319 
320  return UV__ERR(err);
321 }

◆ uv_mutex_lock()

void uv_mutex_lock ( uv_mutex_t mutex)

Definition at line 330 of file thread.c.

330  {
331  if (pthread_mutex_lock(mutex))
332  abort();
333 }

Referenced by uv__custom_sem_post(), uv__custom_sem_wait(), uv_barrier_destroy(), and uv_barrier_wait().

◆ uv_mutex_trylock()

int uv_mutex_trylock ( uv_mutex_t mutex)

Definition at line 336 of file thread.c.

336  {
337  int err;
338 
339  err = pthread_mutex_trylock(mutex);
340  if (err) {
341  if (err != EBUSY && err != EAGAIN)
342  abort();
343  return UV_EBUSY;
344  }
345 
346  return 0;
347 }
#define EBUSY
Definition: sftypes.h:126

Referenced by uv__custom_sem_trywait().

◆ uv_mutex_unlock()

void uv_mutex_unlock ( uv_mutex_t mutex)

Definition at line 350 of file thread.c.

350  {
351  if (pthread_mutex_unlock(mutex))
352  abort();
353 }

Referenced by uv__custom_sem_post(), uv__custom_sem_trywait(), uv__custom_sem_wait(), uv_barrier_destroy(), and uv_barrier_wait().

◆ uv_once()

void uv_once ( uv_once_t guard,
void(*)(void)  callback 
)

Definition at line 419 of file thread.c.

419  {
420  if (pthread_once(guard, callback))
421  abort();
422 }

Referenced by uv__thread_start(), uv_sem_init(), and uv_thread_self().

◆ uv_rwlock_destroy()

void uv_rwlock_destroy ( uv_rwlock_t rwlock)

Definition at line 361 of file thread.c.

361  {
362  if (pthread_rwlock_destroy(rwlock))
363  abort();
364 }

◆ uv_rwlock_init()

int uv_rwlock_init ( uv_rwlock_t rwlock)

Definition at line 356 of file thread.c.

356  {
357  return UV__ERR(pthread_rwlock_init(rwlock, NULL));
358 }

◆ uv_rwlock_rdlock()

void uv_rwlock_rdlock ( uv_rwlock_t rwlock)

Definition at line 367 of file thread.c.

367  {
368  if (pthread_rwlock_rdlock(rwlock))
369  abort();
370 }

◆ uv_rwlock_rdunlock()

void uv_rwlock_rdunlock ( uv_rwlock_t rwlock)

Definition at line 387 of file thread.c.

387  {
388  if (pthread_rwlock_unlock(rwlock))
389  abort();
390 }

◆ uv_rwlock_tryrdlock()

int uv_rwlock_tryrdlock ( uv_rwlock_t rwlock)

Definition at line 373 of file thread.c.

373  {
374  int err;
375 
376  err = pthread_rwlock_tryrdlock(rwlock);
377  if (err) {
378  if (err != EBUSY && err != EAGAIN)
379  abort();
380  return UV_EBUSY;
381  }
382 
383  return 0;
384 }

◆ uv_rwlock_trywrlock()

int uv_rwlock_trywrlock ( uv_rwlock_t rwlock)

Definition at line 399 of file thread.c.

399  {
400  int err;
401 
402  err = pthread_rwlock_trywrlock(rwlock);
403  if (err) {
404  if (err != EBUSY && err != EAGAIN)
405  abort();
406  return UV_EBUSY;
407  }
408 
409  return 0;
410 }

◆ uv_rwlock_wrlock()

void uv_rwlock_wrlock ( uv_rwlock_t rwlock)

Definition at line 393 of file thread.c.

393  {
394  if (pthread_rwlock_wrlock(rwlock))
395  abort();
396 }

◆ uv_rwlock_wrunlock()

void uv_rwlock_wrunlock ( uv_rwlock_t rwlock)

Definition at line 413 of file thread.c.

413  {
414  if (pthread_rwlock_unlock(rwlock))
415  abort();
416 }

◆ uv_sem_destroy()

void uv_sem_destroy ( uv_sem_t sem)

Definition at line 662 of file thread.c.

662  {
665  else
666  uv__sem_destroy(sem);
667 }
static void uv__custom_sem_destroy(uv_sem_t *sem_)
Definition: thread.c:551
static void uv__sem_destroy(uv_sem_t *sem)
Definition: thread.c:610
#define platform_needs_custom_semaphore
Definition: thread.c:511

Referenced by uv_barrier_destroy(), and uv_barrier_init().

◆ uv_sem_init()

int uv_sem_init ( uv_sem_t sem,
unsigned int  value 
)

Definition at line 650 of file thread.c.

650  {
651 #if defined(__GLIBC__) && !defined(__UCLIBC__)
652  uv_once(&glibc_version_check_once, glibc_version_check);
653 #endif
654 
656  return uv__custom_sem_init(sem, value);
657  else
658  return uv__sem_init(sem, value);
659 }
void uv_once(uv_once_t *guard, void(*callback)(void))
Definition: thread.c:419
static int uv__custom_sem_init(uv_sem_t *sem_, unsigned int value)
Definition: thread.c:526
static int uv__sem_init(uv_sem_t *sem, unsigned int value)
Definition: thread.c:603

Referenced by uv_barrier_init().

◆ uv_sem_post()

void uv_sem_post ( uv_sem_t sem)

Definition at line 670 of file thread.c.

670  {
672  uv__custom_sem_post(sem);
673  else
674  uv__sem_post(sem);
675 }
static void uv__custom_sem_post(uv_sem_t *sem_)
Definition: thread.c:561
static void uv__sem_post(uv_sem_t *sem)
Definition: thread.c:616

Referenced by uv_barrier_wait().

◆ uv_sem_trywait()

int uv_sem_trywait ( uv_sem_t sem)

Definition at line 686 of file thread.c.

686  {
688  return uv__custom_sem_trywait(sem);
689  else
690  return uv__sem_trywait(sem);
691 }
static int uv__custom_sem_trywait(uv_sem_t *sem_)
Definition: thread.c:585
static int uv__sem_trywait(uv_sem_t *sem)
Definition: thread.c:634

◆ uv_sem_wait()

void uv_sem_wait ( uv_sem_t sem)

Definition at line 678 of file thread.c.

678  {
680  uv__custom_sem_wait(sem);
681  else
682  uv__sem_wait(sem);
683 }
static void uv__custom_sem_wait(uv_sem_t *sem_)
Definition: thread.c:573
static void uv__sem_wait(uv_sem_t *sem)
Definition: thread.c:622

Referenced by uv_barrier_wait().

◆ uv_thread_create()

int uv_thread_create ( uv_thread_t tid,
void(*)(void *arg entry,
void *  arg 
)

Definition at line 210 of file thread.c.

210  {
211  uv_thread_options_t params;
212  params.flags = UV_THREAD_NO_FLAGS;
213  return uv_thread_create_ex(tid, &params, entry, arg);
214 }
Definition: zipcmp.c:77
unsigned int flags
Definition: uv.h:1753
int uv_thread_create_ex(uv_thread_t *tid, const uv_thread_options_t *params, void(*entry)(void *arg), void *arg)
Definition: thread.c:216
@ UV_THREAD_NO_FLAGS
Definition: uv.h:1748

◆ uv_thread_create_ex()

int uv_thread_create_ex ( uv_thread_t tid,
const uv_thread_options_t params,
void(*)(void *arg entry,
void *  arg 
)

Definition at line 216 of file thread.c.

219  {
220  int err;
221  pthread_attr_t* attr;
222  pthread_attr_t attr_storage;
223  size_t pagesize;
224  size_t stack_size;
225 
226  /* Used to squelch a -Wcast-function-type warning. */
227  union {
228  void (*in)(void*);
229  void* (*out)(void*);
230  } f;
231 
232  stack_size =
233  params->flags & UV_THREAD_HAS_STACK_SIZE ? params->stack_size : 0;
234 
235  attr = NULL;
236  if (stack_size == 0) {
237  stack_size = thread_stack_size();
238  } else {
239  pagesize = (size_t)getpagesize();
240  /* Round up to the nearest page boundary. */
241  stack_size = (stack_size + pagesize - 1) &~ (pagesize - 1);
242 #ifdef PTHREAD_STACK_MIN
243  if (stack_size < PTHREAD_STACK_MIN)
244  stack_size = PTHREAD_STACK_MIN;
245 #endif
246  }
247 
248  if (stack_size > 0) {
249  attr = &attr_storage;
250 
251  if (pthread_attr_init(attr))
252  abort();
253 
254  if (pthread_attr_setstacksize(attr, stack_size))
255  abort();
256  }
257 
258  f.in = entry;
259  err = pthread_create(tid, attr, f.out, arg);
260 
261  if (attr != NULL)
262  pthread_attr_destroy(attr);
263 
264  return UV__ERR(err);
265 }
const lzma_allocator const uint8_t * in
Definition: block.h:527
int size_t
Definition: sftypes.h:40
#define f(i)
Definition: sha256.c:46
size_t stack_size
Definition: uv.h:1754
static size_t thread_stack_size(void)
Definition: thread.c:171
@ UV_THREAD_HAS_STACK_SIZE
Definition: uv.h:1749

Referenced by uv_thread_create().

◆ uv_thread_equal()

int uv_thread_equal ( const uv_thread_t t1,
const uv_thread_t t2 
)

Definition at line 277 of file thread.c.

277  {
278  return pthread_equal(*t1, *t2);
279 }

◆ uv_thread_join()

int uv_thread_join ( uv_thread_t tid)

Definition at line 272 of file thread.c.

272  {
273  return UV__ERR(pthread_join(*tid, NULL));
274 }

◆ uv_thread_self()

uv_thread_t uv_thread_self ( void  )

Definition at line 268 of file thread.c.

268  {
269  return pthread_self();
270 }