aboutsummaryrefslogtreecommitdiff
path: root/odb/details
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2010-08-18 17:51:09 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2010-08-18 17:51:09 +0200
commitacb656e605d91971ee4014da66be1b7ba6201ac3 (patch)
tree96bd2145eafe938647c6d510d58dc217a22344c7 /odb/details
parentd8b70727fdda5c1de19736724809e76e060b417e (diff)
Add multi-threading primitives
Currently only the pthread-based implementation is present.
Diffstat (limited to 'odb/details')
-rw-r--r--odb/details/condition.hxx11
-rw-r--r--odb/details/exception.hxx21
-rw-r--r--odb/details/lock.hxx46
-rw-r--r--odb/details/mutex.hxx11
-rw-r--r--odb/details/posix/condition.hxx42
-rw-r--r--odb/details/posix/condition.ixx40
-rw-r--r--odb/details/posix/exceptions.cxx18
-rw-r--r--odb/details/posix/exceptions.hxx31
-rw-r--r--odb/details/posix/mutex.hxx40
-rw-r--r--odb/details/posix/mutex.ixx39
-rw-r--r--odb/details/posix/thread.cxx47
-rw-r--r--odb/details/posix/thread.hxx37
-rw-r--r--odb/details/posix/thread.ixx34
-rw-r--r--odb/details/posix/tls.hxx96
-rw-r--r--odb/details/posix/tls.ixx22
-rw-r--r--odb/details/posix/tls.txx108
-rw-r--r--odb/details/thread.hxx11
-rw-r--r--odb/details/tls.hxx14
18 files changed, 668 insertions, 0 deletions
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 <boris@codesynthesis.com>
+// 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 <odb/details/posix/condition.hxx>
+
+#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 <boris@codesynthesis.com>
+// 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 <odb/exception.hxx>
+
+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 <boris@codesynthesis.com>
+// 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 <odb/details/posix/mutex.hxx>
+
+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 <boris@codesynthesis.com>
+// 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 <odb/details/posix/mutex.hxx>
+
+#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 <boris@codesynthesis.com>
+// 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 <pthread.h>
+
+#include <odb/details/posix/mutex.hxx>
+
+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 <odb/details/posix/condition.ixx>
+
+#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 <boris@codesynthesis.com>
+// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC
+// license : GNU GPL v2; see accompanying LICENSE file
+
+#include <odb/details/posix/exceptions.hxx>
+
+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 <boris@codesynthesis.com>
+// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC
+// license : GNU GPL v2; see accompanying LICENSE file
+
+#include <odb/details/posix/exceptions.hxx>
+
+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 <boris@codesynthesis.com>
+// 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 <odb/details/exception.hxx>
+
+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 <boris@codesynthesis.com>
+// 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 <pthread.h>
+
+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 <odb/details/posix/mutex.ixx>
+
+#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 <boris@codesynthesis.com>
+// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC
+// license : GNU GPL v2; see accompanying LICENSE file
+
+#include <odb/details/posix/exceptions.hxx>
+
+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 <boris@codesynthesis.com>
+// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC
+// license : GNU GPL v2; see accompanying LICENSE file
+
+#include <memory> // std::auto_ptr
+
+#include <odb/details/posix/thread.hxx>
+#include <odb/details/posix/exceptions.hxx>
+
+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<thread_data*> (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<thread_data> 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 <boris@codesynthesis.com>
+// 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 <pthread.h>
+
+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 <odb/details/posix/thread.ixx>
+
+#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 <boris@codesynthesis.com>
+// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC
+// license : GNU GPL v2; see accompanying LICENSE file
+
+#include <odb/details/posix/exceptions.hxx>
+
+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 <boris@codesynthesis.com>
+// 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 <pthread.h>
+
+namespace odb
+{
+ namespace details
+ {
+ template <typename T>
+ class tls;
+
+ template <typename T>
+ 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 <typename T>
+ class tls<T*>
+ {
+ 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 <typename T>
+ inline T&
+ tls_get (const tls<T>& s)
+ {
+ return s.get ();
+ }
+
+ template <typename T>
+ inline T*
+ tls_get (const tls<T*>& s)
+ {
+ return s.get ();
+ }
+
+ template <typename T>
+ inline void
+ tls_set (tls<T*>& s, T* p)
+ {
+ return s.set (p);
+ }
+ }
+}
+
+#include <odb/details/posix/tls.ixx>
+#include <odb/details/posix/tls.txx>
+
+#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 <boris@codesynthesis.com>
+// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC
+// license : GNU GPL v2; see accompanying LICENSE file
+
+namespace odb
+{
+ namespace details
+ {
+ template <typename T>
+ inline tls<T>::
+ tls ()
+ {
+ }
+
+ template <typename T>
+ inline tls<T*>::
+ 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 <boris@codesynthesis.com>
+// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC
+// license : GNU GPL v2; see accompanying LICENSE file
+
+#include <memory> // std::auto_ptr
+
+#include <odb/details/posix/exceptions.hxx>
+
+namespace odb
+{
+ namespace details
+ {
+ // tls<T>
+ //
+
+ template <typename T>
+ int tls<T>::error_ = 0;
+
+ template <typename T>
+ pthread_once_t tls<T>::once_ = PTHREAD_ONCE_INIT;
+
+ template <typename T>
+ pthread_key_t tls<T>::key_;
+
+ template <typename T>
+ T& tls<T>::
+ 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<T*> (v);
+
+ std::auto_ptr<T> p (new T);
+
+ if (e = pthread_setspecific (key_, p.get ()))
+ throw posix_exception (e);
+
+ T& r (*p);
+ p.release ();
+ return r;
+ }
+
+ template <typename T>
+ void tls<T>::
+ key_init ()
+ {
+ error_ = pthread_key_create (&key_, destructor);
+ }
+
+ template <typename T>
+ void tls<T>::
+ destructor (void* v)
+ {
+ delete static_cast<T*> (v);
+ }
+
+ // tls<T*>
+ //
+
+ template <typename T>
+ int tls<T*>::error_ = 0;
+
+ template <typename T>
+ pthread_once_t tls<T*>::once_ = PTHREAD_ONCE_INIT;
+
+ template <typename T>
+ pthread_key_t tls<T*>::key_;
+
+ template <typename T>
+ T* tls<T*>::
+ get () const
+ {
+ int e (pthread_once (&once_, key_init));
+
+ if (e != 0 || error_ != 0)
+ throw posix_exception (e ? e : error_);
+
+ return static_cast<T*> (pthread_getspecific (key_));
+ }
+
+ template <typename T>
+ void tls<T*>::
+ 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 <typename T>
+ void tls<T*>::
+ 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 <boris@codesynthesis.com>
+// 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 <odb/details/posix/thread.hxx>
+
+#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 <boris@codesynthesis.com>
+// 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 <odb/details/posix/tls.hxx>
+
+#define ODB_TLS_POINTER(type) tls<type*>
+#define ODB_TLS_OBJECT(type) tls<type>
+
+#endif // ODB_DETAILS_TLS_HXX