2014-02-26 22:43:31 -08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2014 Hugh Bailey <obs.jim@gmail.com>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "bmem.h"
|
|
|
|
#include "threading.h"
|
2020-04-29 12:39:44 -07:00
|
|
|
#include "util/platform.h"
|
2014-02-26 22:43:31 -08:00
|
|
|
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#include <windows.h>
|
|
|
|
|
2015-02-08 09:23:02 -08:00
|
|
|
#ifdef __MINGW32__
|
|
|
|
#include <excpt.h>
|
|
|
|
#ifndef TRYLEVEL_NONE
|
|
|
|
#ifndef __MINGW64__
|
|
|
|
#define NO_SEH_MINGW
|
|
|
|
#endif
|
|
|
|
#ifndef __try
|
|
|
|
#define __try
|
|
|
|
#endif
|
|
|
|
#ifndef __except
|
2019-06-22 22:13:45 -07:00
|
|
|
#define __except (x) if (0)
|
2015-02-08 09:23:02 -08:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
int os_event_init(os_event_t **event, enum os_event_type type)
|
2014-02-26 22:43:31 -08:00
|
|
|
{
|
|
|
|
HANDLE handle;
|
|
|
|
|
2014-03-10 19:04:00 -07:00
|
|
|
handle = CreateEvent(NULL, (type == OS_EVENT_TYPE_MANUAL), FALSE, NULL);
|
2014-02-26 22:43:31 -08:00
|
|
|
if (!handle)
|
|
|
|
return -1;
|
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
*event = (os_event_t *)handle;
|
2014-02-26 22:43:31 -08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
void os_event_destroy(os_event_t *event)
|
2014-02-26 22:43:31 -08:00
|
|
|
{
|
2015-11-11 03:45:35 -08:00
|
|
|
if (event)
|
|
|
|
CloseHandle((HANDLE)event);
|
2014-02-26 22:43:31 -08:00
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
int os_event_wait(os_event_t *event)
|
2014-02-26 22:43:31 -08:00
|
|
|
{
|
|
|
|
DWORD code;
|
|
|
|
|
|
|
|
if (!event)
|
|
|
|
return EINVAL;
|
|
|
|
|
2015-11-11 03:45:35 -08:00
|
|
|
code = WaitForSingleObject((HANDLE)event, INFINITE);
|
2014-02-26 22:43:31 -08:00
|
|
|
if (code != WAIT_OBJECT_0)
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
int os_event_timedwait(os_event_t *event, unsigned long milliseconds)
|
2014-02-26 22:43:31 -08:00
|
|
|
{
|
|
|
|
DWORD code;
|
|
|
|
|
|
|
|
if (!event)
|
|
|
|
return EINVAL;
|
|
|
|
|
2015-11-11 03:45:35 -08:00
|
|
|
code = WaitForSingleObject((HANDLE)event, milliseconds);
|
2014-02-26 22:43:31 -08:00
|
|
|
if (code == WAIT_TIMEOUT)
|
|
|
|
return ETIMEDOUT;
|
|
|
|
else if (code != WAIT_OBJECT_0)
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
int os_event_try(os_event_t *event)
|
2014-02-26 22:43:31 -08:00
|
|
|
{
|
|
|
|
DWORD code;
|
|
|
|
|
|
|
|
if (!event)
|
|
|
|
return EINVAL;
|
|
|
|
|
2015-11-11 03:45:35 -08:00
|
|
|
code = WaitForSingleObject((HANDLE)event, 0);
|
2014-02-26 22:43:31 -08:00
|
|
|
if (code == WAIT_TIMEOUT)
|
|
|
|
return EAGAIN;
|
|
|
|
else if (code != WAIT_OBJECT_0)
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
int os_event_signal(os_event_t *event)
|
2014-02-26 22:43:31 -08:00
|
|
|
{
|
|
|
|
if (!event)
|
|
|
|
return EINVAL;
|
|
|
|
|
2015-11-11 03:45:35 -08:00
|
|
|
if (!SetEvent((HANDLE)event))
|
2014-02-26 22:43:31 -08:00
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
void os_event_reset(os_event_t *event)
|
2014-02-26 22:43:31 -08:00
|
|
|
{
|
|
|
|
if (!event)
|
|
|
|
return;
|
|
|
|
|
2015-11-11 03:45:35 -08:00
|
|
|
ResetEvent((HANDLE)event);
|
2014-02-26 22:43:31 -08:00
|
|
|
}
|
2014-03-10 19:04:00 -07:00
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
int os_sem_init(os_sem_t **sem, int value)
|
2014-03-10 19:04:00 -07:00
|
|
|
{
|
|
|
|
HANDLE handle = CreateSemaphore(NULL, (LONG)value, 0x7FFFFFFF, NULL);
|
|
|
|
if (!handle)
|
|
|
|
return -1;
|
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
*sem = (os_sem_t *)handle;
|
2014-03-10 19:04:00 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
void os_sem_destroy(os_sem_t *sem)
|
2014-03-10 19:04:00 -07:00
|
|
|
{
|
2015-11-11 03:45:35 -08:00
|
|
|
if (sem)
|
|
|
|
CloseHandle((HANDLE)sem);
|
2014-03-10 19:04:00 -07:00
|
|
|
}
|
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
int os_sem_post(os_sem_t *sem)
|
2014-03-10 19:04:00 -07:00
|
|
|
{
|
2019-06-22 22:13:45 -07:00
|
|
|
if (!sem)
|
|
|
|
return -1;
|
2015-11-11 03:45:35 -08:00
|
|
|
return ReleaseSemaphore((HANDLE)sem, 1, NULL) ? 0 : -1;
|
2014-03-10 19:04:00 -07:00
|
|
|
}
|
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
int os_sem_wait(os_sem_t *sem)
|
2014-03-10 19:04:00 -07:00
|
|
|
{
|
|
|
|
DWORD ret;
|
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
if (!sem)
|
|
|
|
return -1;
|
2015-11-11 03:45:35 -08:00
|
|
|
ret = WaitForSingleObject((HANDLE)sem, INFINITE);
|
2014-03-10 19:04:00 -07:00
|
|
|
return (ret == WAIT_OBJECT_0) ? 0 : -1;
|
|
|
|
}
|
2014-03-16 18:26:46 -07:00
|
|
|
|
2015-01-02 05:35:04 -08:00
|
|
|
#define VC_EXCEPTION 0x406D1388
|
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
#pragma pack(push, 8)
|
2015-01-02 05:35:04 -08:00
|
|
|
struct vs_threadname_info {
|
|
|
|
DWORD type; /* 0x1000 */
|
|
|
|
const char *name;
|
|
|
|
DWORD thread_id;
|
|
|
|
DWORD flags;
|
|
|
|
};
|
|
|
|
#pragma pack(pop)
|
|
|
|
|
|
|
|
#define THREADNAME_INFO_SIZE \
|
|
|
|
(sizeof(struct vs_threadname_info) / sizeof(ULONG_PTR))
|
|
|
|
|
|
|
|
void os_set_thread_name(const char *name)
|
|
|
|
{
|
2015-02-06 16:40:26 -08:00
|
|
|
#ifdef __MINGW32__
|
|
|
|
UNUSED_PARAMETER(name);
|
|
|
|
#else
|
2015-01-02 05:35:04 -08:00
|
|
|
struct vs_threadname_info info;
|
|
|
|
info.type = 0x1000;
|
|
|
|
info.name = name;
|
|
|
|
info.thread_id = GetCurrentThreadId();
|
|
|
|
info.flags = 0;
|
|
|
|
|
2015-02-08 09:23:02 -08:00
|
|
|
#ifdef NO_SEH_MINGW
|
2019-06-22 22:13:45 -07:00
|
|
|
__try1(EXCEPTION_EXECUTE_HANDLER)
|
|
|
|
{
|
2015-02-08 09:23:02 -08:00
|
|
|
#else
|
2015-01-02 05:35:04 -08:00
|
|
|
__try {
|
2015-02-08 09:23:02 -08:00
|
|
|
#endif
|
2015-01-02 05:35:04 -08:00
|
|
|
RaiseException(VC_EXCEPTION, 0, THREADNAME_INFO_SIZE,
|
2019-06-22 22:13:45 -07:00
|
|
|
(ULONG_PTR *)&info);
|
2015-02-08 09:23:02 -08:00
|
|
|
#ifdef NO_SEH_MINGW
|
2019-06-22 22:13:45 -07:00
|
|
|
}
|
|
|
|
__except1{
|
2015-02-08 09:23:02 -08:00
|
|
|
#else
|
2019-06-22 22:13:45 -07:00
|
|
|
} __except (EXCEPTION_EXECUTE_HANDLER) {
|
2015-02-08 09:23:02 -08:00
|
|
|
#endif
|
2015-01-02 05:35:04 -08:00
|
|
|
}
|
2015-02-06 16:40:26 -08:00
|
|
|
#endif
|
2020-04-29 12:39:44 -07:00
|
|
|
|
|
|
|
typedef HRESULT(WINAPI * set_thread_description_t)(HANDLE thread,
|
|
|
|
PCWSTR desc);
|
|
|
|
|
|
|
|
HMODULE k32 = LoadLibraryW(L"Kernel32.dll");
|
|
|
|
set_thread_description_t std = NULL;
|
|
|
|
std = (set_thread_description_t)GetProcAddress(k32,
|
|
|
|
"SetThreadDescription");
|
|
|
|
if (std) {
|
|
|
|
wchar_t *wname;
|
|
|
|
os_utf8_to_wcs_ptr(name, 0, &wname);
|
|
|
|
|
|
|
|
std(GetCurrentThread(), wname);
|
|
|
|
|
|
|
|
bfree(wname);
|
|
|
|
}
|
|
|
|
FreeLibrary(k32);
|
2015-01-02 05:35:04 -08:00
|
|
|
}
|