10 void Thread_Shutdown(void)
14 qboolean Thread_HasThreads(void)
19 void *_Thread_CreateMutex(const char *filename, int fileline)
21 void *mutex = (void *)CreateMutex(NULL, FALSE, NULL);
23 Sys_PrintfToTerminal("%p create %s:%i\n" , mutex, filename, fileline);
28 void _Thread_DestroyMutex(void *mutex, const char *filename, int fileline)
31 Sys_PrintfToTerminal("%p destroy %s:%i\n", mutex, filename, fileline);
36 int _Thread_LockMutex(void *mutex, const char *filename, int fileline)
39 Sys_PrintfToTerminal("%p lock %s:%i\n" , mutex, filename, fileline);
41 return (WaitForSingleObject(mutex, INFINITE) == WAIT_FAILED) ? -1 : 0;
44 int _Thread_UnlockMutex(void *mutex, const char *filename, int fileline)
47 Sys_PrintfToTerminal("%p unlock %s:%i\n" , mutex, filename, fileline);
49 return (ReleaseMutex(mutex) == FALSE) ? -1 : 0;
52 typedef struct thread_semaphore_s
59 static thread_semaphore_t *Thread_CreateSemaphore(unsigned int v)
61 thread_semaphore_t *s = (thread_semaphore_t *)calloc(sizeof(*s), 1);
62 s->semaphore = CreateSemaphore(NULL, v, 32768, NULL);
67 static void Thread_DestroySemaphore(thread_semaphore_t *s)
69 CloseHandle(s->semaphore);
73 static int Thread_WaitSemaphore(thread_semaphore_t *s, unsigned int msec)
75 int r = WaitForSingleObject(s->semaphore, msec);
76 if (r == WAIT_OBJECT_0)
78 InterlockedDecrement(&s->value);
81 if (r == WAIT_TIMEOUT)
86 static int Thread_PostSemaphore(thread_semaphore_t *s)
88 InterlockedIncrement(&s->value);
89 if (ReleaseSemaphore(s->semaphore, 1, NULL))
91 InterlockedDecrement(&s->value);
95 typedef struct thread_cond_s
100 thread_semaphore_t *sem;
101 thread_semaphore_t *done;
105 void *Thread_CreateCond(void)
107 thread_cond_t *c = (thread_cond_t *)calloc(sizeof(*c), 1);
108 c->mutex = CreateMutex(NULL, FALSE, NULL);
109 c->sem = Thread_CreateSemaphore(0);
110 c->done = Thread_CreateSemaphore(0);
116 void Thread_DestroyCond(void *cond)
118 thread_cond_t *c = (thread_cond_t *)cond;
119 Thread_DestroySemaphore(c->sem);
120 Thread_DestroySemaphore(c->done);
121 CloseHandle(c->mutex);
124 int Thread_CondSignal(void *cond)
126 thread_cond_t *c = (thread_cond_t *)cond;
128 WaitForSingleObject(c->mutex, INFINITE);
129 n = c->waiting - c->signals;
133 Thread_PostSemaphore(c->sem);
135 ReleaseMutex(c->mutex);
137 Thread_WaitSemaphore(c->done, INFINITE);
141 int Thread_CondBroadcast(void *cond)
143 thread_cond_t *c = (thread_cond_t *)cond;
146 WaitForSingleObject(c->mutex, INFINITE);
147 n = c->waiting - c->signals;
151 for (i = 0;i < n;i++)
152 Thread_PostSemaphore(c->sem);
154 ReleaseMutex(c->mutex);
155 for (i = 0;i < n;i++)
156 Thread_WaitSemaphore(c->done, INFINITE);
160 int Thread_CondWait(void *cond, void *mutex)
162 thread_cond_t *c = (thread_cond_t *)cond;
165 WaitForSingleObject(c->mutex, INFINITE);
167 ReleaseMutex(c->mutex);
171 waitresult = Thread_WaitSemaphore(c->sem, INFINITE);
172 WaitForSingleObject(c->mutex, INFINITE);
176 Thread_WaitSemaphore(c->sem, INFINITE);
177 Thread_PostSemaphore(c->done);
181 ReleaseMutex(c->mutex);
183 WaitForSingleObject(mutex, INFINITE);
187 typedef struct threadwrapper_s
190 unsigned int threadid;
197 unsigned int __stdcall Thread_WrapperFunc(void *d)
199 threadwrapper_t *w = (threadwrapper_t *)d;
200 w->result = w->fn(w->data);
201 _endthreadex(w->result);
205 void *Thread_CreateThread(int (*fn)(void *), void *data)
207 threadwrapper_t *w = (threadwrapper_t *)calloc(sizeof(*w), 1);
212 w->handle = (HANDLE)_beginthreadex(NULL, 0, Thread_WrapperFunc, (void *)w, 0, &w->threadid);
216 int Thread_WaitThread(void *d, int retval)
218 threadwrapper_t *w = (threadwrapper_t *)d;
219 WaitForSingleObject(w->handle, INFINITE);
220 CloseHandle(w->handle);