File: PThreads_8hh-source.html

package info (click to toggle)
log4cpp-doc 1.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,528 kB
  • sloc: makefile: 10
file content (141 lines) | stat: -rw-r--r-- 13,910 bytes parent folder | download | duplicates (5)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>log4cpp: PThreads.hh Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
<link href="tabs.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.5.2 -->
<div class="tabs">
  <ul>
    <li><a href="index.html"><span>Main&nbsp;Page</span></a></li>
    <li><a href="namespaces.html"><span>Namespaces</span></a></li>
    <li><a href="annotated.html"><span>Classes</span></a></li>
    <li class="current"><a href="files.html"><span>Files</span></a></li>
    <li><a href="pages.html"><span>Related&nbsp;Pages</span></a></li>
  </ul>
</div>
<div class="tabs">
  <ul>
    <li><a href="files.html"><span>File&nbsp;List</span></a></li>
    <li><a href="globals.html"><span>File&nbsp;Members</span></a></li>
  </ul>
</div>
<h1>PThreads.hh</h1><a href="PThreads_8hh.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span>
<a name="l00002"></a>00002 <span class="comment"> * PThreads.hh</span>
<a name="l00003"></a>00003 <span class="comment"> *</span>
<a name="l00004"></a>00004 <span class="comment"> * Copyright 2002, Emiliano Martin emilianomc@terra.es All rights reserved.</span>
<a name="l00005"></a>00005 <span class="comment"> *</span>
<a name="l00006"></a>00006 <span class="comment"> * See the COPYING file for the terms of usage and distribution.</span>
<a name="l00007"></a>00007 <span class="comment"> */</span>
<a name="l00008"></a>00008 
<a name="l00009"></a>00009 <span class="preprocessor">#ifndef _LOG4CPP_THREADING_PTHREADS_HH</span>
<a name="l00010"></a>00010 <span class="preprocessor"></span><span class="preprocessor">#define _LOG4CPP_THREADING_PTHREADS_HH</span>
<a name="l00011"></a>00011 <span class="preprocessor"></span>
<a name="l00012"></a>00012 <span class="preprocessor">#include &lt;<a class="code" href="Portability_8hh.html">log4cpp/Portability.hh</a>&gt;</span>
<a name="l00013"></a>00013 <span class="preprocessor">#include &lt;stdio.h&gt;</span>
<a name="l00014"></a>00014 <span class="preprocessor">#include &lt;pthread.h&gt;</span>
<a name="l00015"></a>00015 <span class="preprocessor">#include &lt;string&gt;</span>
<a name="l00016"></a>00016 <span class="preprocessor">#include &lt;assert.h&gt;</span>
<a name="l00017"></a>00017 
<a name="l00018"></a>00018 
<a name="l00019"></a>00019 <span class="keyword">namespace </span>log4cpp {
<a name="l00020"></a>00020     <span class="keyword">namespace </span>threading {
<a name="l00021"></a>00021 
<a name="l00025"></a>00025         std::string <a class="code" href="namespacelog4cpp_1_1threading.html#f2f7960b92e64a8beb0dcb08ee6bf378" title="returns the thread ID">getThreadId</a>();
<a name="l00026"></a>00026         
<a name="l00029"></a><a class="code" href="classlog4cpp_1_1threading_1_1Mutex.html">00029</a>         <span class="keyword">class </span><a class="code" href="classlog4cpp_1_1threading_1_1Mutex.html">Mutex</a> {
<a name="l00030"></a>00030             <span class="keyword">private</span>:
<a name="l00031"></a>00031             pthread_mutex_t mutex;
<a name="l00032"></a>00032 
<a name="l00033"></a>00033             <span class="keyword">public</span>:
<a name="l00034"></a><a class="code" href="classlog4cpp_1_1threading_1_1Mutex.html#551ed73a5b9620ef4b95320158ac7a5b">00034</a>             <span class="keyword">inline</span> <a class="code" href="classlog4cpp_1_1threading_1_1Mutex.html#551ed73a5b9620ef4b95320158ac7a5b">Mutex</a>() {
<a name="l00035"></a>00035                 ::pthread_mutex_init(&amp;mutex, NULL);
<a name="l00036"></a>00036             }
<a name="l00037"></a>00037 
<a name="l00038"></a><a class="code" href="classlog4cpp_1_1threading_1_1Mutex.html#64805208eafba79ab6112bf94a03f119">00038</a>             <span class="keyword">inline</span> <span class="keywordtype">void</span> <a class="code" href="classlog4cpp_1_1threading_1_1Mutex.html#64805208eafba79ab6112bf94a03f119">lock</a>() {
<a name="l00039"></a>00039                 ::pthread_mutex_lock(&amp;mutex);
<a name="l00040"></a>00040             }
<a name="l00041"></a>00041 
<a name="l00042"></a><a class="code" href="classlog4cpp_1_1threading_1_1Mutex.html#7a460f445eff3697364a0fb0a94edd4d">00042</a>             <span class="keyword">inline</span> <span class="keywordtype">void</span> <a class="code" href="classlog4cpp_1_1threading_1_1Mutex.html#7a460f445eff3697364a0fb0a94edd4d">unlock</a>() {
<a name="l00043"></a>00043                 ::pthread_mutex_unlock(&amp;mutex);
<a name="l00044"></a>00044             }
<a name="l00045"></a>00045 
<a name="l00046"></a><a class="code" href="classlog4cpp_1_1threading_1_1Mutex.html#aa869652ddfdd364e34acaa7c2cd2d34">00046</a>             <span class="keyword">inline</span> <a class="code" href="classlog4cpp_1_1threading_1_1Mutex.html#aa869652ddfdd364e34acaa7c2cd2d34">~Mutex</a>() {
<a name="l00047"></a>00047                 ::pthread_mutex_destroy(&amp;mutex);
<a name="l00048"></a>00048             }
<a name="l00049"></a>00049 
<a name="l00050"></a>00050             <span class="keyword">private</span>:
<a name="l00051"></a>00051             <a class="code" href="classlog4cpp_1_1threading_1_1Mutex.html#551ed73a5b9620ef4b95320158ac7a5b">Mutex</a>(<span class="keyword">const</span> <a class="code" href="classlog4cpp_1_1threading_1_1Mutex.html">Mutex</a>&amp; m);
<a name="l00052"></a>00052             <a class="code" href="classlog4cpp_1_1threading_1_1Mutex.html">Mutex</a>&amp; operator=(<span class="keyword">const</span> <a class="code" href="classlog4cpp_1_1threading_1_1Mutex.html">Mutex</a> &amp;m);
<a name="l00053"></a>00053         };
<a name="l00054"></a>00054 
<a name="l00058"></a><a class="code" href="classlog4cpp_1_1threading_1_1ScopedLock.html">00058</a>         <span class="keyword">class </span><a class="code" href="classlog4cpp_1_1threading_1_1ScopedLock.html" title="definition of ScopedLock;">ScopedLock</a> {
<a name="l00059"></a>00059             <span class="keyword">private</span>:
<a name="l00060"></a>00060             <a class="code" href="classlog4cpp_1_1threading_1_1Mutex.html">Mutex</a>&amp; _mutex;
<a name="l00061"></a>00061 
<a name="l00062"></a>00062             <span class="keyword">public</span>:
<a name="l00063"></a><a class="code" href="classlog4cpp_1_1threading_1_1ScopedLock.html#282f2648a191e54f8aadec68d46aa0b1">00063</a>             <span class="keyword">inline</span> <a class="code" href="classlog4cpp_1_1threading_1_1ScopedLock.html#282f2648a191e54f8aadec68d46aa0b1">ScopedLock</a>(<a class="code" href="classlog4cpp_1_1threading_1_1Mutex.html">Mutex</a>&amp; mutex) :
<a name="l00064"></a>00064                 _mutex(mutex) {
<a name="l00065"></a>00065                 _mutex.<a class="code" href="classlog4cpp_1_1threading_1_1Mutex.html#64805208eafba79ab6112bf94a03f119">lock</a>();
<a name="l00066"></a>00066             }
<a name="l00067"></a>00067 
<a name="l00068"></a><a class="code" href="classlog4cpp_1_1threading_1_1ScopedLock.html#92363ac02ef7a4ba2b1f55ce69b6dc22">00068</a>             <span class="keyword">inline</span> <a class="code" href="classlog4cpp_1_1threading_1_1ScopedLock.html#92363ac02ef7a4ba2b1f55ce69b6dc22">~ScopedLock</a>() {
<a name="l00069"></a>00069                 _mutex.<a class="code" href="classlog4cpp_1_1threading_1_1Mutex.html#7a460f445eff3697364a0fb0a94edd4d">unlock</a>();
<a name="l00070"></a>00070             }
<a name="l00071"></a>00071         };
<a name="l00072"></a>00072 
<a name="l00076"></a>00076         <span class="keyword">template</span>&lt;<span class="keyword">typename</span> T&gt; <span class="keyword">class </span>ThreadLocalDataHolder {
<a name="l00077"></a>00077             <span class="keyword">private</span>:            
<a name="l00078"></a>00078             pthread_key_t _key;              
<a name="l00079"></a>00079 
<a name="l00080"></a>00080             <span class="keyword">public</span>:
<a name="l00081"></a><a class="code" href="classlog4cpp_1_1threading_1_1ThreadLocalDataHolder.html#a720fa8b0bd3ffe97a628c20220f3c6c">00081</a>             <span class="keyword">typedef</span> T <a class="code" href="classlog4cpp_1_1threading_1_1ThreadLocalDataHolder.html#a720fa8b0bd3ffe97a628c20220f3c6c">data_type</a>;
<a name="l00082"></a>00082 
<a name="l00083"></a><a class="code" href="classlog4cpp_1_1threading_1_1ThreadLocalDataHolder.html#a9b75c83cd533007ea0c724180a42b6f">00083</a>             <span class="keyword">inline</span> <a class="code" href="classlog4cpp_1_1threading_1_1ThreadLocalDataHolder.html#a9b75c83cd533007ea0c724180a42b6f">ThreadLocalDataHolder</a>() {
<a name="l00084"></a>00084                 ::pthread_key_create(&amp;_key, <a class="code" href="classlog4cpp_1_1threading_1_1ThreadLocalDataHolder.html#e110805646ff93764dbfce96536cdc70">freeHolder</a>);         
<a name="l00085"></a>00085             }
<a name="l00086"></a>00086 
<a name="l00087"></a><a class="code" href="classlog4cpp_1_1threading_1_1ThreadLocalDataHolder.html#e110805646ff93764dbfce96536cdc70">00087</a>             <span class="keyword">inline</span> <span class="keyword">static</span> <span class="keywordtype">void</span> <a class="code" href="classlog4cpp_1_1threading_1_1ThreadLocalDataHolder.html#e110805646ff93764dbfce96536cdc70">freeHolder</a>(<span class="keywordtype">void</span> *p) {
<a name="l00088"></a>00088                 assert(p != NULL);
<a name="l00089"></a>00089                 <span class="keyword">delete</span> <span class="keyword">reinterpret_cast&lt;</span>T *<span class="keyword">&gt;</span>(p);
<a name="l00090"></a>00090              }
<a name="l00091"></a>00091 
<a name="l00092"></a><a class="code" href="classlog4cpp_1_1threading_1_1ThreadLocalDataHolder.html#70be3ec2201a72ba1d1d5eee3764fde9">00092</a>             <span class="keyword">inline</span> <a class="code" href="classlog4cpp_1_1threading_1_1ThreadLocalDataHolder.html#70be3ec2201a72ba1d1d5eee3764fde9">~ThreadLocalDataHolder</a>() {
<a name="l00093"></a>00093                 T *data = <span class="keyword">get</span>();
<a name="l00094"></a>00094                 <span class="keywordflow">if</span> (data != NULL) { 
<a name="l00095"></a>00095                     <span class="keyword">delete</span> data;
<a name="l00096"></a>00096                 }
<a name="l00097"></a>00097                 ::pthread_key_delete(_key);
<a name="l00098"></a>00098             }
<a name="l00099"></a>00099             
<a name="l00100"></a><a class="code" href="classlog4cpp_1_1threading_1_1ThreadLocalDataHolder.html#147cbba1fab8ef3eec6d98ef122749b1">00100</a>             <span class="keyword">inline</span> T* <span class="keyword">get</span>() <span class="keyword">const</span> {
<a name="l00101"></a>00101                 <span class="keywordflow">return</span> reinterpret_cast&lt;T *&gt;(::pthread_getspecific(_key)); 
<a name="l00102"></a>00102             }
<a name="l00103"></a>00103 
<a name="l00104"></a><a class="code" href="classlog4cpp_1_1threading_1_1ThreadLocalDataHolder.html#f579e5a7fdcba839bf8d95f2ede8a43e">00104</a>             <span class="keyword">inline</span> T* <a class="code" href="classlog4cpp_1_1threading_1_1ThreadLocalDataHolder.html#f579e5a7fdcba839bf8d95f2ede8a43e">operator-&gt;</a>()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> <span class="keyword">get</span>(); }
<a name="l00105"></a><a class="code" href="classlog4cpp_1_1threading_1_1ThreadLocalDataHolder.html#e97fc1f8d6f9c8b11d94c5e213249dbd">00105</a>             <span class="keyword">inline</span> T&amp; <a class="code" href="classlog4cpp_1_1threading_1_1ThreadLocalDataHolder.html#e97fc1f8d6f9c8b11d94c5e213249dbd">operator*</a>()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> *<span class="keyword">get</span>(); }
<a name="l00106"></a>00106 
<a name="l00107"></a><a class="code" href="classlog4cpp_1_1threading_1_1ThreadLocalDataHolder.html#299e9e1f9e02ef2668e64f81b1d29204">00107</a>             <span class="keyword">inline</span> T* <a class="code" href="classlog4cpp_1_1threading_1_1ThreadLocalDataHolder.html#299e9e1f9e02ef2668e64f81b1d29204">release</a>() {
<a name="l00108"></a>00108                 T* result = <span class="keyword">get</span>();
<a name="l00109"></a>00109                 ::pthread_setspecific(_key, NULL); 
<a name="l00110"></a>00110 
<a name="l00111"></a>00111                 <span class="keywordflow">return</span> result;
<a name="l00112"></a>00112             }
<a name="l00113"></a>00113 
<a name="l00114"></a><a class="code" href="classlog4cpp_1_1threading_1_1ThreadLocalDataHolder.html#e45b1bbee63188f40ed0a1fd262903dc">00114</a>             <span class="keyword">inline</span> <span class="keywordtype">void</span> <a class="code" href="classlog4cpp_1_1threading_1_1ThreadLocalDataHolder.html#e45b1bbee63188f40ed0a1fd262903dc">reset</a>(T* p = NULL) {
<a name="l00115"></a>00115                 T *data = <span class="keyword">get</span>();
<a name="l00116"></a>00116                 <span class="keywordflow">if</span> (data != NULL) {
<a name="l00117"></a>00117                     <span class="keyword">delete</span> data;
<a name="l00118"></a>00118                 }
<a name="l00119"></a>00119                 ::pthread_setspecific(_key, p); 
<a name="l00120"></a>00120             }
<a name="l00121"></a>00121         };
<a name="l00122"></a>00122 
<a name="l00123"></a>00123     }
<a name="l00124"></a>00124 }
<a name="l00125"></a>00125 <span class="preprocessor">#endif</span>
</pre></div><hr size="1"><address style="text-align: right;"><small>Generated on Mon Sep 3 11:54:29 2007 for log4cpp by&nbsp;
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.2 </small></address>
</body>
</html>