diff options
Diffstat (limited to 'odb/details/win32')
-rw-r--r-- | odb/details/win32/condition.cxx | 57 | ||||
-rw-r--r-- | odb/details/win32/condition.hxx | 52 | ||||
-rw-r--r-- | odb/details/win32/condition.ixx | 32 | ||||
-rw-r--r-- | odb/details/win32/exceptions.cxx | 18 | ||||
-rw-r--r-- | odb/details/win32/exceptions.hxx | 39 | ||||
-rw-r--r-- | odb/details/win32/mutex.hxx | 46 | ||||
-rw-r--r-- | odb/details/win32/mutex.ixx | 34 | ||||
-rw-r--r-- | odb/details/win32/thread.cxx | 91 | ||||
-rw-r--r-- | odb/details/win32/thread.hxx | 62 |
9 files changed, 431 insertions, 0 deletions
diff --git a/odb/details/win32/condition.cxx b/odb/details/win32/condition.cxx new file mode 100644 index 0000000..93c396b --- /dev/null +++ b/odb/details/win32/condition.cxx @@ -0,0 +1,57 @@ +// file : odb/details/win32/condition.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 <windows.h> + +#include <odb/details/win32/condition.hxx> +#include <odb/details/win32/exceptions.hxx> + +namespace odb +{ + namespace details + { + void condition:: + signal () + { + mutex_.lock (); + + if (waiters_ > signals_) + { + if (signals_++ == 0) + { + if (SetEvent (event_) == 0) + throw win32_exception (); + } + } + + mutex_.unlock (); + } + + void condition:: + wait () + { + // When we enter this functions the mutex is locked. When we + // return from this function the mutex must be locked. + // + waiters_++; + mutex_.unlock (); + + if (WaitForSingleObject (event_, INFINITE) != 0) + throw win32_exception (); + + mutex_.lock (); + waiters_--; + signals_--; + + if (signals_ > 0) + { + // Wake up the next thread. + // + if (SetEvent (event_) == 0) + throw win32_exception (); + } + } + } +} diff --git a/odb/details/win32/condition.hxx b/odb/details/win32/condition.hxx new file mode 100644 index 0000000..3c40f3a --- /dev/null +++ b/odb/details/win32/condition.hxx @@ -0,0 +1,52 @@ +// file : odb/details/win32/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_WIN32_CONDITION_HXX +#define ODB_DETAILS_WIN32_CONDITION_HXX + +#include <odb/pre.hxx> + +#include <windows.h> + +#include <cstddef> // std::size_t + +#include <odb/details/export.hxx> +#include <odb/details/win32/mutex.hxx> + +namespace odb +{ + namespace details + { + class LIBODB_EXPORT condition + { + public: + ~condition (); + condition (mutex&); + + void + signal (); + + void + wait (); + + private: + condition (const condition&); + condition& operator= (const condition&); + + private: + mutex& mutex_; + HANDLE event_; + + std::size_t waiters_; + std::size_t signals_; + }; + } +} + +#include <odb/details/win32/condition.ixx> + +#include <odb/post.hxx> + +#endif // ODB_DETAILS_WIN32_CONDITION_HXX diff --git a/odb/details/win32/condition.ixx b/odb/details/win32/condition.ixx new file mode 100644 index 0000000..4a6bd1d --- /dev/null +++ b/odb/details/win32/condition.ixx @@ -0,0 +1,32 @@ +// file : odb/details/win32/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/win32/exceptions.hxx> + +namespace odb +{ + namespace details + { + inline condition:: + ~condition () + { + CloseHandle (event_); + } + + inline condition:: + condition (mutex& mutex) + : mutex_ (mutex), waiters_ (0), signals_ (0) + { + // Auto-reset event. Releases one waiting thread and automatically + // resets the event state. If no threads are waiting the event + // remains signalled. + // + event_ = CreateEvent (0, false, false, 0); + + if (event_ == 0) + throw win32_exception (); + } + } +} diff --git a/odb/details/win32/exceptions.cxx b/odb/details/win32/exceptions.cxx new file mode 100644 index 0000000..e8652cd --- /dev/null +++ b/odb/details/win32/exceptions.cxx @@ -0,0 +1,18 @@ +// file : odb/details/win32/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/win32/exceptions.hxx> + +namespace odb +{ + namespace details + { + const char* win32_exception:: + what () const throw () + { + return "Win32 API error"; + } + } +} diff --git a/odb/details/win32/exceptions.hxx b/odb/details/win32/exceptions.hxx new file mode 100644 index 0000000..b9ccf50 --- /dev/null +++ b/odb/details/win32/exceptions.hxx @@ -0,0 +1,39 @@ +// file : odb/details/win32/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_WIN32_EXCEPTIONS_HXX +#define ODB_DETAILS_WIN32_EXCEPTIONS_HXX + +#include <odb/pre.hxx> + +#include <windows.h> + +#include <odb/details/export.hxx> +#include <odb/details/exception.hxx> + +namespace odb +{ + namespace details + { + struct LIBODB_EXPORT win32_exception: details::exception + { + win32_exception () : code_ (GetLastError ()) {} + win32_exception (DWORD code) : code_ (code) {} + + DWORD + code () const {return code_;} + + virtual const char* + what () const throw (); + + private: + DWORD code_; + }; + } +} + +#include <odb/post.hxx> + +#endif // ODB_DETAILS_WIN32_EXCEPTIONS_HXX diff --git a/odb/details/win32/mutex.hxx b/odb/details/win32/mutex.hxx new file mode 100644 index 0000000..9dbe3a8 --- /dev/null +++ b/odb/details/win32/mutex.hxx @@ -0,0 +1,46 @@ +// file : odb/details/win32/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_WIN32_MUTEX_HXX +#define ODB_DETAILS_WIN32_MUTEX_HXX + +#include <odb/pre.hxx> + +#include <windows.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; + CRITICAL_SECTION cs_; + }; + } +} + +#include <odb/details/win32/mutex.ixx> + +#include <odb/post.hxx> + +#endif // ODB_DETAILS_WIN32_MUTEX_HXX diff --git a/odb/details/win32/mutex.ixx b/odb/details/win32/mutex.ixx new file mode 100644 index 0000000..a2b6d37 --- /dev/null +++ b/odb/details/win32/mutex.ixx @@ -0,0 +1,34 @@ +// file : odb/details/win32/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 + +namespace odb +{ + namespace details + { + inline mutex:: + ~mutex () + { + DeleteCriticalSection (&cs_); + } + + inline mutex:: + mutex () + { + InitializeCriticalSection (&cs_); + } + + inline void mutex:: + lock () + { + EnterCriticalSection (&cs_); + } + + inline void mutex:: + unlock () + { + LeaveCriticalSection (&cs_); + } + } +} diff --git a/odb/details/win32/thread.cxx b/odb/details/win32/thread.cxx new file mode 100644 index 0000000..610980d --- /dev/null +++ b/odb/details/win32/thread.cxx @@ -0,0 +1,91 @@ +// file : odb/details/win32/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 <windows.h> +#include <process.h> // _beginthreadex, _endthreadex + +#include <memory> // std::auto_ptr + +#include <odb/details/win32/thread.hxx> +#include <odb/details/win32/exceptions.hxx> + +unsigned int __stdcall +odb_thread_thunk (void* arg) +{ + odb::details::thread::thread_thunk (arg); + _endthreadex (0); + return 0; +} + +namespace odb +{ + namespace details + { + void thread:: + thread_thunk (void* arg) + { + data* d (static_cast<data*> (arg)); + d->ret = d->func (d->arg); + d->mutex.lock (); + unsigned char count = --d->count; + d->mutex.unlock (); + + if (count == 0) + delete d; + } + + thread:: + ~thread () + { + if (handle_ != 0) + { + CloseHandle (handle_); + + // Win32 mutex implementation does not throw. + // + data_->mutex.lock (); + unsigned char count = --data_->count; + data_->mutex.unlock (); + + if (count == 0) + delete data_; + } + } + + thread:: + thread (void* (*func) (void*), void* arg) + { + std::auto_ptr<data> d (new data); + d->func = func; + d->arg = arg; + d->count = 2; // One for the thread and one for us. + + handle_ = (HANDLE)_beginthreadex ( + 0, 0, &odb_thread_thunk, d.get (), 0, 0); + + if (handle_ == 0) + throw win32_exception (); + + data_ = d.release (); + } + + void* thread:: + join () + { + void* r; + + if (WaitForSingleObject (handle_, INFINITE) != 0) + throw win32_exception (); + + r = data_->ret; + + CloseHandle (handle_); + delete data_; + handle_ = 0; + data_ = 0; + return r; + } + } +} diff --git a/odb/details/win32/thread.hxx b/odb/details/win32/thread.hxx new file mode 100644 index 0000000..d52de48 --- /dev/null +++ b/odb/details/win32/thread.hxx @@ -0,0 +1,62 @@ +// file : odb/details/win32/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_WIN32_THREAD_HXX +#define ODB_DETAILS_WIN32_THREAD_HXX + +#include <odb/pre.hxx> + +#include <windows.h> + +#include <odb/details/export.hxx> +#include <odb/details/win32/mutex.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: + typedef void* (*thread_func) (void*); + + struct data + { + thread_func func; + void* arg; + void* ret; + + // Thread-safe reference counter. + // + details::mutex mutex; + unsigned char count; + }; + + + public: + static void + thread_thunk (void*); + + private: + HANDLE handle_; + data* data_; + }; + } +} + +#include <odb/post.hxx> + +#endif // ODB_DETAILS_WIN32_THREAD_HXX |