blob: 96c9e11df3690d38e1e4dea5781c1756b52cbc25 [file] [log] [blame]
Benny Prijono9033e312005-11-21 02:08:39 +00001/* $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 __PJ_LOCK_H__
20#define __PJ_LOCK_H__
21
22/**
23 * @file lock.h
24 * @brief Higher abstraction for locking objects.
25 */
26#include <pj/types.h>
27
28PJ_BEGIN_DECL
29
30/**
31 * @defgroup PJ_LOCK Lock Objects
32 * @ingroup PJ_OS
33 * @{
34 *
35 * <b>Lock Objects</b> are higher abstraction for different lock mechanisms.
36 * It offers the same API for manipulating different lock types (e.g.
37 * @ref PJ_MUTEX "mutex", @ref PJ_SEM "semaphores", or null locks).
38 * Because Lock Objects have the same API for different types of lock
39 * implementation, it can be passed around in function arguments. As the
40 * result, it can be used to control locking policy for a particular
41 * feature.
42 */
43
44
45/**
46 * Create simple, non recursive mutex lock object.
47 *
48 * @param pool Memory pool.
49 * @param name Lock object's name.
50 * @param lock Pointer to store the returned handle.
51 *
52 * @return PJ_SUCCESS or the appropriate error code.
53 */
54PJ_DECL(pj_status_t) pj_lock_create_simple_mutex( pj_pool_t *pool,
55 const char *name,
56 pj_lock_t **lock );
57
58/**
59 * Create recursive mutex lock object.
60 *
61 * @param pool Memory pool.
62 * @param name Lock object's name.
63 * @param lock Pointer to store the returned handle.
64 *
65 * @return PJ_SUCCESS or the appropriate error code.
66 */
67PJ_DECL(pj_status_t) pj_lock_create_recursive_mutex( pj_pool_t *pool,
68 const char *name,
69 pj_lock_t **lock );
70
71
72/**
73 * Create NULL mutex. A NULL mutex doesn't actually have any synchronization
74 * object attached to it.
75 *
76 * @param pool Memory pool.
77 * @param name Lock object's name.
78 * @param lock Pointer to store the returned handle.
79 *
80 * @return PJ_SUCCESS or the appropriate error code.
81 */
82PJ_DECL(pj_status_t) pj_lock_create_null_mutex( pj_pool_t *pool,
83 const char *name,
84 pj_lock_t **lock );
85
86
87#if defined(PJ_HAS_SEMAPHORE) && PJ_HAS_SEMAPHORE != 0
88/**
89 * Create semaphore lock object.
90 *
91 * @param pool Memory pool.
92 * @param name Lock object's name.
93 * @param initial Initial value of the semaphore.
94 * @param max Maximum value of the semaphore.
95 * @param lock Pointer to store the returned handle.
96 *
97 * @return PJ_SUCCESS or the appropriate error code.
98 */
99PJ_DECL(pj_status_t) pj_lock_create_semaphore( pj_pool_t *pool,
100 const char *name,
101 unsigned initial,
102 unsigned max,
103 pj_lock_t **lock );
104
105#endif /* PJ_HAS_SEMAPHORE */
106
107/**
108 * Acquire lock on the specified lock object.
109 *
110 * @param lock The lock object.
111 *
112 * @return PJ_SUCCESS or the appropriate error code.
113 */
114PJ_DECL(pj_status_t) pj_lock_acquire( pj_lock_t *lock );
115
116
117/**
118 * Try to acquire lock on the specified lock object.
119 *
120 * @param lock The lock object.
121 *
122 * @return PJ_SUCCESS or the appropriate error code.
123 */
124PJ_DECL(pj_status_t) pj_lock_tryacquire( pj_lock_t *lock );
125
126
127/**
128 * Release lock on the specified lock object.
129 *
130 * @param lock The lock object.
131 *
132 * @return PJ_SUCCESS or the appropriate error code.
133 */
134PJ_DECL(pj_status_t) pj_lock_release( pj_lock_t *lock );
135
136
137/**
138 * Destroy the lock object.
139 *
140 * @param lock The lock object.
141 *
142 * @return PJ_SUCCESS or the appropriate error code.
143 */
144PJ_DECL(pj_status_t) pj_lock_destroy( pj_lock_t *lock );
145
146
147/** @} */
148
149PJ_END_DECL
150
151
152#endif /* __PJ_LOCK_H__ */
153