• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Data Structures
  • Files
  • Directories
  • File List
  • Globals

atomic_ops.c

Go to the documentation of this file.
00001 /* 
00002  * $Id$
00003  * 
00004  * Copyright (C) 2006 iptelorg GmbH
00005  *
00006  * Permission to use, copy, modify, and distribute this software for any
00007  * purpose with or without fee is hereby granted, provided that the above
00008  * copyright notice and this permission notice appear in all copies.
00009  *
00010  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
00011  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
00012  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
00013  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00014  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00015  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
00016  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00017  */
00018 /*
00019  *  atomic operations init
00020  */
00021 /* 
00022  * History:
00023  * --------
00024  *  2006-03-08  created by andrei
00025  *  2007-05-11  added lock_set support (andrei)
00026  */
00035 #include "atomic_ops_init.h"
00036 #include "atomic_ops.h"
00037 
00038 #if defined ATOMIC_OPS_USE_LOCK  || defined ATOMIC_OPS_USE_LOCK_SET || \
00039         defined MEMBAR_USES_LOCK
00040 #include "locking.h"
00041 #endif
00042 
00043 #ifdef MEMBAR_USES_LOCK
00044 gen_lock_t* __membar_lock=0; /* init in atomic_ops.c */
00045 #endif
00046 
00047 #ifdef ATOMIC_OPS_USE_LOCK_SET
00048 gen_lock_set_t* _atomic_lock_set=0;
00049 #elif defined ATOMIC_OPS_USE_LOCK
00050 gen_lock_t* _atomic_lock=0;
00051 #endif
00052 
00053 
00054 /* returns 0 on success, -1 on error */
00055 int init_atomic_ops()
00056 {
00057         
00058 #ifdef MEMBAR_USES_LOCK
00059         if ((__membar_lock=lock_alloc())==0){
00060                 goto error;
00061         }
00062         if (lock_init(__membar_lock)==0){
00063                 lock_dealloc(__membar_lock);
00064                 __membar_lock=0;
00065                 goto error;
00066         }
00067         _membar_lock; /* start with the lock "taken" so that we can safely use
00068                                          unlock/lock sequences on it later */
00069 #endif
00070 #ifdef ATOMIC_OPS_USE_LOCK_SET
00071         if ((_atomic_lock_set=lock_set_alloc(_ATOMIC_LS_SIZE))==0){
00072                 goto error;
00073         }
00074         if (lock_set_init(_atomic_lock_set)==0){
00075                 lock_set_dealloc(_atomic_lock_set);
00076                 _atomic_lock_set=0;
00077                 goto error;
00078         }
00079 #elif defined ATOMIC_OPS_USE_LOCK
00080         if ((_atomic_lock=lock_alloc())==0){
00081                 goto error;
00082         }
00083         if (lock_init(_atomic_lock)==0){
00084                 lock_dealloc(_atomic_lock);
00085                 _atomic_lock=0;
00086                 goto error;
00087         }
00088 #endif /* ATOMIC_OPS_USE_LOCK_SET/ATOMIC_OPS_USE_LOCK */
00089         return 0;
00090 #if defined MEMBAR_USES_LOCK || defined ATOMIC_OPS_USE_LOCK || \
00091         defined ATOMIC_OPS_USE_LOCK_SET
00092 error:
00093         destroy_atomic_ops();
00094         return -1;
00095 #endif
00096 }
00097 
00098 
00099 
00100 void destroy_atomic_ops()
00101 {
00102 #ifdef MEMBAR_USES_LOCK
00103         if (__membar_lock!=0){
00104                 lock_destroy(__membar_lock);
00105                 lock_dealloc(__membar_lock);
00106                 __membar_lock=0;
00107         }
00108 #endif
00109 #ifdef ATOMIC_OPS_USE_LOCK_SET
00110         if (_atomic_lock_set!=0){
00111                 lock_set_destroy(_atomic_lock_set);
00112                 lock_set_dealloc(_atomic_lock_set);
00113                 _atomic_lock_set=0;
00114         }
00115 #elif defined ATOMIC_OPS_USE_LOCK
00116         if (_atomic_lock!=0){
00117                 lock_destroy(_atomic_lock);
00118                 lock_dealloc(_atomic_lock);
00119                 _atomic_lock=0;
00120         }
00121 #endif /* ATOMIC_OPS_USE_LOCK_SET / ATOMIC_OPS_USE_LOCK*/
00122 }

Generated on Tue May 22 2012 13:10:02 for SIP Router by  doxygen 1.7.1