mdcore  0.1.5
/home/pedro/work/mdcore/src/lock.h
Go to the documentation of this file.
00001 /*******************************************************************************
00002  * This file is part of mdcore.
00003  * Coypright (c) 2012 Pedro Gonnet (pedro.gonnet@durham.ac.uk)
00004  * 
00005  * This program is free software: you can redistribute it and/or modify
00006  * it under the terms of the GNU Lesser General Public License as published
00007  * by the Free Software Foundation, either version 3 of the License, or
00008  * (at your option) any later version.
00009  * 
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  * 
00015  * You should have received a copy of the GNU Lesser General Public License
00016  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017  * 
00018  ******************************************************************************/
00019 
00020 
00021 /* Get the inlining right. */
00022 #ifndef INLINE
00023 # if __GNUC__ && !__GNUC_STDC_INLINE__
00024 #  define INLINE extern inline
00025 # else
00026 #  define INLINE inline
00027 # endif
00028 #endif
00029     
00030 #ifdef PTHREAD_LOCK
00031     #define lock_type pthread_spinlock_t
00032     #define lock_init( l ) ( pthread_spin_init( l , PTHREAD_PROCESS_PRIVATE ) != 0 )
00033     #define lock_destroy( l ) ( pthread_spin_destroy( l ) != 0 )
00034     #define lock_lock( l ) ( pthread_spin_lock( l ) != 0 )
00035     #define lock_trylock( l ) ( pthread_spin_lock( l ) != 0 )
00036     #define lock_unlock( l ) ( pthread_spin_unlock( l ) != 0 )
00037 #else
00038     #define lock_type volatile int
00039     #define lock_init( l ) ( *l = 0 )
00040     #define lock_destroy( l ) 0
00041     INLINE int lock_lock ( volatile int *l ) {
00042         while ( __sync_val_compare_and_swap( l , 0 , 1 ) != 0 )
00043             while( *l );
00044         return 0;
00045         }
00046     #define lock_trylock( l ) ( ( *(l) ) ? 1 : __sync_val_compare_and_swap( l , 0 , 1 ) )
00047     #define lock_unlock( l ) ( __sync_val_compare_and_swap( l , 1 , 0 ) != 1 )
00048 #endif
 All Data Structures Files Functions Variables Typedefs Enumerator Defines