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

Semaphore.hh

Go to the documentation of this file.
00001 /*
00002 
00003     Semaphore wrapper class
00004     Copyright (C) 2000-2001 Jussi Laako
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 
00020 */
00021 
00022 
00023 #ifndef BSDSYS
00024 #include <semaphore.h>
00025 #else
00026 #include <sched.h>
00027 #endif
00028 
00029 
00030 #ifndef SEMAPHORE_HH
00031     #define SEMAPHORE_HH
00032 
00033 
00037     class clSemaphore
00038     {
00039             #ifndef BSDSYS
00040             sem_t semSemaphore;
00041             #else
00042             volatile unsigned int uiSemaphore;
00043             #endif
00044         public:
00049             clSemaphore ()
00050                 #ifndef BSDSYS
00051                 { sem_init(&semSemaphore, 0, 0); }
00052                 #else
00053                 { uiSemaphore = 0; }
00054                 #endif
00055 
00061             clSemaphore (unsigned int uiSemValue)
00062                 #ifndef BSDSYS
00063                 { sem_init(&semSemaphore, 0, uiSemValue); }
00064                 #else
00065                 { uiSemaphore = uiSemValue; }
00066                 #endif
00067 
00070             ~clSemaphore ()
00071                 #ifndef BSDSYS
00072                 { sem_destroy(&semSemaphore); }
00073                 #else
00074                 { }
00075                 #endif
00076 
00083             bool Initialize (unsigned int uiSemValue)
00084                 #ifndef BSDSYS
00085                 { 
00086                     if (sem_init(&semSemaphore, 0, uiSemValue) < 0) 
00087                         return false;
00088                     return true;
00089                 }
00090                 #else
00091                 {
00092                     uiSemaphore = uiSemValue;
00093                     return true;
00094                 }
00095                 #endif
00096 
00100             void Wait ()
00101                 #ifndef BSDSYS
00102                 { sem_wait(&semSemaphore); }
00103                 #else
00104                 {
00105                     while (uiSemaphore == 0) sched_yield();
00106                     uiSemaphore--;
00107                 }
00108                 #endif
00109 
00113             bool TryWait ()
00114                 #ifndef BSDSYS
00115                 { 
00116                     if (sem_trywait(&semSemaphore) < 0) return false;
00117                     return true;
00118                 }
00119                 #else
00120                 {
00121                     if (uiSemaphore > 0) uiSemaphore--;
00122                     else return false;
00123                     return true;
00124                 }
00125                 #endif
00126 
00131             bool Post ()
00132                 #ifndef BSDSYS
00133                 {
00134                     if (sem_post(&semSemaphore) < 0) return false;
00135                     return true;
00136                 }
00137                 #else
00138                 {
00139                     uiSemaphore++;
00140                     return true;
00141                 }
00142                 #endif
00143 
00148             int GetValue ()
00149                 #ifndef BSDSYS
00150                 {
00151                     int iSemValue;
00152                     sem_getvalue(&semSemaphore, &iSemValue);
00153                     return iSemValue;
00154                 }
00155                 #else
00156                 { return ((int) uiSemaphore); }
00157                 #endif
00158     };
00159     
00160 #endif
00161 

Generated on Tue Mar 2 19:46:45 2004 for libDSP by doxygen 1.3.6