API Documentation
00001 // Module: Log4CPLUS 00002 // File: threads.h 00003 // Created: 6/2001 00004 // Author: Tad E. Smith 00005 // 00006 // 00007 // Copyright (C) Tad E. Smith All rights reserved. 00008 // 00009 // This software is published under the terms of the Apache Software 00010 // License version 1.1, a copy of which has been included with this 00011 // distribution in the LICENSE.APL file. 00012 // 00013 00016 #ifndef _LOG4CPLUS_THREADS_HEADER_ 00017 #define _LOG4CPLUS_THREADS_HEADER_ 00018 00019 #include <log4cplus/config.h> 00020 #include <log4cplus/tstring.h> 00021 #include <log4cplus/helpers/sleep.h> 00022 #include <log4cplus/helpers/pointer.h> 00023 #include <memory> 00024 00025 00026 namespace log4cplus { 00027 namespace thread { 00028 00032 class LOG4CPLUS_EXPORT Guard { 00033 public: 00035 Guard(LOG4CPLUS_MUTEX_PTR_DECLARE mutex) { 00036 LOG4CPLUS_MUTEX_ASSIGN( _mutex, mutex ); 00037 LOG4CPLUS_MUTEX_LOCK( _mutex ); 00038 } 00039 00041 ~Guard() { LOG4CPLUS_MUTEX_UNLOCK( _mutex ); } 00042 00043 private: 00044 LOG4CPLUS_MUTEX_PTR_DECLARE _mutex; 00045 00046 // disable copy 00047 Guard(const Guard&); 00048 Guard& operator=(const Guard&); 00049 }; 00050 00051 #ifndef LOG4CPLUS_SINGLE_THREADED 00052 #ifdef LOG4CPLUS_USE_PTHREADS 00053 void* threadStartFunc(void*); 00054 #elif defined(LOG4CPLUS_USE_WIN32_THREADS) 00055 DWORD WINAPI threadStartFunc(LPVOID arg); 00056 #endif 00057 00058 LOG4CPLUS_EXPORT void yield(); 00059 LOG4CPLUS_EXPORT log4cplus::tstring getCurrentThreadName(); 00060 00067 class LOG4CPLUS_EXPORT AbstractThread : public log4cplus::helpers::SharedObject { 00068 public: 00069 AbstractThread(); 00070 bool isRunning() { return running; } 00071 LOG4CPLUS_THREAD_KEY_TYPE getThreadId() { return threadId; } 00072 virtual void start(); 00073 00074 protected: 00075 // Force objects to be constructed on the heap 00076 virtual ~AbstractThread(); 00077 virtual void run() = 0; 00078 00079 private: 00080 bool running; 00081 LOG4CPLUS_THREAD_KEY_TYPE threadId; 00082 00083 // Disallow copying of instances of this class 00084 AbstractThread(const AbstractThread&); 00085 AbstractThread& operator=(const AbstractThread&); 00086 00087 // Friends 00088 #ifdef LOG4CPLUS_USE_PTHREADS 00089 friend void* threadStartFunc(void*); 00090 #elif defined(LOG4CPLUS_USE_WIN32_THREADS) 00091 friend DWORD WINAPI threadStartFunc(LPVOID arg); 00092 #endif 00093 00094 }; 00095 #endif // LOG4CPLUS_SINGLE_THREADED 00096 00097 } // end namespace thread 00098 } // end namespace log4cplus 00099 00100 00101 #endif // _LOG4CPLUS_THREADS_HEADER_ 00102