ROOT
6.14/05
Reference Guide
core
thread
inc
TAtomicCountPthread.h
Go to the documentation of this file.
1
// @(#)root/thread:$Id$
2
// Author: Fons Rademakers 14/11/06
3
4
/*************************************************************************
5
* Copyright (C) 1995-2006, Rene Brun and Fons Rademakers. *
6
* All rights reserved. *
7
* *
8
* For the licensing terms see $ROOTSYS/LICENSE. *
9
* For the list of contributors see $ROOTSYS/README/CREDITS. *
10
*************************************************************************/
11
12
#ifndef ROOT_TAtomicCountPthread
13
#define ROOT_TAtomicCountPthread
14
15
//////////////////////////////////////////////////////////////////////////
16
// //
17
// TAtomicCountPthread //
18
// //
19
// Class providing atomic operations on a long. Setting, getting, //
20
// incrementing and decrementing are atomic, thread safe, operations. //
21
// //
22
// This implementation uses pthread mutexes for locking. This clearly //
23
// is less efficient than the version using asm locking instructions //
24
// as in TAtomicCountGcc.h, but better than nothing. //
25
// //
26
// ATTENTION: Don't use this file directly, it is included by //
27
// TAtomicCount.h. //
28
// //
29
//////////////////////////////////////////////////////////////////////////
30
31
#include <pthread.h>
32
33
#include "
RtypesCore.h
"
34
35
class
TAtomicCount
{
36
private
:
37
Long_t
fCnt
;
// counter
38
mutable
pthread_mutex_t
fMutex
;
// mutex used to lock counter
39
40
TAtomicCount
(
const
TAtomicCount
&);
// not implemented
41
TAtomicCount
&
operator=
(
const
TAtomicCount
&);
// not implemented
42
43
class
LockGuard
{
44
private
:
45
pthread_mutex_t &
fM
;
// mutex to be guarded
46
public
:
47
LockGuard
(pthread_mutex_t &
m
): fM(m) { pthread_mutex_lock(&fM); }
48
~LockGuard
() { pthread_mutex_unlock(&fM); }
49
};
50
51
public
:
52
explicit
TAtomicCount
(
Long_t
v
): fCnt(v) {
53
pthread_mutex_init(&fMutex, 0);
54
}
55
56
~TAtomicCount
() { pthread_mutex_destroy(&fMutex); }
57
58
void
operator++
() {
59
LockGuard
lock(fMutex);
60
++
fCnt
;
61
}
62
63
Long_t
operator--
() {
64
LockGuard
lock(fMutex);
65
return
--
fCnt
;
66
}
67
68
operator
long()
const
{
69
LockGuard
lock(fMutex);
70
return
fCnt
;
71
}
72
73
void
Set
(
Long_t
v
) {
74
LockGuard
lock(fMutex);
75
fCnt =
v
;
76
}
77
78
Long_t
Get
()
const
{
79
LockGuard
lock(fMutex);
80
return
fCnt
;
81
}
82
};
83
84
#endif
TAtomicCount::operator--
Long_t operator--()
Definition:
TAtomicCountPthread.h:63
TAtomicCount::Get
Long_t Get() const
Definition:
TAtomicCountPthread.h:78
m
auto * m
Definition:
textangle.C:8
RtypesCore.h
TAtomicCount::LockGuard
Definition:
TAtomicCountPthread.h:43
TAtomicCount::LockGuard::fM
pthread_mutex_t & fM
Definition:
TAtomicCountPthread.h:45
TAtomicCount
Definition:
TAtomicCount.h:67
TAtomicCount::~TAtomicCount
~TAtomicCount()
Definition:
TAtomicCountPthread.h:56
TAtomicCount::LockGuard::~LockGuard
~LockGuard()
Definition:
TAtomicCountPthread.h:48
TAtomicCount::fMutex
pthread_mutex_t fMutex
Definition:
TAtomicCountPthread.h:38
v
SVector< double, 2 > v
Definition:
Dict.h:5
TAtomicCount::Set
void Set(Long_t v)
Definition:
TAtomicCountPthread.h:73
TAtomicCount::TAtomicCount
TAtomicCount(Long_t v)
Definition:
TAtomicCountPthread.h:52
Long_t
long Long_t
Definition:
RtypesCore.h:50
TAtomicCount::fCnt
Long_t fCnt
Definition:
TAtomicCount.h:69
TAtomicCount::operator=
TAtomicCount & operator=(const TAtomicCount &)
TAtomicCount::TAtomicCount
TAtomicCount(const TAtomicCount &)
TAtomicCount::LockGuard::LockGuard
LockGuard(pthread_mutex_t &m)
Definition:
TAtomicCountPthread.h:47
TAtomicCount::operator++
void operator++()
Definition:
TAtomicCountPthread.h:58