Remove althrd_t from Windows

master
Chris Robinson 2018-11-26 20:34:16 -08:00
parent ecab90802a
commit bf9db1fe3d
5 changed files with 5 additions and 119 deletions

View File

@ -118,7 +118,6 @@ DEFINE_PROPERTYKEY(PKEY_AudioEndpoint_GUID, 0x1da5d803, 0xd492, 0x4edd, 0x8c, 0x
#include "alu.h"
#include "cpu_caps.h"
#include "fpu_modes.h"
#include "uintmap.h"
#include "vector.h"
#include "compat.h"
#include "threads.h"

View File

@ -709,7 +709,6 @@ SET(COMMON_OBJS
common/math_defs.h
common/threads.cpp
common/threads.h
common/uintmap.h
common/vecmat.cpp
common/vecmat.h
)

View File

@ -26,8 +26,6 @@
#include <string.h>
#include <errno.h>
#include "uintmap.h"
#ifndef UNUSED
#if defined(__cplusplus)
@ -51,15 +49,6 @@
#include <mmsystem.h>
/* An associative map of uint:void* pairs. The key is the unique Thread ID and
* the value is the thread HANDLE. The thread ID is passed around as the
* althrd_t since there is only one ID per thread, whereas a thread may be
* referenced by multiple different HANDLEs. This map allows retrieving the
* original handle which is needed to join the thread and get its return value.
*/
static ThrSafeMap<DWORD,HANDLE> ThrdIdHandle{};
void althrd_setname(const char *name)
{
#if defined(_MSC_VER)
@ -89,71 +78,6 @@ void althrd_setname(const char *name)
}
typedef struct thread_cntr {
althrd_start_t func;
void *arg;
} thread_cntr;
static DWORD WINAPI althrd_starter(void *arg)
{
thread_cntr cntr;
memcpy(&cntr, arg, sizeof(cntr));
free(arg);
return (DWORD)((*cntr.func)(cntr.arg));
}
int althrd_create(althrd_t *thr, althrd_start_t func, void *arg)
{
thread_cntr *cntr;
DWORD thrid;
HANDLE hdl;
cntr = static_cast<thread_cntr*>(malloc(sizeof(*cntr)));
if(!cntr) return althrd_nomem;
cntr->func = func;
cntr->arg = arg;
hdl = CreateThread(NULL, THREAD_STACK_SIZE, althrd_starter, cntr, 0, &thrid);
if(!hdl)
{
free(cntr);
return althrd_error;
}
ThrdIdHandle.InsertEntry(thrid, hdl);
*thr = thrid;
return althrd_success;
}
int althrd_detach(althrd_t thr)
{
HANDLE hdl = ThrdIdHandle.RemoveKey(thr);
if(!hdl) return althrd_error;
CloseHandle(hdl);
return althrd_success;
}
int althrd_join(althrd_t thr, int *res)
{
DWORD code;
HANDLE hdl = ThrdIdHandle.RemoveKey(thr);
if(!hdl) return althrd_error;
WaitForSingleObject(hdl, INFINITE);
GetExitCodeThread(hdl, &code);
CloseHandle(hdl);
if(res != NULL)
*res = (int)code;
return althrd_success;
}
int almtx_init(almtx_t *mtx, int type)
{
if(!mtx) return althrd_error;

View File

@ -30,14 +30,11 @@ enum {
almtx_recursive = 1,
};
typedef int (*althrd_start_t)(void*);
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
typedef DWORD althrd_t;
typedef CRITICAL_SECTION almtx_t;
typedef HANDLE alsem_t;
@ -81,6 +78,8 @@ typedef dispatch_semaphore_t alsem_t;
typedef sem_t alsem_t;
#endif /* __APPLE__ */
typedef int (*althrd_start_t)(void*);
inline void althrd_yield(void)
{
@ -102,12 +101,12 @@ inline int almtx_unlock(almtx_t *mtx)
return althrd_success;
}
#endif
int althrd_create(althrd_t *thr, althrd_start_t func, void *arg);
int althrd_detach(althrd_t thr);
int althrd_join(althrd_t thr, int *res);
#endif
void althrd_setname(const char *name);
int almtx_init(almtx_t *mtx, int type);

View File

@ -1,35 +0,0 @@
#ifndef AL_UINTMAP_H
#define AL_UINTMAP_H
#include <unordered_map>
#include <mutex>
#include "AL/al.h"
template<typename T0, typename T1>
class ThrSafeMap {
std::unordered_map<T0, T1> mValues;
std::mutex mLock;
public:
void InsertEntry(T0 key, T1 value) noexcept
{
std::lock_guard<std::mutex> _{mLock};
mValues[key] = value;
}
T1 RemoveKey(T0 key) noexcept
{
T1 retval{};
std::lock_guard<std::mutex> _{mLock};
auto iter = mValues.find(key);
if(iter != mValues.end())
retval = iter->second;
mValues.erase(iter);
return retval;
}
};
#endif /* AL_UINTMAP_H */