PageRenderTime 50ms CodeModel.GetById 15ms app.highlight 29ms RepoModel.GetById 1ms app.codeStats 1ms

/platform/win/scaffold/thread.d

http://github.com/wilkie/djehuty
D | 159 lines | 58 code | 43 blank | 58 comment | 1 complexity | a43b24ff3a671a1ee772ed29171aee2c MD5 | raw file
  1/*
  2 * thread.d
  3 *
  4 * This file implements the Scaffold for platform specific Thread
  5 * operations in Windows.
  6 *
  7 * Author: Dave Wilkinson
  8 *
  9 */
 10
 11module scaffold.thread;
 12
 13import core.main;
 14import core.definitions;
 15import core.string;
 16
 17import synch.thread;
 18
 19import platform.win.main;
 20import platform.win.common;
 21
 22import platform.vars.mutex;
 23import platform.vars.semaphore;
 24import platform.vars.thread;
 25import platform.vars.condition;
 26
 27/*extern(Windows)
 28DWORD _win_djehuty_thread_proc(void* udata)
 29{
 30	Thread t = cast(Thread)udata;
 31
 32	t.run();
 33
 34	ThreadPlatformVars* threadVars = ThreadGetPlatformVars(t);
 35
 36	threadVars.thread = null;
 37	threadVars.thread_id = 0;
 38
 39	ThreadUninit(t);
 40
 41	return 0;
 42}
 43
 44void ThreadStart(ref ThreadPlatformVars threadVars, ref Thread thread)
 45{
 46	threadVars.thread = CreateThread(null, 0, &_win_djehuty_thread_proc, cast(void*)thread, 0, &threadVars.thread_id);
 47}
 48
 49void ThreadStop(ref ThreadPlatformVars threadVars)
 50{
 51	if (threadVars.thread_id == GetCurrentThreadId())
 52	{ // soft exit if called from the created thread
 53		ExitThread(0);
 54	}
 55	else
 56	{ // hard exit if called from another thread
 57		TerminateThread(threadVars.thread, 0);
 58	}
 59
 60	threadVars.thread = null;
 61	threadVars.thread_id = 0;
 62}*/
 63
 64void ThreadSleep(ref ThreadPlatformVars threadVars, ulong milliseconds) {
 65	while (milliseconds > 0xFFFFFFFF) {
 66		.Sleep(0xFFFFFFFF);
 67
 68		milliseconds -= 0xFFFFFFFF;
 69	}
 70	.Sleep(cast(uint)milliseconds);
 71}
 72
 73//bool ThreadIsCurrent(ref ThreadPlatformVars threadVars)
 74//{
 75//	return threadVars.thread_id == GetCurrentThreadId();
 76//}
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89// Semaphores
 90
 91void SemaphoreInit(ref SemaphorePlatformVars semVars, ref uint initialValue) {
 92	semVars._semaphore = CreateSemaphoreA(null, (initialValue), 0xFFFFFFF, null);
 93}
 94
 95void SemaphoreUninit(ref SemaphorePlatformVars semVars) {
 96	CloseHandle(semVars._semaphore);
 97}
 98
 99void SemaphoreUp(ref SemaphorePlatformVars semVars) {
100	ReleaseSemaphore(semVars._semaphore, 1, null);
101}
102
103void SemaphoreDown(ref SemaphorePlatformVars semVars, uint ms) {
104	WaitForSingleObject(semVars._semaphore, ms);
105}
106
107void SemaphoreDown(ref SemaphorePlatformVars semVars) {
108	WaitForSingleObject(semVars._semaphore, INFINITE);
109}
110
111
112
113
114
115// Mutexes
116
117void MutexInit(ref MutexPlatformVars mutVars) {
118//	InitializeCriticalSection(mutVars._mutex);
119	mutVars._semaphore = CreateSemaphoreA(null, (1), 0xFFFFFFF, null);
120}
121
122void MutexUninit(ref MutexPlatformVars mutVars) {
123	//DeleteCriticalSection(mutVars._mutex);
124	CloseHandle(mutVars._semaphore);
125}
126
127void MutexLock(ref MutexPlatformVars mutVars) {
128//	EnterCriticalSection(mutVars._mutex);
129	WaitForSingleObject(mutVars._semaphore, INFINITE);
130}
131
132void MutexLock(ref MutexPlatformVars mutVars, ref uint ms) {
133	// XXX: Use TryEnterCriticalSection in a timed loop here
134	//EnterCriticalSection(mutVars._mutex);
135	WaitForSingleObject(mutVars._semaphore, ms);
136}
137
138void MutexUnlock(ref MutexPlatformVars mutVars) {
139	//LeaveCriticalSection(mutVars._mutex);
140	ReleaseSemaphore(mutVars._semaphore, 1, null);
141}
142
143
144// Conditions
145
146void ConditionInit(ref ConditionPlatformVars condVars) {
147}
148
149void ConditionSignal(ref ConditionPlatformVars condVars) {
150}
151
152void ConditionWait(ref ConditionPlatformVars condVars) {
153}
154
155void ConditionWait(ref ConditionPlatformVars condVars, ref MutexPlatformVars mutVars) {
156}
157
158void ConditionUninit(ref ConditionPlatformVars condVars) {
159}