/home/dko/projects/mobilec/tags/MobileC-v1.10.2/MobileC-v1.10.2/src/mc_rwlock.c

Go to the documentation of this file.
00001 /* SVN FILE INFO
00002  * $Revision: 207 $ : Last Committed Revision
00003  * $Date: 2008-07-11 17:55:19 -0700 (Fri, 11 Jul 2008) $ : Last Committed Date */
00004 /*[
00005  * Copyright (c) 2007 Integration Engineering Laboratory
00006                       University of California, Davis
00007  *
00008  * Permission to use, copy, and distribute this software and its
00009  * documentation for any purpose with or without fee is hereby granted,
00010  * provided that the above copyright notice appear in all copies and
00011  * that both that copyright notice and this permission notice appear
00012  * in supporting documentation.
00013  *
00014  * Permission to modify the software is granted, but not the right to
00015  * distribute the complete modified source code.  Modifications are to
00016  * be distributed as patches to the released version.  Permission to
00017  * distribute binaries produced by compiling modified sources is granted,
00018  * provided you
00019  *   1. distribute the corresponding source modifications from the
00020  *    released version in the form of a patch file along with the binaries,
00021  *   2. add special version identification to distinguish your version
00022  *    in addition to the base release version number,
00023  *   3. provide your name and address as the primary contact for the
00024  *    support of your modified version, and
00025  *   4. retain our contact information in regard to use of the base
00026  *    software.
00027  * Permission to distribute the released version of the source code along
00028  * with corresponding source modifications in the form of a patch file is
00029  * granted with same provisions 2 through 4 for binary distributions.
00030  *
00031  * This software is provided "as is" without express or implied warranty
00032  * to the extent permitted by applicable law.
00033 ]*/
00034 
00035 #include <stdio.h>
00036 #include <stdlib.h>
00037 #include "include/macros.h"
00038 #include "include/mc_error.h"
00039 #include "include/mc_rwlock.h"
00040 
00041 int mc_rwlock_init(mc_rwlock_p rwlock)
00042 {
00043     /* Init values */
00044     rwlock->num_readers = 0;
00045     rwlock->write_flag = 0;
00046     rwlock->write_request = 0;
00047 
00048     /* Alloc sync */
00049     rwlock->lock = (MUTEX_T*)malloc(sizeof(MUTEX_T));
00050     CHECK_NULL(rwlock->lock, return MC_ERR_MEMORY;);
00051     rwlock->cond = (COND_T*)malloc(sizeof(COND_T));
00052     CHECK_NULL(rwlock->cond, return MC_ERR_MEMORY;);
00053 
00054     /* Init sync */
00055     MUTEX_INIT(rwlock->lock);
00056     COND_INIT(rwlock->cond);
00057 
00058     return 0;
00059 }
00060 
00061 int mc_rwlock_destroy(mc_rwlock_p rwlock)
00062 {
00063     /* Destroy mutex/conds */
00064     MUTEX_DESTROY(rwlock->lock);
00065     COND_DESTROY(rwlock->cond);
00066 
00067     /* De-alloc */
00068     free(rwlock->lock);
00069     free(rwlock->cond);
00070     
00071     return 0;
00072 }
00073 
00074 int mc_rwlock_rdlock(mc_rwlock_p rwlock)
00075 {
00076     MUTEX_LOCK(rwlock->lock);
00077     while (
00078             (rwlock->write_flag != 0) ||
00079             (rwlock->write_request != 0)
00080           ) 
00081     {
00082         COND_WAIT(rwlock->cond, rwlock->lock);
00083     }
00084     rwlock->num_readers++;
00085     MUTEX_UNLOCK(rwlock->lock);
00086     return 0;
00087 }
00088 
00089 int mc_rwlock_rdunlock(mc_rwlock_p rwlock)
00090 {
00091     MUTEX_LOCK(rwlock->lock);
00092     if (rwlock->num_readers > 0) {
00093         rwlock->num_readers--;
00094     }
00095     if (rwlock->num_readers == 0) {
00096         COND_SIGNAL( rwlock->cond );
00097     }
00098     MUTEX_UNLOCK(rwlock->lock);
00099     return 0;
00100 }
00101 
00102 int mc_rwlock_wrlock(mc_rwlock_p rwlock)
00103 {
00104     MUTEX_LOCK(rwlock->lock);
00105     rwlock->write_request++;
00106 
00107     while (
00108             (rwlock->num_readers != 0) || 
00109             (rwlock->write_flag != 0) 
00110           )
00111     {
00112         COND_WAIT(rwlock->cond, rwlock->lock);
00113     }
00114 
00115     rwlock->write_request--;
00116     rwlock->write_flag++;
00117     MUTEX_UNLOCK(rwlock->lock);
00118     return 0;
00119 }
00120 
00121 int mc_rwlock_wrunlock(mc_rwlock_p rwlock)
00122 {
00123     MUTEX_LOCK(rwlock->lock);
00124     rwlock->write_flag = 0;
00125     COND_SIGNAL(rwlock->cond);
00126     MUTEX_UNLOCK(rwlock->lock);
00127     return 0;
00128 }

Generated on Fri Jul 11 17:59:45 2008 for Mobile-C by  doxygen 1.5.4