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

Go to the source code of this file.

Functions

RZ_API RZ_OWN RzThreadLockrz_th_lock_new (bool recursive)
 Allocates and initialize a RzThreadLock structure. More...
 
RZ_API void rz_th_lock_enter (RZ_NONNULL RzThreadLock *thl)
 Acquires a RzThreadLock structure. More...
 
RZ_API bool rz_th_lock_tryenter (RZ_NONNULL RzThreadLock *thl)
 Tries to acquire a RzThreadLock structure. More...
 
RZ_API void rz_th_lock_leave (RZ_NONNULL RzThreadLock *thl)
 Releases a RzThreadLock structure. More...
 
RZ_API void rz_th_lock_free (RZ_NULLABLE RzThreadLock *thl)
 Frees a RzThreadLock structure. More...
 

Function Documentation

◆ rz_th_lock_enter()

RZ_API void rz_th_lock_enter ( RZ_NONNULL RzThreadLock thl)

Acquires a RzThreadLock structure.

Parameters
thlThe RzThreadLock to acquire

Definition at line 45 of file thread_lock.c.

45  {
46  rz_return_if_fail(thl);
47 #if HAVE_PTHREAD
48  pthread_mutex_lock(&thl->lock);
49 #elif __WINDOWS__
50  EnterCriticalSection(&thl->lock);
51 #endif
52 }
#define rz_return_if_fail(expr)
Definition: rz_assert.h:100

References rz_return_if_fail.

Referenced by _sendResponsePacket(), backtrace_windows(), basefind_thread_runner(), gdbr_lock_enter(), iob_net_write(), iob_read(), iob_write(), rz_atomic_bool_get(), rz_atomic_bool_set(), rz_core_task_schedule(), rz_path_prefix(), rz_path_set_prefix(), rz_test_main(), rz_th_queue_is_empty(), rz_th_queue_is_full(), rz_th_queue_pop(), rz_th_queue_push(), rz_th_queue_wait_pop(), shared_data_read_at(), shared_ht_up_insert(), subprocess_lock(), task_wakeup(), tasks_lock_enter(), and worker_th().

◆ rz_th_lock_free()

RZ_API void rz_th_lock_free ( RZ_NULLABLE RzThreadLock thl)

Frees a RzThreadLock structure.

Parameters
thlThe RzThreadLock to free

Definition at line 89 of file thread_lock.c.

89  {
90  if (!thl) {
91  return;
92  }
93 #if HAVE_PTHREAD
94  pthread_mutex_destroy(&thl->lock);
95 #elif __WINDOWS__
96  DeleteCriticalSection(&thl->lock);
97 #endif
98  free(thl);
99 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130

References free().

Referenced by gdbr_cleanup(), rz_atomic_bool_free(), rz_basefind(), rz_bin_file_strings(), rz_core_task_scheduler_fini(), rz_subprocess_fini(), rz_subprocess_init(), rz_test_main(), rz_th_queue_free(), task_free(), and winkd_kdctx_free().

◆ rz_th_lock_leave()

RZ_API void rz_th_lock_leave ( RZ_NONNULL RzThreadLock thl)

Releases a RzThreadLock structure.

Parameters
thlThe RzThreadLock to release

Definition at line 75 of file thread_lock.c.

75  {
76  rz_return_if_fail(thl);
77 #if HAVE_PTHREAD
78  pthread_mutex_unlock(&thl->lock);
79 #elif __WINDOWS__
80  LeaveCriticalSection(&thl->lock);
81 #endif
82 }

References rz_return_if_fail.

Referenced by _sendResponsePacket(), backtrace_windows(), basefind_thread_runner(), gdbr_lock_leave(), iob_net_write(), iob_read(), iob_write(), rz_atomic_bool_get(), rz_atomic_bool_set(), rz_core_task_schedule(), rz_path_prefix(), rz_path_set_prefix(), rz_test_main(), rz_th_queue_is_empty(), rz_th_queue_is_full(), rz_th_queue_pop(), rz_th_queue_push(), rz_th_queue_wait_pop(), shared_data_read_at(), shared_ht_up_insert(), subprocess_unlock(), task_wakeup(), tasks_lock_leave(), winkd_lock_leave(), and worker_th().

◆ rz_th_lock_new()

RZ_API RZ_OWN RzThreadLock* rz_th_lock_new ( bool  recursive)

Allocates and initialize a RzThreadLock structure.

Parameters
recursiveSet it to true for recursive locking (on windows, all the locks are always recursive).
Returns
On success returns a valid pointer, otherwise NULL

Definition at line 14 of file thread_lock.c.

14  {
16  if (!thl) {
17  return NULL;
18  }
19 #if HAVE_PTHREAD
20  if (recursive) {
21  pthread_mutexattr_t attr;
22  pthread_mutexattr_init(&attr);
23 #if !defined(__GLIBC__) || __USE_UNIX98__
24  pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
25 #else
26  pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
27 #endif
28  pthread_mutex_init(&thl->lock, &attr);
29  } else {
30  pthread_mutex_init(&thl->lock, NULL);
31  }
32 #elif __WINDOWS__
33  // Windows critical sections always accept recursive
34  // access and it cannot be configured in any other way.
35  InitializeCriticalSection(&thl->lock);
36 #endif
37  return thl;
38 }
#define NULL
Definition: cris-opc.c:27
#define RZ_NEW0(x)
Definition: rz_types.h:284
RZ_TH_LOCK_T lock
Definition: thread.h:77

References rz_th_lock_t::lock, NULL, and RZ_NEW0.

Referenced by backtrace_windows(), gdbr_init(), iob_net_open(), iob_read(), iob_write(), rz_atomic_bool_new(), rz_basefind(), rz_bin_file_strings(), rz_core_task_new(), rz_core_task_scheduler_init(), rz_subprocess_init(), rz_test_main(), rz_th_queue_new(), and winkd_kdctx_new().

◆ rz_th_lock_tryenter()

RZ_API bool rz_th_lock_tryenter ( RZ_NONNULL RzThreadLock thl)

Tries to acquire a RzThreadLock structure.

Parameters
thlThe RzThreadLock to try to acquire
Returns
On success returns true, otherwise false

Definition at line 61 of file thread_lock.c.

61  {
62  rz_return_val_if_fail(thl, false);
63 #if HAVE_PTHREAD
64  return !pthread_mutex_trylock(&thl->lock);
65 #elif __WINDOWS__
66  return TryEnterCriticalSection(&thl->lock);
67 #endif
68 }
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108

References rz_return_val_if_fail.

Referenced by gdbr_lock_tryenter(), and winkd_lock_enter().