Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

PsnPthreadSemaphore Class Reference

a semaphore for use with pthreads. More...

#include <PsnPthreadSemaphore.h>

Inheritance diagram for PsnPthreadSemaphore:

Inheritance graph
[legend]
Collaboration diagram for PsnPthreadSemaphore:

Collaboration graph
[legend]
List of all members.

Public Methods

 PsnPthreadSemaphore (const int valeurInitiale)
 Constructor.

virtual ~PsnPthreadSemaphore ()
 Destructor.

virtual int V ()
 V Increments the semphore value and signals the change to blocked threads.

virtual int P ()
 P decrement the sempaphore value and block the calling thread if this value is negative.


Private Attributes

pthread_cond_t _cond
pthread_mutex_t _mutex
int _v

Detailed Description

a semaphore for use with pthreads.

Semaphores for multithreading using pthreads.

Author:
D. Margery
Version:
1.1

Definition at line 29 of file PsnPthreadSemaphore.h.


Constructor & Destructor Documentation

PsnPthreadSemaphore::PsnPthreadSemaphore const int    valeurInitiale
 

Constructor.

Parameters:
int initial value of the semaphore

Definition at line 21 of file PsnPthreadSemaphore.cxx.

References _cond, _mutex, and _v.

00021                                                                   : PsnSemaphore(valeurInitiale){
00022   int ret;
00023   _v=valeurInitiale;
00024   ret=pthread_mutex_init(&_mutex,NULL);
00025   if(ret) {
00026     perror("PsnPthreadSemaphore : impossible d'initialiser le mutex");
00027   }
00028   ret=pthread_cond_init(&_cond,NULL);
00029   if(ret) {
00030     perror("PsnPthreadSemaphore : impossible d'initialiser le cond");
00031   }
00032 }

PsnPthreadSemaphore::~PsnPthreadSemaphore   [virtual]
 

Destructor.

Definition at line 34 of file PsnPthreadSemaphore.cxx.

References _cond, and _mutex.

00034                                           {
00035   pthread_mutex_destroy(&_mutex);
00036   pthread_cond_destroy(&_cond);
00037 }


Member Function Documentation

int PsnPthreadSemaphore::P   [virtual]
 

P decrement the sempaphore value and block the calling thread if this value is negative.

Returns:
: old value of the semaphore

Implements PsnSemaphore.

Definition at line 58 of file PsnPthreadSemaphore.cxx.

References _cond, _mutex, and _v.

00058                            {
00059   int retval,ret;
00060   ret=pthread_mutex_lock(&_mutex);
00061   if(ret) {
00062     perror("PsnPthreadSemaphore : P a echoue au niveau du mutex lock");
00063   }
00064   _v--;
00065   while(_v<0) {
00066     ret=pthread_cond_wait(&_cond,&_mutex);
00067     if(ret) {
00068       perror("PsnPthreadSemaphore : P a echoue au niveau du cond");
00069     }
00070   }
00071   retval=_v;
00072   ret=pthread_mutex_unlock(&_mutex);
00073   if(ret) {
00074     perror("PsnPthreadSemaphore : P a echoue au niveau du mutex unlock");
00075   }
00076   return (retval);
00077 }

int PsnPthreadSemaphore::V   [virtual]
 

V Increments the semphore value and signals the change to blocked threads.

Returns:
: new value of the semaphore

Implements PsnSemaphore.

Definition at line 39 of file PsnPthreadSemaphore.cxx.

References _cond, _mutex, and _v.

00039                            {
00040   int retval,ret;
00041   ret=pthread_mutex_lock(&_mutex);
00042   if(ret) {
00043     perror("PsnPthreadSemaphore : V a echoue au niveau du mutex lock");
00044   }
00045   _v++;
00046   retval=_v;
00047   ret=pthread_mutex_unlock(&_mutex);
00048   if(ret) {
00049     perror("PsnPthreadSemaphore : V a echoue au niveau du mutex unlock");
00050   }
00051   ret=pthread_cond_signal(&_cond);
00052   if(ret) {
00053     perror("PsnPthreadSemaphore : V a echoue au niveau du cond");
00054   }
00055   return(retval);
00056 }


Member Data Documentation

pthread_cond_t PsnPthreadSemaphore::_cond [private]
 

Definition at line 57 of file PsnPthreadSemaphore.h.

Referenced by P(), PsnPthreadSemaphore(), V(), and ~PsnPthreadSemaphore().

pthread_mutex_t PsnPthreadSemaphore::_mutex [private]
 

Definition at line 58 of file PsnPthreadSemaphore.h.

Referenced by P(), PsnPthreadSemaphore(), V(), and ~PsnPthreadSemaphore().

int PsnPthreadSemaphore::_v [private]
 

Definition at line 59 of file PsnPthreadSemaphore.h.

Referenced by P(), PsnPthreadSemaphore(), and V().


The documentation for this class was generated from the following files:
logo OpenMask

Documentation generated on Mon Nov 25 15:26:20 2002

Generated with doxygen 1.2.12 by Dimitri van Heesch ,   1997-2001