2023-08-15 16:20:26 +02:00
|
|
|
/* SPDX-FileCopyrightText: 2006 Blender Authors
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
Three-in-one commit:
- Compositor now is threaded
Enable it with the Scene buttons "Threads". This will handle over nodes to
individual threads to be calculated. However, if nodes depend on others
they have to wait. The current system only threads per entire node, not for
calculating results in parts.
I've reshuffled the node execution code to evaluate 'changed' events, and
prepare the entire tree to become simply parsed for open jobs with a call
to node = getExecutableNode()
By default, even without 'thread' option active, all node execution is
done within a separate thread.
Also fixed issues in yesterdays commit for 'event based' calculations, it
didn't do animated images, or execute (on rendering) the correct nodes
when you don't have Render-Result nodes included.
- Added generic Thread support in blenlib/ module
The renderer and the node system now both use same code for controlling the
threads. This has been moved to a new C file in blenlib/intern/threads.c.
Check this c file for an extensive doc and example how to use it.
The current implementation for Compositing allows unlimited amount of
threads. For rendering it is still tied to two threads, although it is
pretty easy to extend to 4 already. People with giant amounts of cpus can
poke me once for tests. :)
- Bugfix in creating group nodes
Group node definitions demand a clear separation of 'internal sockets' and
'external sockets'. The first are sockets being linked internally, the latter
are sockets exposed as sockets for the group itself.
When sockets were linked both internal and external, Blender crashed. It is
solved now by removing the external link(s).
2006-01-29 12:36:33 +01:00
|
|
|
|
2018-06-17 16:32:54 +02:00
|
|
|
#pragma once
|
Three-in-one commit:
- Compositor now is threaded
Enable it with the Scene buttons "Threads". This will handle over nodes to
individual threads to be calculated. However, if nodes depend on others
they have to wait. The current system only threads per entire node, not for
calculating results in parts.
I've reshuffled the node execution code to evaluate 'changed' events, and
prepare the entire tree to become simply parsed for open jobs with a call
to node = getExecutableNode()
By default, even without 'thread' option active, all node execution is
done within a separate thread.
Also fixed issues in yesterdays commit for 'event based' calculations, it
didn't do animated images, or execute (on rendering) the correct nodes
when you don't have Render-Result nodes included.
- Added generic Thread support in blenlib/ module
The renderer and the node system now both use same code for controlling the
threads. This has been moved to a new C file in blenlib/intern/threads.c.
Check this c file for an extensive doc and example how to use it.
The current implementation for Compositing allows unlimited amount of
threads. For rendering it is still tied to two threads, although it is
pretty easy to extend to 4 already. People with giant amounts of cpus can
poke me once for tests. :)
- Bugfix in creating group nodes
Group node definitions demand a clear separation of 'internal sockets' and
'external sockets'. The first are sockets being linked internally, the latter
are sockets exposed as sockets for the group itself.
When sockets were linked both internal and external, Blender crashed. It is
solved now by removing the external link(s).
2006-01-29 12:36:33 +01:00
|
|
|
|
2019-02-17 22:08:12 +01:00
|
|
|
/** \file
|
|
|
|
* \ingroup bli
|
2011-02-18 14:58:08 +01:00
|
|
|
*/
|
|
|
|
|
2009-09-30 20:18:32 +02:00
|
|
|
#include <pthread.h>
|
2006-03-13 12:01:17 +01:00
|
|
|
|
2020-03-06 16:45:06 +01:00
|
|
|
#include "BLI_sys_types.h"
|
|
|
|
|
2020-05-08 18:16:39 +02:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2021-12-20 09:01:14 +01:00
|
|
|
/** For tables, button in UI, etc. */
|
2015-06-07 22:48:51 +02:00
|
|
|
#define BLENDER_MAX_THREADS 1024
|
2006-11-29 18:01:09 +01:00
|
|
|
|
|
|
|
struct ListBase;
|
2009-09-30 20:18:32 +02:00
|
|
|
|
|
|
|
/* Threading API */
|
|
|
|
|
2021-12-09 10:01:44 +01:00
|
|
|
/**
|
|
|
|
* This is run once at startup.
|
|
|
|
*/
|
2010-04-13 14:51:03 +02:00
|
|
|
void BLI_threadapi_init(void);
|
2013-08-19 12:51:40 +02:00
|
|
|
void BLI_threadapi_exit(void);
|
2010-04-13 14:51:03 +02:00
|
|
|
|
2021-12-09 10:01:44 +01:00
|
|
|
/**
|
|
|
|
* \param tot: When 0 only initializes malloc mutex in a safe way (see sequence.c)
|
|
|
|
* problem otherwise: scene render will kill of the mutex!
|
|
|
|
*/
|
2018-02-15 15:13:46 +01:00
|
|
|
void BLI_threadpool_init(struct ListBase *threadbase, void *(*do_thread)(void *), int tot);
|
2021-12-09 10:01:44 +01:00
|
|
|
/**
|
|
|
|
* Amount of available threads.
|
|
|
|
*/
|
2012-05-12 22:39:39 +02:00
|
|
|
int BLI_available_threads(struct ListBase *threadbase);
|
2021-12-09 10:01:44 +01:00
|
|
|
/**
|
|
|
|
* Returns thread number, for sample patterns or threadsafe tables.
|
|
|
|
*/
|
2018-02-15 15:13:46 +01:00
|
|
|
int BLI_threadpool_available_thread_index(struct ListBase *threadbase);
|
|
|
|
void BLI_threadpool_insert(struct ListBase *threadbase, void *callerdata);
|
|
|
|
void BLI_threadpool_remove(struct ListBase *threadbase, void *callerdata);
|
|
|
|
void BLI_threadpool_remove_index(struct ListBase *threadbase, int index);
|
|
|
|
void BLI_threadpool_clear(struct ListBase *threadbase);
|
|
|
|
void BLI_threadpool_end(struct ListBase *threadbase);
|
2012-05-12 22:39:39 +02:00
|
|
|
int BLI_thread_is_main(void);
|
2011-10-22 18:16:14 +02:00
|
|
|
|
2009-09-30 20:18:32 +02:00
|
|
|
/* System Information */
|
|
|
|
|
2021-12-09 10:01:44 +01:00
|
|
|
/**
|
|
|
|
* \return the number of threads the system can make use of.
|
|
|
|
*/
|
|
|
|
int BLI_system_thread_count(void);
|
2013-05-08 15:23:17 +02:00
|
|
|
void BLI_system_num_threads_override_set(int num);
|
|
|
|
int BLI_system_num_threads_override_get(void);
|
2018-06-17 16:32:54 +02:00
|
|
|
|
2020-09-18 07:30:46 +02:00
|
|
|
/**
|
|
|
|
* Global Mutex Locks
|
2018-06-01 18:19:39 +02:00
|
|
|
*
|
2020-09-18 07:30:46 +02:00
|
|
|
* One custom lock available now. can be extended.
|
|
|
|
*/
|
|
|
|
enum {
|
|
|
|
LOCK_IMAGE = 0,
|
|
|
|
LOCK_DRAW_IMAGE,
|
|
|
|
LOCK_VIEWER,
|
|
|
|
LOCK_CUSTOM1,
|
|
|
|
LOCK_NODES,
|
|
|
|
LOCK_MOVIECLIP,
|
|
|
|
LOCK_COLORMANAGE,
|
|
|
|
LOCK_FFTW,
|
|
|
|
LOCK_VIEW3D,
|
|
|
|
};
|
2009-09-30 20:18:32 +02:00
|
|
|
|
2018-02-15 15:13:46 +01:00
|
|
|
void BLI_thread_lock(int type);
|
|
|
|
void BLI_thread_unlock(int type);
|
2009-09-30 20:18:32 +02:00
|
|
|
|
|
|
|
/* Mutex Lock */
|
|
|
|
|
|
|
|
typedef pthread_mutex_t ThreadMutex;
|
2012-05-12 22:39:39 +02:00
|
|
|
#define BLI_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
|
2009-09-30 20:18:32 +02:00
|
|
|
|
|
|
|
void BLI_mutex_init(ThreadMutex *mutex);
|
2013-04-24 19:31:09 +02:00
|
|
|
void BLI_mutex_end(ThreadMutex *mutex);
|
|
|
|
|
|
|
|
ThreadMutex *BLI_mutex_alloc(void);
|
|
|
|
void BLI_mutex_free(ThreadMutex *mutex);
|
|
|
|
|
2009-09-30 20:18:32 +02:00
|
|
|
void BLI_mutex_lock(ThreadMutex *mutex);
|
2013-10-12 16:08:59 +02:00
|
|
|
bool BLI_mutex_trylock(ThreadMutex *mutex);
|
2009-09-30 20:18:32 +02:00
|
|
|
void BLI_mutex_unlock(ThreadMutex *mutex);
|
2006-02-11 16:55:00 +01:00
|
|
|
|
2012-11-15 16:59:58 +01:00
|
|
|
/* Spin Lock */
|
|
|
|
|
2020-07-02 16:40:30 +02:00
|
|
|
/* By default we use TBB for spin lock on all platforms. When building without
|
|
|
|
* TBB fall-back to spin lock implementation which is native to the platform.
|
|
|
|
*
|
|
|
|
* On macOS we use mutex lock instead of spin since the spin lock has been
|
|
|
|
* deprecated in SDK 10.12 and is discouraged from use. */
|
|
|
|
|
|
|
|
#ifdef WITH_TBB
|
|
|
|
typedef uint32_t SpinLock;
|
|
|
|
#elif defined(__APPLE__)
|
|
|
|
typedef ThreadMutex SpinLock;
|
2017-11-14 12:21:15 +01:00
|
|
|
#elif defined(_MSC_VER)
|
2020-07-07 16:51:03 +02:00
|
|
|
typedef volatile unsigned int SpinLock;
|
2012-11-15 16:59:58 +01:00
|
|
|
#else
|
|
|
|
typedef pthread_spinlock_t SpinLock;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void BLI_spin_init(SpinLock *spin);
|
|
|
|
void BLI_spin_lock(SpinLock *spin);
|
|
|
|
void BLI_spin_unlock(SpinLock *spin);
|
|
|
|
void BLI_spin_end(SpinLock *spin);
|
|
|
|
|
2009-09-30 20:18:32 +02:00
|
|
|
/* Read/Write Mutex Lock */
|
2008-08-17 00:47:33 +02:00
|
|
|
|
2012-05-12 22:39:39 +02:00
|
|
|
#define THREAD_LOCK_READ 1
|
|
|
|
#define THREAD_LOCK_WRITE 2
|
2009-06-08 12:00:14 +02:00
|
|
|
|
2013-12-29 11:40:34 +01:00
|
|
|
#define BLI_RWLOCK_INITIALIZER PTHREAD_RWLOCK_INITIALIZER
|
|
|
|
|
2009-09-30 20:18:32 +02:00
|
|
|
typedef pthread_rwlock_t ThreadRWMutex;
|
|
|
|
|
|
|
|
void BLI_rw_mutex_init(ThreadRWMutex *mutex);
|
2013-04-24 19:31:09 +02:00
|
|
|
void BLI_rw_mutex_end(ThreadRWMutex *mutex);
|
|
|
|
|
|
|
|
ThreadRWMutex *BLI_rw_mutex_alloc(void);
|
|
|
|
void BLI_rw_mutex_free(ThreadRWMutex *mutex);
|
|
|
|
|
2009-09-30 20:18:32 +02:00
|
|
|
void BLI_rw_mutex_lock(ThreadRWMutex *mutex, int mode);
|
|
|
|
void BLI_rw_mutex_unlock(ThreadRWMutex *mutex);
|
|
|
|
|
2013-07-08 19:56:51 +02:00
|
|
|
/* Ticket Mutex Lock
|
|
|
|
*
|
|
|
|
* This is a 'fair' mutex in that it will grant the lock to the first thread
|
|
|
|
* that requests it. */
|
|
|
|
|
|
|
|
typedef struct TicketMutex TicketMutex;
|
|
|
|
|
|
|
|
TicketMutex *BLI_ticket_mutex_alloc(void);
|
|
|
|
void BLI_ticket_mutex_free(TicketMutex *ticket);
|
|
|
|
void BLI_ticket_mutex_lock(TicketMutex *ticket);
|
|
|
|
void BLI_ticket_mutex_unlock(TicketMutex *ticket);
|
|
|
|
|
2013-10-12 16:08:59 +02:00
|
|
|
/* Condition */
|
2018-06-17 16:32:54 +02:00
|
|
|
|
2013-10-12 16:08:59 +02:00
|
|
|
typedef pthread_cond_t ThreadCondition;
|
2008-08-17 00:47:33 +02:00
|
|
|
|
2013-10-12 16:08:59 +02:00
|
|
|
void BLI_condition_init(ThreadCondition *cond);
|
|
|
|
void BLI_condition_wait(ThreadCondition *cond, ThreadMutex *mutex);
|
2022-01-07 01:38:08 +01:00
|
|
|
void BLI_condition_wait_global_mutex(ThreadCondition *cond, int type);
|
2013-10-12 16:08:59 +02:00
|
|
|
void BLI_condition_notify_one(ThreadCondition *cond);
|
|
|
|
void BLI_condition_notify_all(ThreadCondition *cond);
|
|
|
|
void BLI_condition_end(ThreadCondition *cond);
|
2008-08-17 00:47:33 +02:00
|
|
|
|
2010-01-22 12:06:57 +01:00
|
|
|
/* ThreadWorkQueue
|
|
|
|
*
|
|
|
|
* Thread-safe work queue to push work/pointers between threads. */
|
|
|
|
|
|
|
|
typedef struct ThreadQueue ThreadQueue;
|
|
|
|
|
2010-12-03 13:30:59 +01:00
|
|
|
ThreadQueue *BLI_thread_queue_init(void);
|
2010-01-22 12:06:57 +01:00
|
|
|
void BLI_thread_queue_free(ThreadQueue *queue);
|
|
|
|
|
|
|
|
void BLI_thread_queue_push(ThreadQueue *queue, void *work);
|
|
|
|
void *BLI_thread_queue_pop(ThreadQueue *queue);
|
|
|
|
void *BLI_thread_queue_pop_timeout(ThreadQueue *queue, int ms);
|
2018-02-15 13:36:11 +01:00
|
|
|
int BLI_thread_queue_len(ThreadQueue *queue);
|
2015-06-19 12:30:21 +02:00
|
|
|
bool BLI_thread_queue_is_empty(ThreadQueue *queue);
|
2010-01-22 12:06:57 +01:00
|
|
|
|
2012-06-10 14:26:33 +02:00
|
|
|
void BLI_thread_queue_wait_finish(ThreadQueue *queue);
|
2010-01-22 12:06:57 +01:00
|
|
|
void BLI_thread_queue_nowait(ThreadQueue *queue);
|
2008-08-17 00:47:33 +02:00
|
|
|
|
2016-03-03 11:53:42 +01:00
|
|
|
/* Thread local storage */
|
|
|
|
|
|
|
|
#if defined(__APPLE__)
|
|
|
|
# define ThreadLocal(type) pthread_key_t
|
|
|
|
# define BLI_thread_local_create(name) pthread_key_create(&name, NULL)
|
|
|
|
# define BLI_thread_local_delete(name) pthread_key_delete(name)
|
|
|
|
# define BLI_thread_local_get(name) pthread_getspecific(name)
|
|
|
|
# define BLI_thread_local_set(name, value) pthread_setspecific(name, value)
|
|
|
|
#else /* defined(__APPLE__) */
|
|
|
|
# ifdef _MSC_VER
|
|
|
|
# define ThreadLocal(type) __declspec(thread) type
|
|
|
|
# else
|
|
|
|
# define ThreadLocal(type) __thread type
|
|
|
|
# endif
|
|
|
|
# define BLI_thread_local_create(name)
|
|
|
|
# define BLI_thread_local_delete(name)
|
|
|
|
# define BLI_thread_local_get(name) name
|
|
|
|
# define BLI_thread_local_set(name, value) name = value
|
|
|
|
#endif /* defined(__APPLE__) */
|
|
|
|
|
2012-07-10 08:31:16 +02:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|