admin
2022-08-18 67a5e3d825fde17b7b00906ce42b0bd8cafebc4e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 * PThreads.hh
 *
 * Copyright 2002, Emiliano Martin emilianomc@terra.es All rights reserved.
 *
 * See the COPYING file for the terms of usage and distribution.
 */
 
#ifndef _LOG4CPP_THREADING_PTHREADS_HH
#define _LOG4CPP_THREADING_PTHREADS_HH
 
#include <log4cpp/Portability.hh>
#include <stdio.h>
#include <pthread.h>
#include <string>
#include <assert.h>
 
 
namespace log4cpp {
    namespace threading {
 
        /** 
         * returns the thread ID
         **/
        std::string getThreadId();
        
        /**
         **/
        class Mutex {
            private:
            pthread_mutex_t mutex;
 
            public:
            inline Mutex() {
                ::pthread_mutex_init(&mutex, NULL);
            }
 
            inline void lock() {
                ::pthread_mutex_lock(&mutex);
            }
 
            inline void unlock() {
                ::pthread_mutex_unlock(&mutex);
            }
 
            inline ~Mutex() {
                ::pthread_mutex_destroy(&mutex);
            }
 
            private:
            Mutex(const Mutex& m);
            Mutex& operator=(const Mutex &m);
        };
 
        /**
         *    definition of ScopedLock;
         **/
        class ScopedLock {
            private:
            Mutex& _mutex;
 
            public:
            inline ScopedLock(Mutex& mutex) :
                _mutex(mutex) {
                _mutex.lock();
            }
 
            inline ~ScopedLock() {
                _mutex.unlock();
            }
        };
 
        /**
         * 
         **/
        template<typename T> class ThreadLocalDataHolder {
            private:            
            pthread_key_t _key;              
 
            public:
            typedef T data_type;
 
            inline ThreadLocalDataHolder() {
                ::pthread_key_create(&_key, freeHolder);         
            }
 
            inline static void freeHolder(void *p) {
                assert(p != NULL);
                delete reinterpret_cast<T *>(p);
             }
 
            inline ~ThreadLocalDataHolder() {
                T *data = get();
                if (data != NULL) { 
                    delete data;
                }
                ::pthread_key_delete(_key);
            }
            
            inline T* get() const {
                return reinterpret_cast<T *>(::pthread_getspecific(_key)); 
            }
 
            inline T* operator->() const { return get(); }
            inline T& operator*() const { return *get(); }
 
            inline T* release() {
                T* result = get();
                ::pthread_setspecific(_key, NULL); 
 
                return result;
            }
 
            inline void reset(T* p = NULL) {
                T *data = get();
                if (data != NULL) {
                    delete data;
                }
                ::pthread_setspecific(_key, p); 
            }
        };
 
    }
}
#endif