author | tron |
Fri, 05 Aug 2005 09:15:41 +0000 | |
changeset 2285 | 3193cbd1ba88 |
child 2286 | acb76b379e72 |
permissions | -rw-r--r-- |
2285
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
1 |
/* $Id$ */ |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
2 |
|
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
3 |
#include "stdafx.h" |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
4 |
#include "thread.h" |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
5 |
#include <stdlib.h> |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
6 |
|
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
7 |
#if defined(__AMIGA__) || defined(__MORPHOS__) |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
8 |
Thread* OTTDCreateThread(ThreadFunc function, void* arg) { return NULL; } |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
9 |
void OTTDJoinThread(Thread*) {} |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
10 |
|
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
11 |
|
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
12 |
#elif defined(__OS2__) |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
13 |
|
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
14 |
#define INCL_DOS |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
15 |
#include <os2.h> |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
16 |
#include <process.h> |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
17 |
|
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
18 |
struct Thread { |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
19 |
TID thread; |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
20 |
}; |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
21 |
|
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
22 |
Thread* OTTDCreateThread(ThreadFunc function, void* arg) |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
23 |
{ |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
24 |
Thread* t = malloc(sizeof(*t)); |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
25 |
|
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
26 |
if (t == NULL) return NULL; |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
27 |
|
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
28 |
t->thread = _beginthread(function, NULL, 32768, arg); |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
29 |
if (t->thread != -1) { |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
30 |
return t; |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
31 |
} else { |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
32 |
free(t); |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
33 |
return NULL; |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
34 |
} |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
35 |
} |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
36 |
|
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
37 |
void OTTDJoinThread(Thread* t) |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
38 |
{ |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
39 |
if (t == NULL) return; |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
40 |
|
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
41 |
DosWaitThread(&t->thread, DCWW_WAIT); |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
42 |
free(t); |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
43 |
} |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
44 |
|
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
45 |
|
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
46 |
#elif defined(UNIX) |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
47 |
|
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
48 |
#include <pthread.h> |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
49 |
|
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
50 |
struct Thread { |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
51 |
pthread_t thread; |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
52 |
}; |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
53 |
|
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
54 |
Thread* OTTDCreateThread(ThreadFunc function, void* arg) |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
55 |
{ |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
56 |
Thread* t = malloc(sizeof(*t)); |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
57 |
|
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
58 |
if (t == NULL) return NULL; |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
59 |
|
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
60 |
if (pthread_create(&t->thread, NULL, (void* (*)(void*))function, arg) == 0) { |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
61 |
return t; |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
62 |
} else { |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
63 |
free(t); |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
64 |
return NULL; |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
65 |
} |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
66 |
} |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
67 |
|
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
68 |
void OTTDJoinThread(Thread* t) |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
69 |
{ |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
70 |
if (t == NULL) return; |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
71 |
|
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
72 |
pthread_join(t->thread, NULL); |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
73 |
free(t); |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
74 |
} |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
75 |
|
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
76 |
|
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
77 |
#elif defined(WIN32) |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
78 |
|
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
79 |
#include <windows.h> |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
80 |
|
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
81 |
struct Thread { |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
82 |
HANDLE thread; |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
83 |
}; |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
84 |
|
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
85 |
Thread* OTTDCreateThread(ThreadFunc function, void* arg) |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
86 |
{ |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
87 |
Thread* t = malloc(sizeof(*t)); |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
88 |
DWORD dwThreadId; |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
89 |
|
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
90 |
if (t == NULL) return NULL; |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
91 |
|
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
92 |
t->thread = CreateThread( |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
93 |
NULL, 0, (LPTHREAD_START_ROUTINE)function, arg, 0, &dwThreadId |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
94 |
); |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
95 |
|
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
96 |
if (t->thread != NULL) { |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
97 |
return t; |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
98 |
} else { |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
99 |
free(t); |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
100 |
return NULL; |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
101 |
} |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
102 |
} |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
103 |
|
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
104 |
void OTTDJoinThread(Thread* t) |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
105 |
{ |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
106 |
if (t == NULL) return; |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
107 |
|
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
108 |
WaitForSingleObject(t->thread, INFINITE); |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
109 |
CloseHandle(t->thread); |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
110 |
free(t); |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
111 |
} |
3193cbd1ba88
(svn r2809) Implement more generic threading functions, which allow more than one thread
tron
parents:
diff
changeset
|
112 |
#endif |