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

Condition.hh

Go to the documentation of this file.
00001 /*
00002 
00003     Condition class, header
00004     Copyright (C) 1999-2003 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 USE_NPTL
00024 #include <pthread.h>
00025 #else
00026 #include <nptl/pthread.h>
00027 #endif
00028 #include <unistd.h>
00029 #include <time.h>
00030 #include <sys/time.h>
00031 
00032 
00033 #ifndef CONDITION_HH
00034     #define CONDITION_HH
00035 
00036 
00040     class clCondition
00041     {
00042             pthread_cond_t pthcCond;
00043         public:
00047             clCondition() { pthread_cond_init(&pthcCond, NULL); }
00051             ~clCondition() { pthread_cond_destroy(&pthcCond); }
00061             bool Wait(pthread_mutex_t *pthmCond)
00062                 {
00063                     if (pthread_cond_wait(&pthcCond, pthmCond) == 0)
00064                     {
00065                         return true;
00066                     }
00067                     else
00068                     {
00069                         return false;
00070                     }
00071                 }
00079             bool Wait(pthread_mutex_t *pthmCond, int iCondTO)
00080                 {
00081                     struct timeval sTimeVal;
00082                     struct timespec sTimeSpec;
00083                     gettimeofday(&sTimeVal, NULL);
00084                     sTimeSpec.tv_sec = sTimeVal.tv_sec + iCondTO / 1000;
00085                     sTimeSpec.tv_nsec = (sTimeVal.tv_usec + iCondTO * 1000) %
00086                         1000000 * 1000;
00087                     if (pthread_cond_timedwait(&pthcCond, pthmCond, 
00088                         &sTimeSpec) == 0)
00089                     {
00090                         return true;
00091                     }
00092                     else
00093                     {
00094                         return false;
00095                     }
00096                 }
00102             void Notify()
00103                 { pthread_cond_signal(&pthcCond); }
00109             void NotifyAll() 
00110                 { pthread_cond_broadcast(&pthcCond); }
00111     };
00112 
00113 #endif
00114 

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