Blender V5.3
BLI_threads.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2006 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
10
11#include <pthread.h>
12
13#include "BLI_listbase.hh"
14#include "BLI_sys_types.hh"
15
16namespace blender {
17
19#define BLENDER_MAX_THREADS 1024
20
21struct ThreadSlot;
22
23/* Threading API */
24
30
35void BLI_threadpool_init(ListBaseT<ThreadSlot> *threadbase, void *(*do_thread)(void *), int tot);
39int BLI_available_threads(ListBaseT<ThreadSlot> *threadbase);
43int BLI_threadpool_available_thread_index(ListBaseT<ThreadSlot> *threadbase);
44void BLI_threadpool_insert(ListBaseT<ThreadSlot> *threadbase, void *callerdata);
45void BLI_threadpool_remove(ListBaseT<ThreadSlot> *threadbase, void *callerdata);
46void BLI_threadpool_remove_index(ListBaseT<ThreadSlot> *threadbase, int index);
47void BLI_threadpool_clear(ListBaseT<ThreadSlot> *threadbase);
48void BLI_threadpool_end(ListBaseT<ThreadSlot> *threadbase);
50
51/* System Information */
52
59
65enum {
74};
75
76void BLI_thread_lock(int type);
77void BLI_thread_unlock(int type);
78
79/* Mutex Lock */
80
81typedef pthread_mutex_t ThreadMutex;
82#define BLI_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
83
84void BLI_mutex_init(ThreadMutex *mutex);
85void BLI_mutex_end(ThreadMutex *mutex);
86
88void BLI_mutex_free(ThreadMutex *mutex);
89
90void BLI_mutex_lock(ThreadMutex *mutex);
92void BLI_mutex_unlock(ThreadMutex *mutex);
93
94/* Spin Lock */
95
96/* By default we use TBB for spin lock on all platforms. When building without
97 * TBB fall-back to spin lock implementation which is native to the platform.
98 *
99 * On macOS we use mutex lock instead of spin since the spin lock has been
100 * deprecated in SDK 10.12 and is discouraged from use. */
101
102#ifdef WITH_TBB
103typedef uint32_t SpinLock;
104#elif defined(__APPLE__)
105typedef ThreadMutex SpinLock;
106#elif defined(_MSC_VER)
107typedef volatile unsigned int SpinLock;
108#else
109typedef pthread_spinlock_t SpinLock;
110#endif
111
112void BLI_spin_init(SpinLock *spin);
113void BLI_spin_lock(SpinLock *spin);
114void BLI_spin_unlock(SpinLock *spin);
115void BLI_spin_end(SpinLock *spin);
116
117/* Read/Write Mutex Lock */
118
119#define THREAD_LOCK_READ 1
120#define THREAD_LOCK_WRITE 2
121
122#define BLI_RWLOCK_INITIALIZER PTHREAD_RWLOCK_INITIALIZER
123
124typedef pthread_rwlock_t ThreadRWMutex;
125
128
131
132void BLI_rw_mutex_lock(ThreadRWMutex *mutex, int mode);
134
135/* Ticket Mutex Lock
136 *
137 * This is a 'fair' mutex in that it will grant the lock to the first thread
138 * that requests it. */
139
140struct TicketMutex;
141
147
148/* Condition */
149
150typedef pthread_cond_t ThreadCondition;
151
158
159/* ThreadWorkQueue
160 *
161 * Thread-safe work queue to push work/pointers between threads. */
162
163struct ThreadQueue;
164
170
174ThreadQueue *BLI_thread_queue_init();
175
180void BLI_thread_queue_free(ThreadQueue *queue);
181
190uint64_t BLI_thread_queue_push(ThreadQueue *queue, void *work, ThreadQueueWorkPriority priority);
191
197bool BLI_thread_queue_cancel_work(ThreadQueue *queue, uint64_t work_id);
198
206void *BLI_thread_queue_pop(ThreadQueue *queue);
207
215void *BLI_thread_queue_pop_timeout(ThreadQueue *queue, int ms);
216
220int BLI_thread_queue_len(ThreadQueue *queue);
221
225bool BLI_thread_queue_is_empty(ThreadQueue *queue);
226
230void BLI_thread_queue_wait_finish(ThreadQueue *queue);
231
236void BLI_thread_queue_nowait(ThreadQueue *queue);
237
238/* Thread local storage */
239
240#if defined(__APPLE__)
241# define ThreadLocal(type) pthread_key_t
242# define BLI_thread_local_create(name) pthread_key_create(&name, NULL)
243# define BLI_thread_local_delete(name) pthread_key_delete(name)
244# define BLI_thread_local_get(name) pthread_getspecific(name)
245# define BLI_thread_local_set(name, value) pthread_setspecific(name, value)
246#else /* defined(__APPLE__) */
247# ifdef _MSC_VER
248# define ThreadLocal(type) __declspec(thread) type
249# else
250# define ThreadLocal(type) __thread type
251# endif
252# define BLI_thread_local_create(name)
253# define BLI_thread_local_delete(name)
254# define BLI_thread_local_get(name) name
255# define BLI_thread_local_set(name, value) name = value
256#endif /* defined(__APPLE__) */
257
258} // namespace blender
unsigned long long int uint64_t
#define index(i)
BMesh Iterators.
void BLI_thread_queue_nowait(ThreadQueue *queue)
Definition threads.cc:856
int BLI_available_threads(ListBaseT< ThreadSlot > *threadbase)
Definition threads.cc:148
void BLI_threadapi_exit()
Definition threads.cc:121
void BLI_ticket_mutex_lock(TicketMutex *ticket)
Definition threads.cc:555
ThreadQueueWorkPriority
@ BLI_THREAD_QUEUE_WORK_PRIORITY_HIGH
@ BLI_THREAD_QUEUE_WORK_PRIORITY_NORMAL
@ BLI_THREAD_QUEUE_WORK_PRIORITY_LOW
pthread_spinlock_t SpinLock
void BLI_condition_init(ThreadCondition *cond)
Definition threads.cc:578
void * BLI_thread_queue_pop_timeout(ThreadQueue *queue, int ms)
Definition threads.cc:787
ThreadRWMutex * BLI_rw_mutex_alloc()
Definition threads.cc:490
bool BLI_mutex_trylock(ThreadMutex *mutex)
Definition threads.cc:358
bool BLI_thread_queue_cancel_work(ThreadQueue *queue, uint64_t work_id)
Definition threads.cc:687
void BLI_mutex_unlock(ThreadMutex *mutex)
Definition threads.cc:353
void BLI_rw_mutex_free(ThreadRWMutex *mutex)
Definition threads.cc:497
void BLI_thread_queue_wait_finish(ThreadQueue *queue)
Definition threads.cc:867
void BLI_threadpool_insert(ListBaseT< ThreadSlot > *threadbase, void *callerdata)
Definition threads.cc:189
void BLI_system_num_threads_override_set(int num)
Definition threads.cc:294
bool BLI_thread_queue_is_empty(ThreadQueue *queue)
Definition threads.cc:844
void BLI_threadpool_end(ListBaseT< ThreadSlot > *threadbase)
Definition threads.cc:239
int BLI_threadpool_available_thread_index(ListBaseT< ThreadSlot > *threadbase)
Definition threads.cc:161
ThreadMutex * BLI_mutex_alloc()
Definition threads.cc:368
@ LOCK_DRAW_IMAGE
@ LOCK_MOVIECLIP
void BLI_condition_wait(ThreadCondition *cond, ThreadMutex *mutex)
Definition threads.cc:583
void BLI_rw_mutex_lock(ThreadRWMutex *mutex, int mode)
Definition threads.cc:470
void BLI_threadapi_init()
Definition threads.cc:116
void BLI_rw_mutex_init(ThreadRWMutex *mutex)
Definition threads.cc:465
void BLI_condition_end(ThreadCondition *cond)
Definition threads.cc:603
int BLI_system_thread_count()
Definition threads.cc:258
unsigned int uint32_t
Definition BKE_text.h:81
void BLI_condition_notify_one(ThreadCondition *cond)
Definition threads.cc:593
void BLI_spin_end(SpinLock *spin)
Definition threads.cc:448
void BLI_mutex_lock(ThreadMutex *mutex)
Definition threads.cc:348
void BLI_rw_mutex_unlock(ThreadRWMutex *mutex)
Definition threads.cc:480
void BLI_condition_wait_global_mutex(ThreadCondition *cond, int type)
Definition threads.cc:588
TicketMutex * BLI_ticket_mutex_alloc()
Definition threads.cc:513
void BLI_ticket_mutex_free(TicketMutex *ticket)
Definition threads.cc:523
int BLI_thread_is_main()
Definition threads.cc:184
void BLI_thread_queue_free(ThreadQueue *queue)
Definition threads.cc:638
ThreadQueue * BLI_thread_queue_init()
Definition threads.cc:627
ATTR_WARN_UNUSED_RESULT const size_t num
void * BLI_thread_queue_pop(ThreadQueue *queue)
Definition threads.cc:717
void BLI_threadpool_remove(ListBaseT< ThreadSlot > *threadbase, void *callerdata)
Definition threads.cc:202
void BLI_mutex_end(ThreadMutex *mutex)
Definition threads.cc:363
void BLI_condition_notify_all(ThreadCondition *cond)
Definition threads.cc:598
void BLI_ticket_mutex_unlock(TicketMutex *ticket)
Definition threads.cc:565
void BLI_rw_mutex_end(ThreadRWMutex *mutex)
Definition threads.cc:485
void BLI_spin_unlock(SpinLock *spin)
Definition threads.cc:433
void BLI_spin_init(SpinLock *spin)
Definition threads.cc:394
void BLI_mutex_free(ThreadMutex *mutex)
Definition threads.cc:375
int BLI_system_num_threads_override_get()
Definition threads.cc:299
pthread_cond_t ThreadCondition
pthread_mutex_t ThreadMutex
int BLI_thread_queue_len(ThreadQueue *queue)
Definition threads.cc:832
bool BLI_ticket_mutex_lock_check_recursive(TicketMutex *ticket)
Definition threads.cc:560
void BLI_thread_unlock(int type)
Definition threads.cc:336
uint64_t BLI_thread_queue_push(ThreadQueue *queue, void *work, ThreadQueueWorkPriority priority)
Definition threads.cc:648
pthread_rwlock_t ThreadRWMutex
void BLI_spin_lock(SpinLock *spin)
Definition threads.cc:408
void BLI_threadpool_remove_index(ListBaseT< ThreadSlot > *threadbase, int index)
Definition threads.cc:213
void BLI_mutex_init(ThreadMutex *mutex)
Definition threads.cc:343
void BLI_threadpool_init(ListBaseT< ThreadSlot > *threadbase, void *(*do_thread)(void *), int tot)
Definition threads.cc:123
void BLI_thread_lock(int type)
Definition threads.cc:331
void BLI_threadpool_clear(ListBaseT< ThreadSlot > *threadbase)
Definition threads.cc:228