ROOT
6.06/09
Reference Guide
ROOT Home Page
Main Page
Related Pages
User's Classes
Namespaces
All Classes
Files
Release Notes
File List
File Members
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
class
TAtomicCount
{
34
private
:
35
Long_t
fCnt
;
// counter
36
mutable
pthread_mutex_t
fMutex
;
// mutex used to lock counter
37
38
TAtomicCount
(
const
TAtomicCount
&);
// not implemented
39
TAtomicCount
&
operator=
(
const
TAtomicCount
&);
// not implemented
40
41
class
LockGuard
{
42
private
:
43
pthread_mutex_t &
fM
;
// mutex to be guarded
44
public
:
45
LockGuard
(pthread_mutex_t &
m
): fM(m) { pthread_mutex_lock(&fM); }
46
~LockGuard
() { pthread_mutex_unlock(&fM); }
47
};
48
49
public
:
50
explicit
TAtomicCount
(
Long_t
v
): fCnt(v) {
51
pthread_mutex_init(&fMutex, 0);
52
}
53
54
~TAtomicCount
() { pthread_mutex_destroy(&fMutex); }
55
56
void
operator++
() {
57
LockGuard
lock(fMutex);
58
++
fCnt
;
59
}
60
61
Long_t
operator--
() {
62
LockGuard
lock(fMutex);
63
return
--
fCnt
;
64
}
65
66
operator
long()
const
{
67
LockGuard
lock(fMutex);
68
return
fCnt
;
69
}
70
71
void
Set
(
Long_t
v
) {
72
LockGuard
lock(fMutex);
73
fCnt =
v
;
74
}
75
76
Long_t
Get
()
const
{
77
LockGuard
lock(fMutex);
78
return
fCnt
;
79
}
80
};
81
82
#endif
TAtomicCount::operator--
Long_t operator--()
Definition:
TAtomicCountPthread.h:61
TAtomicCount::LockGuard
Definition:
TAtomicCountPthread.h:41
TAtomicCount::LockGuard::fM
pthread_mutex_t & fM
Definition:
TAtomicCountPthread.h:43
TAtomicCount
Definition:
TAtomicCount.h:71
TAtomicCount::~TAtomicCount
~TAtomicCount()
Definition:
TAtomicCountPthread.h:54
TAtomicCount::LockGuard::~LockGuard
~LockGuard()
Definition:
TAtomicCountPthread.h:46
TAtomicCount::fMutex
pthread_mutex_t fMutex
Definition:
TAtomicCountPthread.h:36
v
SVector< double, 2 > v
Definition:
Dict.h:5
m
TMarker * m
Definition:
textangle.C:8
TAtomicCount::Set
void Set(Long_t v)
Definition:
TAtomicCountPthread.h:71
TAtomicCount::TAtomicCount
TAtomicCount(Long_t v)
Definition:
TAtomicCountPthread.h:50
Long_t
long Long_t
Definition:
RtypesCore.h:50
TAtomicCount::fCnt
Long_t fCnt
Definition:
TAtomicCount.h:73
TAtomicCount::operator=
TAtomicCount & operator=(const TAtomicCount &)
TAtomicCount::Get
Long_t Get() const
Definition:
TAtomicCountPthread.h:76
TAtomicCount::TAtomicCount
TAtomicCount(const TAtomicCount &)
TAtomicCount::LockGuard::LockGuard
LockGuard(pthread_mutex_t &m)
Definition:
TAtomicCountPthread.h:45
TAtomicCount::operator++
void operator++()
Definition:
TAtomicCountPthread.h:56