/* ******************************************************************************* * Copyright (c) Martin Poole ******************************************************************************* ** ** WARNING !! WARNING !! WARNING !! WARNING !! WARNING !! WARNING !! ** ** Any changes to be made to this file should first be checked with ** mplib1 source control for library integrity. ** ** mplib1 source control can be reached at mplib1@quatermass.co.uk ** * * $Source: /home/cvs/cvsroot/onelan/onelan/src/mplib1/libsrc/bpo_lock.c,v $ * $Author: mpoole $ * $Date: 2002/10/07 09:37:37 $ * $Revision: 1.2 $ * ******************************************************************************* * * Change History * * $Log: bpo_lock.c,v $ * Revision 1.2 2002/10/07 09:37:37 mpoole * Initial checkin of mplib1-3.1.0 * * Revision 1.1 2002/10/07 09:36:54 mpoole * Initial checkin of mplib1-3.1.0 * * ******************************************************************************* */ #ident "$Header: /home/cvs/cvsroot/onelan/onelan/src/mplib1/libsrc/bpo_lock.c,v 1.2 2002/10/07 09:37:37 mpoole Exp $" /* ------------------------------------------------------------------ Include files ------------------------------------------------------------------ */ #include #include #include #include #include #include #include #include #include #include /* ------------------------------------------------------------------ defines ------------------------------------------------------------------ */ static int bpo_lock_debug=0; /* ------------------------------------------------------------------ Code starts here ------------------------------------------------------------------ */ #ifdef HAVE_MSEM /* A number of functions to simulate the required primitives */ Xchg( b_lock_t *lockp ) { int rc; rc = msem_lock( lockp, MSEM_IF_NOWAIT ); /* the call can fail, and return -1, if it is interrupted, if the lock is re-initialised or it is already locked (normal) */ return(rc); } Clr( b_lock_t *lockp ) { int rc; rc = msem_unlock( lockp, MSEM_IF_NOWAIT ); } #endif long b_Lockval( b_lock_t *lockp ) { #ifdef HAVE_PTHREADS int rc; rc = pthread_mutex_trylock( &lockp->mp ); if (rc==0) pthread_mutex_unlock( &lockp->mp ); return(rc); #else #ifdef HAVE_MSEM int rc; rc = msem_lock( lockp, MSEM_IF_NOWAIT ); if (rc==0) msem_unlock( lockp, 0 ); return(rc); #else #ifdef Lckval return( Lckval(lockp)); #else int rc; rc = Xchg( lockp ); if (rc==0) Clr( lockp ); return(rc); #endif #endif #endif } int b_TryLock( b_lock_t *lockp ) { int rc; #ifdef HAVE_PTHREADS rc = pthread_mutex_trylock( &lockp->mp ); #else #ifdef HAVE_MSEM rc = msem_lock( lockp, MSEM_IF_NOWAIT ); #else rc = Xchg(lockp); #endif #endif return(rc); } void b_Lock( b_lock_t *lockp ) { int lc=0; bpo_lock_debug = get_config_flag("BPO_LOCK_DEBUG"); #ifdef HAVE_PTHREADS pthread_mutex_lock( &lockp->mp ); #else #ifdef HAVE_MSEM msem_lock( lockp, 0 ); #else while ( Xchg(lockp) ) { bpo_take_a_nap(); if (bpo_lock_debug && lc==0) { lc++; fprintfile(stderr,"b_Lock: Awaiting lock %p\n", lockp ); } }; #endif #endif return; } void b_Unlock( b_lock_t *lockp ) { #ifdef HAVE_PTHREADS pthread_mutex_unlock( &lockp->mp ); #else #ifdef HAVE_MSEM msem_unlock( lockp, 0 ); #else Clr( lockp ); #endif #endif return; } void b_Init( b_lock_t *lockp ) { #ifdef HAVE_PTHREADS #ifdef HAVE_PTHREADS_DCE pthread_mutexattr_create( &lockp->mattr ); pthread_mutexattr_setkind_np( &lockp->mattr, MUTEX_FAST_NP ); pthread_mutex_init( &lockp->mp, lockp->mattr ); #else pthread_mutexattr_init( &lockp->mattr ); pthread_mutexattr_setpshared( &lockp->mattr, PTHREAD_PROCESS_SHARED ); pthread_mutex_init( &lockp->mp, &lockp->mattr ); #endif #else #ifdef HAVE_MSEM msem_init( lockp, MSEM_UNLOCKED ); #else #ifdef HAVE_MUTEX init_lock( lockp ); #else #ifdef HAVE_LWP_MUTEX memset( lockp, '\0', sizeof(b_lock_t) ); #else Clr( lockp ); #endif #endif #endif #endif return; } /* -- End of File -- */