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

FXCondition.h

Go to the documentation of this file.
00001 /********************************************************************************
00002 *                                                                               *
00003 *               Condition Variable                                              *
00004 *                                                                               *
00005 *********************************************************************************
00006 * Copyright (C) 2003 by Mathew Robertson.   All Rights Reserved.                *
00007 *********************************************************************************
00008 * This library is free software; you can redistribute it and/or                 *
00009 * modify it under the terms of the GNU Lesser General Public                    *
00010 * License as published by the Free Software Foundation; either                  *
00011 * version 2.1 of the License, or (at your option) any later version.            *
00012 *                                                                               *
00013 * This library is distributed in the hope that it will be useful,               *
00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of                *
00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU             *
00016 * Lesser General Public License for more details.                               *
00017 *                                                                               *
00018 * You should have received a copy of the GNU Lesser General Public              *
00019 * License along with this library; if not, write to the Free Software           *
00020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.    *
00021 ********************************************************************************/
00022 #ifndef FXCONDITION_H
00023 #define FXCONDITION_H
00024 
00025 #ifndef FXWAITABLE_H
00026 #include "FXWaitable.h"
00027 #endif
00028 namespace FXEX {
00029 class FXMutex;
00030 
00031 /**
00032  * A condition variable is a mechanism by which a programmer can synchronise
00033  * multiple threads (those waiting in wait())
00034  */
00035 class FXAPI FXCondition : public FXWaitable {
00036   FXDECLARE(FXCondition)
00037 
00038 private:
00039   FXThreadCondition  condition;      // sinalling object
00040   FXMutex           *conditionMutex; // points to real application mutex
00041 
00042 private:
00043   // dummy copy constructor and operator= to prevent copying
00044   FXCondition(const FXCondition&);
00045   FXCondition& operator=(const FXCondition&);
00046   
00047 public:
00048   /**
00049    * Constructor must be given a pointer to an existing mutex. The
00050    * condition variable is then linked to the mutex, so that there is an
00051    * implicit unlock and lock around wait().
00052    * 
00053    * If you specify a target (such as the main window), a broadcast will
00054    * also generate an event to that target, on broadcast.
00055    */
00056   FXCondition(FXMutex* m=NULL,FXObject *tgt=NULL,FXSelector sel=0);
00057 
00058   /// wait for the condition variable to be signalled.
00059   void wait();
00060   
00061   /**
00062    * Wait for the condition variable to be signalled.  The mutex is
00063    * implicitly released before waiting and locked again after waking up.
00064    * If wait() is called by multiple threads, a signal may wake up more
00065    * than one thread.  wait() can be given a time to wait until.
00066    * a time of zero, causes it to wait indefinately, thus always returns
00067    * true once it has been signaled.
00068    */
00069   FXbool trywait(FXuint ms);
00070   
00071   /**
00072    * If one or more threads have called wait(), activate() wakes up at least
00073    * one of them, possibly more.
00074    */
00075   void activate();
00076   
00077   /// activateAll() is like activate() but wakes all threads which have called wait().
00078   void activateAll();
00079   
00080   /**
00081    * A condition variable requires a pointer to a mutex, we allow the CV
00082    * to be set to other mutex's.  Implicit activateAll so as to ensure that there
00083    * are no mutex's waiting on us.
00084    */
00085   void setMutex(FXMutex *m);
00086 
00087   /// dtor - implicit activateAll() on destruction
00088   virtual ~FXCondition();
00089   };
00090 
00091 } // namespace FXEX
00092 #endif // FXCONDITION_H