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


log4cpp
Author(s): Stephen Roderick, Bastiaan Bakker, Cedric Le Goater, Steve Ostlind, Marcel Harkema, Walter Stroebel, Glenn Scott and Tony Cheung
autogenerated on Sun Jun 23 2019 19:10:00