rw_lock.c

00001 /* File:      rw_lock.c
00002 ** Author(s): Rui Marques
00003 ** Contact:   xsb-contact@cs.sunysb.edu
00004 ** 
00005 ** Copyright (C) The Research Foundation of SUNY, 1986, 1993-1998
00006 ** Copyright (C) ECRC, Germany, 1990
00007 ** 
00008 ** XSB is free software; you can redistribute it and/or modify it under the
00009 ** terms of the GNU Library General Public License as published by the Free
00010 ** Software Foundation; either version 2 of the License, or (at your option)
00011 ** any later version.
00012 ** 
00013 ** XSB is distributed in the hope that it will be useful, but WITHOUT ANY
00014 ** WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00015 ** FOR A PARTICULAR PURPOSE.  See the GNU Library General Public License for
00016 ** more details.
00017 ** 
00018 ** You should have received a copy of the GNU Library General Public License
00019 ** along with XSB; if not, write to the Free Software Foundation,
00020 ** Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00021 **
00022 ** $Id: rw_lock.c,v 1.4 2005/12/07 13:23:02 ruim Exp $
00023 ** 
00024 */
00025 
00026 /* Implementation of rwlocks in pthreads
00027 
00028    Gives preference to writers to avoid writer starvation.
00029    Assumes there are few enough write locks so that read locks won't starvate.
00030  */
00031 
00032 #include "xsb_config.h"
00033 
00034 #ifdef MULTI_THREAD_RWL
00035 
00036 #include "rw_lock.h"
00037 
00038 rw_lock trie_rw_lock ;
00039 
00040 void rw_lock_init(rw_lock *l)
00041 {
00042         l->writers = l->havelock = 0 ;
00043         pthread_mutex_init(&l->lock, NULL );
00044         pthread_cond_init(&l->waiting, NULL);
00045 }
00046 
00047 void rw_lock_read(rw_lock *l)
00048 {
00049         pthread_mutex_lock(&l->lock) ;
00050         while( l->writers > 0 )
00051                 pthread_cond_wait(&l->waiting,&l->lock) ;
00052         l->havelock++;
00053         pthread_mutex_unlock(&l->lock) ;
00054 }
00055 
00056 void rw_unlock_read(rw_lock *l)
00057 {
00058         pthread_mutex_lock(&l->lock) ;
00059         l->havelock -- ;
00060         if( l->havelock == 0 )
00061                 pthread_cond_broadcast(&l->waiting) ;
00062         pthread_mutex_unlock(&l->lock) ;
00063 }
00064 
00065 void rw_lock_write(rw_lock *l)
00066 {
00067         pthread_mutex_lock(&l->lock) ;
00068         l->writers ++ ;
00069         while( l->havelock > 0 )
00070                 pthread_cond_wait(&l->waiting,&l->lock) ;
00071         l->havelock ++ ;
00072         pthread_mutex_unlock(&l->lock) ;
00073 }
00074 
00075 void rw_unlock_write(rw_lock *l)
00076 {
00077         pthread_mutex_lock(&l->lock) ;
00078         l->havelock -- ;
00079         l->writers -- ;
00080         if( l->havelock == 0 )
00081                 pthread_cond_broadcast(&l->waiting) ;
00082         pthread_mutex_unlock(&l->lock) ;
00083 }
00084 
00085 #endif /* MULTI_THREAD */

Generated on Wed Jul 26 13:30:42 2006 for XSB by  doxygen 1.4.5