1 /* 2 3 Firewall Builder 4 5 Copyright (C) 2000 NetCitadel, LLC 6 7 Author: Vadim Kurland vadim@fwbuilder.org 8 9 $Id$ 10 11 12 This program is free software which we release under the GNU General Public 13 License. You may redistribute and/or modify this program under the terms 14 of that license as published by the Free Software Foundation; either 15 version 2 of the License, or (at your option) any later version. 16 17 This program is distributed in the hope that it will be useful, 18 but WITHOUT ANY WARRANTY; without even the implied warranty of 19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 GNU General Public License for more details. 21 22 To get a copy of the GNU General Public License, write to the Free Software 23 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 24 25 */ 26 27 28 #ifndef __BACKGROUNDOP_HH_FLAG__ 29 #define __BACKGROUNDOP_HH_FLAG__ 30 31 #include <vector> 32 #include <iostream> 33 #ifdef __MINGW32__ //win32 pthread ditrib doesn't have config.h 34 # ifdef HAVE_CONFIG_H 35 # undef HAVE_CONFIG_H 36 # include <pthread.h> 37 # define HAVE_CONFIG_H 38 # endif 39 #else 40 # include <pthread.h> 41 #endif 42 43 #include "fwbuilder/FWException.h" 44 #include "fwbuilder/Tools.h" 45 #include "fwbuilder/ThreadTools.h" 46 #include "fwbuilder/Pool.h" 47 #include "fwbuilder/Logger.h" 48 49 // #include <sigc++/signal_system.h> 50 51 namespace libfwbuilder 52 { 53 54 void *background_thread(void *); 55 56 /** 57 * Abstract class BackgroundOp represents operatioin executed in background 58 */ 59 class BackgroundOp 60 { 61 friend void *background_thread(void *); 62 63 private: 64 65 SyncFlag running ; 66 SyncFlag connected ; 67 68 pthread_attr_t tattr; 69 70 protected: 71 72 SyncFlag *stop_program ; 73 74 FWException *error ; 75 SyncFlag *iamdead ; 76 77 /** 78 * Implementation of the actual operation. Use logger to send 79 * output text to the GUI. The SyncFlag is a mutex flag used to 80 * interrupt background operation. We can't keep this flag as a 81 * member of the class BackgroundOp because object of this class 82 * gets destroyed before actual background operation has finished 83 * (especially if it got stuck in a system call). To avoid having 84 * to use object of the class BackgroundOp and any of its members 85 * or methods, we create this flag as a dynamic variable and pass 86 * pointer to run_impl, which should destroy it when it finishes. 87 */ 88 virtual void run_impl(Logger *,SyncFlag *) throw(FWException) = 0; 89 90 /** 91 * sets flag "running" 92 */ 93 void setRunning(); 94 95 /** 96 * clears flag "running" 97 */ 98 void clearRunning(); 99 100 /** 101 * checks "stop" flag and terminates thread if it is set. Used 102 * from inside run_impl to check if background operation should be 103 * immediately interrupted 104 */ 105 #define CHECK_STOP_AND_RETURN { stop_program->lock();\ 106 if ( stop_program->peek() ){ stop_program->unlock(); return; }\ 107 stop_program->unlock(); } 108 109 #define CHECK_STOP_AND_THROW_EXCEPTION { stop_program->lock();\ 110 if ( stop_program->peek() ){ stop_program->unlock(); throw FWException("Interrupted"); }\ 111 stop_program->unlock(); } 112 113 public: 114 115 BackgroundOp(); 116 virtual ~BackgroundOp(); 117 118 /** 119 * checks whether background operation is connected to GUI 120 */ 121 bool isConnected(); 122 123 /** 124 * disconnects background operation from the GUI 125 */ 126 void disconnect(); 127 128 /** 129 * Initiates background operation 130 */ 131 virtual Logger* start_operation() throw(FWException); 132 133 /** 134 * Stops background operation 135 */ 136 virtual void stop_operation(); 137 138 /** 139 * returns flag "running" 140 */ 141 bool isRunning(); 142 143 /** 144 * returns last error from the background operation. The meaning of 145 * the error is determined by the operation 146 */ get_latest_error()147 FWException *get_latest_error() { return error; } 148 149 }; 150 151 } 152 153 #endif // __BACKGROUNDOP_HH_FLAG__ 154