ROOT  6.06/09
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 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 
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
pthread_mutex_t fMutex
SVector< double, 2 > v
Definition: Dict.h:5
TMarker * m
Definition: textangle.C:8
void Set(Long_t v)
TAtomicCount(Long_t v)
long Long_t
Definition: RtypesCore.h:50
TAtomicCount & operator=(const TAtomicCount &)
Long_t Get() const
TAtomicCount(const TAtomicCount &)
LockGuard(pthread_mutex_t &m)