thread.h
//
// Copyright (C) 2009-2012 Artyom Beilis (Tonkikh)
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOSTER_UTIL_THREAD_H
#define BOOSTER_UTIL_THREAD_H
#include <booster/hold_ptr.h>
#include <booster/noncopyable.h>
#include <booster/refcounted.h>
#include <booster/intrusive_ptr.h>
#include <booster/function.h>
#include <booster/config.h>
namespace booster {
extern "C" void *booster_thread_func(void *);
class BOOSTER_API thread : public noncopyable {
public:
thread(function<void()> const &cb);
~thread();
void join();
void detach();
static unsigned hardware_concurrency();
private:
friend void *booster_thread_func(void *);
struct data;
hold_ptr<data> d;
};
class condition_variable;
class BOOSTER_API mutex : public noncopyable {
public:
mutex();
~mutex();
void lock();
void unlock();
friend class condition_variable;
private:
struct data;
hold_ptr<data> d;
};
class BOOSTER_API recursive_mutex : public noncopyable {
public:
recursive_mutex();
~recursive_mutex();
void lock();
void unlock();
private:
struct data;
hold_ptr<data> d;
};
class BOOSTER_API recursive_shared_mutex : public noncopyable {
public:
recursive_shared_mutex();
~recursive_shared_mutex();
void lock() { unique_lock(); }
void unique_lock();
void shared_lock();
void unlock();
private:
struct data;
hold_ptr<data> d;
};
class BOOSTER_API shared_mutex : public noncopyable {
public:
shared_mutex();
~shared_mutex();
void lock() { unique_lock(); }
void unique_lock();
void shared_lock();
void unlock();
private:
struct data;
hold_ptr<data> d;
};
template<typename Mutex>
class unique_lock;
class BOOSTER_API condition_variable {
public:
condition_variable();
~condition_variable();
void wait(unique_lock<mutex> &m);
void notify_one();
void notify_all();
private:
struct data;
hold_ptr<data> d;
};
namespace details {
struct tls_object;
class key : public refcounted {
public:
key(void (*d)(void *)) :
dtor_(d)
{
}
virtual ~key()
{
}
void *get();
void set(void *);
void destroy(void *p)
{
dtor_(p);
}
virtual tls_object *get_object() = 0;
private:
void (*dtor_)(void *);
};
BOOSTER_API intrusive_ptr<key> make_key(void (*dtor)(void *));
struct tls_object {
tls_object(intrusive_ptr<key> p) :
the_key(p),
obj(0)
{
}
~tls_object()
{
the_key->destroy(obj);
obj = 0;
}
intrusive_ptr<key> the_key;
void *obj;
};
inline void key::set(void *p)
{
get_object()->obj = p;
}
inline void *key::get()
{
return get_object()->obj;
}
} // details
template<typename T>
class thread_specific_ptr {
public:
thread_specific_ptr() : key_(details::make_key(destructor))
{
}
~thread_specific_ptr()
{
}
T *get() const
{
return static_cast<T*>(key_->get());
}
T* operator->() const
{
return get();
}
T& operator*() const
{
return *get();
}
void reset(T *new_val = 0)
{
T *p = get();
if(p)
destructor(p);
key_->set(static_cast<void *>(new_val));
}
T *release()
{
T *p = get();
key_->set(0);
return p;
}
private:
static void destructor(void *ptr)
{
delete static_cast<T*>(ptr);
}
intrusive_ptr<details::key> key_;
};
template<typename Mutex>
class unique_lock : public noncopyable {
public:
unique_lock(Mutex &m) : m_(&m)
{
m_->lock();
}
~unique_lock()
{
m_->unlock();
}
Mutex *mutex() const
{
return m_;
}
private:
Mutex *m_;
};
template<typename Mutex>
class shared_lock : public noncopyable {
public:
shared_lock(Mutex &m) : m_(&m)
{
m_->shared_lock();
}
~shared_lock()
{
m_->unlock();
}
Mutex *mutex() const
{
return m_;
}
private:
Mutex *m_;
};
#ifdef BOOSTER_POSIX
class BOOSTER_API fork_shared_mutex : public noncopyable {
public:
fork_shared_mutex();
~fork_shared_mutex();
bool try_lock() { return try_unique_lock(); }
bool try_unique_lock();
bool try_shared_lock();
void lock() { return unique_lock(); }
void unique_lock();
void shared_lock();
void unlock();
private:
struct data;
hold_ptr<data> d;
};
#endif
}//booster
#endif
booster::thread_specific_ptr::operator->
T * operator->() const
Definition: thread.h:335
booster::mutex
Mutex object.
Definition: thread.h:59
booster::shared_mutex::lock
void lock()
Definition: thread.h:173
booster::thread_specific_ptr::thread_specific_ptr
thread_specific_ptr()
Definition: thread.h:315
booster::recursive_mutex
Recursive mutex object.
Definition: thread.h:106
booster::thread_specific_ptr
Thread specific pointer.
Definition: thread.h:310
booster::recursive_shared_mutex
Recursuve Shared mutex or a.k.a. Read-Write Lock that can be recursively locked by readers...
Definition: thread.h:129
booster::hold_ptr< data >
booster::thread_specific_ptr::~thread_specific_ptr
~thread_specific_ptr()
Definition: thread.h:322
booster::unique_lock::unique_lock
unique_lock(Mutex &m)
Acquire the lock.
Definition: thread.h:387
booster::unique_lock
a Unique lock guard.
Definition: thread.h:194
booster::recursive_shared_mutex::lock
void lock()
Definition: thread.h:138
booster::refcounted
This class is used as base class for reference counted objects that use intrusive_ptr. Deriving from this class allows simple way to manage reference counting for single object.
Definition: refcounted.h:25
booster::unique_lock::mutex
Mutex * mutex() const
Get the reference to the mutex object.
Definition: thread.h:397
booster::shared_lock::shared_lock
shared_lock(Mutex &m)
Acquire the lock.
Definition: thread.h:414
booster::thread_specific_ptr::operator*
T & operator*() const
Definition: thread.h:342
booster::shared_lock::~shared_lock
~shared_lock()
Release the lock.
Definition: thread.h:419
booster::unique_lock::~unique_lock
~unique_lock()
Release the lock.
Definition: thread.h:392
booster::shared_mutex
Shared mutex or a.k.a. Read-Write Lock.
Definition: thread.h:166
booster::shared_lock
a Shared lock guard.
Definition: thread.h:411
booster::condition_variable
This is conditional variable object.
Definition: thread.h:201
booster::thread
the class that allows to start an execution thread
Definition: thread.h:25
booster::thread_specific_ptr::reset
void reset(T *new_val=0)
Definition: thread.h:351
booster::shared_lock::mutex
Mutex * mutex() const
Get the reference to the mutex object.
Definition: thread.h:424
booster::noncopyable
This class makes impossible to copy any class derived from this one.
Definition: noncopyable.h:15
booster::thread_specific_ptr::release
T * release()
Definition: thread.h:363