| 1 | /////////////////////////////////////////////////////////////////////////////// |
| 2 | // |
| 3 | // Copyright (C) 2010-2011 Artyom Beilis (Tonkikh) <[email protected]> |
| 4 | // |
| 5 | // Distributed under: |
| 6 | // |
| 7 | // the Boost Software License, Version 1.0. |
| 8 | // (See accompanying file LICENSE_1_0.txt or copy at |
| 9 | // http://www.boost.org/LICENSE_1_0.txt) |
| 10 | // |
| 11 | // or (at your opinion) under: |
| 12 | // |
| 13 | // The MIT License |
| 14 | // (See accompanying file MIT.txt or a copy at |
| 15 | // http://www.opensource.org/licenses/mit-license.php) |
| 16 | // |
| 17 | /////////////////////////////////////////////////////////////////////////////// |
| 18 | #define CPPDB_SOURCE |
| 19 | #include <cppdb/mutex.h> |
| 20 | |
| 21 | #if defined(WIN32) || defined(_WIN32) || defined(__WIN32) |
| 22 | |
| 23 | # include <windows.h> |
| 24 | |
| 25 | # ifdef impl_ |
| 26 | # undef impl_ |
| 27 | # endif |
| 28 | |
| 29 | # define impl_ ((CRITICAL_SECTION *)mutex_impl_) |
| 30 | |
| 31 | namespace cppdb { |
| 32 | mutex::mutex() : mutex_impl_(0) |
| 33 | { |
| 34 | mutex_impl_ = new CRITICAL_SECTION(); |
| 35 | InitializeCriticalSection(impl_); |
| 36 | } |
| 37 | mutex::~mutex() |
| 38 | { |
| 39 | DeleteCriticalSection(impl_); |
| 40 | delete impl_; |
| 41 | } |
| 42 | void mutex::lock() |
| 43 | { |
| 44 | EnterCriticalSection(impl_); |
| 45 | } |
| 46 | void mutex::unlock() |
| 47 | { |
| 48 | LeaveCriticalSection(impl_); |
| 49 | } |
| 50 | |
| 51 | } |
| 52 | |
| 53 | |
| 54 | #else // POSIX |
| 55 | |
| 56 | # include <pthread.h> |
| 57 | |
| 58 | # ifdef impl_ |
| 59 | # undef impl_ |
| 60 | # endif |
| 61 | |
| 62 | # define impl_ ((pthread_mutex_t *)mutex_impl_) |
| 63 | |
| 64 | namespace cppdb { |
| 65 | mutex::mutex() : mutex_impl_(0) |
| 66 | { |
| 67 | mutex_impl_ = new pthread_mutex_t(); |
| 68 | pthread_mutex_init(impl_,0); |
| 69 | } |
| 70 | mutex::~mutex() |
| 71 | { |
| 72 | pthread_mutex_destroy(impl_); |
| 73 | delete impl_; |
| 74 | } |
| 75 | void mutex::lock() |
| 76 | { |
| 77 | pthread_mutex_lock(impl_); |
| 78 | } |
| 79 | void mutex::unlock() |
| 80 | { |
| 81 | pthread_mutex_unlock(impl_); |
| 82 | } |
| 83 | |
| 84 | } |
| 85 | |
| 86 | #endif |