Blender  V3.3
Classes | Macros | Functions | Variables
threads.cc File Reference
#include <cerrno>
#include <cstdlib>
#include <cstring>
#include "MEM_guardedalloc.h"
#include "BLI_gsqueue.h"
#include "BLI_listbase.h"
#include "BLI_system.h"
#include "BLI_task.h"
#include "BLI_threads.h"
#include "PIL_time.h"
#include <sys/time.h>
#include <unistd.h>
#include "atomic_ops.h"

Go to the source code of this file.

Classes

struct  ThreadSlot
 
struct  TicketMutex
 
struct  ThreadQueue
 

Macros

#define RE_MAX_THREAD   BLENDER_MAX_THREADS
 

Functions

void BLI_threadapi_init ()
 
void BLI_threadapi_exit ()
 
void BLI_threadpool_init (ListBase *threadbase, void *(*do_thread)(void *), int tot)
 
int BLI_available_threads (ListBase *threadbase)
 
int BLI_threadpool_available_thread_index (ListBase *threadbase)
 
static voidtslot_thread_start (void *tslot_p)
 
int BLI_thread_is_main ()
 
void BLI_threadpool_insert (ListBase *threadbase, void *callerdata)
 
void BLI_threadpool_remove (ListBase *threadbase, void *callerdata)
 
void BLI_threadpool_remove_index (ListBase *threadbase, int index)
 
void BLI_threadpool_clear (ListBase *threadbase)
 
void BLI_threadpool_end (ListBase *threadbase)
 
int BLI_system_thread_count ()
 
void BLI_system_num_threads_override_set (int num)
 
int BLI_system_num_threads_override_get ()
 
static ThreadMutexglobal_mutex_from_type (const int type)
 
void BLI_thread_lock (int type)
 
void BLI_thread_unlock (int type)
 
void BLI_mutex_init (ThreadMutex *mutex)
 
void BLI_mutex_lock (ThreadMutex *mutex)
 
void BLI_mutex_unlock (ThreadMutex *mutex)
 
bool BLI_mutex_trylock (ThreadMutex *mutex)
 
void BLI_mutex_end (ThreadMutex *mutex)
 
ThreadMutexBLI_mutex_alloc ()
 
void BLI_mutex_free (ThreadMutex *mutex)
 
void BLI_spin_init (SpinLock *spin)
 
void BLI_spin_lock (SpinLock *spin)
 
void BLI_spin_unlock (SpinLock *spin)
 
void BLI_spin_end (SpinLock *spin)
 
void BLI_rw_mutex_init (ThreadRWMutex *mutex)
 
void BLI_rw_mutex_lock (ThreadRWMutex *mutex, int mode)
 
void BLI_rw_mutex_unlock (ThreadRWMutex *mutex)
 
void BLI_rw_mutex_end (ThreadRWMutex *mutex)
 
ThreadRWMutexBLI_rw_mutex_alloc ()
 
void BLI_rw_mutex_free (ThreadRWMutex *mutex)
 
TicketMutexBLI_ticket_mutex_alloc ()
 
void BLI_ticket_mutex_free (TicketMutex *ticket)
 
void BLI_ticket_mutex_lock (TicketMutex *ticket)
 
void BLI_ticket_mutex_unlock (TicketMutex *ticket)
 
void BLI_condition_init (ThreadCondition *cond)
 
void BLI_condition_wait (ThreadCondition *cond, ThreadMutex *mutex)
 
void BLI_condition_wait_global_mutex (ThreadCondition *cond, const int type)
 
void BLI_condition_notify_one (ThreadCondition *cond)
 
void BLI_condition_notify_all (ThreadCondition *cond)
 
void BLI_condition_end (ThreadCondition *cond)
 
ThreadQueueBLI_thread_queue_init ()
 
void BLI_thread_queue_free (ThreadQueue *queue)
 
void BLI_thread_queue_push (ThreadQueue *queue, void *work)
 
voidBLI_thread_queue_pop (ThreadQueue *queue)
 
static void wait_timeout (struct timespec *timeout, int ms)
 
voidBLI_thread_queue_pop_timeout (ThreadQueue *queue, int ms)
 
int BLI_thread_queue_len (ThreadQueue *queue)
 
bool BLI_thread_queue_is_empty (ThreadQueue *queue)
 
void BLI_thread_queue_nowait (ThreadQueue *queue)
 
void BLI_thread_queue_wait_finish (ThreadQueue *queue)
 

Variables

static pthread_mutex_t _image_lock = PTHREAD_MUTEX_INITIALIZER
 
static pthread_mutex_t _image_draw_lock = PTHREAD_MUTEX_INITIALIZER
 
static pthread_mutex_t _viewer_lock = PTHREAD_MUTEX_INITIALIZER
 
static pthread_mutex_t _custom1_lock = PTHREAD_MUTEX_INITIALIZER
 
static pthread_mutex_t _nodes_lock = PTHREAD_MUTEX_INITIALIZER
 
static pthread_mutex_t _movieclip_lock = PTHREAD_MUTEX_INITIALIZER
 
static pthread_mutex_t _colormanage_lock = PTHREAD_MUTEX_INITIALIZER
 
static pthread_mutex_t _fftw_lock = PTHREAD_MUTEX_INITIALIZER
 
static pthread_mutex_t _view3d_lock = PTHREAD_MUTEX_INITIALIZER
 
static pthread_t mainid
 
static unsigned int thread_levels = 0
 
static int threads_override_num = 0
 

Macro Definition Documentation

◆ RE_MAX_THREAD

#define RE_MAX_THREAD   BLENDER_MAX_THREADS

Definition at line 115 of file threads.cc.

Function Documentation

◆ BLI_available_threads()

int BLI_available_threads ( struct ListBase threadbase)

Amount of available threads.

Definition at line 167 of file threads.cc.

References counter, and LISTBASE_FOREACH.

Referenced by background_task_pool_run(), and ww_write_zstd().

◆ BLI_condition_end()

void BLI_condition_end ( ThreadCondition cond)

◆ BLI_condition_init()

void BLI_condition_init ( ThreadCondition cond)

◆ BLI_condition_notify_all()

void BLI_condition_notify_all ( ThreadCondition cond)

Definition at line 594 of file threads.cc.

Referenced by IMB_thumb_path_unlock(), write_result(), and zstd_write_task().

◆ BLI_condition_notify_one()

void BLI_condition_notify_one ( ThreadCondition cond)

◆ BLI_condition_wait()

void BLI_condition_wait ( ThreadCondition cond,
ThreadMutex mutex 
)

◆ BLI_condition_wait_global_mutex()

void BLI_condition_wait_global_mutex ( ThreadCondition cond,
const int  type 
)

Definition at line 584 of file threads.cc.

References global_mutex_from_type(), and type.

Referenced by IMB_thumb_path_lock().

◆ BLI_mutex_alloc()

ThreadMutex* BLI_mutex_alloc ( void  )

◆ BLI_mutex_end()

void BLI_mutex_end ( ThreadMutex mutex)

◆ BLI_mutex_free()

void BLI_mutex_free ( ThreadMutex mutex)

Definition at line 400 of file threads.cc.

References BLI_mutex_end(), MEM_freeN, and mutex.

Referenced by EEVEE_lightbake_job_data_free(), and free_preview_job().

◆ BLI_mutex_init()

void BLI_mutex_init ( ThreadMutex mutex)

◆ BLI_mutex_lock()

void BLI_mutex_lock ( ThreadMutex mutex)

Definition at line 373 of file threads.cc.

References mutex.

Referenced by BKE_collection_object_cache_get(), BKE_collection_object_cache_instanced_get(), BKE_image_acquire_ibuf(), BKE_image_ensure_viewer_views(), BKE_image_file_format_set(), BKE_image_free_anim_ibufs(), BKE_image_free_buffers_ex(), BKE_image_get_first_ibuf(), BKE_image_get_ibuf_with_name(), BKE_image_has_ibuf(), BKE_image_has_loaded_ibuf(), BKE_image_is_dirty_writable(), BKE_image_merge(), BKE_image_pool_acquire_ibuf(), BKE_image_pool_free(), BKE_image_release_ibuf(), BKE_image_signal(), BKE_mesh_poly_normals_ensure(), BKE_mesh_runtime_looptri_ensure(), BKE_mesh_vertex_normals_ensure(), BKE_mesh_wrapper_ensure_mdata(), BKE_mesh_wrapper_ensure_subdivision(), BKE_object_select_update(), BLI_spin_lock(), bvhcache_find(), ccgDM_copyFinalLoopArray(), colorspace_from_scene_linear_cpu_processor(), colorspace_to_scene_linear_cpu_processor(), COM_deinitialize(), COM_execute(), display_from_scene_linear_processor(), display_to_scene_linear_processor(), draw_image_cache(), drw_attributes_merge(), DRW_cache_free_old_subdiv(), DRW_subdiv_cache_free(), eevee_lightbake_delete_resources(), EEVEE_lightbake_job_create(), engine_render_view_layer(), engine_tile_highlight_set(), evaluate_driver_python(), blender::compositor::ExecutionSystem::execute_work(), filelist_readjob_append_entries(), filelist_readjob_startjob(), filelist_readjob_update(), give_parvert(), gpu_batch_presets_register(), gpu_batch_presets_unregister(), gpu_free_unused_buffers(), image_free_gpu(), image_mem_size(), IMB_colormanagement_color_picking_to_scene_linear_v3(), IMB_colormanagement_scene_linear_to_color_picking_v3(), imb_global_cache_get_tile(), imb_tile_cache_tile_free(), IMB_tiles_to_rect(), blender::compositor::NodeOperation::lock_mutex(), mesh_calc_modifiers(), preview_startjob(), RE_engine_draw_acquire(), RE_engine_get_current_tiles(), RE_engine_tile_highlight_clear_all(), RE_engine_update_render_passes(), RE_point_density_cache(), RE_point_density_sample(), blender::eevee::Instance::render_frame(), render_pipeline_free(), RNA_property_pointer_get(), schedule_write_result(), screen_opengl_render_end(), seq_cache_create(), seq_cache_lock(), seq_disk_cache_enforce_limits(), seq_disk_cache_invalidate(), seq_disk_cache_read_file(), seq_disk_cache_write_file(), seq_prefetch_do_suspend(), SEQ_render_give_ibuf(), seq_sequence_lookup_effects_by_seq(), SEQ_sequence_lookup_free(), seq_sequence_lookup_meta_by_seq(), SEQ_sequence_lookup_seq_by_name(), SEQ_sequence_lookup_tag(), sequencer_preview_add_sound(), stats_background(), view_layer_bases_hash_create(), write_result(), ww_write_zstd(), and zstd_write_task().

◆ BLI_mutex_trylock()

bool BLI_mutex_trylock ( ThreadMutex mutex)

Definition at line 383 of file threads.cc.

References mutex.

◆ BLI_mutex_unlock()

void BLI_mutex_unlock ( ThreadMutex mutex)

Definition at line 378 of file threads.cc.

References mutex.

Referenced by BKE_collection_object_cache_get(), BKE_collection_object_cache_instanced_get(), BKE_image_acquire_ibuf(), BKE_image_ensure_viewer_views(), BKE_image_file_format_set(), BKE_image_free_anim_ibufs(), BKE_image_free_buffers_ex(), BKE_image_get_first_ibuf(), BKE_image_get_ibuf_with_name(), BKE_image_has_ibuf(), BKE_image_has_loaded_ibuf(), BKE_image_is_dirty_writable(), BKE_image_merge(), BKE_image_pool_acquire_ibuf(), BKE_image_pool_free(), BKE_image_release_ibuf(), BKE_image_signal(), BKE_mesh_poly_normals_ensure(), BKE_mesh_runtime_looptri_ensure(), BKE_mesh_vertex_normals_ensure(), BKE_mesh_wrapper_ensure_mdata(), BKE_mesh_wrapper_ensure_subdivision(), BKE_object_select_update(), BLI_spin_unlock(), bvhcache_find(), bvhcache_unlock(), ccgDM_copyFinalLoopArray(), colorspace_from_scene_linear_cpu_processor(), colorspace_to_scene_linear_cpu_processor(), COM_deinitialize(), COM_execute(), display_from_scene_linear_processor(), display_to_scene_linear_processor(), draw_image_cache(), drw_attributes_merge(), DRW_cache_free_old_subdiv(), DRW_subdiv_cache_free(), eevee_lightbake_delete_resources(), EEVEE_lightbake_job_create(), engine_render_view_layer(), engine_tile_highlight_set(), evaluate_driver_python(), blender::compositor::ExecutionSystem::execute_work(), filelist_readjob_append_entries(), filelist_readjob_startjob(), filelist_readjob_update(), give_parvert(), gpu_batch_presets_register(), gpu_batch_presets_unregister(), gpu_free_unused_buffers(), image_free_gpu(), image_mem_size(), IMB_colormanagement_color_picking_to_scene_linear_v3(), IMB_colormanagement_scene_linear_to_color_picking_v3(), imb_global_cache_get_tile(), imb_tile_cache_tile_free(), IMB_tiles_to_rect(), mesh_calc_modifiers(), preview_startjob(), RE_engine_draw_acquire(), RE_engine_draw_release(), RE_engine_get_current_tiles(), RE_engine_tile_highlight_clear_all(), RE_engine_update_render_passes(), RE_point_density_cache(), RE_point_density_sample(), blender::eevee::Instance::render_frame(), render_pipeline_free(), RNA_property_pointer_get(), schedule_write_result(), screen_opengl_render_end(), seq_cache_create(), seq_cache_unlock(), seq_disk_cache_create(), seq_disk_cache_enforce_limits(), seq_disk_cache_invalidate(), seq_disk_cache_read_file(), seq_disk_cache_write_file(), seq_prefetch_do_suspend(), SEQ_render_give_ibuf(), seq_sequence_lookup_effects_by_seq(), SEQ_sequence_lookup_free(), seq_sequence_lookup_meta_by_seq(), SEQ_sequence_lookup_seq_by_name(), SEQ_sequence_lookup_tag(), sequencer_preview_add_sound(), stats_background(), blender::compositor::NodeOperation::unlock_mutex(), view_layer_bases_hash_create(), write_result(), ww_write_zstd(), and zstd_write_task().

◆ BLI_rw_mutex_alloc()

ThreadRWMutex* BLI_rw_mutex_alloc ( void  )

◆ BLI_rw_mutex_end()

void BLI_rw_mutex_end ( ThreadRWMutex mutex)

Definition at line 503 of file threads.cc.

References mutex.

Referenced by BLI_rw_mutex_free(), ccgDM_release(), and RE_FreeRender().

◆ BLI_rw_mutex_free()

void BLI_rw_mutex_free ( ThreadRWMutex mutex)

Definition at line 516 of file threads.cc.

References BLI_rw_mutex_end(), MEM_freeN, and mutex.

Referenced by BKE_fluid_modifier_freeDomain().

◆ BLI_rw_mutex_init()

void BLI_rw_mutex_init ( ThreadRWMutex mutex)

Definition at line 483 of file threads.cc.

References mutex.

Referenced by BLI_rw_mutex_alloc(), getCCGDerivedMesh(), and RE_NewRender().

◆ BLI_rw_mutex_lock()

void BLI_rw_mutex_lock ( ThreadRWMutex mutex,
int  mode 
)

◆ BLI_rw_mutex_unlock()

void BLI_rw_mutex_unlock ( ThreadRWMutex mutex)

◆ BLI_spin_end()

void BLI_spin_end ( SpinLock spin)

◆ BLI_spin_init()

void BLI_spin_init ( SpinLock spin)

◆ BLI_spin_lock()

void BLI_spin_lock ( SpinLock spin)

◆ BLI_spin_unlock()

void BLI_spin_unlock ( SpinLock spin)

◆ BLI_system_num_threads_override_get()

int BLI_system_num_threads_override_get ( void  )

Definition at line 322 of file threads.cc.

References threads_override_num.

Referenced by BKE_render_num_threads(), and BLI_task_scheduler_init().

◆ BLI_system_num_threads_override_set()

void BLI_system_num_threads_override_set ( int  num)

Definition at line 317 of file threads.cc.

References threads_override_num.

Referenced by arg_handle_threads_set().

◆ BLI_system_thread_count()

int BLI_system_thread_count ( void  )

◆ BLI_thread_is_main()

int BLI_thread_is_main ( void  )

◆ BLI_thread_lock()

void BLI_thread_lock ( int  type)

◆ BLI_thread_queue_free()

void BLI_thread_queue_free ( ThreadQueue queue)

◆ BLI_thread_queue_init()

ThreadQueue* BLI_thread_queue_init ( void  )

◆ BLI_thread_queue_is_empty()

bool BLI_thread_queue_is_empty ( ThreadQueue queue)

Definition at line 756 of file threads.cc.

References BLI_gsqueue_is_empty(), and queue.

Referenced by filelist_cache_previews_update().

◆ BLI_thread_queue_len()

int BLI_thread_queue_len ( ThreadQueue queue)

Definition at line 745 of file threads.cc.

References BLI_gsqueue_len(), queue, and size().

◆ BLI_thread_queue_nowait()

void BLI_thread_queue_nowait ( ThreadQueue queue)

◆ BLI_thread_queue_pop()

void* BLI_thread_queue_pop ( ThreadQueue queue)

◆ BLI_thread_queue_pop_timeout()

void* BLI_thread_queue_pop_timeout ( ThreadQueue queue,
int  ms 
)

◆ BLI_thread_queue_push()

void BLI_thread_queue_push ( ThreadQueue queue,
void work 
)

◆ BLI_thread_queue_wait_finish()

void BLI_thread_queue_wait_finish ( ThreadQueue queue)

◆ BLI_thread_unlock()

void BLI_thread_unlock ( int  type)

◆ BLI_threadapi_exit()

void BLI_threadapi_exit ( void  )

◆ BLI_threadapi_init()

void BLI_threadapi_init ( void  )

This is run once at startup.

Definition at line 125 of file threads.cc.

References mainid.

Referenced by main(), BlendfileLoadingBaseTest::SetUpTestCase(), task_listbase_test(), and TEST().

◆ BLI_threadpool_available_thread_index()

int BLI_threadpool_available_thread_index ( struct ListBase threadbase)

Returns thread number, for sample patterns or threadsafe tables.

Definition at line 180 of file threads.cc.

References counter, and LISTBASE_FOREACH.

◆ BLI_threadpool_clear()

void BLI_threadpool_clear ( ListBase threadbase)

Definition at line 251 of file threads.cc.

References LISTBASE_FOREACH.

Referenced by background_task_pool_work_and_wait().

◆ BLI_threadpool_end()

void BLI_threadpool_end ( ListBase threadbase)

◆ BLI_threadpool_init()

void BLI_threadpool_init ( struct ListBase threadbase,
void *(*)(void *)  do_thread,
int  tot 
)

◆ BLI_threadpool_insert()

void BLI_threadpool_insert ( ListBase threadbase,
void callerdata 
)

◆ BLI_threadpool_remove()

void BLI_threadpool_remove ( ListBase threadbase,
void callerdata 
)

◆ BLI_threadpool_remove_index()

void BLI_threadpool_remove_index ( ListBase threadbase,
int  index 
)

Definition at line 236 of file threads.cc.

References counter, and LISTBASE_FOREACH.

◆ BLI_ticket_mutex_alloc()

TicketMutex* BLI_ticket_mutex_alloc ( void  )

Definition at line 530 of file threads.cc.

References TicketMutex::cond, MEM_callocN, and TicketMutex::mutex.

Referenced by DRW_opengl_context_create(), and WM_jobs_get().

◆ BLI_ticket_mutex_free()

void BLI_ticket_mutex_free ( TicketMutex ticket)

Definition at line 541 of file threads.cc.

References TicketMutex::cond, MEM_freeN, and TicketMutex::mutex.

Referenced by DRW_opengl_context_destroy(), and wm_job_free().

◆ BLI_ticket_mutex_lock()

void BLI_ticket_mutex_lock ( TicketMutex ticket)

◆ BLI_ticket_mutex_unlock()

void BLI_ticket_mutex_unlock ( TicketMutex ticket)

◆ global_mutex_from_type()

static ThreadMutex* global_mutex_from_type ( const int  type)
static

◆ tslot_thread_start()

static void* tslot_thread_start ( void tslot_p)
static

Definition at line 194 of file threads.cc.

References ThreadSlot::callerdata, and ThreadSlot::do_thread.

Referenced by BLI_threadpool_insert().

◆ wait_timeout()

static void wait_timeout ( struct timespec *  timeout,
int  ms 
)
static

Definition at line 676 of file threads.cc.

References x.

Referenced by BLI_thread_queue_pop_timeout().

Variable Documentation

◆ _colormanage_lock

pthread_mutex_t _colormanage_lock = PTHREAD_MUTEX_INITIALIZER
static

Definition at line 107 of file threads.cc.

Referenced by global_mutex_from_type().

◆ _custom1_lock

pthread_mutex_t _custom1_lock = PTHREAD_MUTEX_INITIALIZER
static

Definition at line 104 of file threads.cc.

Referenced by global_mutex_from_type().

◆ _fftw_lock

pthread_mutex_t _fftw_lock = PTHREAD_MUTEX_INITIALIZER
static

Definition at line 108 of file threads.cc.

Referenced by global_mutex_from_type().

◆ _image_draw_lock

pthread_mutex_t _image_draw_lock = PTHREAD_MUTEX_INITIALIZER
static

Definition at line 102 of file threads.cc.

Referenced by global_mutex_from_type().

◆ _image_lock

pthread_mutex_t _image_lock = PTHREAD_MUTEX_INITIALIZER
static

Basic Thread Control API

Many thread cases have an X amount of jobs, and only an Y amount of threads are useful (typically amount of CPU's)

This code can be used to start a maximum amount of 'thread slots', which then can be filled in a loop with an idle timer.

A sample loop can look like this (pseudo c);

int max_threads = 2;
int cont = 1;
BLI_threadpool_init(&lb, do_something_func, max_threads);
while (cont) {
if (BLI_available_threads(&lb) && !(escape loop event)) {
// get new job (data pointer)
// tag job 'processed
}
else PIL_sleep_ms(50);
// Find if a job is ready, this the do_something_func() should write in job somewhere.
cont = 0;
for (go over all jobs)
if (job is ready) {
if (job was not removed) {
}
}
else cont = 1;
}
// Conditions to exit loop.
if (if escape loop event) {
if (BLI_available_threadslots(&lb) == max_threads) {
break;
}
}
}
void BLI_threadpool_remove(struct ListBase *threadbase, void *callerdata)
Definition: threads.cc:225
void BLI_threadpool_init(struct ListBase *threadbase, void *(*do_thread)(void *), int tot)
Definition: threads.cc:134
void BLI_threadpool_end(struct ListBase *threadbase)
Definition: threads.cc:262
int BLI_available_threads(struct ListBase *threadbase)
Definition: threads.cc:167
void BLI_threadpool_insert(struct ListBase *threadbase, void *callerdata)
Definition: threads.cc:212
__forceinline bool all(const avxb &b)
Definition: avxb.h:201
void PIL_sleep_ms(int ms)
Definition: time.c:84

Definition at line 101 of file threads.cc.

Referenced by global_mutex_from_type().

◆ _movieclip_lock

pthread_mutex_t _movieclip_lock = PTHREAD_MUTEX_INITIALIZER
static

Definition at line 106 of file threads.cc.

Referenced by global_mutex_from_type().

◆ _nodes_lock

pthread_mutex_t _nodes_lock = PTHREAD_MUTEX_INITIALIZER
static

Definition at line 105 of file threads.cc.

Referenced by global_mutex_from_type().

◆ _view3d_lock

pthread_mutex_t _view3d_lock = PTHREAD_MUTEX_INITIALIZER
static

Definition at line 109 of file threads.cc.

Referenced by global_mutex_from_type().

◆ _viewer_lock

pthread_mutex_t _viewer_lock = PTHREAD_MUTEX_INITIALIZER
static

Definition at line 103 of file threads.cc.

Referenced by global_mutex_from_type().

◆ mainid

pthread_t mainid
static

Definition at line 110 of file threads.cc.

Referenced by BLI_thread_is_main(), and BLI_threadapi_init().

◆ thread_levels

unsigned int thread_levels = 0
static

Definition at line 111 of file threads.cc.

Referenced by BLI_threadpool_init().

◆ threads_override_num

int threads_override_num = 0
static