10 void Thread_Shutdown(void)
14 qboolean Thread_HasThreads(void)
19 void *Thread_CreateMutex(void)
21 return (void *)CreateMutex(NULL, FALSE, NULL);
24 void Thread_DestroyMutex(void *mutex)
29 int Thread_LockMutex(void *mutex)
31 return (WaitForSingleObject(mutex, INFINITE) == WAIT_FAILED) ? -1 : 0;
34 int Thread_UnlockMutex(void *mutex)
36 return (ReleaseMutex(mutex) == FALSE) ? -1 : 0;
39 typedef struct thread_semaphore_s
46 static thread_semaphore_t *Thread_CreateSemaphore(unsigned int v)
48 thread_semaphore_t *s = (thread_semaphore_t *)calloc(sizeof(*s), 1);
49 s->semaphore = CreateSemaphore(NULL, v, 32768, NULL);
54 static void Thread_DestroySemaphore(thread_semaphore_t *s)
56 CloseHandle(s->semaphore);
60 static int Thread_WaitSemaphore(thread_semaphore_t *s, unsigned int msec)
62 int r = WaitForSingleObject(s->semaphore, msec);
63 if (r == WAIT_OBJECT_0)
65 InterlockedDecrement(&s->value);
68 if (r == WAIT_TIMEOUT)
73 static int Thread_PostSemaphore(thread_semaphore_t *s)
75 InterlockedIncrement(&s->value);
76 if (ReleaseSemaphore(s->semaphore, 1, NULL))
78 InterlockedDecrement(&s->value);
82 typedef struct thread_cond_s
87 thread_semaphore_t *sem;
88 thread_semaphore_t *done;
92 void *Thread_CreateCond(void)
94 thread_cond_t *c = (thread_cond_t *)calloc(sizeof(*c), 1);
95 c->mutex = CreateMutex(NULL, FALSE, NULL);
96 c->sem = Thread_CreateSemaphore(0);
97 c->done = Thread_CreateSemaphore(0);
103 void Thread_DestroyCond(void *cond)
105 thread_cond_t *c = (thread_cond_t *)cond;
106 Thread_DestroySemaphore(c->sem);
107 Thread_DestroySemaphore(c->done);
108 CloseHandle(c->mutex);
111 int Thread_CondSignal(void *cond)
113 thread_cond_t *c = (thread_cond_t *)cond;
115 WaitForSingleObject(c->mutex, INFINITE);
116 n = c->waiting - c->signals;
120 Thread_PostSemaphore(c->sem);
122 ReleaseMutex(c->mutex);
124 Thread_WaitSemaphore(c->done, INFINITE);
128 int Thread_CondBroadcast(void *cond)
130 thread_cond_t *c = (thread_cond_t *)cond;
133 WaitForSingleObject(c->mutex, INFINITE);
134 n = c->waiting - c->signals;
138 for (i = 0;i < n;i++)
139 Thread_PostSemaphore(c->sem);
141 ReleaseMutex(c->mutex);
142 for (i = 0;i < n;i++)
143 Thread_WaitSemaphore(c->done, INFINITE);
147 int Thread_CondWait(void *cond, void *mutex)
149 thread_cond_t *c = (thread_cond_t *)cond;
152 WaitForSingleObject(c->mutex, INFINITE);
154 ReleaseMutex(c->mutex);
158 waitresult = Thread_WaitSemaphore(c->sem, INFINITE);
159 WaitForSingleObject(c->mutex, INFINITE);
163 Thread_WaitSemaphore(c->sem, INFINITE);
164 Thread_PostSemaphore(c->done);
168 ReleaseMutex(c->mutex);
170 WaitForSingleObject(mutex, INFINITE);
174 typedef struct threadwrapper_s
177 unsigned int threadid;
184 unsigned int __stdcall Thread_WrapperFunc(void *d)
186 threadwrapper_t *w = (threadwrapper_t *)d;
187 w->result = w->fn(w->data);
188 _endthreadex(w->result);
192 void *Thread_CreateThread(int (*fn)(void *), void *data)
194 threadwrapper_t *w = (threadwrapper_t *)calloc(sizeof(*w), 1);
199 w->handle = (HANDLE)_beginthreadex(NULL, 0, Thread_WrapperFunc, (void *)w, 0, &w->threadid);
203 int Thread_WaitThread(void *d, int retval)
205 threadwrapper_t *w = (threadwrapper_t *)d;
206 WaitForSingleObject(w->handle, INFINITE);
207 CloseHandle(w->handle);