Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
| 3 | * Copyright (C)2003-2006 Benny Prijono <benny@prijono.org> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | #ifndef __PJPP_LOCK_HPP__ |
| 20 | #define __PJPP_LOCK_HPP__ |
| 21 | |
| 22 | #include <pj++/types.hpp> |
| 23 | #include <pj/lock.h> |
| 24 | #include <pj++/pool.hpp> |
| 25 | |
| 26 | ////////////////////////////////////////////////////////////////////////////// |
| 27 | // Lock object. |
| 28 | // |
| 29 | class Pj_Lock : public Pj_Object |
| 30 | { |
| 31 | public: |
| 32 | // |
| 33 | // Constructor. |
| 34 | // |
| 35 | explicit Pj_Lock(pj_lock_t *lock) |
| 36 | : lock_(lock) |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | // |
| 41 | // Destructor. |
| 42 | // |
| 43 | ~Pj_Lock() |
| 44 | { |
| 45 | if (lock_) |
| 46 | pj_lock_destroy(lock_); |
| 47 | } |
| 48 | |
| 49 | // |
| 50 | // Get pjlib compatible lock object. |
| 51 | // |
| 52 | pj_lock_t *pj_lock_t_() |
| 53 | { |
| 54 | return lock_; |
| 55 | } |
| 56 | |
| 57 | // |
| 58 | // acquire lock. |
| 59 | // |
| 60 | pj_status_t acquire() |
| 61 | { |
| 62 | return pj_lock_acquire(lock_); |
| 63 | } |
| 64 | |
| 65 | // |
| 66 | // release lock,. |
| 67 | // |
| 68 | pj_status_t release() |
| 69 | { |
| 70 | return pj_lock_release(lock_); |
| 71 | } |
| 72 | |
| 73 | protected: |
| 74 | pj_lock_t *lock_; |
| 75 | }; |
| 76 | |
| 77 | |
| 78 | ////////////////////////////////////////////////////////////////////////////// |
| 79 | // Null lock object. |
| 80 | // |
| 81 | class Pj_Null_Lock : public Pj_Lock |
| 82 | { |
| 83 | public: |
| 84 | // |
| 85 | // Default constructor. |
| 86 | // |
| 87 | explicit Pj_Null_Lock(Pj_Pool *pool, const char *name = NULL) |
| 88 | : Pj_Lock(NULL) |
| 89 | { |
| 90 | pj_lock_create_null_mutex(pool->pool_(), name, &lock_); |
| 91 | } |
| 92 | }; |
| 93 | |
| 94 | ////////////////////////////////////////////////////////////////////////////// |
| 95 | // Simple mutex lock object. |
| 96 | // |
| 97 | class Pj_Simple_Mutex_Lock : public Pj_Lock |
| 98 | { |
| 99 | public: |
| 100 | // |
| 101 | // Default constructor. |
| 102 | // |
| 103 | explicit Pj_Simple_Mutex_Lock(Pj_Pool *pool, const char *name = NULL) |
| 104 | : Pj_Lock(NULL) |
| 105 | { |
| 106 | pj_lock_create_simple_mutex(pool->pool_(), name, &lock_); |
| 107 | } |
| 108 | }; |
| 109 | |
| 110 | ////////////////////////////////////////////////////////////////////////////// |
| 111 | // Recursive mutex lock object. |
| 112 | // |
| 113 | class Pj_Recursive_Mutex_Lock : public Pj_Lock |
| 114 | { |
| 115 | public: |
| 116 | // |
| 117 | // Default constructor. |
| 118 | // |
| 119 | explicit Pj_Recursive_Mutex_Lock(Pj_Pool *pool, const char *name = NULL) |
| 120 | : Pj_Lock(NULL) |
| 121 | { |
| 122 | pj_lock_create_recursive_mutex(pool->pool_(), name, &lock_); |
| 123 | } |
| 124 | }; |
| 125 | |
| 126 | ////////////////////////////////////////////////////////////////////////////// |
| 127 | // Semaphore lock object. |
| 128 | // |
| 129 | class Pj_Semaphore_Lock : public Pj_Lock |
| 130 | { |
| 131 | public: |
| 132 | // |
| 133 | // Default constructor. |
| 134 | // |
| 135 | explicit Pj_Semaphore_Lock(Pj_Pool *pool, |
| 136 | unsigned max=PJ_MAXINT32, |
| 137 | unsigned initial=0, |
| 138 | const char *name=NULL) |
| 139 | : Pj_Lock(NULL) |
| 140 | { |
| 141 | pj_lock_create_semaphore(pool->pool_(), name, initial, max, &lock_); |
| 142 | } |
| 143 | }; |
| 144 | |
| 145 | |
| 146 | |
| 147 | #endif /* __PJPP_LOCK_HPP__ */ |
| 148 | |