Remove thread::id type (to avoid using pthread_t as an unsigned int, which is an opaque pointer really).

This commit is contained in:
David Capello 2010-10-17 00:41:29 -03:00
parent 13bb881151
commit baae56c7d6
2 changed files with 8 additions and 62 deletions

View File

@ -38,8 +38,7 @@ namespace {
}
base::thread::thread()
: m_native_handle(NULL)
, m_id(this_thread::get_id())
: m_native_handle((native_handle_type)0)
{
}
@ -51,7 +50,7 @@ base::thread::~thread()
bool base::thread::joinable() const
{
return (m_id != 0 && m_id != this_thread::get_id());
return m_native_handle != (native_handle_type)0;
}
void base::thread::join()
@ -60,7 +59,7 @@ void base::thread::join()
#ifdef WIN32
::WaitForSingleObject(m_native_handle, INFINITE);
#else
::pthread_join(m_id.m_native_id, NULL);
::pthread_join((pthread_t)m_native_handle, NULL);
#endif
detach();
}
@ -71,47 +70,34 @@ void base::thread::detach()
if (joinable()) {
#ifdef WIN32
::CloseHandle(m_native_handle);
m_native_handle = NULL;
m_native_handle = (native_handle_type)0;
#else
::pthread_detach(m_id.m_native_id);
::pthread_detach((pthread_t)m_native_handle);
#endif
m_id = id();
}
}
base::thread::id base::thread::get_id() const
{
return m_id;
}
base::thread::native_handle_type base::thread::native_handle()
{
#ifdef WIN32
return (native_handle_type)m_native_handle;
#else
return (native_handle_type)m_id.m_native_id;
#endif
return m_native_handle;
}
void base::thread::launch_thread(func_wrapper* f)
{
m_native_handle = NULL;
m_id = id();
m_native_handle = (native_handle_type)0;
#ifdef WIN32
DWORD native_id;
m_native_handle = ::CreateThread(NULL, 0, win32_thread_proxy, (LPVOID)f,
CREATE_SUSPENDED, &native_id);
m_id = id((unsigned int)native_id);
ResumeThread(m_native_handle);
#else
pthread_t thread;
if (::pthread_create(&thread, NULL, pthread_thread_proxy, f) == 0)
m_id = id((unsigned int)thread);
m_native_handle = (void*)thread;
#endif
}
@ -127,20 +113,6 @@ void base::thread::details::thread_proxy(void* data)
delete f;
}
base::thread::id base::thread::details::get_current_thread_id()
{
#ifdef WIN32
return id(::GetCurrentThreadId());
#else
return id((unsigned int)::pthread_self());
#endif
}
base::thread::id base::this_thread::get_id()
{
return thread::details::get_current_thread_id();
}
void base::this_thread::yield()
{
#ifdef WIN32

View File

@ -11,30 +11,8 @@ namespace base { // Based on C++0x threads lib
class thread {
public:
class details;
class id
{
friend class thread;
friend class details;
unsigned int m_native_id;
id(unsigned int id) : m_native_id(id) { }
public:
id() : m_native_id(0) { }
bool operator==(const id& y) const { return m_native_id == y.m_native_id; }
bool operator!=(const id& y) const { return m_native_id != y.m_native_id; }
bool operator< (const id& y) const { return m_native_id < y.m_native_id; }
bool operator<=(const id& y) const { return m_native_id <= y.m_native_id; }
bool operator> (const id& y) const { return m_native_id > y.m_native_id; }
bool operator>=(const id& y) const { return m_native_id >= y.m_native_id; }
id& operator=(const id& y) { m_native_id = y.m_native_id; return *this; }
};
typedef void* native_handle_type;
public:
// Create an instance to represent the current thread
thread();
@ -62,18 +40,15 @@ namespace base { // Based on C++0x threads lib
void join();
void detach();
id get_id() const;
native_handle_type native_handle();
class details {
public:
static void thread_proxy(void* data);
static id get_current_thread_id();
};
private:
native_handle_type m_native_handle;
id m_id;
class func_wrapper {
public:
@ -113,7 +88,6 @@ namespace base { // Based on C++0x threads lib
namespace this_thread
{
thread::id get_id();
void yield();
void sleep_for(int milliseconds);
}