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

Go to the source code of this file.

Functions

RZ_API RZ_OWN RzThreadCondrz_th_cond_new (void)
 Condition variables are intended to be used to communicate changes in the state of data shared between threads. Condition variables are always associated with a mutex to provide synchronized access to the shared data. More...
 
RZ_API void rz_th_cond_signal (RZ_NONNULL RzThreadCond *cond)
 This function shall unblock at least one of the threads that are blocked on the specified condition. More...
 
RZ_API void rz_th_cond_signal_all (RZ_NONNULL RzThreadCond *cond)
 This function shall unblock all threads currently blocked on the specified condition. More...
 
RZ_API void rz_th_cond_wait (RZ_NONNULL RzThreadCond *cond, RZ_NONNULL RzThreadLock *lock)
 The function shall block on a condition variable and shall be called with RzThreadLock locked by the calling thread. More...
 
RZ_API void rz_th_cond_free (RZ_NULLABLE RzThreadCond *cond)
 Frees a RzThreadCond struct. More...
 

Function Documentation

◆ rz_th_cond_free()

RZ_API void rz_th_cond_free ( RZ_NULLABLE RzThreadCond cond)

Frees a RzThreadCond struct.

Parameters
condThe RzThreadCond to free

Definition at line 77 of file thread_cond.c.

77  {
78  if (!cond) {
79  return;
80  }
81 #if HAVE_PTHREAD
82  pthread_cond_destroy(&cond->cond);
83 #endif
84  free(cond);
85 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
#define cond(bop, top, mask, flags)

References cond, and free().

Referenced by rz_test_main(), rz_th_queue_free(), and task_free().

◆ rz_th_cond_new()

RZ_API RZ_OWN RzThreadCond* rz_th_cond_new ( void  )

Condition variables are intended to be used to communicate changes in the state of data shared between threads. Condition variables are always associated with a mutex to provide synchronized access to the shared data.

Returns
On success returns a valid pointer to a RzThreadCond structure.

Definition at line 13 of file thread_cond.c.

13  {
15  if (!cond) {
16  return NULL;
17  }
18 #if HAVE_PTHREAD
19  if (pthread_cond_init(&cond->cond, NULL) != 0) {
20  free(cond);
21  return NULL;
22  }
23 #elif __WINDOWS__
24  InitializeConditionVariable(&cond->cond);
25 #endif
26  return cond;
27 }
#define NULL
Definition: cris-opc.c:27
#define RZ_NEW0(x)
Definition: rz_types.h:284

References cond, free(), NULL, and RZ_NEW0.

Referenced by rz_core_task_new(), rz_test_main(), and rz_th_queue_new().

◆ rz_th_cond_signal()

RZ_API void rz_th_cond_signal ( RZ_NONNULL RzThreadCond cond)

This function shall unblock at least one of the threads that are blocked on the specified condition.

Parameters
condThe RzThreadCond to use for signalling a waiting thread

Definition at line 34 of file thread_cond.c.

34  {
36 #if HAVE_PTHREAD
37  pthread_cond_signal(&cond->cond);
38 #elif __WINDOWS__
39  WakeConditionVariable(&cond->cond);
40 #endif
41 }
#define rz_return_if_fail(expr)
Definition: rz_assert.h:100

References cond, and rz_return_if_fail.

Referenced by rz_core_task_schedule(), rz_th_queue_push(), and worker_th().

◆ rz_th_cond_signal_all()

RZ_API void rz_th_cond_signal_all ( RZ_NONNULL RzThreadCond cond)

This function shall unblock all threads currently blocked on the specified condition.

Parameters
condThe RzThreadCond to use for signalling all waiting threads

Definition at line 48 of file thread_cond.c.

48  {
50 #if HAVE_PTHREAD
51  pthread_cond_broadcast(&cond->cond);
52 #elif __WINDOWS__
53  WakeAllConditionVariable(&cond->cond);
54 #endif
55 }

References cond, and rz_return_if_fail.

◆ rz_th_cond_wait()

RZ_API void rz_th_cond_wait ( RZ_NONNULL RzThreadCond cond,
RZ_NONNULL RzThreadLock lock 
)

The function shall block on a condition variable and shall be called with RzThreadLock locked by the calling thread.

Parameters
condThe RzThreadCond to use for waiting the signal
lockThe RzThreadLock lock to use (the lock must be already taken by the thread)

Definition at line 63 of file thread_cond.c.

63  {
65 #if HAVE_PTHREAD
66  pthread_cond_wait(&cond->cond, &lock->lock);
67 #elif __WINDOWS__
68  SleepConditionVariableCS(&cond->cond, &lock->lock, INFINITE);
69 #endif
70 }
static void lock(volatile int *lk)
Definition: malloc.c:61

References cond, lock(), and rz_return_if_fail.

Referenced by rz_core_task_schedule(), rz_test_main(), rz_th_queue_wait_pop(), and task_wakeup().