log4cpp 1.1
Loading...
Searching...
No Matches
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 _LOG4CPP_THREADING_PTHREADS_HH
10#define _LOG4CPP_THREADING_PTHREADS_HH
11
13#include <stdio.h>
14#include <pthread.h>
15#include <string>
16#include <assert.h>
17
18
19namespace log4cpp {
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
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
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
Definition: PThreads.hh:29
Mutex()
Definition: PThreads.hh:34
void lock()
Definition: PThreads.hh:38
void unlock()
Definition: PThreads.hh:42
~Mutex()
Definition: PThreads.hh:46
definition of ScopedLock;
Definition: PThreads.hh:58
ScopedLock(Mutex &mutex)
Definition: PThreads.hh:63
~ScopedLock()
Definition: PThreads.hh:68
~ThreadLocalDataHolder()
Definition: PThreads.hh:92
T data_type
Definition: PThreads.hh:81
ThreadLocalDataHolder()
Definition: PThreads.hh:83
T * operator->() const
Definition: PThreads.hh:104
T & operator*() const
Definition: PThreads.hh:105
static void freeHolder(void *p)
Definition: PThreads.hh:87
T * get() const
Definition: BoostThreads.hh:34
void reset(T *p=NULL)
Definition: PThreads.hh:114
T * release()
Definition: PThreads.hh:107
static std::string getThreadId()
Return an identifier for the current thread.
Definition: BoostThreads.hh:22
The top level namespace for all 'Log for C++' types and classes.
Definition: AbortAppender.hh:16