Logo ROOT  
Reference Guide
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#ifndef ROOT_TAtomicCount
16# error This should be #included only by TAtomicCount.h. Please #include TAtomicCount.h.
17#endif //ROOT_TAtomicCount
18
19//////////////////////////////////////////////////////////////////////////
20// //
21// TAtomicCountPthread //
22// //
23// Class providing atomic operations on a long. Setting, getting, //
24// incrementing and decrementing are atomic, thread safe, operations. //
25// //
26// This implementation uses pthread mutexes for locking. This clearly //
27// is less efficient than the version using asm locking instructions //
28// as in TAtomicCountGcc.h, but better than nothing. //
29// //
30// ATTENTION: Don't use this file directly, it is included by //
31// TAtomicCount.h. //
32// //
33//////////////////////////////////////////////////////////////////////////
34
35#include <pthread.h>
36
37class TAtomicCount {
38private:
39 Long_t fCnt; // counter
40 mutable pthread_mutex_t fMutex; // mutex used to lock counter
41
42 TAtomicCount(const TAtomicCount &) = delete;
44
45 class LockGuard {
46 private:
47 pthread_mutex_t &fM; // mutex to be guarded
48 public:
49 LockGuard(pthread_mutex_t &m): fM(m) { pthread_mutex_lock(&fM); }
50 ~LockGuard() { pthread_mutex_unlock(&fM); }
51 };
52
53public:
54 explicit TAtomicCount(Long_t v): fCnt(v) {
55 pthread_mutex_init(&fMutex, 0);
56 }
57
58 ~TAtomicCount() { pthread_mutex_destroy(&fMutex); }
59
60 void operator++() {
61 LockGuard lock(fMutex);
62 ++fCnt;
63 }
64
66 LockGuard lock(fMutex);
67 return --fCnt;
68 }
69
70 operator long() const {
71 LockGuard lock(fMutex);
72 return fCnt;
73 }
74
75 void Set(Long_t v) {
76 LockGuard lock(fMutex);
77 fCnt = v;
78 }
79
80 Long_t Get() const {
81 LockGuard lock(fMutex);
82 return fCnt;
83 }
84 };
85
86#endif
long Long_t
Definition: RtypesCore.h:54
LockGuard(pthread_mutex_t &m)
pthread_mutex_t fMutex
Long_t Get() const
TAtomicCount(const TAtomicCount &)=delete
TAtomicCount(Long_t v)
TAtomicCount & operator=(const TAtomicCount &)=delete
void Set(Long_t v)
auto * m
Definition: textangle.C:8