Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
Benny Prijono | 844653c | 2008-12-23 17:27:53 +0000 | [diff] [blame] | 3 | * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com) |
| 4 | * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | #ifndef __PJ_LOCK_H__ |
| 21 | #define __PJ_LOCK_H__ |
| 22 | |
| 23 | /** |
| 24 | * @file lock.h |
| 25 | * @brief Higher abstraction for locking objects. |
| 26 | */ |
| 27 | #include <pj/types.h> |
| 28 | |
| 29 | PJ_BEGIN_DECL |
| 30 | |
| 31 | /** |
| 32 | * @defgroup PJ_LOCK Lock Objects |
| 33 | * @ingroup PJ_OS |
| 34 | * @{ |
| 35 | * |
| 36 | * <b>Lock Objects</b> are higher abstraction for different lock mechanisms. |
| 37 | * It offers the same API for manipulating different lock types (e.g. |
| 38 | * @ref PJ_MUTEX "mutex", @ref PJ_SEM "semaphores", or null locks). |
| 39 | * Because Lock Objects have the same API for different types of lock |
| 40 | * implementation, it can be passed around in function arguments. As the |
| 41 | * result, it can be used to control locking policy for a particular |
| 42 | * feature. |
| 43 | */ |
| 44 | |
| 45 | |
| 46 | /** |
| 47 | * Create simple, non recursive mutex lock object. |
| 48 | * |
| 49 | * @param pool Memory pool. |
| 50 | * @param name Lock object's name. |
| 51 | * @param lock Pointer to store the returned handle. |
| 52 | * |
| 53 | * @return PJ_SUCCESS or the appropriate error code. |
| 54 | */ |
| 55 | PJ_DECL(pj_status_t) pj_lock_create_simple_mutex( pj_pool_t *pool, |
| 56 | const char *name, |
| 57 | pj_lock_t **lock ); |
| 58 | |
| 59 | /** |
| 60 | * Create recursive mutex lock object. |
| 61 | * |
| 62 | * @param pool Memory pool. |
| 63 | * @param name Lock object's name. |
| 64 | * @param lock Pointer to store the returned handle. |
| 65 | * |
| 66 | * @return PJ_SUCCESS or the appropriate error code. |
| 67 | */ |
| 68 | PJ_DECL(pj_status_t) pj_lock_create_recursive_mutex( pj_pool_t *pool, |
| 69 | const char *name, |
| 70 | pj_lock_t **lock ); |
| 71 | |
| 72 | |
| 73 | /** |
| 74 | * Create NULL mutex. A NULL mutex doesn't actually have any synchronization |
| 75 | * object attached to it. |
| 76 | * |
| 77 | * @param pool Memory pool. |
| 78 | * @param name Lock object's name. |
| 79 | * @param lock Pointer to store the returned handle. |
| 80 | * |
| 81 | * @return PJ_SUCCESS or the appropriate error code. |
| 82 | */ |
| 83 | PJ_DECL(pj_status_t) pj_lock_create_null_mutex( pj_pool_t *pool, |
| 84 | const char *name, |
| 85 | pj_lock_t **lock ); |
| 86 | |
| 87 | |
| 88 | #if defined(PJ_HAS_SEMAPHORE) && PJ_HAS_SEMAPHORE != 0 |
| 89 | /** |
| 90 | * Create semaphore lock object. |
| 91 | * |
| 92 | * @param pool Memory pool. |
| 93 | * @param name Lock object's name. |
| 94 | * @param initial Initial value of the semaphore. |
| 95 | * @param max Maximum value of the semaphore. |
| 96 | * @param lock Pointer to store the returned handle. |
| 97 | * |
| 98 | * @return PJ_SUCCESS or the appropriate error code. |
| 99 | */ |
| 100 | PJ_DECL(pj_status_t) pj_lock_create_semaphore( pj_pool_t *pool, |
| 101 | const char *name, |
| 102 | unsigned initial, |
| 103 | unsigned max, |
| 104 | pj_lock_t **lock ); |
| 105 | |
| 106 | #endif /* PJ_HAS_SEMAPHORE */ |
| 107 | |
| 108 | /** |
| 109 | * Acquire lock on the specified lock object. |
| 110 | * |
| 111 | * @param lock The lock object. |
| 112 | * |
| 113 | * @return PJ_SUCCESS or the appropriate error code. |
| 114 | */ |
| 115 | PJ_DECL(pj_status_t) pj_lock_acquire( pj_lock_t *lock ); |
| 116 | |
| 117 | |
| 118 | /** |
| 119 | * Try to acquire lock on the specified lock object. |
| 120 | * |
| 121 | * @param lock The lock object. |
| 122 | * |
| 123 | * @return PJ_SUCCESS or the appropriate error code. |
| 124 | */ |
| 125 | PJ_DECL(pj_status_t) pj_lock_tryacquire( pj_lock_t *lock ); |
| 126 | |
| 127 | |
| 128 | /** |
| 129 | * Release lock on the specified lock object. |
| 130 | * |
| 131 | * @param lock The lock object. |
| 132 | * |
| 133 | * @return PJ_SUCCESS or the appropriate error code. |
| 134 | */ |
| 135 | PJ_DECL(pj_status_t) pj_lock_release( pj_lock_t *lock ); |
| 136 | |
| 137 | |
| 138 | /** |
| 139 | * Destroy the lock object. |
| 140 | * |
| 141 | * @param lock The lock object. |
| 142 | * |
| 143 | * @return PJ_SUCCESS or the appropriate error code. |
| 144 | */ |
| 145 | PJ_DECL(pj_status_t) pj_lock_destroy( pj_lock_t *lock ); |
| 146 | |
| 147 | |
| 148 | /** @} */ |
| 149 | |
| 150 | PJ_END_DECL |
| 151 | |
| 152 | |
| 153 | #endif /* __PJ_LOCK_H__ */ |
| 154 | |