Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 
24 ClassImp(TPosixMutex);
25 
26 ////////////////////////////////////////////////////////////////////////////////
27 /// Create a posix mutex lock.
28 
29 TPosixMutex::TPosixMutex(Bool_t recursive) : TMutexImp()
30 {
31  if (recursive) {
32  SetBit(kIsRecursive);
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 
64 TPosixMutex::~TPosixMutex()
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 
74 Int_t TPosixMutex::Lock()
75 {
76  return pthread_mutex_lock(&fMutex);
77 }
78 
79 ////////////////////////////////////////////////////////////////////////////////
80 /// Try locking the mutex. Returns 0 if mutex can be locked.
81 
82 Int_t TPosixMutex::TryLock()
83 {
84  return pthread_mutex_trylock(&fMutex);
85 }
86 
87 ////////////////////////////////////////////////////////////////////////////////
88 /// Unlock the mutex.
89 
90 Int_t TPosixMutex::UnLock(void)
91 {
92  return pthread_mutex_unlock(&fMutex);
93 }