Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef _LOG4CPP_THREADING_PTHREADS_HH
00010 #define _LOG4CPP_THREADING_PTHREADS_HH
00011
00012 #include <log4cpp/Portability.hh>
00013 #include <stdio.h>
00014 #include <pthread.h>
00015 #include <string>
00016 #include <assert.h>
00017
00018
00019 namespace log4cpp {
00020 namespace threading {
00021
00025 std::string getThreadId();
00026
00032 char* getThreadId(char* buffer);
00033
00036 class Mutex {
00037 private:
00038 pthread_mutex_t mutex;
00039
00040 public:
00041 inline Mutex() {
00042 ::pthread_mutex_init(&mutex, NULL);
00043 }
00044
00045 inline void lock() {
00046 ::pthread_mutex_lock(&mutex);
00047 }
00048
00049 inline void unlock() {
00050 ::pthread_mutex_unlock(&mutex);
00051 }
00052
00053 inline ~Mutex() {
00054 ::pthread_mutex_destroy(&mutex);
00055 }
00056
00057 private:
00058 Mutex(const Mutex& m);
00059 Mutex& operator=(const Mutex &m);
00060 };
00061
00065 class ScopedLock {
00066 private:
00067 Mutex& _mutex;
00068
00069 public:
00070 inline ScopedLock(Mutex& mutex) :
00071 _mutex(mutex) {
00072 _mutex.lock();
00073 }
00074
00075 inline ~ScopedLock() {
00076 _mutex.unlock();
00077 }
00078 };
00079
00083 template<typename T> class ThreadLocalDataHolder {
00084 private:
00085 pthread_key_t _key;
00086
00087 public:
00088 typedef T data_type;
00089
00090 inline ThreadLocalDataHolder() {
00091 ::pthread_key_create(&_key, freeHolder);
00092 }
00093
00094 inline static void freeHolder(void *p) {
00095 assert(p != NULL);
00096 delete reinterpret_cast<T *>(p);
00097 }
00098
00099 inline ~ThreadLocalDataHolder() {
00100 T *data = get();
00101 if (data != NULL) {
00102 delete data;
00103 }
00104 ::pthread_key_delete(_key);
00105 }
00106
00107 inline T* get() const {
00108 return reinterpret_cast<T *>(::pthread_getspecific(_key));
00109 }
00110
00111 inline T* operator->() const { return get(); }
00112 inline T& operator*() const { return *get(); }
00113
00114 inline T* release() {
00115 T* result = get();
00116 ::pthread_setspecific(_key, NULL);
00117
00118 return result;
00119 }
00120
00121 inline void reset(T* p = NULL) {
00122 T *data = get();
00123 if (data != NULL) {
00124 delete data;
00125 }
00126 ::pthread_setspecific(_key, p);
00127 }
00128 };
00129
00130 }
00131 }
00132 #endif
log4cpp
Author(s): Stephen Roderick, Bastiaan Bakker, Cedric Le Goater, Steve Ostlind, Marcel Harkema, Walter Stroebel, Glenn Scott and Tony Cheung
autogenerated on Sun Aug 14 2016 03:18:26