PageRenderTime 39ms CodeModel.GetById 2ms app.highlight 32ms RepoModel.GetById 1ms app.codeStats 0ms

/platform/osx/scaffold/thread.d

http://github.com/wilkie/djehuty
D | 185 lines | 65 code | 38 blank | 82 comment | 0 complexity | a6af937cc5285fbaf482e1ad4efe44eb MD5 | raw file
  1/*
  2 * thread.d
  3 *
  4 * This Scaffold holds the Thread implementations for the Linux platform
  5 *
  6 * Author: Dave Wilkinson
  7 *
  8 */
  9
 10module scaffold.thread;
 11
 12import core.string;
 13import core.color;
 14import core.main;
 15import core.definitions;
 16import core.string;
 17
 18import synch.thread;
 19
 20import platform.osx.common;
 21import platform.unix.common;
 22
 23import platform.vars.thread;
 24import platform.vars.condition;
 25import platform.vars.mutex;
 26import platform.vars.semaphore;
 27
 28import binding.c;
 29
 30void ThreadSleep(ref ThreadPlatformVars threadVars, ulong milliseconds) {
 31	timespec timetoexpire;
 32
 33	timetoexpire.tv_sec = (milliseconds / 1000);
 34	timetoexpire.tv_nsec = (milliseconds % 1000) * 1000000;
 35
 36	int ret = nanosleep(&timetoexpire, null);
 37}
 38
 39/*
 40
 41extern (C)
 42void *_djehuty_unix_thread_proc(void* udata)
 43{
 44	Thread t_info = cast(Thread)(udata);
 45	ThreadPlatformVars* threadVars = ThreadGetPlatformVars(t_info);
 46
 47	t_info.run();
 48
 49	threadVars.id = 0;
 50
 51	ThreadUninit(t_info);
 52
 53	pthread_exit(null);
 54
 55	return null;
 56}
 57
 58
 59void ThreadStart(ref ThreadPlatformVars threadVars, ref Thread thread)
 60{
 61	int ret = pthread_create(&threadVars.id, null, &_djehuty_unix_thread_proc, cast(void *)thread);
 62	if (ret)
 63	{
 64		// error creating thread
 65		threadVars.id = 0;
 66	}
 67}
 68
 69void ThreadStop(ref ThreadPlatformVars threadVars)
 70{
 71	if (threadVars.id)
 72	{
 73		if (threadVars.id == pthread_self())
 74		{
 75			//soft exit
 76			printf("thread - soft kill\n");
 77			threadVars.id = 0;
 78			pthread_exit(null);
 79		}
 80		else
 81		{
 82			//hard exit
 83			printf("thread - hard kill\n");
 84			pthread_kill(threadVars.id, SIGKILL);
 85		}
 86
 87		threadVars.id = 0;
 88	}
 89}
 90
 91void ThreadSleep(ref ThreadPlatformVars threadVars, ulong milliseconds)
 92{
 93	timespec timetoexpire;
 94
 95	timetoexpire.tv_sec = (milliseconds / 1000);
 96	timetoexpire.tv_nsec = (milliseconds % 1000) * 1000000;
 97
 98	nanosleep(&timetoexpire, null);
 99}
100
101bool ThreadIsCurrent(ref ThreadPlatformVars threadVars)
102{
103	return false;
104}
105
106
107
108*/
109
110
111
112
113
114
115
116
117// Semaphores
118
119void SemaphoreInit(ref SemaphorePlatformVars semVars, ref uint initialValue)
120{
121	sem_init(&semVars.sem_id, 0, initialValue);
122}
123
124void SemaphoreUninit(ref SemaphorePlatformVars semVars)
125{
126	sem_destroy(&semVars.sem_id);
127}
128
129void SemaphoreUp(ref SemaphorePlatformVars semVars)
130{
131	sem_post(&semVars.sem_id);
132}
133
134void SemaphoreDown(ref SemaphorePlatformVars semVars, uint ms)
135{
136	// TODO: semaphore timeout
137	sem_wait(&semVars.sem_id);
138}
139
140void SemaphoreDown(ref SemaphorePlatformVars semVars)
141{
142	sem_wait(&semVars.sem_id);
143}
144
145
146
147
148
149// Mutexes
150
151void MutexInit(ref MutexPlatformVars mutVars)
152{
153}
154
155void MutexUninit(ref MutexPlatformVars mutVars)
156{
157}
158
159void MutexLock(ref MutexPlatformVars mutVars)
160{
161}
162
163void MutexLock(ref MutexPlatformVars mutVars, ref uint ms)
164{
165}
166
167void MutexUnlock(ref MutexPlatformVars mutVars)
168{
169}
170
171void ConditionInit(ref ConditionPlatformVars condVars) {
172}
173
174void ConditionSignal(ref ConditionPlatformVars condVars) {
175}
176
177void ConditionWait(ref ConditionPlatformVars condVars) {
178}
179
180void ConditionWait(ref ConditionPlatformVars condVars, ref MutexPlatformVars mutVars) {
181}
182
183void ConditionUninit(ref ConditionPlatformVars condVars) {
184	//pthread_cond_destroy(&condVars.cond_id);
185}