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//////////////////////////////////////////////////////////////////////////
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
35class TAtomicCount {
36private:
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
51public:
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
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
long Long_t
Definition: RtypesCore.h:50
LockGuard(pthread_mutex_t &m)
pthread_mutex_t fMutex
TAtomicCount(const TAtomicCount &)
Long_t Get() const
TAtomicCount & operator=(const TAtomicCount &)
TAtomicCount(Long_t v)
void Set(Long_t v)
auto * m
Definition: textangle.C:8