Benny Prijono | f010e69 | 2005-11-11 19:01:31 +0000 | [diff] [blame] | 1 | /* $Id$ |
| 2 | */ |
| 3 | #ifndef __PJPP_LOCK_HPP__ |
| 4 | #define __PJPP_LOCK_HPP__ |
| 5 | |
| 6 | #include <pj++/types.hpp> |
| 7 | #include <pj/lock.h> |
| 8 | #include <pj++/pool.hpp> |
| 9 | |
| 10 | ////////////////////////////////////////////////////////////////////////////// |
| 11 | // Lock object. |
| 12 | // |
| 13 | class Pj_Lock : public Pj_Object |
| 14 | { |
| 15 | public: |
| 16 | // |
| 17 | // Constructor. |
| 18 | // |
| 19 | explicit Pj_Lock(pj_lock_t *lock) |
| 20 | : lock_(lock) |
| 21 | { |
| 22 | } |
| 23 | |
| 24 | // |
| 25 | // Destructor. |
| 26 | // |
| 27 | ~Pj_Lock() |
| 28 | { |
| 29 | if (lock_) |
| 30 | pj_lock_destroy(lock_); |
| 31 | } |
| 32 | |
| 33 | // |
| 34 | // Get pjlib compatible lock object. |
| 35 | // |
| 36 | pj_lock_t *pj_lock_t_() |
| 37 | { |
| 38 | return lock_; |
| 39 | } |
| 40 | |
| 41 | // |
| 42 | // acquire lock. |
| 43 | // |
| 44 | pj_status_t acquire() |
| 45 | { |
| 46 | return pj_lock_acquire(lock_); |
| 47 | } |
| 48 | |
| 49 | // |
| 50 | // release lock,. |
| 51 | // |
| 52 | pj_status_t release() |
| 53 | { |
| 54 | return pj_lock_release(lock_); |
| 55 | } |
| 56 | |
| 57 | protected: |
| 58 | pj_lock_t *lock_; |
| 59 | }; |
| 60 | |
| 61 | |
| 62 | ////////////////////////////////////////////////////////////////////////////// |
| 63 | // Null lock object. |
| 64 | // |
| 65 | class Pj_Null_Lock : public Pj_Lock |
| 66 | { |
| 67 | public: |
| 68 | // |
| 69 | // Default constructor. |
| 70 | // |
| 71 | explicit Pj_Null_Lock(Pj_Pool *pool, const char *name = NULL) |
| 72 | : Pj_Lock(NULL) |
| 73 | { |
| 74 | pj_lock_create_null_mutex(pool->pool_(), name, &lock_); |
| 75 | } |
| 76 | }; |
| 77 | |
| 78 | ////////////////////////////////////////////////////////////////////////////// |
| 79 | // Simple mutex lock object. |
| 80 | // |
| 81 | class Pj_Simple_Mutex_Lock : public Pj_Lock |
| 82 | { |
| 83 | public: |
| 84 | // |
| 85 | // Default constructor. |
| 86 | // |
| 87 | explicit Pj_Simple_Mutex_Lock(Pj_Pool *pool, const char *name = NULL) |
| 88 | : Pj_Lock(NULL) |
| 89 | { |
| 90 | pj_lock_create_simple_mutex(pool->pool_(), name, &lock_); |
| 91 | } |
| 92 | }; |
| 93 | |
| 94 | ////////////////////////////////////////////////////////////////////////////// |
| 95 | // Recursive mutex lock object. |
| 96 | // |
| 97 | class Pj_Recursive_Mutex_Lock : public Pj_Lock |
| 98 | { |
| 99 | public: |
| 100 | // |
| 101 | // Default constructor. |
| 102 | // |
| 103 | explicit Pj_Recursive_Mutex_Lock(Pj_Pool *pool, const char *name = NULL) |
| 104 | : Pj_Lock(NULL) |
| 105 | { |
| 106 | pj_lock_create_recursive_mutex(pool->pool_(), name, &lock_); |
| 107 | } |
| 108 | }; |
| 109 | |
| 110 | ////////////////////////////////////////////////////////////////////////////// |
| 111 | // Semaphore lock object. |
| 112 | // |
| 113 | class Pj_Semaphore_Lock : public Pj_Lock |
| 114 | { |
| 115 | public: |
| 116 | // |
| 117 | // Default constructor. |
| 118 | // |
| 119 | explicit Pj_Semaphore_Lock(Pj_Pool *pool, |
| 120 | unsigned max=PJ_MAXINT32, |
| 121 | unsigned initial=0, |
| 122 | const char *name=NULL) |
| 123 | : Pj_Lock(NULL) |
| 124 | { |
| 125 | pj_lock_create_semaphore(pool->pool_(), name, initial, max, &lock_); |
| 126 | } |
| 127 | }; |
| 128 | |
| 129 | |
| 130 | |
| 131 | #endif /* __PJPP_LOCK_HPP__ */ |
| 132 | |