/*
 * 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>

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

extern void (*su_home_mutex_locker)(void *mutex);
extern void (*su_home_mutex_unlocker)(void *mutex);

/** 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);
}
#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_unlocker = mutex_unlocker;
    su_home_locker = (void (*)(void *))pthread_mutex_lock;
    su_home_unlocker = (void (*)(void *))pthread_mutex_unlock;
  }

  mutex = su_alloc(home, 2 * sizeof (pthread_mutex_t));
  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;
}