?
Solved

Looking for rw_rdlock() etc source

Posted on 1997-11-11
1
Medium Priority
?
274 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 

Accepted Solution

by:
aditya070797 earned 200 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

Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

Question has a verified solution.

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

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…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

764 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