2022-02-11 09:07:11 +11:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
* Copyright 2006 Blender Foundation. All rights reserved. */
|
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 11:36:33 +00: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 11:36:33 +00:00
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
* \ingroup bli
|
2011-02-18 13:58:08 +00:00
|
|
|
*/
|
|
|
|
|
2009-09-30 18:18:32 +00:00
|
|
|
#include <pthread.h>
|
2006-03-13 11:01:17 +00: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 19:01:14 +11:00
|
|
|
/** For tables, button in UI, etc. */
|
2015-06-07 22:48:51 +02:00
|
|
|
#define BLENDER_MAX_THREADS 1024
|
2006-11-29 17:01:09 +00:00
|
|
|
|
|
|
|
struct ListBase;
|
2009-09-30 18:18:32 +00:00
|
|
|
|
|
|
|
/* Threading API */
|
|
|
|
|
2021-12-09 20:01:44 +11:00
|
|
|
/**
|
|
|
|
* This is run once at startup.
|
|
|
|
*/
|
2010-04-13 12:51:03 +00:00
|
|
|
void BLI_threadapi_init(void);
|
2013-08-19 10:51:40 +00:00
|
|
|
void BLI_threadapi_exit(void);
|
2010-04-13 12:51:03 +00:00
|
|
|
|
2021-12-09 20:01:44 +11: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-16 01:13:46 +11:00
|
|
|
void BLI_threadpool_init(struct ListBase *threadbase, void *(*do_thread)(void *), int tot);
|
2021-12-09 20:01:44 +11:00
|
|
|
/**
|
|
|
|
* Amount of available threads.
|
|
|
|
*/
|
2012-05-12 20:39:39 +00:00
|
|
|
int BLI_available_threads(struct ListBase *threadbase);
|
2021-12-09 20:01:44 +11:00
|
|
|
/**
|
|
|
|
* Returns thread number, for sample patterns or threadsafe tables.
|
|
|
|
*/
|
2018-02-16 01:13:46 +11: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 20:39:39 +00:00
|
|
|
int BLI_thread_is_main(void);
|
2011-10-22 16:16:14 +00:00
|
|
|
|
2009-09-30 18:18:32 +00:00
|
|
|
/* System Information */
|
|
|
|
|
2021-12-09 20:01:44 +11:00
|
|
|
/**
|
|
|
|
* \return the number of threads the system can make use of.
|
|
|
|
*/
|
|
|
|
int BLI_system_thread_count(void);
|
2013-05-08 13:23:17 +00: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 15:30:46 +10:00
|
|
|
/**
|
|
|
|
* Global Mutex Locks
|
2018-06-01 18:19:39 +02:00
|
|
|
*
|
2020-09-18 15:30:46 +10: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 18:18:32 +00:00
|
|
|
|
2018-02-16 01:13:46 +11:00
|
|
|
void BLI_thread_lock(int type);
|
|
|
|
void BLI_thread_unlock(int type);
|
2009-09-30 18:18:32 +00:00
|
|
|
|
|
|
|
/* Mutex Lock */
|
|
|
|
|
|
|
|
typedef pthread_mutex_t ThreadMutex;
|
2012-05-12 20:39:39 +00:00
|
|
|
#define BLI_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
|
2009-09-30 18:18:32 +00:00
|
|
|
|
|
|
|
void BLI_mutex_init(ThreadMutex *mutex);
|
2013-04-24 17:31:09 +00:00
|
|
|
void BLI_mutex_end(ThreadMutex *mutex);
|
|
|
|
|
|
|
|
ThreadMutex *BLI_mutex_alloc(void);
|
|
|
|
void BLI_mutex_free(ThreadMutex *mutex);
|
|
|
|
|
2009-09-30 18:18:32 +00:00
|
|
|
void BLI_mutex_lock(ThreadMutex *mutex);
|
2013-10-12 14:08:59 +00:00
|
|
|
bool BLI_mutex_trylock(ThreadMutex *mutex);
|
2009-09-30 18:18:32 +00:00
|
|
|
void BLI_mutex_unlock(ThreadMutex *mutex);
|
2006-02-11 15:55:00 +00:00
|
|
|
|
2012-11-15 15:59:58 +00: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 15:59:58 +00: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 18:18:32 +00:00
|
|
|
/* Read/Write Mutex Lock */
|
2008-08-16 22:47:33 +00:00
|
|
|
|
2012-05-12 20:39:39 +00:00
|
|
|
#define THREAD_LOCK_READ 1
|
|
|
|
#define THREAD_LOCK_WRITE 2
|
2009-06-08 10:00:14 +00:00
|
|
|
|
2013-12-29 16:40:34 +06:00
|
|
|
#define BLI_RWLOCK_INITIALIZER PTHREAD_RWLOCK_INITIALIZER
|
|
|
|
|
2009-09-30 18:18:32 +00:00
|
|
|
typedef pthread_rwlock_t ThreadRWMutex;
|
|
|
|
|
|
|
|
void BLI_rw_mutex_init(ThreadRWMutex *mutex);
|
2013-04-24 17:31:09 +00:00
|
|
|
void BLI_rw_mutex_end(ThreadRWMutex *mutex);
|
|
|
|
|
|
|
|
ThreadRWMutex *BLI_rw_mutex_alloc(void);
|
|
|
|
void BLI_rw_mutex_free(ThreadRWMutex *mutex);
|
|
|
|
|
2009-09-30 18:18:32 +00:00
|
|
|
void BLI_rw_mutex_lock(ThreadRWMutex *mutex, int mode);
|
|
|
|
void BLI_rw_mutex_unlock(ThreadRWMutex *mutex);
|
|
|
|
|
2013-07-08 17:56:51 +00: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 14:08:59 +00:00
|
|
|
/* Condition */
|
2018-06-17 16:32:54 +02:00
|
|
|
|
2013-10-12 14:08:59 +00:00
|
|
|
typedef pthread_cond_t ThreadCondition;
|
2008-08-16 22:47:33 +00:00
|
|
|
|
2013-10-12 14:08:59 +00:00
|
|
|
void BLI_condition_init(ThreadCondition *cond);
|
|
|
|
void BLI_condition_wait(ThreadCondition *cond, ThreadMutex *mutex);
|
2022-01-07 11:38:08 +11:00
|
|
|
void BLI_condition_wait_global_mutex(ThreadCondition *cond, int type);
|
2013-10-12 14:08:59 +00:00
|
|
|
void BLI_condition_notify_one(ThreadCondition *cond);
|
|
|
|
void BLI_condition_notify_all(ThreadCondition *cond);
|
|
|
|
void BLI_condition_end(ThreadCondition *cond);
|
2008-08-16 22:47:33 +00:00
|
|
|
|
2010-01-22 11:06:57 +00:00
|
|
|
/* ThreadWorkQueue
|
|
|
|
*
|
|
|
|
* Thread-safe work queue to push work/pointers between threads. */
|
|
|
|
|
|
|
|
typedef struct ThreadQueue ThreadQueue;
|
|
|
|
|
2010-12-03 12:30:59 +00:00
|
|
|
ThreadQueue *BLI_thread_queue_init(void);
|
2010-01-22 11:06:57 +00: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 23:36:11 +11: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 11:06:57 +00:00
|
|
|
|
2012-06-10 12:26:33 +00:00
|
|
|
void BLI_thread_queue_wait_finish(ThreadQueue *queue);
|
2010-01-22 11:06:57 +00:00
|
|
|
void BLI_thread_queue_nowait(ThreadQueue *queue);
|
2008-08-16 22:47:33 +00:00
|
|
|
|
2016-03-03 15:53:42 +05: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 06:31:16 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|