Solved

Looking for rw_rdlock() etc source

Posted on 1997-11-11
1
268 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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now