Moved mutex/semaphore/thread implementation details to .cpp file.

This commit is contained in:
Branimir Karadžić
2017-02-15 22:14:17 -08:00
parent 3cda29b011
commit f7abed893f
8 changed files with 286 additions and 236 deletions

View File

@@ -7,21 +7,9 @@
#define BX_MUTEX_H_HEADER_GUARD
#include "bx.h"
#include "cpu.h"
#include "os.h"
#include "sem.h"
#if BX_CONFIG_SUPPORTS_THREADING
#if 0 \
|| BX_PLATFORM_ANDROID \
|| BX_PLATFORM_LINUX \
|| BX_PLATFORM_NACL \
|| BX_PLATFORM_IOS \
|| BX_PLATFORM_OSX
# include <pthread.h>
#endif //
namespace bx
{
///
@@ -46,11 +34,7 @@ namespace bx
void unlock();
private:
#if BX_PLATFORM_WINDOWS || BX_PLATFORM_XBOX360 || BX_PLATFORM_XBOXONE || BX_PLATFORM_WINRT
CRITICAL_SECTION m_handle;
#else
pthread_mutex_t m_handle;
#endif // BX_PLATFORM_WINDOWS || BX_PLATFORM_XBOX360 || BX_PLATFORM_XBOXONE || BX_PLATFORM_WINRT
BX_ALIGN_DECL(16, uint8_t) m_internal[64];
};
///

View File

@@ -10,21 +10,6 @@
#if BX_CONFIG_SUPPORTS_THREADING
#if BX_PLATFORM_POSIX
# include <errno.h>
# include <semaphore.h>
# include <time.h>
# include <pthread.h>
#elif BX_PLATFORM_XBOX360 || BX_PLATFORM_WINDOWS || BX_PLATFORM_WINRT || BX_PLATFORM_XBOXONE
# include <windows.h>
# include <limits.h>
# if BX_PLATFORM_XBOXONE
# include <synchapi.h>
# endif // BX_PLATFORM_XBOXONE
#endif // BX_PLATFORM_
#include "mutex.h"
namespace bx
{
///
@@ -49,17 +34,7 @@ namespace bx
bool wait(int32_t _msecs = -1);
private:
#if BX_PLATFORM_POSIX
# if BX_CONFIG_SEMAPHORE_PTHREAD
pthread_mutex_t m_mutex;
pthread_cond_t m_cond;
int32_t m_count;
# else
sem_t m_handle;
# endif // BX_CONFIG_SEMAPHORE_PTHREAD
#elif BX_PLATFORM_XBOX360 || BX_PLATFORM_XBOXONE || BX_PLATFORM_WINDOWS || BX_PLATFORM_WINRT
HANDLE m_handle;
#endif // BX_PLATFORM_
BX_ALIGN_DECL(16, uint8_t) m_internal[64];
};
} // namespace bx

View File

@@ -9,6 +9,7 @@
#include "bx.h"
#include "cpu.h"
#include "mutex.h"
#include "sem.h"
#include "uint32_t.h"
#include <list>

View File

@@ -7,21 +7,6 @@
#define BX_THREAD_H_HEADER_GUARD
#include "bx.h"
#if BX_PLATFORM_POSIX
# include <pthread.h>
# if defined(__FreeBSD__)
# include <pthread_np.h>
# endif
# if BX_PLATFORM_LINUX && (BX_CRT_GLIBC < 21200)
# include <sys/prctl.h>
# endif // BX_PLATFORM_
#elif BX_PLATFORM_WINRT
using namespace Platform;
using namespace Windows::Foundation;
using namespace Windows::System::Threading;
#endif // BX_PLATFORM_
#include "sem.h"
#if BX_CONFIG_SUPPORTS_THREADING
@@ -62,16 +47,10 @@ namespace bx
void setThreadName(const char* _name);
private:
friend class ThreadInternal;
int32_t entry();
#if BX_PLATFORM_WINDOWS || BX_PLATFORM_XBOX360 || BX_PLATFORM_XBOXONE || BX_PLATFORM_WINRT
static DWORD WINAPI threadFunc(LPVOID _arg);
HANDLE m_handle;
DWORD m_threadId;
#elif BX_PLATFORM_POSIX
static void* threadFunc(void* _arg);
pthread_t m_handle;
#endif // BX_PLATFORM_
BX_ALIGN_DECL(16, uint8_t) m_internal[64];
ThreadFn m_fn;
void* m_userData;
@@ -98,17 +77,11 @@ namespace bx
void set(void* _ptr);
private:
#if BX_PLATFORM_WINDOWS
uint32_t m_id;
#elif !(BX_PLATFORM_XBOXONE || BX_PLATFORM_WINRT)
pthread_key_t m_id;
#endif // BX_PLATFORM_*
BX_ALIGN_DECL(16, uint8_t) m_internal[64];
};
} // namespace bx
#endif // BX_CONFIG_SUPPORTS_THREADING
#include "thread.inl"
#endif // BX_THREAD_H_HEADER_GUARD

View File

@@ -1,65 +0,0 @@
/*
* Copyright 2010-2017 Branimir Karadzic. All rights reserved.
* License: https://github.com/bkaradzic/bx#license-bsd-2-clause
*/
#ifndef BX_THREAD_H_HEADER_GUARD
# error "Must be included from bx/thread.h!"
#endif // BX_THREAD_H_HEADER_GUARD
#if BX_CONFIG_SUPPORTS_THREADING
namespace bx
{
#if BX_PLATFORM_WINDOWS
inline TlsData::TlsData()
{
m_id = TlsAlloc();
BX_CHECK(TLS_OUT_OF_INDEXES != m_id, "Failed to allocated TLS index (err: 0x%08x).", GetLastError() );
}
inline TlsData::~TlsData()
{
BOOL result = TlsFree(m_id);
BX_CHECK(0 != result, "Failed to free TLS index (err: 0x%08x).", GetLastError() ); BX_UNUSED(result);
}
inline void* TlsData::get() const
{
return TlsGetValue(m_id);
}
inline void TlsData::set(void* _ptr)
{
TlsSetValue(m_id, _ptr);
}
#elif !(BX_PLATFORM_XBOXONE || BX_PLATFORM_WINRT)
inline TlsData::TlsData()
{
int result = pthread_key_create(&m_id, NULL);
BX_CHECK(0 == result, "pthread_key_create failed %d.", result); BX_UNUSED(result);
}
inline TlsData::~TlsData()
{
int result = pthread_key_delete(m_id);
BX_CHECK(0 == result, "pthread_key_delete failed %d.", result); BX_UNUSED(result);
}
inline void* TlsData::get() const
{
return pthread_getspecific(m_id);
}
inline void TlsData::set(void* _ptr)
{
int result = pthread_setspecific(m_id, _ptr);
BX_CHECK(0 == result, "pthread_setspecific failed %d.", result); BX_UNUSED(result);
}
#endif // BX_PLATFORM_*
} // namespace bx
#endif // BX_CONFIG_SUPPORTS_THREADING