forked from Mirrors/freeswitch
b7c6c0e483
Thu Nov 15 08:40:38 EST 2007 Pekka.Pessi@nokia.com * htable2.h: fixed usize_t/size_t confusion Fri Nov 16 06:38:51 EST 2007 Youness Alaoui <youness.alaoui@collabora.co.uk> * Added #include <stdio.h> Fri Nov 16 10:27:58 EST 2007 Pekka.Pessi@nokia.com * auth_client.c: allow multiple challenges in auc_credentials() or auc_all_credentials() Fri Nov 16 10:29:00 EST 2007 Pekka.Pessi@nokia.com * nua/test_proxy.[hc], nua/test_register.c: test support of multiple realms. Fri Nov 16 11:17:09 EST 2007 Pekka.Pessi@nokia.com * sofia-sip/su_alloc.h, su_alloc.c: added su_home_lock(), su_home_trylock(), su_home_unlock() Added test in torture_su_alloc.c. Using in su_pthread_port.c. Fri Nov 16 12:29:55 EST 2007 Pekka.Pessi@nokia.com * test_register.c, test_proxy.c: use realm "test-proxy" during normal tests Fri Nov 16 12:34:00 EST 2007 Pekka.Pessi@nokia.com * nua_register.c: sf.net bug #1816647: Outbound contact does not make it to dialogs Now use Contact from first registration instead of Contact generated from transport. Mon Nov 19 12:00:06 EST 2007 Pekka Pessi <Pekka.Pessi@nokia.com> * su_alloc.c: silenced warnings on Sun CC git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@6338 d0543943-73ff-0310-b7d9-9358b9ac24b2
135 lines
3.4 KiB
C
135 lines
3.4 KiB
C
/*
|
|
* This file is part of the Sofia-SIP package
|
|
*
|
|
* Copyright (C) 2005 Nokia Corporation.
|
|
*
|
|
* Contact: Pekka Pessi <pekka.pessi@nokia.com>
|
|
*
|
|
* This library is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU Lesser General Public License
|
|
* as published by the Free Software Foundation; either version 2.1 of
|
|
* the License, or (at your option) any later version.
|
|
*
|
|
* This library is distributed in the hope that it will be useful, but
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
* Lesser General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
* License along with this library; if not, write to the Free Software
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
|
|
* 02110-1301 USA
|
|
*
|
|
*/
|
|
|
|
/**@ingroup su_alloc
|
|
* @CFILE su_alloc_lock.c
|
|
* @brief Thread-locking for su_alloc module.
|
|
*
|
|
* @author Pekka Pessi <Pekka.Pessi@nokia.com>
|
|
*
|
|
* @date Created: Fri Feb 23 17:38:11 2001 ppessi
|
|
*/
|
|
|
|
#include "config.h"
|
|
|
|
#include <sofia-sip/su_alloc.h>
|
|
#include <sofia-sip/su.h>
|
|
|
|
#if SU_HAVE_PTHREADS
|
|
#include <pthread.h>
|
|
#include <assert.h>
|
|
#include <stdlib.h>
|
|
|
|
extern int (*_su_home_locker)(void *mutex);
|
|
extern int (*_su_home_unlocker)(void *mutex);
|
|
|
|
extern int (*_su_home_mutex_locker)(void *mutex);
|
|
extern int (*_su_home_mutex_trylocker)(void *mutex);
|
|
extern int (*_su_home_mutex_unlocker)(void *mutex);
|
|
|
|
extern void (*_su_home_destroy_mutexes)(void *mutex);
|
|
|
|
/** Mutex */
|
|
static int mutex_locker(void *_mutex)
|
|
{
|
|
pthread_mutex_t *mutex = _mutex;
|
|
return pthread_mutex_lock(mutex + 1);
|
|
}
|
|
|
|
int mutex_trylocker(void *_mutex)
|
|
{
|
|
pthread_mutex_t *mutex = _mutex;
|
|
return pthread_mutex_trylock(mutex + 1);
|
|
}
|
|
|
|
static int mutex_unlocker(void *_mutex)
|
|
{
|
|
pthread_mutex_t *mutex = _mutex;
|
|
return pthread_mutex_unlock(mutex + 1);
|
|
}
|
|
|
|
static void mutex_destroy(void *_mutex)
|
|
{
|
|
pthread_mutex_t *mutex = _mutex;
|
|
pthread_mutex_destroy(mutex + 0);
|
|
pthread_mutex_destroy(mutex + 1);
|
|
free(_mutex);
|
|
}
|
|
#endif
|
|
|
|
|
|
/** Convert su_home_t object to a thread-safe one.
|
|
*
|
|
* Convert a memory home object as thread-safe by allocating mutexes and
|
|
* modifying function pointers in su_alloc.c module.
|
|
*
|
|
* @param home memory home object to be converted thread-safe.
|
|
*
|
|
* @retval 0 when successful,
|
|
* @retval -1 upon an error.
|
|
*/
|
|
int su_home_threadsafe(su_home_t *home)
|
|
{
|
|
pthread_mutex_t *mutex;
|
|
|
|
if (home == NULL)
|
|
return su_seterrno(EFAULT);
|
|
|
|
if (home->suh_lock) /* Already? */
|
|
return 0;
|
|
|
|
#if 0 /* Allow threadsafe subhomes */
|
|
assert(!su_home_has_parent(home));
|
|
if (su_home_has_parent(home))
|
|
return su_seterrno(EINVAL);
|
|
#endif
|
|
|
|
#if SU_HAVE_PTHREADS
|
|
if (!_su_home_unlocker) {
|
|
/* Avoid linking pthread library just for memory management */
|
|
_su_home_mutex_locker = mutex_locker;
|
|
_su_home_mutex_trylocker = mutex_trylocker;
|
|
_su_home_mutex_unlocker = mutex_unlocker;
|
|
_su_home_locker = (int (*)(void *))pthread_mutex_lock;
|
|
_su_home_unlocker = (int (*)(void *))pthread_mutex_unlock;
|
|
_su_home_destroy_mutexes = mutex_destroy;
|
|
}
|
|
|
|
mutex = calloc(1, 2 * (sizeof *mutex));
|
|
assert(mutex);
|
|
if (mutex) {
|
|
/* Mutex for memory operations */
|
|
pthread_mutex_init(mutex, NULL);
|
|
/* Mutex used for explicit locking */
|
|
pthread_mutex_init(mutex + 1, NULL);
|
|
home->suh_lock = (void *)mutex;
|
|
return 0;
|
|
}
|
|
#else
|
|
su_seterrno(ENOSYS);
|
|
#endif
|
|
|
|
return -1;
|
|
}
|