Blender  V2.93
BLI_threads.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2006 Blender Foundation.
17  * All rights reserved.
18  */
19 
20 #pragma once
21 
26 #include <pthread.h>
27 
28 #include "BLI_sys_types.h"
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 /* for tables, button in UI, etc */
35 #define BLENDER_MAX_THREADS 1024
36 
37 struct ListBase;
38 
39 /* Threading API */
40 
41 /*this is run once at startup*/
42 void BLI_threadapi_init(void);
43 void BLI_threadapi_exit(void);
44 
45 void BLI_threadpool_init(struct ListBase *threadbase, void *(*do_thread)(void *), int tot);
46 int BLI_available_threads(struct ListBase *threadbase);
47 int BLI_threadpool_available_thread_index(struct ListBase *threadbase);
48 void BLI_threadpool_insert(struct ListBase *threadbase, void *callerdata);
49 void BLI_threadpool_remove(struct ListBase *threadbase, void *callerdata);
50 void BLI_threadpool_remove_index(struct ListBase *threadbase, int index);
51 void BLI_threadpool_clear(struct ListBase *threadbase);
52 void BLI_threadpool_end(struct ListBase *threadbase);
53 int BLI_thread_is_main(void);
54 
55 /* System Information */
56 
57 int BLI_system_thread_count(void); /* gets the number of threads the system can make use of */
60 
66 enum {
76 };
77 
78 void BLI_thread_lock(int type);
79 void BLI_thread_unlock(int type);
80 
81 /* Mutex Lock */
82 
83 typedef pthread_mutex_t ThreadMutex;
84 #define BLI_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
85 
88 
91 
95 
96 /* Spin Lock */
97 
98 /* By default we use TBB for spin lock on all platforms. When building without
99  * TBB fall-back to spin lock implementation which is native to the platform.
100  *
101  * On macOS we use mutex lock instead of spin since the spin lock has been
102  * deprecated in SDK 10.12 and is discouraged from use. */
103 
104 #ifdef WITH_TBB
105 typedef uint32_t SpinLock;
106 #elif defined(__APPLE__)
107 typedef ThreadMutex SpinLock;
108 #elif defined(_MSC_VER)
109 typedef volatile unsigned int SpinLock;
110 #else
111 typedef pthread_spinlock_t SpinLock;
112 #endif
113 
117 void BLI_spin_end(SpinLock *spin);
118 
119 /* Read/Write Mutex Lock */
120 
121 #define THREAD_LOCK_READ 1
122 #define THREAD_LOCK_WRITE 2
123 
124 #define BLI_RWLOCK_INITIALIZER PTHREAD_RWLOCK_INITIALIZER
125 
126 typedef pthread_rwlock_t ThreadRWMutex;
127 
130 
133 
134 void BLI_rw_mutex_lock(ThreadRWMutex *mutex, int mode);
136 
137 /* Ticket Mutex Lock
138  *
139  * This is a 'fair' mutex in that it will grant the lock to the first thread
140  * that requests it. */
141 
142 typedef struct TicketMutex TicketMutex;
143 
145 void BLI_ticket_mutex_free(TicketMutex *ticket);
146 void BLI_ticket_mutex_lock(TicketMutex *ticket);
148 
149 /* Condition */
150 
151 typedef pthread_cond_t ThreadCondition;
152 
159 
160 /* ThreadWorkQueue
161  *
162  * Thread-safe work queue to push work/pointers between threads. */
163 
164 typedef struct ThreadQueue ThreadQueue;
165 
168 
169 void BLI_thread_queue_push(ThreadQueue *queue, void *work);
174 
177 
178 /* Thread local storage */
179 
180 #if defined(__APPLE__)
181 # define ThreadLocal(type) pthread_key_t
182 # define BLI_thread_local_create(name) pthread_key_create(&name, NULL)
183 # define BLI_thread_local_delete(name) pthread_key_delete(name)
184 # define BLI_thread_local_get(name) pthread_getspecific(name)
185 # define BLI_thread_local_set(name, value) pthread_setspecific(name, value)
186 #else /* defined(__APPLE__) */
187 # ifdef _MSC_VER
188 # define ThreadLocal(type) __declspec(thread) type
189 # else
190 # define ThreadLocal(type) __thread type
191 # endif
192 # define BLI_thread_local_create(name)
193 # define BLI_thread_local_delete(name)
194 # define BLI_thread_local_get(name) name
195 # define BLI_thread_local_set(name, value) name = value
196 #endif /* defined(__APPLE__) */
197 
198 /* **** Special functions to help performance on crazy NUMA setups. **** */
199 
200 /* Make sure process/thread is using NUMA node with fast memory access. */
203 
204 #ifdef __cplusplus
205 }
206 #endif
void BLI_condition_notify_all(ThreadCondition *cond)
Definition: threads.cc:622
bool BLI_mutex_trylock(ThreadMutex *mutex)
Definition: threads.cc:411
void BLI_rw_mutex_end(ThreadRWMutex *mutex)
Definition: threads.cc:531
void BLI_thread_queue_push(ThreadQueue *queue, void *work)
Definition: threads.cc:669
pthread_spinlock_t SpinLock
Definition: BLI_threads.h:111
void BLI_thread_unlock(int type)
Definition: threads.cc:389
void BLI_ticket_mutex_unlock(TicketMutex *ticket)
Definition: threads.cc:590
void BLI_mutex_end(ThreadMutex *mutex)
Definition: threads.cc:416
void BLI_mutex_free(ThreadMutex *mutex)
Definition: threads.cc:428
pthread_rwlock_t ThreadRWMutex
Definition: BLI_threads.h:126
void BLI_thread_lock(int type)
Definition: threads.cc:384
void BLI_threadpool_remove(struct ListBase *threadbase, void *callerdata)
Definition: threads.cc:252
void BLI_threadapi_init(void)
Definition: threads.cc:143
void BLI_thread_put_process_on_fast_node(void)
Definition: threads.cc:902
void BLI_thread_put_thread_on_fast_node(void)
Definition: threads.cc:918
void BLI_condition_wait(ThreadCondition *cond, ThreadMutex *mutex)
Definition: threads.cc:607
void BLI_threadpool_init(struct ListBase *threadbase, void *(*do_thread)(void *), int tot)
Definition: threads.cc:159
void * BLI_thread_queue_pop_timeout(ThreadQueue *queue, int ms)
Definition: threads.cc:739
void BLI_mutex_init(ThreadMutex *mutex)
Definition: threads.cc:396
void BLI_system_num_threads_override_set(int num)
Definition: threads.cc:345
pthread_cond_t ThreadCondition
Definition: BLI_threads.h:151
void BLI_condition_end(ThreadCondition *cond)
Definition: threads.cc:627
int BLI_system_thread_count(void)
Definition: threads.cc:309
void BLI_thread_queue_free(ThreadQueue *queue)
Definition: threads.cc:657
int BLI_system_num_threads_override_get(void)
Definition: threads.cc:350
void BLI_threadapi_exit(void)
Definition: threads.cc:151
void BLI_threadpool_end(struct ListBase *threadbase)
Definition: threads.cc:289
void BLI_rw_mutex_lock(ThreadRWMutex *mutex, int mode)
Definition: threads.cc:516
void BLI_ticket_mutex_lock(TicketMutex *ticket)
Definition: threads.cc:576
void BLI_condition_notify_one(ThreadCondition *cond)
Definition: threads.cc:617
bool BLI_thread_queue_is_empty(ThreadQueue *queue)
Definition: threads.cc:784
void BLI_ticket_mutex_free(TicketMutex *ticket)
Definition: threads.cc:569
void BLI_condition_wait_global_mutex(ThreadCondition *cond, const int type)
Definition: threads.cc:612
ThreadMutex * BLI_mutex_alloc(void)
Definition: threads.cc:421
int BLI_thread_is_main(void)
Definition: threads.cc:234
void BLI_condition_init(ThreadCondition *cond)
Definition: threads.cc:602
@ LOCK_NODES
Definition: BLI_threads.h:71
@ LOCK_VIEW3D
Definition: BLI_threads.h:75
@ LOCK_DRAW_IMAGE
Definition: BLI_threads.h:68
@ LOCK_COLORMANAGE
Definition: BLI_threads.h:73
@ LOCK_MOVIECLIP
Definition: BLI_threads.h:72
@ LOCK_CUSTOM1
Definition: BLI_threads.h:70
@ LOCK_IMAGE
Definition: BLI_threads.h:67
@ LOCK_VIEWER
Definition: BLI_threads.h:69
@ LOCK_FFTW
Definition: BLI_threads.h:74
void BLI_mutex_lock(ThreadMutex *mutex)
Definition: threads.cc:401
void BLI_thread_queue_nowait(ThreadQueue *queue)
Definition: threads.cc:795
void BLI_thread_queue_wait_finish(ThreadQueue *queue)
Definition: threads.cc:806
void BLI_mutex_unlock(ThreadMutex *mutex)
Definition: threads.cc:406
void BLI_rw_mutex_init(ThreadRWMutex *mutex)
Definition: threads.cc:511
void BLI_threadpool_remove_index(struct ListBase *threadbase, int index)
Definition: threads.cc:263
void BLI_threadpool_clear(struct ListBase *threadbase)
Definition: threads.cc:278
int BLI_available_threads(struct ListBase *threadbase)
Definition: threads.cc:193
ThreadRWMutex * BLI_rw_mutex_alloc(void)
Definition: threads.cc:536
int BLI_threadpool_available_thread_index(struct ListBase *threadbase)
Definition: threads.cc:207
void BLI_spin_init(SpinLock *spin)
Definition: threads.cc:447
void BLI_spin_unlock(SpinLock *spin)
Definition: threads.cc:480
void * BLI_thread_queue_pop(ThreadQueue *queue)
Definition: threads.cc:680
void BLI_spin_lock(SpinLock *spin)
Definition: threads.cc:461
void BLI_rw_mutex_free(ThreadRWMutex *mutex)
Definition: threads.cc:544
void BLI_threadpool_insert(struct ListBase *threadbase, void *callerdata)
Definition: threads.cc:239
ThreadQueue * BLI_thread_queue_init(void)
Definition: threads.cc:643
void BLI_rw_mutex_unlock(ThreadRWMutex *mutex)
Definition: threads.cc:526
pthread_mutex_t ThreadMutex
Definition: BLI_threads.h:83
void BLI_spin_end(SpinLock *spin)
Definition: threads.cc:495
TicketMutex * BLI_ticket_mutex_alloc(void)
Definition: threads.cc:558
int BLI_thread_queue_len(ThreadQueue *queue)
Definition: threads.cc:773
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:152
ThreadQueue * queue
all scheduled work for the cpu
unsigned int uint32_t
Definition: stdint.h:83
pthread_cond_t cond
Definition: threads.cc:553