log4shib 2.0.0

PThreads.hh
Go to the documentation of this file.
1/*
2 * PThreads.hh
3 *
4 * Copyright 2002, Emiliano Martin emilianomc@terra.es All rights reserved.
5 *
6 * See the COPYING file for the terms of usage and distribution.
7 */
8
9#ifndef _LOG4SHIB_THREADING_PTHREADS_HH
10#define _LOG4SHIB_THREADING_PTHREADS_HH
11
12#include <log4shib/Portability.hh>
13#include <stdio.h>
14#include <pthread.h>
15#include <string>
16#include <assert.h>
17
18
19 namespace log4shib {
20 namespace threading {
21
25 std::string getThreadId();
26
29 class Mutex {
30 private:
31 pthread_mutex_t mutex;
32
33 public:
34 inline Mutex() {
35 ::pthread_mutex_init(&mutex, NULL);
36 }
37
38 inline void lock() {
39 ::pthread_mutex_lock(&mutex);
40 }
41
42 inline void unlock() {
43 ::pthread_mutex_unlock(&mutex);
44 }
45
46 inline ~Mutex() {
47 ::pthread_mutex_destroy(&mutex);
48 }
49
50 private:
51 Mutex(const Mutex& m);
52 Mutex& operator=(const Mutex &m);
53 };
54
58 class ScopedLock {
59 private:
60 Mutex& _mutex;
61
62 public:
63 inline ScopedLock(Mutex& mutex) :
64 _mutex(mutex) {
65 _mutex.lock();
66 }
67
68 inline ~ScopedLock() {
69 _mutex.unlock();
70 }
71 };
72
76 template<typename T> class ThreadLocalDataHolder {
77 private:
78 pthread_key_t _key;
79
80 public:
81 typedef T data_type;
82
83 inline ThreadLocalDataHolder() {
84 ::pthread_key_create(&_key, freeHolder);
85 }
86
87 inline static void freeHolder(void *p) {
88 assert(p != NULL);
89 delete reinterpret_cast<T *>(p);
90 }
91
92 inline ~ThreadLocalDataHolder() {
93 T *data = get();
94 if (data != NULL) {
95 delete data;
96 }
97 ::pthread_key_delete(_key);
98 }
99
100 inline T* get() const {
101 return reinterpret_cast<T *>(::pthread_getspecific(_key));
102 }
103
104 inline T* operator->() const { return get(); }
105 inline T& operator*() const { return *get(); }
106
107 inline T* release() {
108 T* result = get();
109 ::pthread_setspecific(_key, NULL);
110
111 return result;
112 }
113
114 inline void reset(T* p = NULL) {
115 T *data = get();
116 if (data != NULL) {
117 delete data;
118 }
119 ::pthread_setspecific(_key, p);
120 }
121 };
122
123 }
124}
125#endif
Portability.hh
log4shib::threading::Mutex
Definition: PThreads.hh:29
log4shib::threading::Mutex::Mutex
Mutex()
Definition: PThreads.hh:34
log4shib::threading::Mutex::~Mutex
~Mutex()
Definition: PThreads.hh:46
log4shib::threading::Mutex::lock
void lock()
Definition: PThreads.hh:38
log4shib::threading::Mutex::unlock
void unlock()
Definition: PThreads.hh:42
log4shib::threading::ScopedLock
definition of ScopedLock;
Definition: PThreads.hh:58
log4shib::threading::ScopedLock::~ScopedLock
~ScopedLock()
Definition: PThreads.hh:68
log4shib::threading::ScopedLock::ScopedLock
ScopedLock(Mutex &mutex)
Definition: PThreads.hh:63
log4shib::threading::ThreadLocalDataHolder::operator->
T * operator->() const
Definition: PThreads.hh:104
log4shib::threading::ThreadLocalDataHolder::operator*
T & operator*() const
Definition: PThreads.hh:105
log4shib::threading::ThreadLocalDataHolder::~ThreadLocalDataHolder
~ThreadLocalDataHolder()
Definition: PThreads.hh:92
log4shib::threading::ThreadLocalDataHolder::reset
void reset(T *p=NULL)
Definition: PThreads.hh:114
log4shib::threading::ThreadLocalDataHolder::get
T * get() const
Definition: BoostThreads.hh:34
log4shib::threading::ThreadLocalDataHolder::data_type
T data_type
Definition: PThreads.hh:81
log4shib::threading::ThreadLocalDataHolder::freeHolder
static void freeHolder(void *p)
Definition: PThreads.hh:87
log4shib::threading::ThreadLocalDataHolder::release
T * release()
Definition: PThreads.hh:107
log4shib::threading::ThreadLocalDataHolder::ThreadLocalDataHolder
ThreadLocalDataHolder()
Definition: PThreads.hh:83
log4shib::threading::getThreadId
static std::string getThreadId()
Return an identifier for the current thread.
Definition: BoostThreads.hh:22
log4shib
The top level namespace for all 'Log for C++' types and classes.
Definition: AbortAppender.hh:16