log4cpp 1.1.6
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
12#include <assert.h>
14#include <pthread.h>
15#include <stdio.h>
16#include <string>
17
18namespace log4cpp {
19 namespace threading {
20
24 std::string getThreadId();
25
28 class Mutex {
29 private:
30 pthread_mutex_t mutex;
31
32 public:
33 inline Mutex() {
34 ::pthread_mutex_init(&mutex, NULL);
35 }
36
37 inline void lock() {
38 ::pthread_mutex_lock(&mutex);
39 }
40
41 inline void unlock() {
42 ::pthread_mutex_unlock(&mutex);
43 }
44
45 inline ~Mutex() {
46 ::pthread_mutex_destroy(&mutex);
47 }
48
49 private:
50 Mutex(const Mutex& m);
51 Mutex& operator=(const Mutex& m);
52 };
53
57 class ScopedLock {
58 private:
59 Mutex& _mutex;
60
61 public:
62 inline ScopedLock(Mutex& mutex) : _mutex(mutex) {
63 _mutex.lock();
64 }
65
66 inline ~ScopedLock() {
67 _mutex.unlock();
68 }
69 };
70
74 template <typename T> class ThreadLocalDataHolder {
75 private:
76 pthread_key_t _key;
77
78 public:
79 typedef T data_type;
80
82 ::pthread_key_create(&_key, freeHolder);
83 }
84
85 inline static void freeHolder(void* p) {
86 assert(p != NULL);
87 delete reinterpret_cast<T*>(p);
88 }
89
91 T* data = get();
92 if (data != NULL) {
93 delete data;
94 }
95 ::pthread_key_delete(_key);
96 }
97
98 inline T* get() const {
99 return reinterpret_cast<T*>(::pthread_getspecific(_key));
100 }
101
102 inline T* operator->() const {
103 return get();
104 }
105 inline T& operator*() const {
106 return *get();
107 }
108
109 inline T* release() {
110 T* result = get();
111 ::pthread_setspecific(_key, NULL);
112
113 return result;
114 }
115
116 inline void reset(T* p = NULL) {
117 T* data = get();
118 if (data != NULL) {
119 delete data;
120 }
121 ::pthread_setspecific(_key, p);
122 }
123 };
124
125 } // namespace threading
126} // namespace log4cpp
127#endif
Definition PThreads.hh:28
Mutex()
Definition PThreads.hh:33
void lock()
Definition PThreads.hh:37
void unlock()
Definition PThreads.hh:41
~Mutex()
Definition PThreads.hh:45
ScopedLock(Mutex &mutex)
Definition PThreads.hh:62
~ScopedLock()
Definition PThreads.hh:66
~ThreadLocalDataHolder()
Definition PThreads.hh:90
T data_type
Definition DummyThreads.hh:34
ThreadLocalDataHolder()
Definition PThreads.hh:81
T * operator->() const
Definition PThreads.hh:102
T & operator*() const
Definition PThreads.hh:105
static void freeHolder(void *p)
Definition PThreads.hh:85
T * get() const
Definition BoostThreads.hh:34
void reset(T *p=NULL)
Definition PThreads.hh:116
T * release()
Definition PThreads.hh:109
Definition BoostThreads.hh:21
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