1
/* $Id: lock.c 3553 2011-05-05 06:14:19Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
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.
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.
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
22
#include <pj/assert.h>
24
#include <pj/string.h>
28
typedef void LOCK_OBJ;
35
LOCK_OBJ *lock_object;
37
pj_status_t (*acquire) (LOCK_OBJ*);
38
pj_status_t (*tryacquire) (LOCK_OBJ*);
39
pj_status_t (*release) (LOCK_OBJ*);
40
pj_status_t (*destroy) (LOCK_OBJ*);
43
typedef pj_status_t (*FPTR)(LOCK_OBJ*);
45
/******************************************************************************
46
* Implementation of lock object with mutex.
48
static pj_lock_t mutex_lock_template =
51
(FPTR) &pj_mutex_lock,
52
(FPTR) &pj_mutex_trylock,
53
(FPTR) &pj_mutex_unlock,
54
(FPTR) &pj_mutex_destroy
57
static pj_status_t create_mutex_lock( pj_pool_t *pool,
66
PJ_ASSERT_RETURN(pool && lock, PJ_EINVAL);
68
p_lock = PJ_POOL_ALLOC_T(pool, pj_lock_t);
72
pj_memcpy(p_lock, &mutex_lock_template, sizeof(pj_lock_t));
73
rc = pj_mutex_create(pool, name, type, &mutex);
77
p_lock->lock_object = mutex;
83
PJ_DEF(pj_status_t) pj_lock_create_simple_mutex( pj_pool_t *pool,
87
return create_mutex_lock(pool, name, PJ_MUTEX_SIMPLE, lock);
90
PJ_DEF(pj_status_t) pj_lock_create_recursive_mutex( pj_pool_t *pool,
94
return create_mutex_lock(pool, name, PJ_MUTEX_RECURSE, lock);
98
/******************************************************************************
99
* Implementation of NULL lock object.
101
static pj_status_t null_op(void *arg)
107
static pj_lock_t null_lock_template =
116
PJ_DEF(pj_status_t) pj_lock_create_null_mutex( pj_pool_t *pool,
123
PJ_ASSERT_RETURN(lock, PJ_EINVAL);
125
*lock = &null_lock_template;
130
/******************************************************************************
131
* Implementation of semaphore lock object.
133
#if defined(PJ_HAS_SEMAPHORE) && PJ_HAS_SEMAPHORE != 0
135
static pj_lock_t sem_lock_template =
139
(FPTR) &pj_sem_trywait,
141
(FPTR) &pj_sem_destroy
144
PJ_DEF(pj_status_t) pj_lock_create_semaphore( pj_pool_t *pool,
154
PJ_ASSERT_RETURN(pool && lock, PJ_EINVAL);
156
p_lock = PJ_POOL_ALLOC_T(pool, pj_lock_t);
160
pj_memcpy(p_lock, &sem_lock_template, sizeof(pj_lock_t));
161
rc = pj_sem_create( pool, name, initial, max, &sem);
162
if (rc != PJ_SUCCESS)
165
p_lock->lock_object = sem;
172
#endif /* PJ_HAS_SEMAPHORE */
175
PJ_DEF(pj_status_t) pj_lock_acquire( pj_lock_t *lock )
177
PJ_ASSERT_RETURN(lock != NULL, PJ_EINVAL);
178
return (*lock->acquire)(lock->lock_object);
181
PJ_DEF(pj_status_t) pj_lock_tryacquire( pj_lock_t *lock )
183
PJ_ASSERT_RETURN(lock != NULL, PJ_EINVAL);
184
return (*lock->tryacquire)(lock->lock_object);
187
PJ_DEF(pj_status_t) pj_lock_release( pj_lock_t *lock )
189
PJ_ASSERT_RETURN(lock != NULL, PJ_EINVAL);
190
return (*lock->release)(lock->lock_object);
193
PJ_DEF(pj_status_t) pj_lock_destroy( pj_lock_t *lock )
195
PJ_ASSERT_RETURN(lock != NULL, PJ_EINVAL);
196
return (*lock->destroy)(lock->lock_object);