Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Looking for rw_rdlock() etc source

Posted on 1997-11-11
1
Medium Priority
?
275 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

636 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