Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
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
25////////////////////////////////////////////////////////////////////////////////
26/// Create a posix mutex lock.
27
29{
30 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, nullptr);
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 Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
bool Bool_t
Boolean (0=false, 1=true) (bool).
Definition RtypesCore.h:77
virtual void SysError(const char *method, const char *msgfmt,...) const
Issue system error message.
Definition TObject.cxx:1112
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition TObject.cxx:888
pthread_mutex_t fMutex
Definition TPosixMutex.h:33
Int_t UnLock() override
Unlock the mutex.
static constexpr int kIsRecursive
Definition TPosixMutex.h:35
TPosixMutex(Bool_t recursive=kFALSE)
Create a posix mutex lock.
Int_t Lock() override
Lock the mutex.
Int_t TryLock() override
Try locking the mutex. Returns 0 if mutex can be locked.
virtual ~TPosixMutex()
TMutex dtor.