su_alloc_lock.c 3.17 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 42 43 44 45

#if SU_HAVE_PTHREADS
#include <pthread.h>
#include <assert.h>

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

Pekka Pessi's avatar
Pekka Pessi committed
46 47 48
extern void (*su_home_mutex_locker)(void *mutex);
extern void (*su_home_mutex_unlocker)(void *mutex);

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

Pekka Pessi's avatar
Pekka Pessi committed
51 52 53 54 55 56 57 58 59 60 61 62
/** 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);
}
63 64 65 66 67 68 69 70

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

71 72
#endif

Pekka Pessi's avatar
Pekka Pessi committed
73

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

88 89 90 91
  if (home == NULL)
    return su_seterrno(EFAULT);

  if (home->suh_lock)		/* Already? */
Pekka Pessi's avatar
Pekka Pessi committed
92 93
    return 0;

94
#if 0				/* Allow threadsafe subhomes */
Pekka Pessi's avatar
Pekka Pessi committed
95 96
  assert(!su_home_has_parent(home));
  if (su_home_has_parent(home))
97
    return su_seterrno(EINVAL);
98
#endif
Pekka Pessi's avatar
Pekka Pessi committed
99

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

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

Pekka Pessi's avatar
Pekka Pessi committed
124 125
  return -1;
}