From acb656e605d91971ee4014da66be1b7ba6201ac3 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Wed, 18 Aug 2010 17:51:09 +0200 Subject: Add multi-threading primitives Currently only the pthread-based implementation is present. --- odb/details/condition.hxx | 11 ++++ odb/details/exception.hxx | 21 ++++++++ odb/details/lock.hxx | 46 +++++++++++++++++ odb/details/mutex.hxx | 11 ++++ odb/details/posix/condition.hxx | 42 +++++++++++++++ odb/details/posix/condition.ixx | 40 +++++++++++++++ odb/details/posix/exceptions.cxx | 18 +++++++ odb/details/posix/exceptions.hxx | 31 +++++++++++ odb/details/posix/mutex.hxx | 40 +++++++++++++++ odb/details/posix/mutex.ixx | 39 ++++++++++++++ odb/details/posix/thread.cxx | 47 +++++++++++++++++ odb/details/posix/thread.hxx | 37 ++++++++++++++ odb/details/posix/thread.ixx | 34 ++++++++++++ odb/details/posix/tls.hxx | 96 ++++++++++++++++++++++++++++++++++ odb/details/posix/tls.ixx | 22 ++++++++ odb/details/posix/tls.txx | 108 +++++++++++++++++++++++++++++++++++++++ odb/details/thread.hxx | 11 ++++ odb/details/tls.hxx | 14 +++++ 18 files changed, 668 insertions(+) create mode 100644 odb/details/condition.hxx create mode 100644 odb/details/exception.hxx create mode 100644 odb/details/lock.hxx create mode 100644 odb/details/mutex.hxx create mode 100644 odb/details/posix/condition.hxx create mode 100644 odb/details/posix/condition.ixx create mode 100644 odb/details/posix/exceptions.cxx create mode 100644 odb/details/posix/exceptions.hxx create mode 100644 odb/details/posix/mutex.hxx create mode 100644 odb/details/posix/mutex.ixx create mode 100644 odb/details/posix/thread.cxx create mode 100644 odb/details/posix/thread.hxx create mode 100644 odb/details/posix/thread.ixx create mode 100644 odb/details/posix/tls.hxx create mode 100644 odb/details/posix/tls.ixx create mode 100644 odb/details/posix/tls.txx create mode 100644 odb/details/thread.hxx create mode 100644 odb/details/tls.hxx (limited to 'odb/details') diff --git a/odb/details/condition.hxx b/odb/details/condition.hxx new file mode 100644 index 0000000..9e416d6 --- /dev/null +++ b/odb/details/condition.hxx @@ -0,0 +1,11 @@ +// file : odb/details/condition.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_DETAILS_CONDITION_HXX +#define ODB_DETAILS_CONDITION_HXX + +#include + +#endif // ODB_DETAILS_CONDITION_HXX diff --git a/odb/details/exception.hxx b/odb/details/exception.hxx new file mode 100644 index 0000000..2da18ac --- /dev/null +++ b/odb/details/exception.hxx @@ -0,0 +1,21 @@ +// file : odb/details/exception.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_DETAILS_EXCEPTION_HXX +#define ODB_DETAILS_EXCEPTION_HXX + +#include + +namespace odb +{ + namespace details + { + struct exception: odb::exception + { + }; + } +} + +#endif // ODB_DETAILS_EXCEPTION_HXX diff --git a/odb/details/lock.hxx b/odb/details/lock.hxx new file mode 100644 index 0000000..0ee5a6d --- /dev/null +++ b/odb/details/lock.hxx @@ -0,0 +1,46 @@ +// file : odb/details/lock.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_DETAILS_LOCK_HXX +#define ODB_DETAILS_LOCK_HXX + +#include + +namespace odb +{ + namespace details + { + struct lock + { + lock (mutex& m) + : mutex_ (m), locked_ (true) + { + mutex_.lock (); + } + + ~lock () + { + if (locked_) + mutex_.unlock (); + } + + void + unlock () + { + if (locked_) + { + mutex_.unlock (); + locked_ = true; + } + } + + private: + mutex& mutex_; + bool locked_; + }; + } +} + +#endif // ODB_DETAILS_LOCK_HXX diff --git a/odb/details/mutex.hxx b/odb/details/mutex.hxx new file mode 100644 index 0000000..e1121b9 --- /dev/null +++ b/odb/details/mutex.hxx @@ -0,0 +1,11 @@ +// file : odb/details/mutex.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_DETAILS_MUTEX_HXX +#define ODB_DETAILS_MUTEX_HXX + +#include + +#endif // ODB_DETAILS_MUTEX_HXX diff --git a/odb/details/posix/condition.hxx b/odb/details/posix/condition.hxx new file mode 100644 index 0000000..8e8e05c --- /dev/null +++ b/odb/details/posix/condition.hxx @@ -0,0 +1,42 @@ +// file : odb/details/posix/condition.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_DETAILS_POSIX_CONDITION_HXX +#define ODB_DETAILS_POSIX_CONDITION_HXX + +#include + +#include + +namespace odb +{ + namespace details + { + class condition + { + public: + ~condition (); + condition (mutex&); + + void + signal (); + + void + wait (); + + private: + condition (const condition&); + condition& operator= (const condition&); + + private: + mutex& mutex_; + pthread_cond_t cond_; + }; + } +} + +#include + +#endif // ODB_DETAILS_POSIX_CONDITION_HXX diff --git a/odb/details/posix/condition.ixx b/odb/details/posix/condition.ixx new file mode 100644 index 0000000..5b61e74 --- /dev/null +++ b/odb/details/posix/condition.ixx @@ -0,0 +1,40 @@ +// file : odb/details/posix/condition.ixx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#include + +namespace odb +{ + namespace details + { + inline condition:: + ~condition () + { + pthread_cond_destroy (&cond_); + } + + inline condition:: + condition (mutex& mutex) + : mutex_ (mutex) + { + if (int e = pthread_cond_init (&cond_, 0)) + throw posix_exception (e); + } + + inline void condition:: + signal () + { + if (int e = pthread_cond_signal (&cond_)) + throw posix_exception (e); + } + + inline void condition:: + wait () + { + if (int e = pthread_cond_wait (&cond_, &mutex_.mutex_)) + throw posix_exception (e); + } + } +} diff --git a/odb/details/posix/exceptions.cxx b/odb/details/posix/exceptions.cxx new file mode 100644 index 0000000..4b11ff8 --- /dev/null +++ b/odb/details/posix/exceptions.cxx @@ -0,0 +1,18 @@ +// file : odb/details/posix/exceptions.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#include + +namespace odb +{ + namespace details + { + const char* posix_exception:: + what () const throw () + { + return "POSIX API error"; + } + } +} diff --git a/odb/details/posix/exceptions.hxx b/odb/details/posix/exceptions.hxx new file mode 100644 index 0000000..f4b4572 --- /dev/null +++ b/odb/details/posix/exceptions.hxx @@ -0,0 +1,31 @@ +// file : odb/details/posix/exceptions.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_DETAILS_POSIX_EXCEPTIONS_HXX +#define ODB_DETAILS_POSIX_EXCEPTIONS_HXX + +#include + +namespace odb +{ + namespace details + { + struct posix_exception: details::exception + { + posix_exception (int code) : code_ (code) {} + + int + code () const {return code_;} + + virtual const char* + what () const throw (); + + private: + int code_; + }; + } +} + +#endif // ODB_DETAILS_POSIX_EXCEPTIONS_HXX diff --git a/odb/details/posix/mutex.hxx b/odb/details/posix/mutex.hxx new file mode 100644 index 0000000..d06a8c3 --- /dev/null +++ b/odb/details/posix/mutex.hxx @@ -0,0 +1,40 @@ +// file : odb/details/posix/mutex.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_DETAILS_POSIX_MUTEX_HXX +#define ODB_DETAILS_POSIX_MUTEX_HXX + +#include + +namespace odb +{ + namespace details + { + class mutex + { + public: + ~mutex (); + mutex (); + + void + lock (); + + void + unlock (); + + private: + mutex (const mutex&); + mutex& operator= (const mutex&); + + private: + friend class condition; + pthread_mutex_t mutex_; + }; + } +} + +#include + +#endif // ODB_DETAILS_POSIX_MUTEX_HXX diff --git a/odb/details/posix/mutex.ixx b/odb/details/posix/mutex.ixx new file mode 100644 index 0000000..f362d98 --- /dev/null +++ b/odb/details/posix/mutex.ixx @@ -0,0 +1,39 @@ +// file : odb/details/posix/mutex.ixx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#include + +namespace odb +{ + namespace details + { + inline mutex:: + ~mutex () + { + pthread_mutex_destroy (&mutex_); + } + + inline mutex:: + mutex () + { + if (int e = pthread_mutex_init (&mutex_, 0)) + throw posix_exception (e); + } + + inline void mutex:: + lock () + { + if (int e = pthread_mutex_lock (&mutex_)) + throw posix_exception (e); + } + + inline void mutex:: + unlock () + { + if (int e = pthread_mutex_unlock (&mutex_)) + throw posix_exception (e); + } + } +} diff --git a/odb/details/posix/thread.cxx b/odb/details/posix/thread.cxx new file mode 100644 index 0000000..52ab6e9 --- /dev/null +++ b/odb/details/posix/thread.cxx @@ -0,0 +1,47 @@ +// file : odb/details/posix/thread.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#include // std::auto_ptr + +#include +#include + +typedef void* (*thread_func) (void*); + +struct thread_data +{ + thread_func func; + void* arg; +}; + +extern "C" void* +odb_thread_thunk (void* arg) +{ + thread_data* data (static_cast (arg)); + thread_func func = data->func; + arg = data->arg; + delete data; + return func (arg); +} + +namespace odb +{ + namespace details + { + thread:: + thread (void* (*func) (void*), void* arg) + : detached_ (false) + { + std::auto_ptr data (new thread_data); + data->func = func; + data->arg = arg; + + if (int e = pthread_create (&id_, 0, &odb_thread_thunk, data.get ())) + throw posix_exception (e); + + data.release (); // Thread thunk will free this. + } + } +} diff --git a/odb/details/posix/thread.hxx b/odb/details/posix/thread.hxx new file mode 100644 index 0000000..5036b37 --- /dev/null +++ b/odb/details/posix/thread.hxx @@ -0,0 +1,37 @@ +// file : odb/details/posix/thread.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_DETAILS_POSIX_THREAD_HXX +#define ODB_DETAILS_POSIX_THREAD_HXX + +#include + +namespace odb +{ + namespace details + { + class thread + { + public: + ~thread (); + thread (void* (*thread_func) (void*), void* arg = 0); + + void* + join (); + + private: + thread (const thread&); + thread& operator= (const thread&); + + private: + bool detached_; + pthread_t id_; + }; + } +} + +#include + +#endif // ODB_DETAILS_POSIX_THREAD_HXX diff --git a/odb/details/posix/thread.ixx b/odb/details/posix/thread.ixx new file mode 100644 index 0000000..389cc2e --- /dev/null +++ b/odb/details/posix/thread.ixx @@ -0,0 +1,34 @@ +// file : odb/details/posix/thread.ixx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#include + +namespace odb +{ + namespace details + { + inline thread:: + ~thread () + { + if (!detached_) + { + if (int e = pthread_detach (id_)) + throw posix_exception (e); + } + } + + inline void* thread:: + join () + { + void* r; + + if (int e = pthread_join (id_, &r)) + throw posix_exception (e); + + detached_ = true; + return r; + } + } +} diff --git a/odb/details/posix/tls.hxx b/odb/details/posix/tls.hxx new file mode 100644 index 0000000..6e11a64 --- /dev/null +++ b/odb/details/posix/tls.hxx @@ -0,0 +1,96 @@ +// file : odb/details/posix/tls.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_DETAILS_POSIX_TLS_HXX +#define ODB_DETAILS_POSIX_TLS_HXX + +#include + +namespace odb +{ + namespace details + { + template + class tls; + + template + class tls + { + public: + tls (); + + T& + get () const; + + private: + tls (const tls&); + tls& operator= (const tls&); + + private: + static void + key_init (); + + static void + destructor (void*); + + private: + static int error_; + static pthread_once_t once_; + static pthread_key_t key_; + }; + + template + class tls + { + public: + tls (); + + T* + get () const; + + void + set (T* p); + + private: + tls (const tls&); + tls& operator= (const tls&); + + private: + static void + key_init (); + + private: + static int error_; + static pthread_once_t once_; + static pthread_key_t key_; + }; + + template + inline T& + tls_get (const tls& s) + { + return s.get (); + } + + template + inline T* + tls_get (const tls& s) + { + return s.get (); + } + + template + inline void + tls_set (tls& s, T* p) + { + return s.set (p); + } + } +} + +#include +#include + +#endif // ODB_DETAILS_POSIX_TLS_HXX diff --git a/odb/details/posix/tls.ixx b/odb/details/posix/tls.ixx new file mode 100644 index 0000000..5c9a796 --- /dev/null +++ b/odb/details/posix/tls.ixx @@ -0,0 +1,22 @@ +// file : odb/details/posix/tls.ixx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +namespace odb +{ + namespace details + { + template + inline tls:: + tls () + { + } + + template + inline tls:: + tls () + { + } + } +} diff --git a/odb/details/posix/tls.txx b/odb/details/posix/tls.txx new file mode 100644 index 0000000..8cc78a4 --- /dev/null +++ b/odb/details/posix/tls.txx @@ -0,0 +1,108 @@ +// file : odb/details/posix/tls.txx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#include // std::auto_ptr + +#include + +namespace odb +{ + namespace details + { + // tls + // + + template + int tls::error_ = 0; + + template + pthread_once_t tls::once_ = PTHREAD_ONCE_INIT; + + template + pthread_key_t tls::key_; + + template + T& tls:: + get () const + { + int e (pthread_once (&once_, key_init)); + + if (e != 0 || error_ != 0) + throw posix_exception (e ? e : error_); + + void* v (pthread_getspecific (key_)); + + if (v != 0) + return *static_cast (v); + + std::auto_ptr p (new T); + + if (e = pthread_setspecific (key_, p.get ())) + throw posix_exception (e); + + T& r (*p); + p.release (); + return r; + } + + template + void tls:: + key_init () + { + error_ = pthread_key_create (&key_, destructor); + } + + template + void tls:: + destructor (void* v) + { + delete static_cast (v); + } + + // tls + // + + template + int tls::error_ = 0; + + template + pthread_once_t tls::once_ = PTHREAD_ONCE_INIT; + + template + pthread_key_t tls::key_; + + template + T* tls:: + get () const + { + int e (pthread_once (&once_, key_init)); + + if (e != 0 || error_ != 0) + throw posix_exception (e ? e : error_); + + return static_cast (pthread_getspecific (key_)); + } + + template + void tls:: + set (T* p) + { + int e (pthread_once (&once_, key_init)); + + if (e != 0 || error_ != 0) + throw posix_exception (e ? e : error_); + + if (e = pthread_setspecific (key_, p)) + throw posix_exception (e); + } + + template + void tls:: + key_init () + { + error_ = pthread_key_create (&key_, 0); + } + } +} diff --git a/odb/details/thread.hxx b/odb/details/thread.hxx new file mode 100644 index 0000000..4dba45e --- /dev/null +++ b/odb/details/thread.hxx @@ -0,0 +1,11 @@ +// file : odb/details/thread.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_DETAILS_THREAD_HXX +#define ODB_DETAILS_THREAD_HXX + +#include + +#endif // ODB_DETAILS_THREAD_HXX diff --git a/odb/details/tls.hxx b/odb/details/tls.hxx new file mode 100644 index 0000000..6d38e6b --- /dev/null +++ b/odb/details/tls.hxx @@ -0,0 +1,14 @@ +// file : odb/details/tls.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_DETAILS_TLS_HXX +#define ODB_DETAILS_TLS_HXX + +#include + +#define ODB_TLS_POINTER(type) tls +#define ODB_TLS_OBJECT(type) tls + +#endif // ODB_DETAILS_TLS_HXX -- cgit v1.1