/src/aspell/common/lock.hpp
Line | Count | Source (jump to first uncovered line) |
1 | | // File: lock.hpp |
2 | | // |
3 | | // Copyright (c) 2002,2003,2011 |
4 | | // Kevin Atkinson |
5 | | // |
6 | | // Permission to use, copy, modify, distribute and sell this software |
7 | | // and its documentation for any purpose is hereby granted without |
8 | | // fee, provided that the above copyright notice appear in all copies |
9 | | // and that both that copyright notice and this permission notice |
10 | | // appear in supporting documentation. Kevin Atkinson makes no |
11 | | // representations about the suitability of this software for any |
12 | | // purpose. It is provided "as is" without express or implied |
13 | | // warranty. |
14 | | |
15 | | #ifndef DISTRIBNET_LOCK__HPP |
16 | | #define DISTRIBNET_LOCK__HPP |
17 | | |
18 | | #include <assert.h> |
19 | | |
20 | | #include "settings.h" |
21 | | |
22 | | #ifdef USE_POSIX_MUTEX |
23 | | # include <pthread.h> |
24 | | #endif |
25 | | |
26 | | namespace acommon { |
27 | | |
28 | 62.5k | #define LOCK(l) const Lock the_lock(l); |
29 | | |
30 | | #ifdef USE_POSIX_MUTEX |
31 | | class Mutex { |
32 | | pthread_mutex_t l_; |
33 | | private: |
34 | | Mutex(const Mutex &); |
35 | | void operator=(const Mutex &); |
36 | | public: |
37 | 20 | Mutex() {pthread_mutex_init(&l_, 0);} |
38 | 0 | ~Mutex() {pthread_mutex_destroy(&l_);} |
39 | 63.9k | void lock() {pthread_mutex_lock(&l_);} |
40 | 63.9k | void unlock() {pthread_mutex_unlock(&l_);} |
41 | | }; |
42 | | #else |
43 | | class Mutex { |
44 | | private: |
45 | | Mutex(const Mutex &); |
46 | | void operator=(const Mutex &); |
47 | | public: |
48 | | Mutex() {} |
49 | | ~Mutex() {} |
50 | | void lock() {} |
51 | | void unlock() {} |
52 | | }; |
53 | | #endif |
54 | | |
55 | | class Lock { |
56 | | private: |
57 | | Lock(const Lock &); |
58 | | void operator= (const Lock &); |
59 | | Mutex * lock_; |
60 | | public: |
61 | 65.3k | Lock(Mutex * l) : lock_(l) {if (lock_) lock_->lock();} |
62 | 1.41k | void set(Mutex * l) {assert(!lock_); lock_ = l; if (lock_) lock_->lock();} |
63 | 2.83k | void release() {if (lock_) lock_->unlock(); lock_ = NULL;} |
64 | 65.3k | ~Lock() {if (lock_) lock_->unlock();} |
65 | | }; |
66 | | }; |
67 | | |
68 | | #endif |