PageRenderTime 17ms CodeModel.GetById 2ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/platform/unix/scaffold/thread.d

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