Blender  V3.3
BLI_threads.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2006 Blender Foundation. All rights reserved. */
3 
4 #pragma once
5 
10 #include <pthread.h>
11 
12 #include "BLI_sys_types.h"
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
19 #define BLENDER_MAX_THREADS 1024
20 
21 struct ListBase;
22 
23 /* Threading API */
24 
28 void BLI_threadapi_init(void);
29 void BLI_threadapi_exit(void);
30 
35 void BLI_threadpool_init(struct ListBase *threadbase, void *(*do_thread)(void *), int tot);
39 int BLI_available_threads(struct ListBase *threadbase);
43 int BLI_threadpool_available_thread_index(struct ListBase *threadbase);
44 void BLI_threadpool_insert(struct ListBase *threadbase, void *callerdata);
45 void BLI_threadpool_remove(struct ListBase *threadbase, void *callerdata);
46 void BLI_threadpool_remove_index(struct ListBase *threadbase, int index);
47 void BLI_threadpool_clear(struct ListBase *threadbase);
48 void BLI_threadpool_end(struct ListBase *threadbase);
49 int BLI_thread_is_main(void);
50 
51 /* System Information */
52 
56 int BLI_system_thread_count(void);
59 
65 enum {
75 };
76 
77 void BLI_thread_lock(int type);
78 void BLI_thread_unlock(int type);
79 
80 /* Mutex Lock */
81 
82 typedef pthread_mutex_t ThreadMutex;
83 #define BLI_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
84 
87 
90 
94 
95 /* Spin Lock */
96 
97 /* By default we use TBB for spin lock on all platforms. When building without
98  * TBB fall-back to spin lock implementation which is native to the platform.
99  *
100  * On macOS we use mutex lock instead of spin since the spin lock has been
101  * deprecated in SDK 10.12 and is discouraged from use. */
102 
103 #ifdef WITH_TBB
104 typedef uint32_t SpinLock;
105 #elif defined(__APPLE__)
106 typedef ThreadMutex SpinLock;
107 #elif defined(_MSC_VER)
108 typedef volatile unsigned int SpinLock;
109 #else
110 typedef pthread_spinlock_t SpinLock;
111 #endif
112 
116 void BLI_spin_end(SpinLock *spin);
117 
118 /* Read/Write Mutex Lock */
119 
120 #define THREAD_LOCK_READ 1
121 #define THREAD_LOCK_WRITE 2
122 
123 #define BLI_RWLOCK_INITIALIZER PTHREAD_RWLOCK_INITIALIZER
124 
125 typedef pthread_rwlock_t ThreadRWMutex;
126 
129 
132 
133 void BLI_rw_mutex_lock(ThreadRWMutex *mutex, int mode);
135 
136 /* Ticket Mutex Lock
137  *
138  * This is a 'fair' mutex in that it will grant the lock to the first thread
139  * that requests it. */
140 
141 typedef struct TicketMutex TicketMutex;
142 
144 void BLI_ticket_mutex_free(TicketMutex *ticket);
145 void BLI_ticket_mutex_lock(TicketMutex *ticket);
147 
148 /* Condition */
149 
150 typedef pthread_cond_t ThreadCondition;
151 
158 
159 /* ThreadWorkQueue
160  *
161  * Thread-safe work queue to push work/pointers between threads. */
162 
163 typedef struct ThreadQueue ThreadQueue;
164 
167 
168 void BLI_thread_queue_push(ThreadQueue *queue, void *work);
173 
176 
177 /* Thread local storage */
178 
179 #if defined(__APPLE__)
180 # define ThreadLocal(type) pthread_key_t
181 # define BLI_thread_local_create(name) pthread_key_create(&name, NULL)
182 # define BLI_thread_local_delete(name) pthread_key_delete(name)
183 # define BLI_thread_local_get(name) pthread_getspecific(name)
184 # define BLI_thread_local_set(name, value) pthread_setspecific(name, value)
185 #else /* defined(__APPLE__) */
186 # ifdef _MSC_VER
187 # define ThreadLocal(type) __declspec(thread) type
188 # else
189 # define ThreadLocal(type) __thread type
190 # endif
191 # define BLI_thread_local_create(name)
192 # define BLI_thread_local_delete(name)
193 # define BLI_thread_local_get(name) name
194 # define BLI_thread_local_set(name, value) name = value
195 #endif /* defined(__APPLE__) */
196 
197 #ifdef __cplusplus
198 }
199 #endif
void BLI_condition_notify_all(ThreadCondition *cond)
Definition: threads.cc:594
bool BLI_mutex_trylock(ThreadMutex *mutex)
Definition: threads.cc:383
void BLI_rw_mutex_end(ThreadRWMutex *mutex)
Definition: threads.cc:503
void BLI_thread_queue_push(ThreadQueue *queue, void *work)
Definition: threads.cc:641
pthread_spinlock_t SpinLock
Definition: BLI_threads.h:110
void BLI_thread_unlock(int type)
Definition: threads.cc:361
void BLI_ticket_mutex_unlock(TicketMutex *ticket)
Definition: threads.cc:562
@ LOCK_NODES
Definition: BLI_threads.h:70
@ LOCK_VIEW3D
Definition: BLI_threads.h:74
@ LOCK_DRAW_IMAGE
Definition: BLI_threads.h:67
@ LOCK_COLORMANAGE
Definition: BLI_threads.h:72
@ LOCK_MOVIECLIP
Definition: BLI_threads.h:71
@ LOCK_CUSTOM1
Definition: BLI_threads.h:69
@ LOCK_IMAGE
Definition: BLI_threads.h:66
@ LOCK_VIEWER
Definition: BLI_threads.h:68
@ LOCK_FFTW
Definition: BLI_threads.h:73
void BLI_mutex_end(ThreadMutex *mutex)
Definition: threads.cc:388
void BLI_mutex_free(ThreadMutex *mutex)
Definition: threads.cc:400
pthread_rwlock_t ThreadRWMutex
Definition: BLI_threads.h:125
void BLI_thread_lock(int type)
Definition: threads.cc:356
void BLI_threadpool_remove(struct ListBase *threadbase, void *callerdata)
Definition: threads.cc:225
void BLI_threadapi_init(void)
Definition: threads.cc:125
void BLI_condition_wait(ThreadCondition *cond, ThreadMutex *mutex)
Definition: threads.cc:579
void BLI_threadpool_init(struct ListBase *threadbase, void *(*do_thread)(void *), int tot)
Definition: threads.cc:134
void BLI_condition_wait_global_mutex(ThreadCondition *cond, int type)
Definition: threads.cc:584
void * BLI_thread_queue_pop_timeout(ThreadQueue *queue, int ms)
Definition: threads.cc:711
void BLI_mutex_init(ThreadMutex *mutex)
Definition: threads.cc:368
void BLI_system_num_threads_override_set(int num)
Definition: threads.cc:317
pthread_cond_t ThreadCondition
Definition: BLI_threads.h:150
void BLI_condition_end(ThreadCondition *cond)
Definition: threads.cc:599
int BLI_system_thread_count(void)
Definition: threads.cc:281
void BLI_thread_queue_free(ThreadQueue *queue)
Definition: threads.cc:629
int BLI_system_num_threads_override_get(void)
Definition: threads.cc:322
void BLI_threadapi_exit(void)
Definition: threads.cc:130
void BLI_threadpool_end(struct ListBase *threadbase)
Definition: threads.cc:262
void BLI_rw_mutex_lock(ThreadRWMutex *mutex, int mode)
Definition: threads.cc:488
void BLI_ticket_mutex_lock(TicketMutex *ticket)
Definition: threads.cc:548
void BLI_condition_notify_one(ThreadCondition *cond)
Definition: threads.cc:589
bool BLI_thread_queue_is_empty(ThreadQueue *queue)
Definition: threads.cc:756
void BLI_ticket_mutex_free(TicketMutex *ticket)
Definition: threads.cc:541
ThreadMutex * BLI_mutex_alloc(void)
Definition: threads.cc:393
int BLI_thread_is_main(void)
Definition: threads.cc:207
void BLI_condition_init(ThreadCondition *cond)
Definition: threads.cc:574
void BLI_mutex_lock(ThreadMutex *mutex)
Definition: threads.cc:373
void BLI_thread_queue_nowait(ThreadQueue *queue)
Definition: threads.cc:767
void BLI_thread_queue_wait_finish(ThreadQueue *queue)
Definition: threads.cc:778
void BLI_mutex_unlock(ThreadMutex *mutex)
Definition: threads.cc:378
void BLI_rw_mutex_init(ThreadRWMutex *mutex)
Definition: threads.cc:483
void BLI_threadpool_remove_index(struct ListBase *threadbase, int index)
Definition: threads.cc:236
void BLI_threadpool_clear(struct ListBase *threadbase)
Definition: threads.cc:251
int BLI_available_threads(struct ListBase *threadbase)
Definition: threads.cc:167
ThreadRWMutex * BLI_rw_mutex_alloc(void)
Definition: threads.cc:508
int BLI_threadpool_available_thread_index(struct ListBase *threadbase)
Definition: threads.cc:180
void BLI_spin_init(SpinLock *spin)
Definition: threads.cc:419
void BLI_spin_unlock(SpinLock *spin)
Definition: threads.cc:452
void * BLI_thread_queue_pop(ThreadQueue *queue)
Definition: threads.cc:652
void BLI_spin_lock(SpinLock *spin)
Definition: threads.cc:433
void BLI_rw_mutex_free(ThreadRWMutex *mutex)
Definition: threads.cc:516
void BLI_threadpool_insert(struct ListBase *threadbase, void *callerdata)
Definition: threads.cc:212
ThreadQueue * BLI_thread_queue_init(void)
Definition: threads.cc:615
void BLI_rw_mutex_unlock(ThreadRWMutex *mutex)
Definition: threads.cc:498
pthread_mutex_t ThreadMutex
Definition: BLI_threads.h:82
void BLI_spin_end(SpinLock *spin)
Definition: threads.cc:467
TicketMutex * BLI_ticket_mutex_alloc(void)
Definition: threads.cc:530
int BLI_thread_queue_len(ThreadQueue *queue)
Definition: threads.cc:745
ThreadMutex mutex
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
static SpinLock spin
Definition: cachefile.c:162
SyclQueue * queue
unsigned int uint32_t
Definition: stdint.h:80
pthread_cond_t cond
Definition: threads.cc:525