Solved

Looking for rw_rdlock() etc source

Posted on 1997-11-11
1
269 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Powershell based monitoring system 2 138
Pressing cancel button with C# UI Automation 3 51
unable to delete all specified values regedit 38 270
unix example issues 18 70
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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 is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

932 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

13 Experts available now in Live!

Get 1:1 Help Now