Logo ROOT   6.12/07
Reference Guide
TPosixMutex.cxx
Go to the documentation of this file.
1 // @(#)root/thread:$Id$
2 // Author: Fons Rademakers 25/06/97
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, 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 //////////////////////////////////////////////////////////////////////////
13 // //
14 // TPosixMutex //
15 // //
16 // This class provides an interface to the posix mutex routines. //
17 // //
18 //////////////////////////////////////////////////////////////////////////
19 
20 #include "TThread.h"
21 #include "TPosixMutex.h"
22 #include "PosixThreadInc.h"
23 
25 
26 ////////////////////////////////////////////////////////////////////////////////
27 /// Create a posix mutex lock.
28 
30 {
31  if (recursive) {
33 
34  int rc;
35  pthread_mutexattr_t attr;
36 
37  rc = pthread_mutexattr_init(&attr);
38 
39  if (!rc) {
40  rc = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
41  if (!rc) {
42  rc = pthread_mutex_init(&fMutex, &attr);
43  if (rc)
44  SysError("TPosixMutex", "pthread_mutex_init error");
45  } else
46  SysError("TPosixMutex", "pthread_mutexattr_settype error");
47  } else
48  SysError("TPosixMutex", "pthread_mutex_init error");
49 
50  pthread_mutexattr_destroy(&attr);
51 
52  } else {
53 
54  int rc = pthread_mutex_init(&fMutex, 0);
55  if (rc)
56  SysError("TPosixMutex", "pthread_mutex_init error");
57 
58  }
59 }
60 
61 ////////////////////////////////////////////////////////////////////////////////
62 /// TMutex dtor.
63 
65 {
66  int rc = pthread_mutex_destroy(&fMutex);
67  if (rc)
68  SysError("~TPosixMutex", "pthread_mutex_destroy error");
69 }
70 
71 ////////////////////////////////////////////////////////////////////////////////
72 /// Lock the mutex.
73 
75 {
76  return pthread_mutex_lock(&fMutex);
77 }
78 
79 ////////////////////////////////////////////////////////////////////////////////
80 /// Try locking the mutex. Returns 0 if mutex can be locked.
81 
83 {
84  return pthread_mutex_trylock(&fMutex);
85 }
86 
87 ////////////////////////////////////////////////////////////////////////////////
88 /// Unlock the mutex.
89 
91 {
92  return pthread_mutex_unlock(&fMutex);
93 }
Int_t UnLock()
Unlock the mutex.
Definition: TPosixMutex.cxx:90
virtual void SysError(const char *method, const char *msgfmt,...) const
Issue system error message.
Definition: TObject.cxx:894
Int_t TryLock()
Try locking the mutex. Returns 0 if mutex can be locked.
Definition: TPosixMutex.cxx:82
pthread_mutex_t fMutex
Definition: TPosixMutex.h:37
static constexpr int kIsRecursive
Definition: TPosixMutex.h:39
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
TPosixMutex(Bool_t recursive=kFALSE)
Create a posix mutex lock.
Definition: TPosixMutex.cxx:29
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition: TObject.cxx:694
Int_t Lock()
Lock the mutex.
Definition: TPosixMutex.cxx:74
virtual ~TPosixMutex()
TMutex dtor.
Definition: TPosixMutex.cxx:64
#define ClassImp(name)
Definition: Rtypes.h:359