fdhwlib  2.0.25
/home/kopmann/git-mirror/fdhwlib/fdhwlib/akutil/semaphore.h
Go to the documentation of this file.
00001 /***************************************************************************
00002     semaphore.h  -  description
00003 
00004     begin                : Thu Jun 29 2000
00005     copyright            : (C) 2000 by Andreas Kopmann
00006     email                : kopmann@hpe.fzk.de
00007  ***************************************************************************/
00008 
00009 
00010 
00011 
00012 #ifndef _INC_SEMAPHORE_1234_INCLUDED
00013 #define _INC_SEMAPHORE_1234_INCLUDED
00014 
00015 //#define SEM_DEBUG // turn on debug information
00016 
00017 #ifdef __GNUC__
00018 #define USE_SYSV_SYNC
00019 #endif
00020 
00021 // Do not realy work for Windows
00022 // Use the appropriate MFC commands directly!!!
00023 //#define USE_MFC_SYNC
00024 
00025 #include <cstdio>
00026 #include <stdexcept>
00027 
00028 #ifdef USE_SYSV_SYNC
00029 // Semaphore für Linux
00030 // Sys V style
00031 #include <sys/types.h>
00032 #include <sys/ipc.h>
00033 #include <sys/sem.h>
00034 
00035 #ifdef __linux__
00036 //#include <linux/sem.h>
00037 // from linux/sem.h  don't know how to handle it. You can't invoke
00038 // both sys/sem.h and linux/sem.h !?
00039 
00040 /* arg for semctl system calls. */
00041 union semun {
00042         int val;                        /* value for SETVAL */
00043         struct semid_ds *buf;           /* buffer for IPC_STAT & IPC_SET */
00044         unsigned short *array;          /* array for GETALL & SETALL */
00045         struct seminfo *__buf;          /* buffer for IPC_INFO */
00046         void *__pad;
00047 };
00048 #endif
00049 
00050 #define SEM_NUMBER_MAX 16  // Number of Semas in the array
00051 #define SEM_RESOURCE_MAX 1 //Initial value of all semaphores
00052 #endif
00053 
00054 #ifdef USE_MFC_SYNC
00055 #include <afxwin.h>
00056 #include <afxmt.h>
00057 #endif
00058 
00092 class semaphore
00093 {
00094 public:
00095    //semaphore();
00096    //semaphore(int mid);
00101    semaphore(int mid=0, const char *dir="/");
00102 
00103    //void request();
00104    //void release();
00105    void request(int nr=0);
00106    void release(int nr=0);
00107 
00110    int number();
00111 
00116 #ifdef USE_SYSV_SYNC
00117    key_t getKey();
00118 #endif
00119 
00120 private:
00121 
00122    int memid;
00123 
00124 #ifdef USE_SYSV_SYNC
00125    key_t key;
00126 
00127    int sid;
00128    int max;
00129 
00130    void init(int mid, const char *dir);
00131 #endif
00132 
00133 #ifdef USE_MFC_SYNC
00134    CMutex *mutex0;
00135    CMutex *mutex1;
00136 
00137    CSingleLock *lock0;
00138    CSingleLock *lock1;
00139 #endif
00140 
00141 };
00142 
00143 #ifdef USE_SYSV_SYNC
00144 
00158 class semaControl
00159 {
00160 public:
00161    semaControl();
00162 
00166    semaControl(const char *dir);
00167 
00170    int number();
00171 
00179    int value(int nr);
00180 
00185    void markForDelete();
00186 
00187 
00189    void open();
00190 
00192    void create(int members);
00193 
00199    void lock(int nr);
00200 
00206    void unlock(int nr);
00207 
00212 #ifdef USE_SYSV_SYNC
00213    key_t getKey();
00214 #endif
00215 
00216 private:
00217    int sid;
00218    int memid;
00219    int max;
00220 
00221    key_t key;
00222 
00223    void init(const char *dir);
00224 };
00225 #endif
00226 
00227 #endif
00228 
00229