-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathposixemu.cpp
297 lines (257 loc) · 5.84 KB
/
posixemu.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
* UAE - The Un*x Amiga Emulator
*
* Win32 interface
*
* Copyright 1997 Mathias Ortmann
*/
#include "sysconfig.h"
#include "sysdeps.h"
#include <windows.h>
#include <stdlib.h>
#include <stdarg.h>
#include <commctrl.h>
#include <commdlg.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <io.h>
#include <sys/types.h>
#include <sys/timeb.h>
#include <sys/utime.h>
#include <process.h>
#include "options.h"
#include "posixemu.h"
#include "threaddep/thread.h"
#include "filesys.h"
#include "win32.h"
#include <Avrt.h>
extern HANDLE AVTask;
uae_u32 getlocaltime (void)
{
SYSTEMTIME st;
FILETIME ft;
ULARGE_INTEGER t;
GetLocalTime (&st);
SystemTimeToFileTime (&st, &ft);
t.LowPart = ft.dwLowDateTime;
t.HighPart = ft.dwHighDateTime;
t.QuadPart -= 11644473600000 * 10000;
return (uae_u32)(t.QuadPart / 10000000);
}
/* Our Win32 implementation of this function */
void gettimeofday (struct timeval *tv, void *blah)
{
#if 1
struct timeb time;
ftime (&time);
tv->tv_sec = (long)time.time;
tv->tv_usec = time.millitm * 1000;
#else
SYSTEMTIME st;
FILETIME ft;
uae_u64 v, sec;
GetSystemTime (&st);
SystemTimeToFileTime (&st, &ft);
v = (ft.dwHighDateTime << 32) | ft.dwLowDateTime;
v /= 10;
sec = v / 1000000;
tv->tv_usec = (unsigned long)(v - (sec * 1000000));
tv->tv_sec = (unsigned long)(sec - 11644463600);
#endif
}
#if 0
static DWORD getattr (const TCHAR *name, LPFILETIME lpft, uae_s64 *size)
{
HANDLE hFind;
WIN32_FIND_DATA fd;
if ((hFind = FindFirstFile (name, &fd)) == INVALID_HANDLE_VALUE) {
fd.dwFileAttributes = GetFileAttributes (name);
return fd.dwFileAttributes;
}
FindClose (hFind);
if (lpft)
*lpft = fd.ftLastWriteTime;
if (size)
*size = (((uae_u64)fd.nFileSizeHigh) << 32) | fd.nFileSizeLow;
return fd.dwFileAttributes;
}
#endif
void uae_sem_init (uae_sem_t * event, int manual_reset, int initial_state)
{
if(*event) {
if (initial_state)
SetEvent (*event);
else
ResetEvent (*event);
} else {
*event = CreateEvent (NULL, manual_reset, initial_state, NULL);
}
}
void uae_sem_wait (uae_sem_t * event)
{
WaitForSingleObject (*event, INFINITE);
}
void uae_sem_unpost (uae_sem_t * event)
{
ResetEvent(*event);
}
void uae_sem_post (uae_sem_t * event)
{
SetEvent (*event);
}
int uae_sem_trywait_delay(uae_sem_t * event, int millis)
{
int v = WaitForSingleObject(*event, millis);
if (v == WAIT_OBJECT_0)
return 0;
if (v == WAIT_ABANDONED)
return -2;
return -1;
}
int uae_sem_trywait (uae_sem_t * event)
{
return uae_sem_trywait_delay(event, 0);
}
void uae_sem_destroy (uae_sem_t * event)
{
if (*event) {
CloseHandle (*event);
*event = NULL;
}
}
uae_thread_id uae_thread_get_id(void)
{
return (uae_thread_id)(INT_PTR)GetCurrentThreadId();
}
#ifndef _CONSOLE
typedef unsigned (__stdcall *BEGINTHREADEX_FUNCPTR)(void *);
struct thparms
{
void (*f)(void *);
void *arg;
};
static unsigned __stdcall thread_init (void *f)
{
struct thparms *thp = (struct thparms*)f;
void (*fp)(void *) = thp->f;
void *arg = thp->arg;
xfree (f);
#ifndef _CONSOLE
__try {
fp(arg);
#endif
#ifndef _CONSOLE
} __except (WIN32_ExceptionFilter (GetExceptionInformation (), GetExceptionCode ())) {
}
#endif
return 0;
}
void uae_end_thread (uae_thread_id *tid)
{
if (tid) {
CloseHandle (*tid);
*tid = NULL;
}
}
typedef BOOL(WINAPI* AVSETMMTHREADPRIORITY)(HANDLE, AVRT_PRIORITY);
static AVSETMMTHREADPRIORITY pAvSetMmThreadPriority;
int uae_start_thread (const TCHAR *name, void (*f)(void *), void *arg, uae_thread_id *tid)
{
HANDLE hThread;
int result = 1;
unsigned foo;
struct thparms *thp;
if (!pAvSetMmThreadPriority && AVTask) {
pAvSetMmThreadPriority = (AVSETMMTHREADPRIORITY)GetProcAddress(GetModuleHandle(_T("Avrt.dll")), "AvSetMmThreadPriority");
}
thp = xmalloc (struct thparms, 1);
thp->f = f;
thp->arg = arg;
hThread = (HANDLE)_beginthreadex (NULL, 0, thread_init, thp, 0, &foo);
if (hThread) {
if (name) {
//write_log (_T("Thread '%s' started (%d)\n"), name, hThread);
if (!AVTask) {
SetThreadPriority (hThread, THREAD_PRIORITY_HIGHEST);
} else if (pAvSetMmThreadPriority) {
pAvSetMmThreadPriority(AVTask, AVRT_PRIORITY_HIGH);
}
}
} else {
result = 0;
write_log (_T("Thread '%s' failed to start!?\n"), name ? name : _T("<unknown>"));
}
if (tid)
*tid = hThread;
else
CloseHandle (hThread);
return result;
}
int uae_start_thread_fast (void (*f)(void *), void *arg, uae_thread_id *tid)
{
int v = uae_start_thread (NULL, f, arg, tid);
if (*tid) {
if (!AVTask) {
SetThreadPriority (*tid, THREAD_PRIORITY_HIGHEST);
} else if (pAvSetMmThreadPriority) {
pAvSetMmThreadPriority(AVTask, AVRT_PRIORITY_HIGH);
}
}
return v;
}
DWORD_PTR cpu_affinity = 1, cpu_paffinity = 1;
void uae_set_thread_priority (uae_thread_id *tid, int pri)
{
#if 0
int pri2;
HANDLE th;
if (tid)
th = *tid;
else
th = GetCurrentThread ();
pri2 = GetThreadPriority (th);
if (pri2 == THREAD_PRIORITY_ERROR_RETURN)
pri2 = 0;
if (pri > 0)
pri2 = THREAD_PRIORITY_HIGHEST;
else
pri2 = THREAD_PRIORITY_ABOVE_NORMAL;
pri2 += pri;
if (pri2 > 1)
pri2 = 1;
if (pri2 < -1)
pri2 = -1;
SetThreadPriority (th, pri2);
#endif
if (!AVTask) {
if (!SetThreadPriority (GetCurrentThread(), THREAD_PRIORITY_HIGHEST))
SetThreadPriority (GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL);
} else if (pAvSetMmThreadPriority) {
pAvSetMmThreadPriority(AVTask, AVRT_PRIORITY_HIGH);
}
}
uae_atomic atomic_and(volatile uae_atomic *p, uae_u32 v)
{
return _InterlockedAnd(p, v);
}
uae_atomic atomic_or(volatile uae_atomic *p, uae_u32 v)
{
return _InterlockedOr(p, v);
}
void atomic_set(volatile uae_atomic *p, uae_u32 v)
{
}
uae_atomic atomic_inc(volatile uae_atomic *p)
{
return _InterlockedIncrement(p);
}
uae_atomic atomic_dec(volatile uae_atomic *p)
{
return _InterlockedDecrement(p);
}
uae_u32 atomic_bit_test_and_reset(volatile uae_atomic *p, uae_u32 v)
{
return _interlockedbittestandreset(p, v);
}
#endif