25# include <sys/timeb.h>
27#elif defined(__APPLE__)
28# include <sys/sysctl.h>
29# include <sys/types.h>
36# include <tbb/spin_mutex.h>
93static pthread_mutex_t
_image_lock = PTHREAD_MUTEX_INITIALIZER;
97static pthread_mutex_t
_nodes_lock = PTHREAD_MUTEX_INITIALIZER;
99static pthread_mutex_t
_fftw_lock = PTHREAD_MUTEX_INITIALIZER;
106#define RE_MAX_THREAD BLENDER_MAX_THREADS
110 void *(*do_thread)(
void *);
127 if (threadbase !=
nullptr && tot > 0) {
128 threadbase->clear_no_delete();
137 for (a = 0; a < tot; a++) {
186 return pthread_equal(pthread_self(),
mainid);
194 tslot.callerdata = callerdata;
199 printf(
"ERROR: could not insert thread slot\n");
205 if (tslot.callerdata == callerdata) {
206 pthread_join(tslot.pthread,
nullptr);
207 tslot.callerdata =
nullptr;
218 if (counter ==
index && tslot.avail == 0) {
219 pthread_join(tslot.pthread,
nullptr);
220 tslot.callerdata =
nullptr;
231 if (tslot.avail == 0) {
232 pthread_join(tslot.pthread,
nullptr);
233 tslot.callerdata =
nullptr;
244 if (threadbase ==
nullptr || threadbase->is_empty()) {
249 if (tslot.avail == 0) {
250 pthread_join(tslot.pthread,
nullptr);
253 threadbase->free_no_destruct();
265 if (t != -1) [[likely]] {
272 GetSystemInfo(&info);
273 t = int(info.dwNumberOfProcessors);
282 sysctl(mib, 2, &t, &
len,
nullptr, 0);
284 t = int(sysconf(_SC_NPROCESSORS_ONLN));
345 pthread_mutex_init(mutex,
nullptr);
350 pthread_mutex_lock(mutex);
355 pthread_mutex_unlock(mutex);
360 return (pthread_mutex_trylock(mutex) == 0);
365 pthread_mutex_destroy(mutex);
384static tbb::spin_mutex *tbb_spin_mutex_cast(
SpinLock *spin)
386 static_assert(
sizeof(
SpinLock) >=
sizeof(tbb::spin_mutex),
387 "SpinLock must match tbb::spin_mutex");
388 static_assert(
alignof(
SpinLock) %
alignof(tbb::spin_mutex) == 0,
389 "SpinLock must be aligned same as tbb::spin_mutex");
390 return reinterpret_cast<tbb::spin_mutex *
>(spin);
397 tbb::spin_mutex *spin_mutex = tbb_spin_mutex_cast(spin);
398 new (spin_mutex) tbb::spin_mutex();
399#elif defined(__APPLE__)
401#elif defined(_MSC_VER)
404 pthread_spin_init(spin, 0);
411 tbb::spin_mutex *spin_mutex = tbb_spin_mutex_cast(spin);
413#elif defined(__APPLE__)
415#elif defined(_MSC_VER)
416# if defined(_M_ARM64)
418 static_assert(
sizeof(long) ==
sizeof(
SpinLock));
419 while (InterlockedExchangeAcquire((
volatile long *)spin, 1)) {
421 while (InterlockedExchangeAcquire(spin, 1)) {
429 pthread_spin_lock(spin);
436 tbb::spin_mutex *spin_mutex = tbb_spin_mutex_cast(spin);
437 spin_mutex->unlock();
438#elif defined(__APPLE__)
440#elif defined(_MSC_VER)
444 pthread_spin_unlock(spin);
451 tbb::spin_mutex *spin_mutex = tbb_spin_mutex_cast(spin);
452 spin_mutex->~spin_mutex();
453#elif defined(__APPLE__)
455#elif defined(_MSC_VER)
459 pthread_spin_destroy(spin);
467 pthread_rwlock_init(mutex,
nullptr);
473 pthread_rwlock_rdlock(mutex);
476 pthread_rwlock_wrlock(mutex);
482 pthread_rwlock_unlock(mutex);
487 pthread_rwlock_destroy(mutex);
517 pthread_cond_init(&ticket->
cond,
nullptr);
518 pthread_mutex_init(&ticket->
mutex,
nullptr);
525 pthread_mutex_destroy(&ticket->
mutex);
526 pthread_cond_destroy(&ticket->
cond);
534 pthread_mutex_lock(&ticket->
mutex);
537 if (check_recursive && ticket->
has_owner && pthread_equal(pthread_self(), ticket->
owner)) {
538 pthread_mutex_unlock(&ticket->
mutex);
545 pthread_cond_wait(&ticket->
cond, &ticket->
mutex);
548 ticket->
owner = pthread_self();
551 pthread_mutex_unlock(&ticket->
mutex);
567 pthread_mutex_lock(&ticket->
mutex);
570 pthread_cond_broadcast(&ticket->
cond);
571 pthread_mutex_unlock(&ticket->
mutex);
580 pthread_cond_init(cond,
nullptr);
585 pthread_cond_wait(cond, mutex);
595 pthread_cond_signal(cond);
600 pthread_cond_broadcast(cond);
605 pthread_cond_destroy(cond);
629 ThreadQueue *queue = MEM_new<ThreadQueue>(__func__);
631 pthread_mutex_init(&queue->
mutex,
nullptr);
632 pthread_cond_init(&queue->
push_cond,
nullptr);
643 pthread_mutex_destroy(&queue->
mutex);
652 pthread_mutex_lock(&queue->
mutex);
655 work_reference.
work = work;
672 pthread_mutex_unlock(&queue->
mutex);
674 return work_reference.
id;
689 pthread_mutex_lock(&queue->
mutex);
693 if (work.id == work_id) {
700 auto cancel = [&](std::deque<ThreadQueueWork> &sub_queue) {
701 sub_queue.erase(std::remove_if(sub_queue.begin(), sub_queue.end(), check), sub_queue.end());
712 pthread_mutex_unlock(&queue->
mutex);
722 pthread_mutex_lock(&queue->
mutex);
730 for (std::deque<ThreadQueueWork> *sub_queue :
733 if (sub_queue->empty()) {
736 work_reference = sub_queue->front();
737 sub_queue->pop_front();
743 if (work_reference.
work) {
747 pthread_mutex_unlock(&queue->
mutex);
749 return work_reference.
work;
762 usec = now.millitm * 1000;
767 gettimeofday(&now,
nullptr);
774 div_result = ldiv(ms, 1000);
775 timeout->tv_sec = sec + div_result.quot;
777 x = usec + (div_result.rem * 1000);
784 timeout->tv_nsec =
x * 1000;
797 pthread_mutex_lock(&queue->
mutex);
801 if (pthread_cond_timedwait(&queue->
push_cond, &queue->
mutex, &timeout) == ETIMEDOUT) {
810 for (std::deque<ThreadQueueWork> *sub_queue :
813 if (sub_queue->empty()) {
816 work_reference = sub_queue->front();
817 sub_queue->pop_front();
823 if (work_reference.
work) {
827 pthread_mutex_unlock(&queue->
mutex);
829 return work_reference.
work;
836 pthread_mutex_lock(&queue->
mutex);
839 pthread_mutex_unlock(&queue->
mutex);
848 pthread_mutex_lock(&queue->
mutex);
851 pthread_mutex_unlock(&queue->
mutex);
858 pthread_mutex_lock(&queue->
mutex);
863 pthread_cond_broadcast(&queue->
push_cond);
864 pthread_mutex_unlock(&queue->
mutex);
870 pthread_mutex_lock(&queue->
mutex);
878 pthread_mutex_unlock(&queue->
mutex);
#define SIMD_SET_FLUSH_TO_ZERO
Platform independent time functions.
Read Blender memory allocation and freeing.
Provides wrapper around system-specific atomic primitives, and some extensions (faked-atomic operatio...
ATOMIC_INLINE unsigned int atomic_fetch_and_add_u(unsigned int *p, unsigned int x)
#define BLI_assert_unreachable()
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned __int64 uint64_t
static void * tslot_thread_start(void *tslot_p)
void BLI_thread_queue_nowait(ThreadQueue *queue)
int BLI_available_threads(ListBaseT< ThreadSlot > *threadbase)
void BLI_threadapi_exit()
void BLI_ticket_mutex_lock(TicketMutex *ticket)
@ 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)
void * BLI_thread_queue_pop_timeout(ThreadQueue *queue, int ms)
ThreadRWMutex * BLI_rw_mutex_alloc()
bool BLI_mutex_trylock(ThreadMutex *mutex)
static uint thread_levels
bool BLI_thread_queue_cancel_work(ThreadQueue *queue, uint64_t work_id)
void BLI_mutex_unlock(ThreadMutex *mutex)
void BLI_rw_mutex_free(ThreadRWMutex *mutex)
void BLI_thread_queue_wait_finish(ThreadQueue *queue)
void BLI_threadpool_insert(ListBaseT< ThreadSlot > *threadbase, void *callerdata)
static void wait_timeout(timespec *timeout, int ms)
void BLI_system_num_threads_override_set(int num)
bool BLI_thread_queue_is_empty(ThreadQueue *queue)
void BLI_threadpool_end(ListBaseT< ThreadSlot > *threadbase)
static int threads_override_num
int BLI_threadpool_available_thread_index(ListBaseT< ThreadSlot > *threadbase)
ThreadMutex * BLI_mutex_alloc()
void BLI_condition_wait(ThreadCondition *cond, ThreadMutex *mutex)
static pthread_mutex_t _movieclip_lock
void BLI_rw_mutex_lock(ThreadRWMutex *mutex, int mode)
void BLI_threadapi_init()
void BLI_rw_mutex_init(ThreadRWMutex *mutex)
void BLI_condition_end(ThreadCondition *cond)
static pthread_mutex_t _image_lock
static pthread_mutex_t _custom1_lock
int BLI_system_thread_count()
static ThreadMutex * global_mutex_from_type(const int type)
void BLI_condition_notify_one(ThreadCondition *cond)
void BLI_spin_end(SpinLock *spin)
void BLI_mutex_lock(ThreadMutex *mutex)
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
void BLI_rw_mutex_unlock(ThreadRWMutex *mutex)
static pthread_mutex_t _nodes_lock
static pthread_mutex_t _view3d_lock
void BLI_condition_wait_global_mutex(ThreadCondition *cond, int type)
static pthread_mutex_t _image_draw_lock
TicketMutex * BLI_ticket_mutex_alloc()
static void check_finalization(ThreadQueue *queue)
void BLI_ticket_mutex_free(TicketMutex *ticket)
void BLI_thread_queue_free(ThreadQueue *queue)
ThreadQueue * BLI_thread_queue_init()
ATTR_WARN_UNUSED_RESULT const size_t num
void * BLI_thread_queue_pop(ThreadQueue *queue)
void BLI_threadpool_remove(ListBaseT< ThreadSlot > *threadbase, void *callerdata)
void BLI_mutex_end(ThreadMutex *mutex)
void BLI_condition_notify_all(ThreadCondition *cond)
void BLI_ticket_mutex_unlock(TicketMutex *ticket)
void BLI_rw_mutex_end(ThreadRWMutex *mutex)
void BLI_spin_unlock(SpinLock *spin)
void BLI_spin_init(SpinLock *spin)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
void BLI_mutex_free(ThreadMutex *mutex)
int BLI_system_num_threads_override_get()
pthread_cond_t ThreadCondition
pthread_mutex_t ThreadMutex
static bool ticket_mutex_lock(TicketMutex *ticket, const bool check_recursive)
int BLI_thread_queue_len(ThreadQueue *queue)
static pthread_mutex_t _fftw_lock
double BLI_time_now_seconds()
bool BLI_ticket_mutex_lock_check_recursive(TicketMutex *ticket)
void BLI_thread_unlock(int type)
uint64_t BLI_thread_queue_push(ThreadQueue *queue, void *work, ThreadQueueWorkPriority priority)
static pthread_mutex_t _viewer_lock
pthread_rwlock_t ThreadRWMutex
void BLI_spin_lock(SpinLock *spin)
void BLI_threadpool_remove_index(ListBaseT< ThreadSlot > *threadbase, int index)
void BLI_mutex_init(ThreadMutex *mutex)
void BLI_threadpool_init(ListBaseT< ThreadSlot > *threadbase, void *(*do_thread)(void *), int tot)
void BLI_thread_lock(int type)
void BLI_threadpool_clear(ListBaseT< ThreadSlot > *threadbase)
std::deque< ThreadQueueWork > queue_high_priority
std::deque< ThreadQueueWork > queue_normal_priority
std::deque< ThreadQueueWork > queue_low_priority
pthread_cond_t finish_cond
void *(*) do_thread(void *)