1 /* 2 KHOMP generic endpoint/channel library. 3 Copyright (C) 2007-2009 Khomp Ind. & Com. 4 5 The contents of this file are subject to the Mozilla Public License Version 1.1 6 (the "License"); you may not use this file except in compliance with the 7 License. You may obtain a copy of the License at http://www.mozilla.org/MPL/ 8 9 Software distributed under the License is distributed on an "AS IS" basis, 10 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for 11 the specific language governing rights and limitations under the License. 12 13 Alternatively, the contents of this file may be used under the terms of the 14 "GNU Lesser General Public License 2.1" license (the “LGPL" License), in which 15 case the provisions of "LGPL License" are applicable instead of those above. 16 17 If you wish to allow use of your version of this file only under the terms of 18 the LGPL License and not to allow others to use your version of this file under 19 the MPL, indicate your decision by deleting the provisions above and replace them 20 with the notice and other provisions required by the LGPL License. If you do not 21 delete the provisions above, a recipient may use your version of this file under 22 either the MPL or the LGPL License. 23 24 The LGPL header follows below: 25 26 This library is free software; you can redistribute it and/or 27 modify it under the terms of the GNU Lesser General Public 28 License as published by the Free Software Foundation; either 29 version 2.1 of the License, or (at your option) any later version. 30 31 This library is distributed in the hope that it will be useful, 32 but WITHOUT ANY WARRANTY; without even the implied warranty of 33 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 34 Lesser General Public License for more details. 35 36 You should have received a copy of the GNU Lesser General Public License 37 along with this library; if not, write to the Free Software Foundation, Inc., 38 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 39 40 */ 41 42 #ifndef _SAVED_CONDITION_ 43 #define _SAVED_CONDITION_ 44 45 #include <saved_condition.hpp> 46 47 extern "C" 48 { 49 #include <switch.h> 50 } 51 52 struct SavedCondition : public SavedConditionCommon// : public RefCounter < SavedCondition > 53 { 54 typedef switch_thread_cond_t BaseConditionType; 55 typedef switch_mutex_t BaseMutexType; 56 SavedConditionSavedCondition57 SavedCondition(switch_memory_pool_t *pool=NULL): 58 _pool(pool), 59 _can_delete_pool(false) 60 { 61 if(!_pool) 62 { 63 switch_core_new_memory_pool(&_pool); 64 _can_delete_pool = true; 65 } 66 67 switch_thread_cond_create(&_condition, _pool); 68 switch_mutex_init(&_mutex, SWITCH_MUTEX_DEFAULT, _pool); 69 } 70 71 //SavedCondition(const SavedCondition &); ~SavedConditionSavedCondition72 ~SavedCondition() 73 { 74 switch_thread_cond_destroy(_condition); 75 switch_mutex_destroy(_mutex); 76 77 if(_can_delete_pool) 78 switch_core_destroy_memory_pool(&_pool); 79 } 80 signalSavedCondition81 void signal(void) 82 { 83 switch_mutex_lock(_mutex); 84 85 _signaled = true; 86 switch_thread_cond_signal(_condition); 87 88 switch_mutex_unlock(_mutex); 89 } 90 broadcastSavedCondition91 void broadcast(void) 92 { 93 switch_mutex_lock(_mutex); 94 95 _signaled = true; 96 switch_thread_cond_broadcast(_condition); 97 98 switch_mutex_unlock(_mutex); 99 } 100 waitSavedCondition101 void wait(void) 102 { 103 switch_mutex_lock(_mutex); 104 105 if (!_signaled) 106 switch_thread_cond_wait(_condition, _mutex); 107 108 _signaled = false; 109 110 switch_mutex_unlock(_mutex); 111 } 112 113 bool wait(unsigned int); 114 resetSavedCondition115 void reset(void) 116 { 117 switch_mutex_lock(_mutex); 118 119 _signaled = false; 120 121 switch_mutex_unlock(_mutex); 122 } 123 mutexSavedCondition124 BaseMutexType * mutex() { return _mutex; }; conditionSavedCondition125 BaseConditionType * condition() { return _condition; }; 126 127 protected: 128 129 BaseConditionType *_condition; 130 BaseMutexType *_mutex; 131 switch_memory_pool_t *_pool; 132 bool _can_delete_pool; 133 }; 134 135 #endif /* _SAVED_CONDITION_ */ 136 137