Solved

Looking for rw_rdlock() etc source

Posted on 1997-11-11
1
270 Views
Last Modified: 2013-12-26
I am looking for the source for Solaris functions
rw_rdlock(), rw_wrlock() etc. These are functions used for
thread synchronization using mutex locks.
I would like to see how these functions are developed.

Thanks !!

Madhukar
0
Comment
Question by:mandapati
1 Comment
 

Accepted Solution

by:
aditya070797 earned 100 total points
ID: 1295545
Hi Madhukar,
            I am not sure about the Solaris functions that you mentioned, I am not even aware of their existence ( I did very little programming on Solaris, mostly Digital Unix and HP-UX), but
I am posting my implementation of reader/writer locks for pthreads below. The idea is simple, any number of readers (no writers) or only one writer (no readers) are allowed access at one time to the resource.
             I define a structure for a reader-writer lock object and provide functionality that works on this structure ( which is returned to you as a handle after creation.)
             Note that you have to call rw_create_lock() first to create the lock handle and pass this handle to all the lock functions. I should actually assert on the handle in every function but was just too lazy to do that.  
             Hope the code below helps..
--Aditya Vedula.

/******************************************************************
 * rdwr.h:
 * IMPLEMENTATION OF READER AND WRITER LOCKS FOR PTHREADS
 * DISCLAIMER:
 * Please be warned that the code presented below is here only for
 * illustration purposes, I do not give guarantees of any sort
 * regarding it's quality or usability. However, if you find it
 * useful, please feel free to use it and keep this disclaimer with it
 * AND USE IT AT YOUR OWN RISK.
 * Author: Aditya Vedula
 *
 ******************************************************************/
#ifndef _rdwr_h_
#define _rdwr_h_

#include < stdio.h >
#include < pthread.h >

#if defined(true) || defined(false)
#undef true
#undef false
#endif

enum bool {
      false = 0,
      true
};
typedef enum bool bool;

struct pth_rw_s {
pthread_mutex_t rw_mutx;
pthread_cond_t rw_cond;
int readers;
int writers;
};
typedef struct pth_rw_s rw_lock_t;

rw_lock_t* rw_create_lock();
bool rw_read_lock(rw_lock_t* this);
bool rw_write_lock(rw_lock_t* this);
bool rw_read_unlock(rw_lock_t* this);
bool rw_write_unlock(rw_lock_t* this);
void rw_destroy_lock(rw_lock_t* this);

#endif /* rdwr.h */

-------------------------------------------------------------------

/***********************************************************************
 * rdwr.c:
 * READER/WRITER LOCKS IMPLEMENTATION FOR PTHREADS.
 * DISCLAIMER:
 * Please be warned that the code presented below is here only for
 * illustration purposes, I do not give guarantees of any sort
 * regarding it's quality or usability. However, if you find it
 * useful, please feel free to use it and keep this disclaimer with it
 * AND USE IT AT YOUR OWN RISK.
 * Author: Aditya Vedula
 *
 ***********************************************************************/
#include "rdwr.h"
#include "a_error.h"

#define RW_LOCK_M(this) \ pthread_mutex_lock(&(this->rw_mutx))
#define RW_UNLOCK_M(this)  \ pthread_mutex_unlock(&(this->rw_mutx))

/****************************
 * rw_create_lock:
 * Create a reader/writer lock
 *
 ****************************/
rw_lock_t* rw_create_lock()
{
rw_lock_t* this;
      
      this = (rw_lock_t*)malloc(sizeof(rw_lock_t));
      pthread_mutex_init(&(this->rw_mutx), NULL);
      pthread_cond_init(&(this->rw_cond), NULL);
      this->readers = this->writers = 0;

      return(this);
}

/*****************************
 * rw_destroy_lock:
 * Destroy reader/writer lock
 *
 *****************************/
 void rw_destroy_lock(rw_lock_t* this)
 {
      if(!this) {
            a_warn("%s: rw_destroy_lock: %d: Null pointer to Reader/Writer lock object passed..\n", __FILE__, __LINE__);
            return;
      }
      free(this);
}

/*****************************
 * rw_read_lock:
 * Obtain a reader lock.
 *
 *****************************/
bool rw_read_lock(rw_lock_t* this)
{
      RW_LOCK_M(this);

      while(this->writers) {
            pthread_cond_wait(&(this->rw_cond), &(this->rw_mutx));
      }
      this->readers++;

      RW_UNLOCK_M(this);

      return(true);
}

/*******************************
 * rw_write_lock:
 * Obtain a writer lock
 *
 *******************************/
bool rw_write_lock(rw_lock_t* this)
{
      RW_LOCK_M(this);

      while( this->writers || this->readers ) {
            pthread_cond_wait(&(this->rw_cond), &(this->rw_mutx));
      }
      this->writers++;

      RW_UNLOCK_M(this);
      
      return(true);
}

/*********************************
 * rw_read_unlock:
 * Release previously obtained read lock
 *
 *********************************/
bool rw_read_unlock(rw_lock_t* this)
{
      RW_LOCK_M(this);

      if(this->readers == 0) {
            a_warn("%s: rw_read_unlock: %d: read unlocking 0 readers !!?? \n", __FILE__, __LINE__);
            RW_UNLOCK_M(this);
            return(false);
      }
      this->readers--;
      if(this->readers == 0)
            pthread_cond_signal(&(this->rw_cond));

      RW_UNLOCK_M(this);

      return(true);
}      

/************************************
 * rw_write_unlock:
 * Release previously obtained write-lock
 *
 ************************************/
bool rw_write_unlock(rw_lock_t* this)
{
      RW_LOCK_M(this);
      
      if(this->writers == 0) {
            a_warn("%s: rw_write_unlock: %d :write unlocking 0 writers.. ??!!\n", __FILE__, __LINE__);
            RW_UNLOCK_M(this);
            return(false);
      }
      this->writers = 0;
      pthread_cond_broadcast(&(this->rw_cond));

      RW_UNLOCK_M(this);

      return(true);
}      
--------------------------------------------------------------------
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to create frequencies of a variable from SAS dataset? 10 125
xyBalance chalenge 58 93
substring method in java 1 122
centeredAverage challenge 8 165
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question