00001 /* 00002 wefts_subscription.h 00003 Subscribe + notify system implementation 00004 00005 $Id: wefts_subscription.h,v 1.2 2003/08/05 15:40:20 jonnymind Exp $ 00006 ---------------------------------------------- 00007 Begin : 2003-08-02 00:10 00008 Author : Giancarlo Niccolai 00009 00010 Last modified because: 00011 00012 */ 00013 00014 /************************************************************************** 00015 * This program is free software; you can redistribute it and/or modify * 00016 * it under the terms of the GNU Library General Public License as * 00017 * published by the Free Software Foundation; either version 2.1 of the * 00018 * License, or (at your option) any later version. * 00019 ***************************************************************************/ 00020 00021 #ifndef WT_SUBSCRIPTION_H 00022 #define WT_SUBSCRIPTION_H 00023 00024 #include <wefts_fastcond.h> 00025 #include <wefts_referenced.h> 00026 #include <list> 00027 00028 namespace Wefts { 00029 00030 00112 class Subscription 00113 { 00114 private: 00115 FastCondition m_cond; 00116 std::list<Referenced *> m_items; 00117 std::list<pthread_t> m_waiting; 00119 bool m_front; 00120 00121 bool subscribeInternal( double time, Referenced **result ); 00122 00123 public: 00126 Subscription( bool fairPolicy = true) { m_front = fairPolicy; } 00127 00142 bool subscribe( double time = 0, Referenced **result=0 ); 00143 00153 bool subscribeNow( double time = 0, Referenced ** result=0); 00154 00165 void notify( Referenced *data = 0 ); 00166 00182 void notifyAll( Referenced *data = 0 ); 00183 00188 int subscribers() { return m_waiting.size(); } 00189 00197 void setPolicy( bool fair ) 00198 { 00199 m_cond.lock(); 00200 m_front = fair; // some subscriber may have waited for that. 00201 m_cond.signal(); 00202 m_cond.unlock(); 00203 } 00204 }; 00205 00206 } 00207 00208 #endif 00209 /* end of wefts_subscription.h */