This source file includes following definitions.
- no_atomic_or
- no_atomic_and
- no_atomic_inc
- no_atomic_dec
- no_atomic_add
- stackAlignMain
- ThreadShim
- start
- stop
- ThreadShim
- start
- stop
#include "common.h"
#include "threading.h"
#include "cpu.h"
namespace X265_NS {
#if X265_ARCH_X86 && !defined(X86_64) && ENABLE_ASSEMBLY && defined(__GNUC__)
extern "C" intptr_t PFX(stack_align)(void (*func)(), ...);
#define STACK_ALIGN(func, ...) PFX(stack_align)((void (*)())func, __VA_ARGS__)
#else
#define STACK_ALIGN(func, ...) func(__VA_ARGS__)
#endif
#if NO_ATOMICS
pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
int no_atomic_or(int* ptr, int mask)
{
pthread_mutex_lock(&g_mutex);
int ret = *ptr;
*ptr |= mask;
pthread_mutex_unlock(&g_mutex);
return ret;
}
int no_atomic_and(int* ptr, int mask)
{
pthread_mutex_lock(&g_mutex);
int ret = *ptr;
*ptr &= mask;
pthread_mutex_unlock(&g_mutex);
return ret;
}
int no_atomic_inc(int* ptr)
{
pthread_mutex_lock(&g_mutex);
*ptr += 1;
int ret = *ptr;
pthread_mutex_unlock(&g_mutex);
return ret;
}
int no_atomic_dec(int* ptr)
{
pthread_mutex_lock(&g_mutex);
*ptr -= 1;
int ret = *ptr;
pthread_mutex_unlock(&g_mutex);
return ret;
}
int no_atomic_add(int* ptr, int val)
{
pthread_mutex_lock(&g_mutex);
*ptr += val;
int ret = *ptr;
pthread_mutex_unlock(&g_mutex);
return ret;
}
#endif
static void stackAlignMain(Thread *instance)
{
instance->threadMain();
}
#if _WIN32
static DWORD WINAPI ThreadShim(Thread *instance)
{
STACK_ALIGN(stackAlignMain, instance);
return 0;
}
bool Thread::start()
{
DWORD threadId;
thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadShim, this, 0, &threadId);
return threadId > 0;
}
void Thread::stop()
{
if (thread)
WaitForSingleObject(thread, INFINITE);
}
Thread::~Thread()
{
if (thread)
CloseHandle(thread);
}
#else
static void *ThreadShim(void *opaque)
{
Thread *instance = reinterpret_cast<Thread *>(opaque);
STACK_ALIGN(stackAlignMain, instance);
return NULL;
}
bool Thread::start()
{
if (pthread_create(&thread, NULL, ThreadShim, this))
{
thread = 0;
return false;
}
return true;
}
void Thread::stop()
{
if (thread)
pthread_join(thread, NULL);
}
Thread::~Thread() {}
#endif
Thread::Thread()
{
thread = 0;
}
}