diff options
author | Karen Arutyunov <karen@codesynthesis.com> | 2024-01-24 16:53:38 +0300 |
---|---|---|
committer | Karen Arutyunov <karen@codesynthesis.com> | 2024-01-24 16:53:38 +0300 |
commit | 26e36b3a9d7b49d46ecfa69b447482251acba8ac (patch) | |
tree | 5436b7857fae9aee50d018ea0eef35a529fc03e4 /libodb/odb/details/posix | |
parent | 0074faad1b27f3cd52a606c438e4f1375956d731 (diff) |
Turn libodb repository into package for muti-package repositorylibodb
Diffstat (limited to 'libodb/odb/details/posix')
-rw-r--r-- | libodb/odb/details/posix/condition.hxx | 47 | ||||
-rw-r--r-- | libodb/odb/details/posix/condition.ixx | 38 | ||||
-rw-r--r-- | libodb/odb/details/posix/exceptions.cxx | 22 | ||||
-rw-r--r-- | libodb/odb/details/posix/exceptions.hxx | 38 | ||||
-rw-r--r-- | libodb/odb/details/posix/mutex.hxx | 44 | ||||
-rw-r--r-- | libodb/odb/details/posix/mutex.ixx | 37 | ||||
-rw-r--r-- | libodb/odb/details/posix/thread.cxx | 44 | ||||
-rw-r--r-- | libodb/odb/details/posix/thread.hxx | 41 | ||||
-rw-r--r-- | libodb/odb/details/posix/thread.ixx | 29 | ||||
-rw-r--r-- | libodb/odb/details/posix/tls.hxx | 106 | ||||
-rw-r--r-- | libodb/odb/details/posix/tls.ixx | 20 | ||||
-rw-r--r-- | libodb/odb/details/posix/tls.txx | 121 |
12 files changed, 587 insertions, 0 deletions
diff --git a/libodb/odb/details/posix/condition.hxx b/libodb/odb/details/posix/condition.hxx new file mode 100644 index 0000000..4f7c42a --- /dev/null +++ b/libodb/odb/details/posix/condition.hxx @@ -0,0 +1,47 @@ +// file : odb/details/posix/condition.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_DETAILS_POSIX_CONDITION_HXX +#define ODB_DETAILS_POSIX_CONDITION_HXX + +#include <odb/pre.hxx> + +#include <pthread.h> + +#include <odb/details/export.hxx> +#include <odb/details/posix/mutex.hxx> + +namespace odb +{ + namespace details + { + class lock; + + class LIBODB_EXPORT condition + { + public: + ~condition (); + condition (mutex&); + + void + signal (); + + void + wait (lock&); + + private: + condition (const condition&); + condition& operator= (const condition&); + + private: + mutex& mutex_; + pthread_cond_t cond_; + }; + } +} + +#include <odb/details/posix/condition.ixx> + +#include <odb/post.hxx> + +#endif // ODB_DETAILS_POSIX_CONDITION_HXX diff --git a/libodb/odb/details/posix/condition.ixx b/libodb/odb/details/posix/condition.ixx new file mode 100644 index 0000000..9b68d9f --- /dev/null +++ b/libodb/odb/details/posix/condition.ixx @@ -0,0 +1,38 @@ +// file : odb/details/posix/condition.ixx +// 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 (lock&) + { + if (int e = pthread_cond_wait (&cond_, &mutex_.mutex_)) + throw posix_exception (e); + } + } +} diff --git a/libodb/odb/details/posix/exceptions.cxx b/libodb/odb/details/posix/exceptions.cxx new file mode 100644 index 0000000..c346655 --- /dev/null +++ b/libodb/odb/details/posix/exceptions.cxx @@ -0,0 +1,22 @@ +// file : odb/details/posix/exceptions.cxx +// license : GNU GPL v2; see accompanying LICENSE file + +#include <odb/details/posix/exceptions.hxx> + +namespace odb +{ + namespace details + { + const char* posix_exception:: + what () const ODB_NOTHROW_NOEXCEPT + { + return "POSIX API error"; + } + + posix_exception* posix_exception:: + clone () const + { + return new posix_exception (*this); + } + } +} diff --git a/libodb/odb/details/posix/exceptions.hxx b/libodb/odb/details/posix/exceptions.hxx new file mode 100644 index 0000000..aff33b6 --- /dev/null +++ b/libodb/odb/details/posix/exceptions.hxx @@ -0,0 +1,38 @@ +// file : odb/details/posix/exceptions.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_DETAILS_POSIX_EXCEPTIONS_HXX +#define ODB_DETAILS_POSIX_EXCEPTIONS_HXX + +#include <odb/pre.hxx> + +#include <odb/details/config.hxx> // ODB_NOTHROW_NOEXCEPT +#include <odb/details/export.hxx> +#include <odb/details/exception.hxx> + +namespace odb +{ + namespace details + { + struct LIBODB_EXPORT posix_exception: details::exception + { + posix_exception (int code) : code_ (code) {} + + int + code () const {return code_;} + + virtual const char* + what () const ODB_NOTHROW_NOEXCEPT; + + virtual posix_exception* + clone () const; + + private: + int code_; + }; + } +} + +#include <odb/post.hxx> + +#endif // ODB_DETAILS_POSIX_EXCEPTIONS_HXX diff --git a/libodb/odb/details/posix/mutex.hxx b/libodb/odb/details/posix/mutex.hxx new file mode 100644 index 0000000..0cb94db --- /dev/null +++ b/libodb/odb/details/posix/mutex.hxx @@ -0,0 +1,44 @@ +// file : odb/details/posix/mutex.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_DETAILS_POSIX_MUTEX_HXX +#define ODB_DETAILS_POSIX_MUTEX_HXX + +#include <odb/pre.hxx> + +#include <pthread.h> + +#include <odb/details/export.hxx> + +namespace odb +{ + namespace details + { + class LIBODB_EXPORT 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> + +#include <odb/post.hxx> + +#endif // ODB_DETAILS_POSIX_MUTEX_HXX diff --git a/libodb/odb/details/posix/mutex.ixx b/libodb/odb/details/posix/mutex.ixx new file mode 100644 index 0000000..ee73d09 --- /dev/null +++ b/libodb/odb/details/posix/mutex.ixx @@ -0,0 +1,37 @@ +// file : odb/details/posix/mutex.ixx +// 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/libodb/odb/details/posix/thread.cxx b/libodb/odb/details/posix/thread.cxx new file mode 100644 index 0000000..045f32a --- /dev/null +++ b/libodb/odb/details/posix/thread.cxx @@ -0,0 +1,44 @@ +// file : odb/details/posix/thread.cxx +// license : GNU GPL v2; see accompanying LICENSE file + +#include <odb/details/unique-ptr.hxx> +#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) + { + unique_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/libodb/odb/details/posix/thread.hxx b/libodb/odb/details/posix/thread.hxx new file mode 100644 index 0000000..f0d29a7 --- /dev/null +++ b/libodb/odb/details/posix/thread.hxx @@ -0,0 +1,41 @@ +// file : odb/details/posix/thread.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_DETAILS_POSIX_THREAD_HXX +#define ODB_DETAILS_POSIX_THREAD_HXX + +#include <odb/pre.hxx> + +#include <pthread.h> + +#include <odb/details/export.hxx> + +namespace odb +{ + namespace details + { + class LIBODB_EXPORT 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> + +#include <odb/post.hxx> + +#endif // ODB_DETAILS_POSIX_THREAD_HXX diff --git a/libodb/odb/details/posix/thread.ixx b/libodb/odb/details/posix/thread.ixx new file mode 100644 index 0000000..6576101 --- /dev/null +++ b/libodb/odb/details/posix/thread.ixx @@ -0,0 +1,29 @@ +// file : odb/details/posix/thread.ixx +// license : GNU GPL v2; see accompanying LICENSE file + +#include <odb/details/posix/exceptions.hxx> + +namespace odb +{ + namespace details + { + inline thread:: + ~thread () + { + if (!detached_) + pthread_detach (id_); + } + + inline void* thread:: + join () + { + void* r; + + if (int e = pthread_join (id_, &r)) + throw posix_exception (e); + + detached_ = true; + return r; + } + } +} diff --git a/libodb/odb/details/posix/tls.hxx b/libodb/odb/details/posix/tls.hxx new file mode 100644 index 0000000..e868819 --- /dev/null +++ b/libodb/odb/details/posix/tls.hxx @@ -0,0 +1,106 @@ +// file : odb/details/posix/tls.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_DETAILS_POSIX_TLS_HXX +#define ODB_DETAILS_POSIX_TLS_HXX + +#include <odb/pre.hxx> + +#include <pthread.h> + +namespace odb +{ + namespace details + { + template <typename T> + class tls + { + public: + tls (); + + T& + get () const; + + void + free (); + + 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>& t) + { + return t.get (); + } + + template <typename T> + inline void + tls_free (tls<T>& t) + { + t.free (); + } + + template <typename T> + inline T* + tls_get (const tls<T*>& t) + { + return t.get (); + } + + template <typename T, typename T1> + inline void + tls_set (tls<T*>& t, T1* p1) + { + T* p (p1); + t.set (p); + } + } +} + +#include <odb/details/posix/tls.ixx> +#include <odb/details/posix/tls.txx> + +#include <odb/post.hxx> + +#endif // ODB_DETAILS_POSIX_TLS_HXX diff --git a/libodb/odb/details/posix/tls.ixx b/libodb/odb/details/posix/tls.ixx new file mode 100644 index 0000000..7acc173 --- /dev/null +++ b/libodb/odb/details/posix/tls.ixx @@ -0,0 +1,20 @@ +// file : odb/details/posix/tls.ixx +// 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/libodb/odb/details/posix/tls.txx b/libodb/odb/details/posix/tls.txx new file mode 100644 index 0000000..e4c5b8f --- /dev/null +++ b/libodb/odb/details/posix/tls.txx @@ -0,0 +1,121 @@ +// file : odb/details/posix/tls.txx +// license : GNU GPL v2; see accompanying LICENSE file + +#include <odb/details/unique-ptr.hxx> +#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_); + + if (void* v = pthread_getspecific (key_)) + return *static_cast<T*> (v); + + unique_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>:: + free () + { + int e (pthread_once (&once_, key_init)); + + if (e != 0 || error_ != 0) + throw posix_exception (e ? e : error_); + + if (void* v = pthread_getspecific (key_)) + { + if ((e = pthread_setspecific (key_, 0))) + throw posix_exception (e); + + delete static_cast<T*> (v); + } + } + + 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); + } + } +} |