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

frame_mutex.h

00001 /***************************************************************************
00002                            frame_mutex.h  -  description
00003                              -------------------
00004     begin                : Mit Jun 25 2003
00005     copyright            : (C) 2003 by Dirk Henrici
00006     email                : henrici@informatik.uni-kl.de
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of license found in "COPYING".                     *
00013  *                                                                         *
00014  ***************************************************************************/
00015 
00016 #ifndef FRAME_MUTEX_H
00017 #define FRAME_MUTEX_H
00018 
00019 #include <pthread.h>    //pThread-Mutex
00020 #include <semaphore.h>  //Posix-Semaphoren
00021 #include <fcntl.h>      //wg. "O_"-Konstanten
00022 #include <sys/sem.h>    //System V - Semaphoren
00023 #include "definitions.h"
00024 #include "frame_utils.h"
00025 
00039 //Compiler-Flag "-lpthread" verwenden
00040 
00044 class tAbstractMutex {
00045 protected:
00046   bool _master;
00047   bool _connected;
00048   int _lockcount;
00049 public: 
00050   tAbstractMutex(bool master = false) : _master(master), _connected(false), _lockcount(0) {};
00051   virtual ~tAbstractMutex() {};
00052   void setmaster(bool master) {_master=master;};
00053   virtual int getmutexsize() {return 0;};
00054   virtual void connect(void* mutexptr = NULL) {_connected = true; mutexptr = mutexptr;};
00055   virtual bool islocked() {return _lockcount!=0;}; 
00056   virtual tERROR create();      
00057   virtual tERROR remove();
00058   virtual tERROR acquire();
00059   virtual tERROR tryacquire();
00060   virtual tERROR acquire_read();
00061   virtual tERROR tryacquire_read();
00062   virtual tERROR acquire_upgrade();
00063   virtual tERROR tryacquire_upgrade();      
00064   virtual tERROR release();
00065   virtual tERROR release_read();  
00066   virtual tERROR release_downgrade();      
00067 };
00068 
00071 class tGuardCurrentScope {
00072   tAbstractMutex* _mutex;
00073 public:
00074   tGuardCurrentScope(tAbstractMutex* mutex) : _mutex(mutex) {mutex->acquire();};
00075   ~tGuardCurrentScope() {_mutex->release();};
00076 };
00077 
00080 class tNullMutex : public tAbstractMutex {
00081 public:
00082   tNullMutex() : tAbstractMutex() {};
00083   virtual tERROR create();
00084   virtual tERROR remove();
00085   virtual tERROR acquire();
00086   virtual tERROR tryacquire();
00087   virtual tERROR acquire_read();
00088   virtual tERROR tryacquire_read();
00089   virtual tERROR acquire_upgrade();
00090   virtual tERROR tryacquire_upgrade();
00091   virtual tERROR release();
00092   virtual tERROR release_read();
00093   virtual tERROR release_downgrade();
00094 };
00095 
00100 class tAtomicMutex : public tAbstractMutex {
00101   atomic_t* _mutexptr;
00102   atomic_t _localmutex;
00103 public:
00104   tAtomicMutex(bool master = false) : tAbstractMutex(master) {};
00105   virtual ~tAtomicMutex() {};
00106   virtual int getmutexsize() {return sizeof(atomic_t);};
00107   virtual void connect(void* mutexptr = NULL);
00108   virtual tERROR create();
00109   virtual tERROR remove();
00110   virtual tERROR acquire();
00111   virtual tERROR release();
00112 };
00113 
00117 class tAtomicRWMutex : public tAbstractMutex {
00118   typedef tAtomicRWLock locktype;
00119   locktype* _mutexptr;
00120   locktype _localmutex;
00121 public:
00122   tAtomicRWMutex(bool master = false) : tAbstractMutex(master) {};
00123   virtual ~tAtomicRWMutex() {};
00124   virtual int getmutexsize() {return sizeof(locktype);};
00125   virtual void connect(void* mutexptr = NULL);
00126   virtual tERROR create();
00127   virtual tERROR remove();
00128   virtual tERROR acquire();
00129   virtual tERROR acquire_read();
00130   virtual tERROR acquire_upgrade();
00131   virtual tERROR release();
00132   virtual tERROR release_read();
00133   virtual tERROR release_downgrade();  
00134 };
00135 
00139 class tPthreadMutex : public tAbstractMutex {
00140   pthread_mutex_t* _mutexptr;
00141   pthread_mutex_t _localmutex;
00142 public:
00143   tPthreadMutex(bool master = false) : tAbstractMutex(master) {};
00144   virtual ~tPthreadMutex() {};
00145   virtual int getmutexsize() {return sizeof(pthread_mutex_t);};  
00146   virtual void connect(void* mutexptr = NULL);
00147   virtual tERROR create();          
00148   virtual tERROR remove();
00149   virtual tERROR acquire();
00150   virtual tERROR release();
00151 };
00152 
00156 class tPosixSemaphore : public tAbstractMutex {
00157 protected:
00158   sem_t* _semptr;
00159   sem_t _localsem;
00160 public:
00161   tPosixSemaphore(bool master = false) : tAbstractMutex(master) {};
00162   virtual ~tPosixSemaphore() {};
00163   virtual tERROR acquire();
00164   virtual tERROR release();
00165 };
00166 
00171 class tUnnamedPosixSemaphore : public tPosixSemaphore {
00172   int _pshared;
00173 public:
00174   tUnnamedPosixSemaphore(bool master = false, int pshared=0) : tPosixSemaphore(master), _pshared(pshared) {};
00175   virtual ~tUnnamedPosixSemaphore() {};
00176   virtual int getmutexsize() {return sizeof(sem_t);};  
00177   virtual void connect(void* mutexptr = NULL);
00178   virtual tERROR create();
00179   virtual tERROR remove();
00180   void setattr(int pshared = 0) {_pshared = pshared;};
00181 };
00182 
00186 class tNamedPosixSemaphore : public tPosixSemaphore {
00187   sem_t* _semptr;
00188   sem_t _localsem;
00189   string _name;
00190   int _mode;
00191 public:
00192   tNamedPosixSemaphore(bool master = false, string name = "", int mode = 0666) : tPosixSemaphore(master), _name(name), _mode(mode) {};
00193   virtual ~tNamedPosixSemaphore() {};
00194   virtual tERROR create();
00195   virtual tERROR remove();
00196   void setattr(string name, int mode = 0) {_name = name; _mode = mode;};
00197 };
00198 
00201 class tSystemVSemaphore : public tAbstractMutex {
00202   int _semid;
00203   string _name;
00204   int _mode;
00205 public:
00206 
00207   union semun {
00208     int val;
00209     struct semid_ds* buf;
00210     unsigned short* array;
00211     struct seminfo* __buf;
00212   };
00213 
00214   tSystemVSemaphore(bool master = false, string name = "", int mode = 0666) : tAbstractMutex(master), _name(name), _mode(mode) {};
00215   virtual ~tSystemVSemaphore() {};
00216   virtual tERROR create();
00217   virtual tERROR remove();
00218   virtual tERROR acquire();
00219   virtual tERROR tryacquire();  
00220   virtual tERROR release();
00221 };
00222 
00223 #endif

Generated on Wed Sep 3 08:36:34 2003 for STL shared memory allocator by doxygen1.2.18