Logo ROOT   6.08/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) {
32 
33  int rc;
34  pthread_mutexattr_t attr;
35 
36  rc = pthread_mutexattr_init(&attr);
37 
38  if (!rc) {
39  rc = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
40  if (!rc) {
41  rc = pthread_mutex_init(&fMutex, &attr);
42  if (rc)
43  SysError("TPosixMutex", "pthread_mutex_init error");
44  } else
45  SysError("TPosixMutex", "pthread_mutexattr_settype error");
46  } else
47  SysError("TPosixMutex", "pthread_mutex_init error");
48 
49  pthread_mutexattr_destroy(&attr);
50 
51  } else {
52 
53  int rc = pthread_mutex_init(&fMutex, 0);
54  if (rc)
55  SysError("TPosixMutex", "pthread_mutex_init error");
56 
57  }
58 }
59 
60 ////////////////////////////////////////////////////////////////////////////////
61 /// TMutex dtor.
62 
64 {
65  int rc = pthread_mutex_destroy(&fMutex);
66  if (rc)
67  SysError("~TPosixMutex", "pthread_mutex_destroy error");
68 }
69 
70 ////////////////////////////////////////////////////////////////////////////////
71 /// Lock the mutex.
72 
74 {
75  return pthread_mutex_lock(&fMutex);
76 }
77 
78 ////////////////////////////////////////////////////////////////////////////////
79 /// Try locking the mutex. Returns 0 if mutex can be locked.
80 
82 {
83  return pthread_mutex_trylock(&fMutex);
84 }
85 
86 ////////////////////////////////////////////////////////////////////////////////
87 /// Unlock the mutex.
88 
90 {
91  return pthread_mutex_unlock(&fMutex);
92 }
Int_t UnLock()
Unlock the mutex.
Definition: TPosixMutex.cxx:89
virtual void SysError(const char *method, const char *msgfmt,...) const
Issue system error message.
Definition: TObject.cxx:939
Int_t TryLock()
Try locking the mutex. Returns 0 if mutex can be locked.
Definition: TPosixMutex.cxx:81
pthread_mutex_t fMutex
Definition: TPosixMutex.h:39
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
void SysError(const char *location, const char *msgfmt,...)
Int_t Lock()
Lock the mutex.
Definition: TPosixMutex.cxx:73
virtual ~TPosixMutex()
TMutex dtor.
Definition: TPosixMutex.cxx:63
#define ClassImp(name)
Definition: Rtypes.h:279