lock_alloc.h
ba98af46
 /*
  *
53c7e0f1
  * Copyright (C) 2001-2003 FhG Fokus
ba98af46
  *
2266bb9c
  * This file is part of Kamailio, a free SIP server.
ba98af46
  *
2266bb9c
  * Kamailio is free software; you can redistribute it and/or modify
ba98af46
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version
  *
2266bb9c
  * For a license to use the Kamailio software under conditions
ba98af46
  * other than those described here, or to purchase support for this
  * software, please contact iptel.org by e-mail at the following addresses:
  *    info@iptel.org
  *
2266bb9c
  * Kamailio is distributed in the hope that it will be useful,
ba98af46
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License 
  * along with this program; if not, write to the Free Software 
9e1ff448
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
ba98af46
  */
 
2266bb9c
 /*!
 * \file
 * \brief Kamailio core :: Kamailio locking library
 * \author andrei
 * \ingroup core
 * Module: \ref core
 *
ba98af46
  *   WARNING: don't include this directly include instead locking.h!
  *
 Implements: (see also locking.h)
 
 	simple locks:
 	-------------
 	gen_lock_t* lock_alloc();                - allocates a lock in shared mem.
 	void    lock_dealloc(gen_lock_t* lock);  - deallocates the lock's shared m.
 	
 	lock sets: [implemented only for FL & SYSV so far]
 	----------
194b6a35
 	gen_lock_set_t* lock_set_alloc(no)               - allocs a lock set in shm.
 	void lock_set_dealloc(gen_lock_set_t* s);        - deallocs the lock set shm.
ba98af46
 
 */
 
 #ifndef _lock_alloc_h
 #define _lock_alloc_h
 
 /*shm_{malloc, free}*/
 #include "mem/mem.h"
 #ifdef SHM_MEM
 #include "mem/shm_mem.h"
 #else
28427aa4
 #error "locking requires shared memory support"
ba98af46
 #endif
 
 #if defined(FAST_LOCK) || defined(USE_PTHREAD_MUTEX) || defined(USE_POSIX_SEM)
 /* simple locks*/
 #define lock_alloc() shm_malloc(sizeof(gen_lock_t))
e3dccdc9
 #define lock_dealloc(lock) shm_free((void*)lock)
ba98af46
 /* lock sets */
 
194b6a35
 inline static gen_lock_set_t* lock_set_alloc(int n)
ba98af46
 {
194b6a35
 	gen_lock_set_t* ls;
 	ls=(gen_lock_set_t*)shm_malloc(sizeof(gen_lock_set_t)+n*sizeof(gen_lock_t));
ba98af46
 	if (ls==0){
4c7fb776
 		LM_CRIT("could not allocate lock_set\n");
ba98af46
 	}else{
194b6a35
 		ls->locks=(gen_lock_t*)((char*)ls+sizeof(gen_lock_set_t));
ba98af46
 		ls->size=n;
 	}
 	return ls;
 }
 
8b7e37cf
 #define lock_set_dealloc(lock_set) shm_free((void*)lock_set)
ba98af46
 
 #elif defined USE_SYSV_SEM
 
 /*simple locks*/
 #define lock_alloc() shm_malloc(sizeof(gen_lock_t))
e3dccdc9
 #define lock_dealloc(lock) shm_free((void*)lock)
ba98af46
 /* lock sets */
 
194b6a35
 inline static gen_lock_set_t* lock_set_alloc(int n)
ba98af46
 {
194b6a35
 	gen_lock_set_t* ls;
 	ls=(gen_lock_set_t*)shm_malloc(sizeof(gen_lock_set_t));
ba98af46
 	if (ls){
 		ls->size=n;
 		ls->semid=-1;
 	};
 	return ls;
 }
 
 
8b7e37cf
 #define lock_set_dealloc(lock_set) shm_free((void*)lock_set)
ba98af46
 
 
 #else
 #error "no locking method selected"
 #endif
 
 
 #endif