log4cplus
2.0.0
|
00001 // -*- C++ -*- 00002 // Module: Log4CPLUS 00003 // File: threads.h 00004 // Created: 6/2001 00005 // Author: Tad E. Smith 00006 // 00007 // 00008 // Copyright 2001-2015 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_THREADS_HEADER_ 00025 #define LOG4CPLUS_THREADS_HEADER_ 00026 00027 #include <log4cplus/config.hxx> 00028 00029 #if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE) 00030 #pragma once 00031 #endif 00032 00033 #include <memory> 00034 #include <thread> 00035 00036 #include <log4cplus/tstring.h> 00037 #include <log4cplus/helpers/pointer.h> 00038 00039 00040 namespace log4cplus { namespace thread { 00041 00042 00043 LOG4CPLUS_EXPORT log4cplus::tstring const & getCurrentThreadName(); 00044 LOG4CPLUS_EXPORT log4cplus::tstring const & getCurrentThreadName2(); 00045 LOG4CPLUS_EXPORT void setCurrentThreadName(const log4cplus::tstring & name); 00046 LOG4CPLUS_EXPORT void setCurrentThreadName2(const log4cplus::tstring & name); 00047 LOG4CPLUS_EXPORT void yield(); 00048 LOG4CPLUS_EXPORT void blockAllSignals(); 00049 00050 00051 #ifndef LOG4CPLUS_SINGLE_THREADED 00052 00053 00060 class LOG4CPLUS_EXPORT AbstractThread 00061 : public virtual log4cplus::helpers::SharedObject 00062 { 00063 public: 00064 AbstractThread(); 00065 // Disallow copying of instances of this class. 00066 AbstractThread(const AbstractThread&) = delete; 00067 AbstractThread& operator=(const AbstractThread&) = delete; 00068 00069 bool isRunning() const; 00070 virtual void start(); 00071 void join () const; 00072 virtual void run() = 0; 00073 00074 protected: 00075 // Force objects to be constructed on the heap 00076 virtual ~AbstractThread(); 00077 00078 private: 00079 enum Flags 00080 { 00081 fRUNNING = 1, 00082 fJOINED = 2 00083 }; 00084 00085 std::unique_ptr<std::thread> thread; 00086 mutable std::atomic<int> flags; 00087 }; 00088 00089 typedef helpers::SharedObjectPtr<AbstractThread> AbstractThreadPtr; 00090 00091 00092 #endif // LOG4CPLUS_SINGLE_THREADED 00093 00094 00095 } } // namespace log4cplus { namespace thread { 00096 00097 00098 #endif // LOG4CPLUS_THREADS_HEADER_