comparison vis_threading.h @ 0:76568becd6d6

Rhope Alpha 2a source import
author Mike Pavone <pavone@retrodev.com>
date Tue, 28 Apr 2009 23:06:07 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:76568becd6d6
1 #ifndef _VIS_THREADING_H_
2 #define _VIS_THREADING_H_
3
4 #ifndef COMPILE_THREADS
5 #ifdef NO_THREADS
6 #define COMPILE_THREADS 1
7 #else
8 #define COMPILE_THREADS 4
9 #endif
10 #endif
11
12 #if COMPILE_THREADS > 1
13
14 #ifdef WIN32
15
16 #include "windows.h"
17 /*
18 __inline void VIS_EnterCriticalSectionImpl(short * cs)
19 {
20 int tmp = (int)cs;
21 __asm
22 {
23 mov ebx, tmp
24 CriticalLoop:
25 bts [ebx], 0
26 jnc End
27 }
28 Sleep(0);
29 __asm
30 {
31 jmp CriticalLoop
32 }
33
34 End:
35 return;
36 }*/
37
38 extern int lock_fail_counter;
39 extern int lock_counter;
40 extern int spin_counter;
41
42 volatile __inline void VIS_EnterCriticalSectionImpl(short * cs)
43 {
44 int tmp = (int)cs;
45 //int spins = 0;
46 __asm
47 {
48 mov ebx, tmp
49 CriticalLoop:
50 mov ax, 1
51 xchg ax, [ebx]
52 test ax, ax
53
54
55 jz End
56 }
57 //++spins;
58 Sleep(0);
59 __asm
60 {
61 jmp CriticalLoop
62 }
63
64 End:
65 /* if(spins > 0)
66 {
67 tmp = (int)(&lock_fail_counter);
68 __asm
69 {
70 mov ebx, tmp
71 lock inc dword ptr [ebx]
72 }
73 tmp = (int)(&spin_counter);
74 __asm
75 {
76 mov ebx, tmp
77 mov eax, spins
78 lock xadd dword ptr [ebx], eax
79 }
80 }
81 tmp = (int)(&lock_counter);
82 __asm
83 {
84 mov ebx, tmp
85 lock inc dword ptr [ebx]
86 }*/
87 return;
88 }
89
90 volatile __inline void VIS_LeaveCriticalSectionImpl(short * cs)
91 {
92 int tmp = (int)cs;
93 __asm
94 {
95 mov ebx, tmp
96 mov ax, 0
97 xchg ax, [ebx]
98 }
99 }
100 #else
101 /*
102 #include <unistd.h>
103
104 volatile inline void VIS_EnterCriticalSectionImpl(short * cs)
105 {
106 int code;
107 VIS_EnterStart:
108 asm(
109 "movw $1, %%ax\n\t"
110 "xchg %%ax, (%1)\n\t"
111 "test %%ax, %%ax\n\t"
112 "jz VIS_EnterEnd\n\t"
113 "movl $1, %0\n\t"
114 "jmp VIS_EnterCont\n"
115 "VIS_EnterEnd:\n\t"
116 "movl $0, %0\n"
117 "VIS_EnterCont:":
118 "=r"(code):
119 "r"(cs):
120 "%ax");
121 if(!code)
122 return;
123 sleep(0);
124 goto VIS_EnterStart;
125 }
126
127 volatile inline void VIS_LeaveCriticalSectionImpl(short * cs)
128 {
129 asm(
130 "movw $0, %%ax\n\t"
131 "mfence\n\t"
132 "xchg %%ax, (%0)"::
133 "r"(cs):
134 "%ax");
135 }*/
136 #ifdef CPLUSPLUS
137 extern "C" {
138 #endif
139 volatile void VIS_EnterCriticalSectionImpl(short * cs);
140 volatile void VIS_LeaveCriticalSectionImpl(short * cs);
141
142 #ifdef CPLUSPLUS
143 }
144 #endif
145
146 #endif //Win32
147
148 #ifdef USE_OS_PRIMITIVES
149 #ifdef WIN32
150 //TODO fill this in
151 #else
152 #ifdef SYLLABLE
153 //TODO fill this in
154 #else
155 #include <pthread.h>
156 #define VIS_CRITICAL_SECTION(cs) pthread_mutex_t cs;
157 #define VIS_EXTERN_CRITICAL_SECTION(cs) extern pthread_mutex_t cs;
158 #define VIS_InitializeCriticalSection(cs) pthread_mutex_init(&(cs), NULL)
159 #define VIS_EnterCriticalSection(cs) pthread_mutex_lock(&(cs))
160 #define VIS_LeaveCriticalSection(cs) pthread_mutex_unlock(&(cs))
161 #define VIS_DeleteCriticalSection(cs) pthread_mutex_destroy(&(cs))
162 #endif //SYLLABLE
163 #endif //WIN32
164 #else
165 #define VIS_CRITICAL_SECTION(cs) short cs;//CRITICAL_SECTION cs;//
166 #define VIS_EXTERN_CRITICAL_SECTION(cs) extern short cs;
167 #define VIS_InitializeCriticalSection(cs) cs = 0
168 #define VIS_EnterCriticalSection(cs) VIS_EnterCriticalSectionImpl(&(cs))
169 #define VIS_LeaveCriticalSection(cs) cs = 0//VIS_LeaveCriticalSectionImpl(&(cs))
170 #define VIS_DeleteCriticalSection(cs)
171 #endif/*
172 #define VIS_InitializeCriticalSection(cs) InitializeCriticalSectionAndSpinCount(&(cs),0x400);
173 #define VIS_EnterCriticalSection(cs) EnterCriticalSection(&(cs))
174 #define VIS_LeaveCriticalSection(cs) LeaveCriticalSection(&(cs))
175 #define VIS_DeleteCriticalSection(cs) DeleteCriticalSection(&(cs))*/
176
177 #ifdef WIN32
178
179 #define VIS_Event(evt) HANDLE evt;
180 #define VIS_CreateEvent(evt) evt = CreateEvent(NULL, TRUE, FALSE, NULL)
181 #define VIS_DestroyEvent(evt) CloseHandle(evt)
182 #define VIS_SetEvent(evt) SetEvent(evt)
183 #define VIS_ResetEvent(evt) ResetEvent(evt)
184 #define VIS_WaitEvent(evt) WaitForSingleObject(evt, INFINITE)
185
186 #else
187 #ifdef SYLLABLE
188 //TODO fill this in
189 #define VIS_Event(evt)
190 #define VIS_CreateEvent(evt)
191 #define VIS_DestroyEvent(evt)
192 #define VIS_SetEvent(evt)
193 #define VIS_ResetEvent(evt)
194 #define VIS_WaitEvent(evt) sleep(0)
195 #else
196 #define VIS_Event(evt) pthread_cond_t evt; pthread_mutex_t evt##_mutex;
197 #define VIS_CreateEvent(evt) pthread_cond_init(&(evt), NULL); pthread_mutex_init(&(evt##_mutex),NULL)
198 #define VIS_DestroyEvent(evt) pthread_cond_destroy(&(evt)); pthread_mutex_destroy(&(evt##_mutex))
199 #define VIS_SetEvent(evt) pthread_mutex_lock(&(evt##_mutex)); pthread_cond_signal(&(evt)); pthread_mutex_unlock(&evt##_mutex);
200 #define VIS_ResetEvent(evt)
201 #define VIS_WaitEvent(evt) pthread_mutex_lock(&(evt##_mutex)); pthread_cond_wait(&(evt), &(evt##_mutex)); pthread_mutex_unlock(&evt##_mutex)
202 #endif //SYLLABLE
203 #endif //WIN32
204
205 #ifdef WIN32
206 #define VIS_NewThread(func, data) CreateThread(NULL, 0, func, data, 0, NULL)
207 #else
208 #ifdef SYLLABLE
209 #define VIS_NewThread(func, data) resume_thread(spawn_thread("vis_worker", func, 1, 0, data));
210 #else
211 #include <pthread.h>
212 pthread_t pid;
213 #define VIS_NewThread(func, data) pthread_create(&pid, NULL, func, data);
214 #endif
215 #endif
216
217 #else
218
219 #define VIS_CRITICAL_SECTION(cs)
220 #define VIS_EXTERN_CRITICAL_SECTION(cs)
221 #define VIS_InitializeCriticalSection(cs)
222 #define VIS_EnterCriticalSection(cs)
223 #define VIS_LeaveCriticalSection(cs)
224 #define VIS_DeleteCriticalSection(cs)
225
226 #define VIS_Event(evt)
227 #define VIS_CreateEvent(evt)
228 #define VIS_DestroyEvent(evt)
229 #define VIS_SetEvent(evt)
230 #define VIS_WaitEvent(evt)
231 #define VIS_ResetEvent(evt)
232 #define VIS_NewThread(func, data)
233
234 #endif //COMPILE_THREADS > 1
235
236
237 #endif //_VIS_THREADING_H_
238
239
240
241
242