log4cplus
1.1.0
|
00001 // -*- C++ -*- 00002 // Module: Log4CPLUS 00003 // File: threads.h 00004 // Created: 6/2001 00005 // Author: Tad E. Smith 00006 // 00007 // 00008 // Copyright 2001-2010 Tad E. Smith 00009 // 00010 // Licensed under the Apache License, Version 2.0 (the "License"); 00011 // you may not use this file except in compliance with the License. 00012 // You may obtain a copy of the License at 00013 // 00014 // http://www.apache.org/licenses/LICENSE-2.0 00015 // 00016 // Unless required by applicable law or agreed to in writing, software 00017 // distributed under the License is distributed on an "AS IS" BASIS, 00018 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00019 // See the License for the specific language governing permissions and 00020 // limitations under the License. 00021 00024 #ifndef LOG4CPLUS_IMPL_THREADS_IMPL_HEADER_ 00025 #define LOG4CPLUS_IMPL_THREADS_IMPL_HEADER_ 00026 00027 #include <log4cplus/config.hxx> 00028 00029 #if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE) 00030 #pragma once 00031 #endif 00032 00033 #if defined (_WIN32) 00034 #include <log4cplus/config/windowsh-inc.h> 00035 #endif 00036 #include <log4cplus/tstring.h> 00037 #include <log4cplus/helpers/pointer.h> 00038 #include <log4cplus/thread/threads.h> 00039 00040 #if ! defined (INSIDE_LOG4CPLUS) 00041 # error "This header must not be be used outside log4cplus' implementation files." 00042 #endif 00043 00044 00045 namespace log4cplus { namespace thread { namespace impl { 00046 00047 00048 #if defined (LOG4CPLUS_USE_PTHREADS) 00049 00050 typedef pthread_t os_handle_type; 00051 typedef pthread_t os_id_type; 00052 00053 00054 inline 00055 pthread_t 00056 getCurrentThreadId () 00057 { 00058 return pthread_self (); 00059 } 00060 00061 00062 #elif defined (LOG4CPLUS_USE_WIN32_THREADS) 00063 00064 typedef HANDLE os_handle_type; 00065 typedef DWORD os_id_type; 00066 00067 00068 inline 00069 DWORD 00070 getCurrentThreadId () 00071 { 00072 return GetCurrentThreadId (); 00073 } 00074 00075 00076 #elif defined (LOG4CPLUS_SINGLE_THREADED) 00077 00078 typedef void * os_handle_type; 00079 typedef int os_id_type; 00080 00081 00082 inline 00083 int 00084 getCurrentThreadId () 00085 { 00086 return 1; 00087 } 00088 00089 00090 #endif 00091 00092 00093 #ifndef LOG4CPLUS_SINGLE_THREADED 00094 00095 00096 struct ThreadStart 00097 { 00098 # ifdef LOG4CPLUS_USE_PTHREADS 00099 static void* threadStartFuncWorker(void *); 00100 # elif defined(LOG4CPLUS_USE_WIN32_THREADS) 00101 static unsigned threadStartFuncWorker(void *); 00102 # endif 00103 }; 00104 00105 00112 class Thread 00113 : public ThreadImplBase 00114 { 00115 public: 00116 Thread(); 00117 bool isRunning() const; 00118 os_id_type getThreadId() const; 00119 os_handle_type getThreadHandle () const; 00120 void start(); 00121 void join (); 00122 00123 protected: 00124 // Force objects to be constructed on the heap 00125 virtual ~Thread(); 00126 virtual void run() = 0; 00127 00128 private: 00129 // Friends. 00130 friend struct ThreadStart; 00131 00132 enum Flags 00133 { 00134 fRUNNING = 0x01, 00135 fJOINED = 0x02 00136 }; 00137 00138 unsigned flags; 00139 00140 os_handle_type handle; 00141 00142 # if defined(LOG4CPLUS_USE_WIN32_THREADS) 00143 unsigned thread_id; 00144 # endif 00145 00146 // Disallow copying of instances of this class. 00147 Thread(const Thread&); 00148 Thread& operator=(const Thread&); 00149 }; 00150 00151 typedef helpers::SharedObjectPtr<Thread> ThreadPtr; 00152 00153 00154 #endif // LOG4CPLUS_SINGLE_THREADED 00155 00156 00157 } } } // namespace log4cplus { namespace thread { namespace impl { 00158 00159 00160 #endif // LOG4CPLUS_IMPL_THREADS_IMPL_HEADER_