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 
19 namespace threading {
20 
24  std::string getThreadId();
25 
28 #if defined(VXWORKS)
29  class Mutex {
30  private:
31  SEM_ID m_sem;
32 
33  public:
34  inline Mutex() {
35  m_sem = semMCreate( SEM_Q_PRIORITY | SEM_INVERSION_SAFE );
36  }
37 
38  inline void lock() {
39  STATUS result = semTake( m_sem, WAIT_FOREVER );
40  if (result != OK) {
41  // throw here?
42  }
43  }
44 
45  inline void unlock() {
46  semGive( m_sem );
47  }
48 
49  inline ~Mutex() {
50  semDelete( m_sem );
51  }
52 
53  private:
54  Mutex(const Mutex& m);
55  Mutex& operator=(const Mutex &m);
56  };
57 #else
58  class Mutex {
59  private:
60  pthread_mutexattr_t mutexattr;
61  pthread_mutex_t mutex;
62 
63  public:
64  inline Mutex() {
65  ::pthread_mutexattr_init(&mutexattr);
66  ::pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_RECURSIVE);
67  ::pthread_mutex_init(&mutex, &mutexattr);
68  }
69 
70  inline void lock() {
71  ::pthread_mutex_lock(&mutex);
72  }
73 
74  inline void unlock() {
75  ::pthread_mutex_unlock(&mutex);
76  }
77 
78  inline ~Mutex() {
79  ::pthread_mutex_destroy(&mutex);
80  ::pthread_mutexattr_destroy(&mutexattr);
81  }
82 
83  private:
84  Mutex(const Mutex& m);
85  Mutex& operator=(const Mutex &m);
86  };
87 #endif
88 
92  class ScopedLock {
93  private:
95 
96  public:
97  inline ScopedLock(Mutex& mutex) :
98  _mutex(mutex) {
99  _mutex.lock();
100  }
101 
102  inline ~ScopedLock() {
103  _mutex.unlock();
104  }
105  };
106 
110  template<typename T> class ThreadLocalDataHolder {
111  private:
112  pthread_key_t _key;
113 
114  public:
115  typedef T data_type;
116 
118  ::pthread_key_create(&_key, freeHolder);
119  }
120 
121  inline static void freeHolder(void *p) {
122  assert(p != NULL);
123  delete reinterpret_cast<T *>(p);
124  }
125 
127  T *data = get();
128  if (data != NULL) {
129  delete data;
130  }
131  ::pthread_key_delete(_key);
132  }
133 
134  inline T* get() const {
135  return reinterpret_cast<T *>(::pthread_getspecific(_key));
136  }
137 
138  inline T* operator->() const { return get(); }
139  inline T& operator*() const { return *get(); }
140 
141  inline T* release() {
142  T* result = get();
143  ::pthread_setspecific(_key, NULL);
144 
145  return result;
146  }
147 
148  inline void reset(T* p = NULL) {
149  T *data = get();
150  if (data != NULL) {
151  delete data;
152  }
153  ::pthread_setspecific(_key, p);
154  }
155  };
156 
157 }
159 #endif
#define LOG4CPP_NS_END
Definition: Portability.hh:50
definition of ScopedLock;
Definition: PThreads.hh:92
static void freeHolder(void *p)
Definition: PThreads.hh:121
Mutex & operator=(const Mutex &m)
pthread_mutexattr_t mutexattr
Definition: PThreads.hh:60
This class holds Thread local data of type T, i.e.
Definition: BoostThreads.hh:32
static std::string getThreadId()
Return an identifier for the current thread.
Definition: BoostThreads.hh:22
boost::mutex Mutex
Dummy type &#39;int&#39; for Mutex.
Definition: BoostThreads.hh:27
ScopedLock(Mutex &mutex)
Definition: PThreads.hh:97
pthread_mutex_t mutex
Definition: PThreads.hh:61
#define LOG4CPP_NS_BEGIN
Definition: Portability.hh:49


rc_genicam_api
Author(s): Heiko Hirschmueller
autogenerated on Wed Mar 17 2021 02:48:41