2017-10-07 00:46:54 -04:00
|
|
|
/*
|
|
|
|
|
* 86Box A hypervisor and IBM PC system emulator that specializes in
|
|
|
|
|
* running old operating systems and software designed for IBM
|
|
|
|
|
* PC systems and compatibles from 1981 through fairly recent
|
|
|
|
|
* system designs based on the PCI bus.
|
|
|
|
|
*
|
|
|
|
|
* This file is part of the 86Box distribution.
|
|
|
|
|
*
|
|
|
|
|
* Implement threads and mutexes for the Win32 platform.
|
|
|
|
|
*
|
2018-04-25 23:51:13 +02:00
|
|
|
* Version: @(#)win_thread.c 1.0.6 2018/03/28
|
2017-10-07 00:46:54 -04:00
|
|
|
*
|
|
|
|
|
* Authors: Sarah Walker, <http://pcem-emulator.co.uk/>
|
|
|
|
|
* Fred N. van Kempen, <decwiz@yahoo.com>
|
2017-10-11 05:40:44 -04:00
|
|
|
*
|
2018-04-25 23:51:13 +02:00
|
|
|
* Copyright 2008-2018 Sarah Walker.
|
|
|
|
|
* Copyright 2017,2018 Fred N. van Kempen.
|
2017-10-07 00:46:54 -04:00
|
|
|
*/
|
|
|
|
|
#define UNICODE
|
|
|
|
|
#define BITMAP WINDOWS_BITMAP
|
|
|
|
|
#include <windows.h>
|
|
|
|
|
#include <windowsx.h>
|
|
|
|
|
#include <process.h>
|
|
|
|
|
#undef BITMAP
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <wchar.h>
|
2017-10-17 01:59:09 -04:00
|
|
|
#include "../86box.h"
|
2017-10-11 05:40:44 -04:00
|
|
|
#include "../plat.h"
|
2017-10-07 00:46:54 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
HANDLE handle;
|
|
|
|
|
} win_event_t;
|
|
|
|
|
|
|
|
|
|
|
2017-10-21 20:29:11 -04:00
|
|
|
thread_t *
|
|
|
|
|
thread_create(void (*func)(void *param), void *param)
|
2017-10-07 00:46:54 -04:00
|
|
|
{
|
2018-04-25 23:51:13 +02:00
|
|
|
uintptr_t bt = _beginthread(func, 0, param);
|
|
|
|
|
return((thread_t *)bt);
|
2017-10-07 00:46:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2017-10-21 20:29:11 -04:00
|
|
|
thread_kill(void *arg)
|
2017-10-07 00:46:54 -04:00
|
|
|
{
|
2017-10-21 20:29:11 -04:00
|
|
|
if (arg == NULL) return;
|
2017-10-07 00:46:54 -04:00
|
|
|
|
2017-10-21 20:29:11 -04:00
|
|
|
TerminateThread(arg, 0);
|
2017-10-07 00:46:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-10-19 23:55:51 +02:00
|
|
|
int
|
|
|
|
|
thread_wait(thread_t *arg, int timeout)
|
|
|
|
|
{
|
|
|
|
|
if (arg == NULL) return(0);
|
|
|
|
|
|
|
|
|
|
if (timeout == -1)
|
|
|
|
|
timeout = INFINITE;
|
|
|
|
|
|
|
|
|
|
if (WaitForSingleObject(arg, timeout)) return(1);
|
|
|
|
|
|
|
|
|
|
return(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-10-07 00:46:54 -04:00
|
|
|
event_t *
|
|
|
|
|
thread_create_event(void)
|
|
|
|
|
{
|
2017-10-14 20:04:21 -04:00
|
|
|
win_event_t *ev = malloc(sizeof(win_event_t));
|
2017-10-07 00:46:54 -04:00
|
|
|
|
2017-10-14 20:04:21 -04:00
|
|
|
ev->handle = CreateEvent(NULL, FALSE, FALSE, NULL);
|
2017-10-07 00:46:54 -04:00
|
|
|
|
2017-10-14 20:04:21 -04:00
|
|
|
return((event_t *)ev);
|
2017-10-07 00:46:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2017-10-14 20:04:21 -04:00
|
|
|
thread_set_event(event_t *arg)
|
2017-10-07 00:46:54 -04:00
|
|
|
{
|
2017-10-14 20:04:21 -04:00
|
|
|
win_event_t *ev = (win_event_t *)arg;
|
2017-10-14 07:03:19 +02:00
|
|
|
|
2017-10-14 20:04:21 -04:00
|
|
|
if (arg == NULL) return;
|
2017-10-07 00:46:54 -04:00
|
|
|
|
2017-10-14 20:04:21 -04:00
|
|
|
SetEvent(ev->handle);
|
2017-10-07 00:46:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2017-10-14 20:04:21 -04:00
|
|
|
thread_reset_event(event_t *arg)
|
2017-10-07 00:46:54 -04:00
|
|
|
{
|
2017-10-14 20:04:21 -04:00
|
|
|
win_event_t *ev = (win_event_t *)arg;
|
2017-10-14 07:03:19 +02:00
|
|
|
|
2017-10-14 20:04:21 -04:00
|
|
|
if (arg == NULL) return;
|
2017-10-07 00:46:54 -04:00
|
|
|
|
2017-10-14 20:04:21 -04:00
|
|
|
ResetEvent(ev->handle);
|
2017-10-07 00:46:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2017-10-14 20:04:21 -04:00
|
|
|
thread_wait_event(event_t *arg, int timeout)
|
2017-10-07 00:46:54 -04:00
|
|
|
{
|
2017-10-14 20:04:21 -04:00
|
|
|
win_event_t *ev = (win_event_t *)arg;
|
2017-10-14 07:03:19 +02:00
|
|
|
|
2017-10-14 20:04:21 -04:00
|
|
|
if (arg == NULL) return(0);
|
2017-10-07 00:46:54 -04:00
|
|
|
|
2017-10-19 23:55:51 +02:00
|
|
|
if (ev->handle == NULL) return(0);
|
|
|
|
|
|
2017-10-07 00:46:54 -04:00
|
|
|
if (timeout == -1)
|
|
|
|
|
timeout = INFINITE;
|
|
|
|
|
|
2017-10-14 20:04:21 -04:00
|
|
|
if (WaitForSingleObject(ev->handle, timeout)) return(1);
|
2017-10-07 00:46:54 -04:00
|
|
|
|
|
|
|
|
return(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2017-10-14 20:04:21 -04:00
|
|
|
thread_destroy_event(event_t *arg)
|
2017-10-07 00:46:54 -04:00
|
|
|
{
|
2017-10-14 20:04:21 -04:00
|
|
|
win_event_t *ev = (win_event_t *)arg;
|
2017-10-07 00:46:54 -04:00
|
|
|
|
2017-10-14 20:04:21 -04:00
|
|
|
if (arg == NULL) return;
|
2017-10-14 07:03:19 +02:00
|
|
|
|
2017-10-14 20:04:21 -04:00
|
|
|
CloseHandle(ev->handle);
|
2017-10-07 00:46:54 -04:00
|
|
|
|
2017-10-14 20:04:21 -04:00
|
|
|
free(ev);
|
2017-10-07 00:46:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-10-14 20:04:21 -04:00
|
|
|
mutex_t *
|
2017-10-07 00:46:54 -04:00
|
|
|
thread_create_mutex(wchar_t *name)
|
|
|
|
|
{
|
2017-10-14 20:04:21 -04:00
|
|
|
return((mutex_t*)CreateMutex(NULL, FALSE, name));
|
2017-10-07 00:46:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2017-10-14 20:04:21 -04:00
|
|
|
thread_close_mutex(mutex_t *mutex)
|
2017-10-07 00:46:54 -04:00
|
|
|
{
|
2017-10-14 20:04:21 -04:00
|
|
|
if (mutex == NULL) return;
|
2017-10-14 07:03:19 +02:00
|
|
|
|
2017-10-07 00:46:54 -04:00
|
|
|
CloseHandle((HANDLE)mutex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-10-14 20:04:21 -04:00
|
|
|
int
|
|
|
|
|
thread_wait_mutex(mutex_t *mutex)
|
2017-10-07 00:46:54 -04:00
|
|
|
{
|
2017-10-14 20:04:21 -04:00
|
|
|
if (mutex == NULL) return(0);
|
2017-10-14 07:03:19 +02:00
|
|
|
|
2017-10-07 00:46:54 -04:00
|
|
|
DWORD dwres = WaitForSingleObject((HANDLE)mutex, INFINITE);
|
|
|
|
|
|
2017-10-14 20:04:21 -04:00
|
|
|
if (dwres == WAIT_OBJECT_0) return(1);
|
2017-10-07 00:46:54 -04:00
|
|
|
|
2017-10-14 20:04:21 -04:00
|
|
|
return(0);
|
2017-10-07 00:46:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-10-14 20:04:21 -04:00
|
|
|
int
|
|
|
|
|
thread_release_mutex(mutex_t *mutex)
|
2017-10-07 00:46:54 -04:00
|
|
|
{
|
2017-10-14 20:04:21 -04:00
|
|
|
if (mutex == NULL) return(0);
|
2017-10-14 07:03:19 +02:00
|
|
|
|
2017-10-07 00:46:54 -04:00
|
|
|
return(!!ReleaseMutex((HANDLE)mutex));
|
|
|
|
|
}
|