1 |
/* |
2 |
* PearPC |
3 |
* systhread.cc |
4 |
* |
5 |
* Copyright (C) 2003 Sebastian Biallas (sb@biallas.net) |
6 |
* |
7 |
* This program is free software; you can redistribute it and/or modify |
8 |
* it under the terms of the GNU General Public License version 2 as |
9 |
* published by the Free Software Foundation. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
19 |
*/ |
20 |
|
21 |
#include <cstdlib> |
22 |
#include "system/systhread.h" |
23 |
|
24 |
#define WIN32_LEAN_AND_MEAN |
25 |
#include <windows.h> |
26 |
#include <windowsx.h> |
27 |
#include <commctrl.h> |
28 |
#include <process.h> |
29 |
|
30 |
struct sys_win32_semaphore { |
31 |
CRITICAL_SECTION cs; |
32 |
HANDLE sem; |
33 |
}; |
34 |
|
35 |
int sys_create_mutex(sys_mutex *m) |
36 |
{ |
37 |
*m = malloc(sizeof (CRITICAL_SECTION)); |
38 |
InitializeCriticalSection((CRITICAL_SECTION *)*m); |
39 |
return 0; |
40 |
} |
41 |
|
42 |
int sys_create_semaphore(sys_semaphore *s) |
43 |
{ |
44 |
*s = malloc(sizeof (sys_win32_semaphore)); |
45 |
InitializeCriticalSection(&((sys_win32_semaphore *)*s)->cs); |
46 |
((sys_win32_semaphore *)*s)->sem = CreateSemaphore(NULL, 0, 1000000, NULL); |
47 |
return 0; |
48 |
} |
49 |
|
50 |
int sys_create_thread(sys_thread *t, int flags, sys_thread_function start_routine, void *arg) |
51 |
{ |
52 |
unsigned long *p = (unsigned long *)malloc(sizeof (unsigned long)); |
53 |
*t = p; |
54 |
*p = _beginthread((void (*)(void *))start_routine, 0, arg); |
55 |
return 0; |
56 |
} |
57 |
|
58 |
void sys_destroy_mutex(sys_mutex m) |
59 |
{ |
60 |
DeleteCriticalSection((CRITICAL_SECTION *)m); |
61 |
free(m); |
62 |
} |
63 |
|
64 |
void sys_destroy_semaphore(sys_semaphore s) |
65 |
{ |
66 |
DeleteCriticalSection(&((sys_win32_semaphore *)s)->cs); |
67 |
CloseHandle(((sys_win32_semaphore *)s)->sem); |
68 |
free(s); |
69 |
} |
70 |
|
71 |
void sys_destroy_thread(sys_thread t) |
72 |
{ |
73 |
// NOOP |
74 |
} |
75 |
|
76 |
int sys_lock_mutex(sys_mutex m) |
77 |
{ |
78 |
EnterCriticalSection((CRITICAL_SECTION *)m); |
79 |
return 0; |
80 |
} |
81 |
|
82 |
int sys_trylock_mutex(sys_mutex m) |
83 |
{ |
84 |
// this doesnt seems to be possible on windows |
85 |
EnterCriticalSection((CRITICAL_SECTION *)m); |
86 |
return 0; |
87 |
} |
88 |
|
89 |
void sys_unlock_mutex(sys_mutex m) |
90 |
{ |
91 |
LeaveCriticalSection((CRITICAL_SECTION *)m); |
92 |
} |
93 |
|
94 |
void sys_signal_semaphore(sys_semaphore s) |
95 |
{ |
96 |
ReleaseSemaphore(((sys_win32_semaphore *)s)->sem, 1, NULL); |
97 |
} |
98 |
|
99 |
void sys_signal_all_semaphore(sys_semaphore s) |
100 |
{ |
101 |
ReleaseSemaphore(((sys_win32_semaphore *)s)->sem, 1, NULL); |
102 |
} |
103 |
|
104 |
void sys_wait_semaphore(sys_semaphore s) |
105 |
{ |
106 |
LeaveCriticalSection(&((sys_win32_semaphore *)s)->cs); |
107 |
WaitForSingleObject(((sys_win32_semaphore *)s)->sem, INFINITE); |
108 |
EnterCriticalSection(&((sys_win32_semaphore *)s)->cs); |
109 |
} |
110 |
|
111 |
void sys_wait_semaphore_bounded(sys_semaphore s, int ms) |
112 |
{ |
113 |
LeaveCriticalSection(&((sys_win32_semaphore *)s)->cs); |
114 |
WaitForSingleObject(((sys_win32_semaphore *)s)->sem, ms); |
115 |
EnterCriticalSection(&((sys_win32_semaphore *)s)->cs); |
116 |
} |
117 |
|
118 |
void sys_lock_semaphore(sys_semaphore s) |
119 |
{ |
120 |
EnterCriticalSection(&((sys_win32_semaphore *)s)->cs); |
121 |
} |
122 |
|
123 |
void sys_unlock_semaphore(sys_semaphore s) |
124 |
{ |
125 |
LeaveCriticalSection(&((sys_win32_semaphore *)s)->cs); |
126 |
} |
127 |
|
128 |
void sys_exit_thread(void *ret) |
129 |
{ |
130 |
} |
131 |
|
132 |
void *sys_join_thread(sys_thread t) |
133 |
{ |
134 |
} |