su_alloc_lock.c 3.42 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3 4 5 6 7
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
8
 * This library is free software; you can redistribute it and/or
Pekka Pessi's avatar
Pekka Pessi committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * 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
26
 * @CFILE su_alloc_lock.c
Pekka Pessi's avatar
Pekka Pessi committed
27 28 29
 * @brief Thread-locking for su_alloc module.
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
30
 *
Pekka Pessi's avatar
Pekka Pessi committed
31 32 33 34 35
 * @date Created: Fri Feb 23 17:38:11 2001 ppessi
 */

#include "config.h"

36 37
#include <sofia-sip/su_alloc.h>
#include <sofia-sip/su.h>
Pekka Pessi's avatar
Pekka Pessi committed
38 39 40 41

#if SU_HAVE_PTHREADS
#include <pthread.h>
#include <assert.h>
42
#include <stdlib.h>
Pekka Pessi's avatar
Pekka Pessi committed
43

44 45
extern int (*_su_home_locker)(void *mutex);
extern int (*_su_home_unlocker)(void *mutex);
Pekka Pessi's avatar
Pekka Pessi committed
46

47 48 49
extern int (*_su_home_mutex_locker)(void *mutex);
extern int (*_su_home_mutex_trylocker)(void *mutex);
extern int (*_su_home_mutex_unlocker)(void *mutex);
50

51
extern void (*_su_home_destroy_mutexes)(void *mutex);
52

53
/** Mutex */
54
static int mutex_locker(void *_mutex)
55 56
{
  pthread_mutex_t *mutex = _mutex;
57
  return pthread_mutex_lock(mutex + 1);
58 59
}

60
int mutex_trylocker(void *_mutex)
61 62
{
  pthread_mutex_t *mutex = _mutex;
63 64 65 66 67 68 69
  return pthread_mutex_trylock(mutex + 1);
}

static int mutex_unlocker(void *_mutex)
{
  pthread_mutex_t *mutex = _mutex;
  return pthread_mutex_unlock(mutex + 1);
70
}
71

72
static void mutex_destroy(void *_mutex)
73
{
74 75 76 77
  pthread_mutex_t *mutex = _mutex;
  pthread_mutex_destroy(mutex + 0);
  pthread_mutex_destroy(mutex + 1);
  free(_mutex);
78
}
79 80
#endif

81

Pekka Pessi's avatar
Pekka Pessi committed
82 83
/** Convert su_home_t object to a thread-safe one.
 *
84 85
 * Convert a memory home object as thread-safe by allocating mutexes and
 * modifying function pointers in su_alloc.c module.
86
 *
Pekka Pessi's avatar
Pekka Pessi committed
87 88
 * @param home memory home object to be converted thread-safe.
 *
89 90
 * @retval 0 when successful,
 * @retval -1 upon an error.
Pekka Pessi's avatar
Pekka Pessi committed
91 92 93
 */
int su_home_threadsafe(su_home_t *home)
{
94
  pthread_mutex_t *mutex;
Pekka Pessi's avatar
Pekka Pessi committed
95

96 97
  if (home == NULL)
    return su_seterrno(EFAULT);
98

99
  if (home->suh_lock)		/* Already? */
100 101
    return 0;

102 103 104 105 106 107
#if 0				/* Allow threadsafe subhomes */
  assert(!su_home_has_parent(home));
  if (su_home_has_parent(home))
    return su_seterrno(EINVAL);
#endif

108
#if SU_HAVE_PTHREADS
109
  if (!_su_home_unlocker) {
110
    /* Avoid linking pthread library just for memory management */
111 112 113 114 115 116
    _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;
Pekka Pessi's avatar
Pekka Pessi committed
117 118
  }

119 120 121 122 123
  mutex = calloc(1, 2 * (sizeof *mutex));
  assert(mutex);
  if (mutex) {
    /* Mutex for memory operations */
    pthread_mutex_init(mutex, NULL);
124
    /* Mutex used for explicit locking */
125 126 127 128
    pthread_mutex_init(mutex + 1, NULL);
    home->suh_lock = (void *)mutex;
    return 0;
  }
Pekka Pessi's avatar
Pekka Pessi committed
129
#else
130
  su_seterrno(ENOSYS);
131
#endif
132 133

  return -1;
Pekka Pessi's avatar
Pekka Pessi committed
134
}