]> git.xonotic.org Git - xonotic/darkplaces.git/blob - thread.h
Overhauled r_shadow_bouncegrid, it performs much faster, makes use of as many threads...
[xonotic/darkplaces.git] / thread.h
1 #ifndef THREAD_H
2
3 // enable Sys_PrintfToTerminal calls on nearly every threading call
4 //#define THREADDEBUG
5 //#define THREADDISABLE
6 // use recursive mutex (non-posix) extensions in thread_pthread
7 #define THREADRECURSIVE
8
9 typedef int Thread_SpinLock;
10 typedef struct {int value;} Thread_Atomic;
11
12 #define Thread_CreateMutex()              (_Thread_CreateMutex(__FILE__, __LINE__))
13 #define Thread_DestroyMutex(m)            (_Thread_DestroyMutex(m, __FILE__, __LINE__))
14 #define Thread_LockMutex(m)               (_Thread_LockMutex(m, __FILE__, __LINE__))
15 #define Thread_UnlockMutex(m)             (_Thread_UnlockMutex(m, __FILE__, __LINE__))
16 #define Thread_CreateCond()               (_Thread_CreateCond(__FILE__, __LINE__))
17 #define Thread_DestroyCond(cond)          (_Thread_DestroyCond(cond, __FILE__, __LINE__))
18 #define Thread_CondSignal(cond)           (_Thread_CondSignal(cond, __FILE__, __LINE__))
19 #define Thread_CondBroadcast(cond)        (_Thread_CondBroadcast(cond, __FILE__, __LINE__))
20 #define Thread_CondWait(cond, mutex)      (_Thread_CondWait(cond, mutex, __FILE__, __LINE__))
21 #define Thread_CreateThread(fn, data)     (_Thread_CreateThread(fn, data, __FILE__, __LINE__))
22 #define Thread_WaitThread(thread, retval) (_Thread_WaitThread(thread, retval, __FILE__, __LINE__))
23 #define Thread_CreateBarrier(count)       (_Thread_CreateBarrier(count, __FILE__, __LINE__))
24 #define Thread_DestroyBarrier(barrier)    (_Thread_DestroyBarrier(barrier, __FILE__, __LINE__))
25 #define Thread_WaitBarrier(barrier)       (_Thread_WaitBarrier(barrier, __FILE__, __LINE__))
26 #define Thread_AtomicGet(a)               (_Thread_AtomicGet(a, __FILE__, __LINE__))
27 #define Thread_AtomicSet(a, v)            (_Thread_AtomicSet(a, v, __FILE__, __LINE__))
28 #define Thread_AtomicAdd(a, v)            (_Thread_AtomicAdd(a, v, __FILE__, __LINE__))
29 #define Thread_AtomicIncRef(a)            (_Thread_AtomicIncRef(a, __FILE__, __LINE__))
30 #define Thread_AtomicDecRef(a)            (_Thread_AtomicDecRef(a, __FILE__, __LINE__))
31 #define Thread_AtomicTryLock(lock)        (_Thread_AtomicTryLock(lock, __FILE__, __LINE__))
32 #define Thread_AtomicLock(lock)           (_Thread_AtomicLock(lock, __FILE__, __LINE__))
33 #define Thread_AtomicUnlock(lock)         (_Thread_AtomicUnlock(lock, __FILE__, __LINE__))
34
35 int Thread_Init(void);
36 void Thread_Shutdown(void);
37 qboolean Thread_HasThreads(void);
38 void *_Thread_CreateMutex(const char *filename, int fileline);
39 void _Thread_DestroyMutex(void *mutex, const char *filename, int fileline);
40 int _Thread_LockMutex(void *mutex, const char *filename, int fileline);
41 int _Thread_UnlockMutex(void *mutex, const char *filename, int fileline);
42 void *_Thread_CreateCond(const char *filename, int fileline);
43 void _Thread_DestroyCond(void *cond, const char *filename, int fileline);
44 int _Thread_CondSignal(void *cond, const char *filename, int fileline);
45 int _Thread_CondBroadcast(void *cond, const char *filename, int fileline);
46 int _Thread_CondWait(void *cond, void *mutex, const char *filename, int fileline);
47 void *_Thread_CreateThread(int (*fn)(void *), void *data, const char *filename, int fileline);
48 int _Thread_WaitThread(void *thread, int retval, const char *filename, int fileline);
49 void *_Thread_CreateBarrier(unsigned int count, const char *filename, int fileline);
50 void _Thread_DestroyBarrier(void *barrier, const char *filename, int fileline);
51 void _Thread_WaitBarrier(void *barrier, const char *filename, int fileline);
52 int _Thread_AtomicGet(Thread_Atomic *ref, const char *filename, int fileline);
53 int _Thread_AtomicSet(Thread_Atomic *ref, int v, const char *filename, int fileline);
54 int _Thread_AtomicAdd(Thread_Atomic *ref, int v, const char *filename, int fileline);
55 void _Thread_AtomicIncRef(Thread_Atomic *ref, const char *filename, int fileline);
56 qboolean _Thread_AtomicDecRef(Thread_Atomic *ref, const char *filename, int fileline);
57 qboolean _Thread_AtomicTryLock(Thread_SpinLock *lock, const char *filename, int fileline);
58 void _Thread_AtomicLock(Thread_SpinLock *lock, const char *filename, int fileline);
59 void _Thread_AtomicUnlock(Thread_SpinLock *lock, const char *filename, int fileline);
60
61 typedef struct taskqueue_task_s
62 {
63         // doubly linked list
64         struct taskqueue_task_s * volatile prev;
65         struct taskqueue_task_s * volatile next;
66
67         // if not NULL, this task must be done before this one will dequeue (faster than simply Yielding immediately)
68         struct taskqueue_task_s *preceding;
69
70         // see TaskQueue_IsDone() to use proper atomics to poll done status
71         volatile int started;
72         volatile int done;
73
74         // function to call, and parameters for it to use
75         void(*func)(struct taskqueue_task_s *task);
76         void *p[4];
77         size_t i[4];
78
79         // stats:
80         unsigned int yieldcount; // number of times this task has been requeued
81 }
82 taskqueue_task_t;
83
84 // immediately execute any pending tasks if threading is disabled (or if force is true)
85 // TRY NOT TO USE THIS IF POSSIBLE - poll task->done instead.
86 void TaskQueue_Execute(qboolean force);
87
88 // queue the tasks to be executed, or executes them immediately if threading is disabled.
89 void TaskQueue_Enqueue(int numtasks, taskqueue_task_t *tasks);
90
91 // if the task can not be completed due yet to preconditions, just enqueue it again...
92 void TaskQueue_Yield(taskqueue_task_t *t);
93
94 // polls for status of task and returns the result immediately - use this instead of checking ->done directly, as this uses atomics
95 qboolean TaskQueue_IsDone(taskqueue_task_t *t);
96
97 // polls for status of task and waits for it to be done
98 void TaskQueue_WaitForTaskDone(taskqueue_task_t *t);
99
100 // updates thread count based on the cvar.
101 void TaskQueue_Frame(qboolean shutdown);
102
103 // convenience function for setting up a task structure.  Does not do the Enqueue, just fills in the struct.
104 void TaskQueue_Setup(taskqueue_task_t *t, taskqueue_task_t *preceding, void(*func)(taskqueue_task_t *), size_t i0, size_t i1, void *p0, void *p1);
105
106 // general purpose tasks
107 // t->i[0] = number of tasks in array
108 // t->p[0] = array of taskqueue_task_t to check
109 void TaskQueue_Task_CheckTasksDone(taskqueue_task_t *t);
110
111 #endif