Rizin
unix-like reverse engineering framework and cli tools
thread_pool.c File Reference
#include <rz_th.h>
#include "thread.h"

Go to the source code of this file.

Classes

struct  rz_th_pool_t
 RzThreadPool is a structure which handles n-threads threads. More...
 

Functions

RZ_API size_t rz_th_physical_core_number ()
 Returns the number of available physical cores of the host machine. More...
 
RZ_API size_t rz_th_request_physical_cores (size_t max_cores)
 Returns the maximum number of cores available regardless of the number of cores requested. When set to 0, it will be the max number of physical cores. More...
 
RZ_API RZ_OWN RzThreadPoolrz_th_pool_new (size_t max_threads)
 returns a new RzThreadPool structure with a pool of thread More...
 
RZ_API void rz_th_pool_free (RZ_NULLABLE RzThreadPool *pool)
 Kills (and frees) the threads and frees the RzThreadPool struct. More...
 
RZ_API bool rz_th_pool_add_thread (RZ_NONNULL RzThreadPool *pool, RZ_NONNULL RzThread *thread)
 Adds a thread to the thread pool. More...
 
RZ_API RZ_BORROW RzThreadrz_th_pool_get_thread (RZ_NONNULL RzThreadPool *pool, size_t index)
 Returns the n-th thread in the thread pool. More...
 
RZ_API bool rz_th_pool_wait (RZ_NONNULL RzThreadPool *pool)
 Waits the end of all the threads in the thread pool. More...
 
RZ_API size_t rz_th_pool_size (RZ_NONNULL RzThreadPool *pool)
 Returns the thread pool size. More...
 

Function Documentation

◆ rz_th_physical_core_number()

RZ_API size_t rz_th_physical_core_number ( )

Returns the number of available physical cores of the host machine.

Returns
The number of available physical cores (always >= 1)

Definition at line 22 of file thread_pool.c.

22  {
23 #ifdef __WINDOWS__
24  SYSTEM_INFO sysinfo;
25  GetSystemInfo(&sysinfo);
26  return sysinfo.dwNumberOfProcessors;
27 #elif __APPLE__ || __FreeBSD__ || __OpenBSD__ || __DragonFly__ || __NetBSD__
28  int os_status = 0;
29  int mib[4];
30  unsigned long n_cpus = 1;
31  size_t n_cpus_length = sizeof(n_cpus);
32 
33  /* set the mib for hw.ncpu */
34  mib[0] = CTL_HW;
35 #if __NetBSD__
36  mib[1] = HW_NCPUONLINE;
37 #elif __OpenBSD__ || __FreeBSD__ || __DragonFly__
38  mib[1] = HW_NCPU;
39 #else
40  mib[1] = HW_AVAILCPU;
41 #endif
42 
43  os_status = sysctl(mib, 2, &n_cpus, &n_cpus_length, NULL, 0);
44 
45  if (os_status != 0) {
46 #if __OpenBSD__ || __FreeBSD__
47  n_cpus = 1;
48 #else
49  // HW_AVAILCPU does not exist.
50  mib[1] = HW_NCPU;
51  os_status = sysctl(mib, 2, &n_cpus, &n_cpus_length, NULL, 0);
52  if (os_status != 0) {
53  n_cpus = 1;
54  }
55 #endif
56  }
57  // this is needed because the upper bits are set on bsd platforms
58  n_cpus &= UT32_MAX;
59 
60  return n_cpus;
61 #elif __HAIKU__
62  system_info info;
63  get_system_info(&info);
64  return info.cpu_count;
65 #else
66  return sysconf(_SC_NPROCESSORS_ONLN);
67 #endif
68 }
RzBinInfo * info(RzBinFile *bf)
Definition: bin_ne.c:86
#define NULL
Definition: cris-opc.c:27
static const char struct stat static buf struct stat static buf static vhangup int struct rusage static rusage sysinfo
Definition: sflib.h:147
#define UT32_MAX
Definition: rz_types_base.h:99

References info(), NULL, sysinfo, and UT32_MAX.

Referenced by rz_th_request_physical_cores().

◆ rz_th_pool_add_thread()

RZ_API bool rz_th_pool_add_thread ( RZ_NONNULL RzThreadPool pool,
RZ_NONNULL RzThread thread 
)

Adds a thread to the thread pool.

Parameters
RzThreadPoolThe thread pool where to add the thread
RzThreadThe thread to add to the pool
Returns
true if a slot is found, false otherwise

Definition at line 138 of file thread_pool.c.

138  {
139  rz_return_val_if_fail(pool && thread, false);
140  for (ut32 i = 0; i < pool->size; ++i) {
141  if (!pool->threads[i]) {
142  RZ_LOG_DEBUG("thread: thread %u added\n", i);
143  pool->threads[i] = thread;
144  return true;
145  }
146  }
147  return false;
148 }
lzma_index ** i
Definition: index.h:629
uint32_t ut32
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
#define RZ_LOG_DEBUG(fmtstr,...)
Definition: rz_log.h:49

References i, RZ_LOG_DEBUG, and rz_return_val_if_fail.

Referenced by create_string_search_thread(), and create_thread_interval().

◆ rz_th_pool_free()

RZ_API void rz_th_pool_free ( RZ_NULLABLE RzThreadPool pool)

Kills (and frees) the threads and frees the RzThreadPool struct.

Parameters
RzThreadPool*The thread pool to free

Definition at line 117 of file thread_pool.c.

117  {
118  if (!pool) {
119  return;
120  }
121  for (ut32 i = 0; i < pool->size; ++i) {
122  if (pool->threads[i]) {
123  rz_th_free(pool->threads[i]);
124  pool->threads[i] = NULL;
125  }
126  }
127  free(pool->threads);
128  free(pool);
129 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
RZ_API void rz_th_free(RZ_NULLABLE RzThread *th)
Frees a RzThread structure.
Definition: thread.c:246

References free(), i, NULL, and rz_th_free().

Referenced by rz_basefind(), and rz_bin_file_strings().

◆ rz_th_pool_get_thread()

RZ_API RZ_BORROW RzThread* rz_th_pool_get_thread ( RZ_NONNULL RzThreadPool pool,
size_t  index 
)

Returns the n-th thread in the thread pool.

Parameters
poolThe thread pool to use
indexThe index of the thread to get
Returns
Returns the pointer of the n-th thread in the thread pool.

Definition at line 158 of file thread_pool.c.

158  {
159  rz_return_val_if_fail(pool && index < pool->size, NULL);
160  return pool->threads[index];
161 }
voidpf void uLong size
Definition: ioapi.h:138

References NULL, and rz_return_val_if_fail.

Referenced by basefind_stop_all_search_threads(), basefind_thread_ui(), interrupt_pool(), rz_basefind(), and rz_bin_file_strings().

◆ rz_th_pool_new()

RZ_API RZ_OWN RzThreadPool* rz_th_pool_new ( size_t  max_threads)

returns a new RzThreadPool structure with a pool of thread

Returns a new RzThreadPool structure with a pool of thread limited by either the physical core number count or by the value specified by the user (if set to 0, it will be the max physical cores number)

Parameters
max_threadsThe maximum number of threads needed in the pool
Returns
RzThreadPool The RzThreadPool structure

Definition at line 96 of file thread_pool.c.

96  {
98  if (!pool) {
99  return NULL;
100  }
101 
102  pool->size = rz_th_request_physical_cores(max_threads);
103  pool->threads = RZ_NEWS0(RzThread *, pool->size);
104  if (!pool->threads) {
105  free(pool);
106  return NULL;
107  }
108 
109  return pool;
110 }
#define RZ_NEW0(x)
Definition: rz_types.h:284
#define RZ_NEWS0(x, y)
Definition: rz_types.h:282
RzThreadPool is a structure which handles n-threads threads.
Definition: thread_pool.c:12
RzThread ** threads
Definition: thread_pool.c:14
size_t size
Definition: thread_pool.c:13
Definition: thread.h:84
RZ_API size_t rz_th_request_physical_cores(size_t max_cores)
Returns the maximum number of cores available regardless of the number of cores requested....
Definition: thread_pool.c:78

References free(), NULL, RZ_NEW0, RZ_NEWS0, rz_th_request_physical_cores(), rz_th_pool_t::size, and rz_th_pool_t::threads.

Referenced by rz_basefind(), and rz_bin_file_strings().

◆ rz_th_pool_size()

RZ_API size_t rz_th_pool_size ( RZ_NONNULL RzThreadPool pool)

Returns the thread pool size.

Parameters
poolThe RzThreadPool to use
Returns
The size of the thread pool (always >= 1).

Definition at line 189 of file thread_pool.c.

189  {
190  rz_return_val_if_fail(pool, 1);
191  return pool->size;
192 }

References rz_return_val_if_fail.

Referenced by basefind_stop_all_search_threads(), basefind_thread_ui(), interrupt_pool(), rz_basefind(), and rz_bin_file_strings().

◆ rz_th_pool_wait()

RZ_API bool rz_th_pool_wait ( RZ_NONNULL RzThreadPool pool)

Waits the end of all the threads in the thread pool.

Parameters
RzThreadPoolThe thread pool to wait for
Returns
true if managed to wait all threads, otherwise false

Definition at line 170 of file thread_pool.c.

170  {
171  rz_return_val_if_fail(pool, false);
172  bool has_exited = true;
173  for (ut32 i = 0; i < pool->size; ++i) {
174  if (pool->threads[i]) {
175  RZ_LOG_DEBUG("thread: waiting for thread %u\n", i);
176  has_exited = has_exited && rz_th_wait(pool->threads[i]);
177  }
178  }
179  return has_exited;
180 }
RZ_API bool rz_th_wait(RZ_NONNULL RzThread *th)
Awaits indefinetely for a thread to join.
Definition: thread.c:231

References i, RZ_LOG_DEBUG, rz_return_val_if_fail, and rz_th_wait().

Referenced by rz_basefind(), and rz_bin_file_strings().

◆ rz_th_request_physical_cores()

RZ_API size_t rz_th_request_physical_cores ( size_t  max_cores)

Returns the maximum number of cores available regardless of the number of cores requested. When set to 0, it will be the max number of physical cores.

Parameters
[in]max_coresThe maximum number of physical cores to request
Returns
The actual max number of cores available

Definition at line 78 of file thread_pool.c.

78  {
79  size_t n_cores = rz_th_physical_core_number();
80  if (!max_cores) {
81  return n_cores;
82  }
83  return RZ_MIN(n_cores, max_cores);
84 }
#define RZ_MIN(x, y)
RZ_API size_t rz_th_physical_core_number()
Returns the number of available physical cores of the host machine.
Definition: thread_pool.c:22

References RZ_MIN, and rz_th_physical_core_number().

Referenced by rz_core_bin_basefind_print(), and rz_th_pool_new().