Rizin
unix-like reverse engineering framework and cli tools
thread_sem.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2018 thestr4ng3r <info@florianmaerkl.de>
2 // SPDX-FileCopyrightText: 2022 deroad <wargio@libero.it>
3 // SPDX-License-Identifier: LGPL-3.0-only
4 
5 #include "thread.h"
6 
7 #ifdef __APPLE__
8 #define RZ_SEM_NAMED_ONLY 1
9 #define RZ_SEM_NAME_LEN_MAX 31
10 #else
11 #define RZ_SEM_NAMED_ONLY 0
12 #endif
13 
14 #if RZ_SEM_NAMED_ONLY
15 #include <uuid/uuid.h>
16 #include <limits.h>
17 #endif
18 
26 RZ_API RZ_OWN RzThreadSemaphore *rz_th_sem_new(unsigned int initial) {
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 }
67 
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 }
91 
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 }
105 
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 }
#define RZ_API
#define NULL
Definition: cris-opc.c:27
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
#define LONG
void * malloc(size_t size)
Definition: malloc.c:123
int sem_wait(UV_PLATFORM_SEM_T *semid)
int sem_destroy(UV_PLATFORM_SEM_T *semid)
int sem_post(UV_PLATFORM_SEM_T *semid)
int sem_init(UV_PLATFORM_SEM_T *semid, int pshared, unsigned int value)
#define rz_return_if_fail(expr)
Definition: rz_assert.h:100
#define RZ_NULLABLE
Definition: rz_types.h:65
#define RZ_OWN
Definition: rz_types.h:62
#define RZ_NONNULL
Definition: rz_types.h:64
#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
RZ_API RZ_OWN RzThreadSemaphore * rz_th_sem_new(unsigned int initial)
Allocates and initialize a RzThreadSemaphore structure.
Definition: thread_sem.c:26
RZ_API void rz_th_sem_free(RZ_NULLABLE RzThreadSemaphore *sem)
Frees a RzThreadSemaphore struct.
Definition: thread_sem.c:73
RZ_API void rz_th_sem_post(RZ_NONNULL RzThreadSemaphore *sem)
increments (releases) a semaphore
Definition: thread_sem.c:97
RZ_API void rz_th_sem_wait(RZ_NONNULL RzThreadSemaphore *sem)
Decrements (acquires) the semaphore (waits indefinetely)
Definition: thread_sem.c:111