00001 // Module: Log4CPLUS 00002 // File: threads.h 00003 // Created: 6/2001 00004 // Author: Tad E. Smith 00005 // 00006 // 00007 // Copyright 2001-2010 Tad E. Smith 00008 // 00009 // Licensed under the Apache License, Version 2.0 (the "License"); 00010 // you may not use this file except in compliance with the License. 00011 // You may obtain a copy of the License at 00012 // 00013 // http://www.apache.org/licenses/LICENSE-2.0 00014 // 00015 // Unless required by applicable law or agreed to in writing, software 00016 // distributed under the License is distributed on an "AS IS" BASIS, 00017 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00018 // See the License for the specific language governing permissions and 00019 // limitations under the License. 00020 00023 #ifndef _LOG4CPLUS_THREADS_HEADER_ 00024 #define _LOG4CPLUS_THREADS_HEADER_ 00025 00026 #include "dcmtk/oflog/config.h" 00027 #include "dcmtk/oflog/tstring.h" 00028 #include "dcmtk/oflog/helpers/pointer.h" 00029 00030 00031 namespace log4cplus { namespace thread { 00032 00036 class Guard 00037 { 00038 public: 00040 Guard(LOG4CPLUS_MUTEX_PTR_DECLARE mutex) 00041 : _mutex (mutex) 00042 { 00043 LOG4CPLUS_MUTEX_LOCK( _mutex ); 00044 } 00045 00047 ~Guard() 00048 { 00049 LOG4CPLUS_MUTEX_UNLOCK( _mutex ); 00050 } 00051 00052 private: 00053 LOG4CPLUS_MUTEX_PTR_DECLARE _mutex; 00054 00055 // disable copy 00056 Guard(const Guard&); 00057 Guard& operator=(const Guard&); 00058 }; 00059 00060 00061 #ifndef LOG4CPLUS_SINGLE_THREADED 00062 00063 LOG4CPLUS_EXPORT void blockAllSignals(); 00064 LOG4CPLUS_EXPORT tstring getCurrentThreadName(); 00065 00066 00067 struct ThreadStart; 00068 00069 00076 class LOG4CPLUS_EXPORT AbstractThread 00077 : public virtual log4cplus::helpers::SharedObject 00078 { 00079 public: 00080 AbstractThread(); 00081 bool isRunning() const { return running; } 00082 LOG4CPLUS_THREAD_KEY_TYPE getThreadId() const; 00083 LOG4CPLUS_THREAD_HANDLE_TYPE getThreadHandle () const; 00084 virtual void start(); 00085 void join () const; 00086 00087 protected: 00088 // Force objects to be constructed on the heap 00089 virtual ~AbstractThread(); 00090 virtual void run() = 0; 00091 00092 private: 00093 bool running; 00094 00095 // Friends. 00096 friend struct ThreadStart; 00097 00098 # ifdef LOG4CPLUS_USE_PTHREADS 00099 pthread_t handle; 00100 00101 # elif defined(LOG4CPLUS_USE_WIN32_THREADS) 00102 HANDLE handle; 00103 unsigned thread_id; 00104 00105 # endif 00106 00107 // Disallow copying of instances of this class. 00108 AbstractThread(const AbstractThread&); 00109 AbstractThread& operator=(const AbstractThread&); 00110 }; 00111 00112 typedef helpers::SharedObjectPtr<AbstractThread> AbstractThreadPtr; 00113 00114 00115 #endif // LOG4CPLUS_SINGLE_THREADED 00116 00117 00118 } } // namespace log4cplus { namespace thread { 00119 00120 00121 #endif // _LOG4CPLUS_THREADS_HEADER_ 00122