| 1 | /* -*- C++ -*- */ |
| 2 | |
| 3 | /* |
| 4 | |
| 5 | Heap Layers: An Extensible Memory Allocation Infrastructure |
| 6 | |
| 7 | Copyright (C) 2000-2020 by Emery Berger |
| 8 | http://www.emeryberger.com |
| 9 | [email protected] |
| 10 | |
| 11 | Heap Layers is distributed under the terms of the Apache 2.0 license. |
| 12 | |
| 13 | You may obtain a copy of the License at |
| 14 | http://www.apache.org/licenses/LICENSE-2.0 |
| 15 | |
| 16 | */ |
| 17 | |
| 18 | #ifndef HL_POSIXLOCK_H |
| 19 | #define HL_POSIXLOCK_H |
| 20 | |
| 21 | #if !defined(_WIN32) |
| 22 | |
| 23 | #include <pthread.h> |
| 24 | |
| 25 | /** |
| 26 | * @class PosixLockType |
| 27 | * @brief Locking using POSIX mutex objects. |
| 28 | */ |
| 29 | |
| 30 | namespace HL { |
| 31 | |
| 32 | class PosixLockType { |
| 33 | public: |
| 34 | |
| 35 | PosixLockType (void) |
| 36 | { |
| 37 | int r = pthread_mutex_init (&mutex, NULL); |
| 38 | if (r) { |
| 39 | throw 0; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | ~PosixLockType (void) |
| 44 | { |
| 45 | pthread_mutex_destroy (&mutex); |
| 46 | } |
| 47 | |
| 48 | void lock (void) { |
| 49 | pthread_mutex_lock (&mutex); |
| 50 | } |
| 51 | |
| 52 | void unlock (void) { |
| 53 | pthread_mutex_unlock (&mutex); |
| 54 | } |
| 55 | |
| 56 | private: |
| 57 | union { |
| 58 | pthread_mutex_t mutex; |
| 59 | double _dummy[sizeof(pthread_mutex_t)/sizeof(double) + 1]; |
| 60 | }; |
| 61 | }; |
| 62 | |
| 63 | typedef PosixLockType PosixLock; |
| 64 | |
| 65 | } |
| 66 | |
| 67 | #endif |
| 68 | |
| 69 | #endif |