~ubuntu-branches/ubuntu/trusty/log4shib/trusty

« back to all changes in this revision

Viewing changes to include/log4shib/threading/PThreads.hh

  • Committer: Package Import Robot
  • Author(s): Russ Allbery
  • Date: 2012-06-05 21:20:25 UTC
  • Revision ID: package-import@ubuntu.com-20120605212025-uyigtav7dqwvnf41
Tags: upstream-1.0.4
ImportĀ upstreamĀ versionĀ 1.0.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
22
        /** 
 
23
         * returns the thread ID
 
24
         **/
 
25
        std::string getThreadId();
 
26
        
 
27
        /**
 
28
         **/
 
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
 
 
55
        /**
 
56
         *      definition of ScopedLock;
 
57
         **/
 
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
 
 
73
        /**
 
74
         * 
 
75
         **/
 
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