su_alloc_lock.c 3.2 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 26 27 28 29 30 31 32 33 34 35
 * 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"

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 46

extern void (*su_home_locker)(void *mutex);
extern void (*su_home_unlocker)(void *mutex);

47 48 49
extern void (*su_home_mutex_locker)(void *mutex);
extern void (*su_home_mutex_unlocker)(void *mutex);

50 51
extern void (*su_home_destroy_mutexes)(void *mutex);

52 53 54 55 56 57 58 59 60 61 62 63
/** Mutex */
static void mutex_locker(void *_mutex)
{
  pthread_mutex_t *mutex = _mutex;
  pthread_mutex_lock(mutex + 1);
}

static void mutex_unlocker(void *_mutex)
{
  pthread_mutex_t *mutex = _mutex;
  pthread_mutex_unlock(mutex + 1);
}
64 65 66 67 68 69

static void mutex_destroy(void *_mutex)
{
  pthread_mutex_t *mutex = _mutex;
  pthread_mutex_destroy(mutex + 0);
  pthread_mutex_destroy(mutex + 1);
70
  free(_mutex);
71 72
}

73 74
#endif

75

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

90 91 92 93
  if (home == NULL)
    return su_seterrno(EFAULT);

  if (home->suh_lock)		/* Already? */
94 95
    return 0;

96
#if 0				/* Allow threadsafe subhomes */
97 98
  assert(!su_home_has_parent(home));
  if (su_home_has_parent(home))
99
    return su_seterrno(EINVAL);
100
#endif
101

102
#if SU_HAVE_PTHREADS
103 104 105 106
  if (!su_home_unlocker) {
    /* Avoid linking pthread library just for memory management */
    su_home_mutex_locker = mutex_locker;
    su_home_mutex_unlocker = mutex_unlocker;
107 108
    su_home_locker = (void (*)(void *))pthread_mutex_lock;
    su_home_unlocker = (void (*)(void *))pthread_mutex_unlock;
109
    su_home_destroy_mutexes = mutex_destroy;
Pekka Pessi's avatar
Pekka Pessi committed
110 111
  }

112
  mutex = calloc(1, 2 * (sizeof *mutex));
113
  assert(mutex);
Pekka Pessi's avatar
Pekka Pessi committed
114
  if (mutex) {
115
    /* Mutex for memory operations */
Pekka Pessi's avatar
Pekka Pessi committed
116
    pthread_mutex_init(mutex, NULL);
117 118
    /* Mutex used for explicit locking */ 
    pthread_mutex_init(mutex + 1, NULL);
Pekka Pessi's avatar
Pekka Pessi committed
119 120 121 122
    home->suh_lock = (void *)mutex;
    return 0;
  }
#else
123 124 125
  su_seterrno(ENOSYS);
#endif

Pekka Pessi's avatar
Pekka Pessi committed
126 127
  return -1;
}