Rizin
unix-like reverse engineering framework and cli tools
thread_sem.c File Reference
#include "thread.h"

Go to the source code of this file.

Macros

#define RZ_SEM_NAMED_ONLY   0
 

Functions

RZ_API RZ_OWN RzThreadSemaphorerz_th_sem_new (unsigned int initial)
 Allocates and initialize a RzThreadSemaphore structure. More...
 
RZ_API void rz_th_sem_free (RZ_NULLABLE RzThreadSemaphore *sem)
 Frees a RzThreadSemaphore struct. More...
 
RZ_API void rz_th_sem_post (RZ_NONNULL RzThreadSemaphore *sem)
 increments (releases) a semaphore More...
 
RZ_API void rz_th_sem_wait (RZ_NONNULL RzThreadSemaphore *sem)
 Decrements (acquires) the semaphore (waits indefinetely) More...
 

Macro Definition Documentation

◆ RZ_SEM_NAMED_ONLY

#define RZ_SEM_NAMED_ONLY   0

Definition at line 11 of file thread_sem.c.

Function Documentation

◆ rz_th_sem_free()

RZ_API void rz_th_sem_free ( RZ_NULLABLE RzThreadSemaphore sem)

Frees a RzThreadSemaphore struct.

Parameters
semThe RzThreadSemaphore to free

Definition at line 73 of file thread_sem.c.

73  {
74  if (!sem) {
75  return;
76  }
77 #if HAVE_PTHREAD
78  if (sem->sem) {
79 #if RZ_SEM_NAMED_ONLY
80  sem_close(sem->sem);
81 #else
82  sem_destroy(sem->sem);
83  free(sem->sem);
84 #endif
85  }
86 #elif __WINDOWS__
87  CloseHandle(sem->sem);
88 #endif
89  free(sem);
90 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
int sem_destroy(UV_PLATFORM_SEM_T *semid)

References free(), and sem_destroy().

Referenced by task_free().

◆ rz_th_sem_new()

RZ_API RZ_OWN RzThreadSemaphore* rz_th_sem_new ( unsigned int  initial)

Allocates and initialize a RzThreadSemaphore structure.

Parameters
initialThe initial status of the semaphore
Returns
On success returns a valid RzThreadSemaphore pointer, otherwise NULL

Definition at line 26 of file thread_sem.c.

26  {
28  if (!sem) {
29  return NULL;
30  }
31 #if HAVE_PTHREAD
32 #if RZ_SEM_NAMED_ONLY
33  uuid_t uuid;
34  uuid_generate(uuid);
35  char name[38];
36  name[0] = '/';
37  uuid_unparse(uuid, name + 1);
38  if (strlen(name) > RZ_SEM_NAME_LEN_MAX - 1) {
39  name[RZ_SEM_NAME_LEN_MAX - 1] = '\0';
40  }
41  sem->sem = sem_open(name, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR, initial);
42  if (sem->sem == SEM_FAILED) {
43  free(sem);
44  return NULL;
45  }
46 #else
47  sem->sem = malloc(sizeof(sem_t));
48  if (!sem->sem) {
49  free(sem);
50  return NULL;
51  }
52  if (sem_init(sem->sem, 0, initial) != 0) {
53  free(sem->sem);
54  free(sem);
55  return NULL;
56  }
57 #endif
58 #elif __WINDOWS__
59  sem->sem = CreateSemaphore(NULL, (LONG)initial, ST32_MAX, NULL);
60  if (!sem->sem) {
61  free(sem);
62  return NULL;
63  }
64 #endif
65  return sem;
66 }
#define NULL
Definition: cris-opc.c:27
#define LONG
void * malloc(size_t size)
Definition: malloc.c:123
int sem_init(UV_PLATFORM_SEM_T *semid, int pshared, unsigned int value)
#define RZ_NEW(x)
Definition: rz_types.h:285
#define ST32_MAX
Definition: rz_types_base.h:97
#define O_CREAT
Definition: sftypes.h:489
#define O_EXCL
Definition: sftypes.h:490
Definition: z80asm.h:102
RZ_TH_SEM_T sem
Definition: thread.h:73

References free(), LONG, malloc(), NULL, O_CREAT, O_EXCL, RZ_NEW, rz_th_sem_t::sem, sem_init(), and ST32_MAX.

Referenced by rz_core_task_enqueue().

◆ rz_th_sem_post()

RZ_API void rz_th_sem_post ( RZ_NONNULL RzThreadSemaphore sem)

increments (releases) a semaphore

Parameters
semThe RzThreadSemaphore to increment (release)

Definition at line 97 of file thread_sem.c.

97  {
98  rz_return_if_fail(sem);
99 #if HAVE_PTHREAD
100  sem_post(sem->sem);
101 #elif __WINDOWS__
102  ReleaseSemaphore(sem->sem, 1, NULL);
103 #endif
104 }
int sem_post(UV_PLATFORM_SEM_T *semid)
#define rz_return_if_fail(expr)
Definition: rz_assert.h:100

References NULL, rz_return_if_fail, and sem_post().

Referenced by task_join(), and task_run_thread().

◆ rz_th_sem_wait()

RZ_API void rz_th_sem_wait ( RZ_NONNULL RzThreadSemaphore sem)

Decrements (acquires) the semaphore (waits indefinetely)

Parameters
semThe RzThreadSemaphore to decrement (acquire)

Definition at line 111 of file thread_sem.c.

111  {
112  rz_return_if_fail(sem);
113 #if HAVE_PTHREAD
114  sem_wait(sem->sem);
115 #elif __WINDOWS__
116  WaitForSingleObject(sem->sem, INFINITE);
117 #endif
118 }
int sem_wait(UV_PLATFORM_SEM_T *semid)

References rz_return_if_fail, and sem_wait().

Referenced by rz_core_task_enqueue(), and task_join().