condition.H

00001 /*
00002  * This file is part of the "Archon" framework.
00003  * (http://files3d.sourceforge.net)
00004  *
00005  * Copyright © 2002 by Kristian Spangsege and Brian Kristiansen.
00006  *
00007  * Permission to use, copy, modify, and distribute this software and
00008  * its documentation under the terms of the GNU General Public License is
00009  * hereby granted. No representations are made about the suitability of
00010  * this software for any purpose. It is provided "as is" without express
00011  * or implied warranty. See the GNU General Public License
00012  * (http://www.gnu.org/copyleft/gpl.html) for more details.
00013  *
00014  * The characters in this file are ISO8859-1 encoded.
00015  *
00016  * The documentation in this file is in "Doxygen" style
00017  * (http://www.doxygen.org).
00018  */
00019 
00020 #ifndef ARCHON_UTILITIES_CONDITION_H
00021 #define ARCHON_UTILITIES_CONDITION_H
00022 
00023 #include <list>
00024 
00025 #include <sys/select.h> // fd_set
00026 
00027 #include <archon/util/exception.H>
00028 #include <archon/util/time.H>
00029 #include <archon/util/mutex.H>
00030 
00031 namespace Archon
00032 {
00033   namespace Utilities
00034   {
00035     using namespace std;
00036 
00041     struct ThreadTerminatedException: UnexpectedException
00042     {
00043       ThreadTerminatedException(string l): UnexpectedException(l) {}
00044     };
00045 
00049     struct Condition
00050     {
00051       Condition(Mutex &m);
00052       ~Condition();
00053 
00054       void notifyOne()
00055       {
00056         const int e = pthread_cond_signal(&cond);
00057         if(e != 0)
00058           ARCHON_THROW1(InternalException,
00059                         "Attempt to signal on condition failed");
00060         Mutex::Lock l(sigThreadsMutex);
00061         if(sigThreads.size()) notifySigThreads();
00062       }
00063 
00064       void notifyAll()
00065       {
00066         const int e = pthread_cond_broadcast(&cond);
00067         if(e != 0)
00068           ARCHON_THROW1(InternalException,
00069                         "Attempt to broadcast on condition failed");
00070         Mutex::Lock l(sigThreadsMutex);
00071         if(sigThreads.size()) notifySigThreads();
00072       }
00073 
00084       void wait() throw(UnexpectedException);
00085 
00100       bool timedWait(Time timeout) throw(UnexpectedException);
00101 
00122       int select(int n, fd_set *readfds, fd_set *writefds,
00123                  fd_set *exceptfds, Time timeout)
00124         throw(UnexpectedException);
00125 
00126     private:
00127       friend struct Thread;
00128 
00129       pthread_cond_t cond;
00130       Mutex *mutex;
00131 
00132       Mutex sigThreadsMutex;
00133       list<pthread_t> sigThreads;
00134 
00135       Condition(const Condition &); // Hide
00136       Condition &operator=(const Condition &); // Hide
00137 
00138       void notifySigThreads();
00139     };
00140   }
00141 }
00142 
00143 #endif // ARCHON_UTILITIES_CONDITION_H

Generated on Sun Jul 30 22:55:44 2006 for Archon by  doxygen 1.4.4