CondVar.hh

00001 //********************************************************************
00002 //*** Thread/CondVar.hh
00003 //*** Copyright (c) 2003-2009 by Markus Winand <mws@fatalmind.com>
00004 //*** $Id: CondVar.hh,v 1.25 2009-03-02 14:16:46 mws Exp $
00005 //********************************************************************
00006 /*
00007 This file is part of ResourcePool.
00008 
00009 ResourcePool is free software; you can redistribute it
00010 and/or modify it under the terms of the GNU General Public License
00011 as published by the Free Software Foundation; either version 2 of 
00012 the License, or (at your option) any later version.
00013 
00014 ResourcePool is distributed in the hope that it will be
00015 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
00016 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 GNU General Public License for more details.
00018 
00019 You should have received a copy of the GNU General Public License
00020 along with ResourcePool; if not, write to the Free Software
00021 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00022 02111-1307  USA */
00023 
00024 #ifndef INCLUDED_CONDVAR_H
00025 #define INCLUDED_CONDVAR_H
00026 
00027 
00028 #ifndef INCLUDED_CONDVARFWD_H
00029 #include "CondVarFwd.hh"
00030 #endif
00031 
00032 #ifndef INCLUDED_POSIXMUTEX_H
00033 #include "PosixMutex.hh"
00034 #endif
00035 
00036 #ifndef INCLUDED_ERRNO_H
00037 #include "Exceptions/Errno.hh"
00038 #endif
00039 
00040 #ifndef POSIX_TIMEFWD_HH
00041 #include "POSIX/timeFwd.hh"
00042 #endif
00043 
00044 #include "LockFwd.hh"
00045 
00046 // TODO: should not be public visible
00047 #include <pthread.h>
00048 
00049 
00050 namespace fatalmind {
00051 
00052 template<class MutexType>
00053 class CondVar: public MutexType {
00054         typedef MutexType super;
00055         using super::d_mutex;
00056     public:
00057         CondVar() : MutexType() {
00058             Errno::ThrowOnError(
00059                 pthread_cond_init(&d_condvar, NULL)
00060             );  
00061         }
00062 
00063         ~CondVar() {
00064             pthread_cond_destroy(&d_condvar);
00065         }
00066 
00067         template<class TM>
00068         bool wait(const POSIX::timespec<TM>*const timeout);
00069 
00070         bool waitAbsolute(const POSIX::timespec<SingleThreadedModel<DefaultLockStatistics> >& timeout);
00071 
00072         void wait() {
00073             super::stat_unlock();
00074             Errno::ThrowOnError(
00075                 pthread_cond_wait(&d_condvar, &d_mutex)
00076             );  
00077             super::stat_lock();
00078         }
00079 
00080         void signal() {
00081             Errno::ThrowOnError(
00082                 pthread_cond_signal(&d_condvar)
00083             );
00084         }
00085 
00086         void broadcast() {
00087             Errno::ThrowOnError(
00088                 pthread_cond_broadcast(&d_condvar)
00089             );
00090         }
00091     protected:
00092         pthread_cond_t      d_condvar;
00093 
00094         bool _waitAbsolute(const POSIX::timespec<SingleThreadedModel<DefaultLockStatistics> >* timeout);
00095 
00096 };
00097 
00098 } // namespace fatalmind
00099 
00100 #include "Lock.hh"
00101 
00102 #endif

Generated on Mon Nov 9 16:21:24 2009 for ResourcePool by  doxygen 1.5.3