1 /* 2 * Copyright (C) 2002-2003 Fhg Fokus 3 * 4 * This file is part of SEMS, a free SIP media server. 5 * 6 * SEMS is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. This program is released under 10 * the GPL with the additional exemption that compiling, linking, 11 * and/or using OpenSSL is allowed. 12 * 13 * For a license to use the SEMS software under conditions 14 * other than those described here, or to purchase support for this 15 * software, please contact iptel.org by e-mail at the following addresses: 16 * info@iptel.org 17 * 18 * SEMS is distributed in the hope that it will be useful, 19 * but WITHOUT ANY WARRANTY; without even the implied warranty of 20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21 * GNU General Public License for more details. 22 * 23 * You should have received a copy of the GNU General Public License 24 * along with this program; if not, write to the Free Software 25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 26 */ 27 /** @file AmEventQueue.h */ 28 #ifndef _AMEVENTQUEUE_H_ 29 #define _AMEVENTQUEUE_H_ 30 31 #include "AmThread.h" 32 #include "AmEvent.h" 33 #include "atomic_types.h" 34 35 #include <queue> 36 37 class AmEventQueueInterface 38 { 39 public: ~AmEventQueueInterface()40 virtual ~AmEventQueueInterface() {} 41 virtual void postEvent(AmEvent*)=0; 42 }; 43 44 class AmEventQueue; 45 /** a receiver for notifications about 46 the fact that events are pending */ 47 class AmEventNotificationSink 48 { 49 public: ~AmEventNotificationSink()50 virtual ~AmEventNotificationSink() { } 51 virtual void notify(AmEventQueue* sender) = 0; 52 }; 53 54 /** 55 * \brief Asynchronous event queue implementation 56 * 57 * This class implements an event queue; a queue into which 58 * \ref AmEvent can safely be posted at any time from any 59 * thread, which are then processed by the registered event 60 * handler. 61 */ 62 class AmEventQueue 63 : public AmEventQueueInterface, 64 public atomic_ref_cnt 65 { 66 protected: 67 AmEventHandler* handler; 68 AmEventNotificationSink* wakeup_handler; 69 70 std::queue<AmEvent*> ev_queue; 71 AmMutex m_queue; 72 AmCondition<bool> ev_pending; 73 74 bool finalized; 75 76 public: 77 AmEventQueue(AmEventHandler* handler); 78 virtual ~AmEventQueue(); 79 80 void postEvent(AmEvent*); 81 void processEvents(); 82 void waitForEvent(); 83 void processSingleEvent(); 84 bool eventPending(); 85 86 void setEventNotificationSink(AmEventNotificationSink* _wakeup_handler); 87 is_finalized()88 bool is_finalized() { return finalized; } 89 90 // return true to continue processing startup()91 virtual bool startup() { return true; } processingCycle()92 virtual bool processingCycle() { processEvents(); return true; } finalize()93 virtual void finalize() { finalized = true; } 94 }; 95 96 #endif 97 98