zstd/lib/common/threading.cpp

101 lines
2.8 KiB
C++

// TODO: copyright header
#include "threading.h"
#include "KInternal.hpp"
extern "C"
{
int ZSTD_pthread_mutex_init(ZSTD_pthread_mutex_t* in)
{
*in = Aurora::Threading::ConditionalMutexNew();
return *in == nullptr;
}
void ZSTD_pthread_mutex_destroy(ZSTD_pthread_mutex_t* in)
{
if (*in)
{
Aurora::Threading::ConditionalMutexDestroy(reinterpret_cast<Aurora::Threading::IConditionalMutex *>(*in));
*in = nullptr;
}
}
void ZSTD_pthread_mutex_lock(ZSTD_pthread_mutex_t* in)
{
auto mutex = reinterpret_cast<Aurora::Threading::IConditionalMutex*>(*in);
mutex->Lock();
}
void ZSTD_pthread_mutex_unlock(ZSTD_pthread_mutex_t* in)
{
auto mutex = reinterpret_cast<Aurora::Threading::IConditionalMutex*>(*in);
mutex->Unlock();
}
int ZSTD_pthread_cond_init(ZSTD_pthread_cond_t* a, ZSTD_pthread_mutex_t* b)
{
if (*b) return 1;
auto mutex = reinterpret_cast<Aurora::Threading::IConditionalMutex*>(*b);
*a = Aurora::Threading::ConditionalNew(mutex);
return *a == nullptr;
}
void ZSTD_pthread_cond_destroy(ZSTD_pthread_cond_t* a)
{
if (*a)
{
Aurora::Threading::ConditionalDestroy(reinterpret_cast<Aurora::Threading::IConditional*>(*a));
*a = nullptr;
}
}
void ZSTD_pthread_cond_wait(ZSTD_pthread_cond_t* a)
{
auto cond = reinterpret_cast<Aurora::Threading::IConditional*>(*a);
cond->WaitForSignal();
}
void ZSTD_pthread_cond_signal(ZSTD_pthread_cond_t* a)
{
auto cond = reinterpret_cast<Aurora::Threading::IConditional*>(*a);
cond->Signal();
}
void ZSTD_pthread_cond_broadcast(ZSTD_pthread_cond_t* a)
{
auto cond = reinterpret_cast<Aurora::Threading::IConditional*>(*a);
cond->Broadcast();
}
int ZSTD_pthread_create(ZSTD_pthread_t* thread, const void* unused,
void* (*start_routine) (void*), void* arg)
{
*thread = ZSTD_pthread_t{};
Aurora::Threading::IUserThreadHandler handler;
handler.DoRun = [start_routine, arg](Aurora::Threading::IAuroraThread* thread)
{
start_routine(arg);
};
auto handle = Aurora::Threading::NewThread(handler);
if (handle != nullptr) return 1;
handle->Run();
*thread = ZSTD_pthread_t{ handle, start_routine, arg};
return 0;
}
int ZSTD_pthread_join(ZSTD_pthread_t thread, void** value_ptr)
{
if (thread.handle)
{
auto handle = reinterpret_cast<Aurora::Threading::IAuroraThread*>(thread.handle);
handle->Exit();
Aurora::Threading::ExterminateThread(handle);
}
return 0;
}
}