ONPOSIX
2.0
Main Page
Namespaces
Classes
Files
File List
File Members
ONPOSIX
OnPosix Library
Namespaces
Classes
Files
File List
include
AbstractDescriptorReader.hpp
AbstractThread.hpp
Assert.hpp
Buffer.hpp
DescriptorsMonitor.hpp
DgramSocketClientDescriptor.hpp
DgramSocketServerDescriptor.hpp
FifoDescriptor.hpp
FileDescriptor.hpp
Logger.hpp
Pipe.hpp
PosixCondition.hpp
PosixDescriptor.hpp
PosixMutex.hpp
PosixPrioritySharedQueue.hpp
PosixSharedQueue.hpp
Process.hpp
SimpleThread.hpp
StreamSocketClientDescriptor.hpp
StreamSocketServer.hpp
StreamSocketServerDescriptor.hpp
Time.hpp
src
File Members
•
All
Classes
Namespaces
Files
Functions
Variables
Enumerator
Friends
Macros
Pages
PosixMutex.hpp
Go to the documentation of this file.
1
/*
2
* PosixMutex.hpp
3
*
4
* Copyright (C) 2012 Evidence Srl - www.evidence.eu.com
5
*
6
* This library is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU Library General Public
8
* License as published by the Free Software Foundation; either
9
* version 2 of the License, or (at your option) any later version.
10
*
11
* This library is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* Library General Public License for more details.
15
*
16
* You should have received a copy of the GNU Library General Public
17
* License along with this library; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
*/
20
21
#ifndef POSIXMUTEX_HPP_
22
#define POSIXMUTEX_HPP_
23
24
#include <pthread.h>
25
26
namespace
onposix {
27
28
/**
29
* \brief Implementation of a mutex class.
30
*
31
* The class is non copyable and makes use of the pthread library.
32
*/
33
class
PosixMutex
{
34
35
PosixMutex
(
const
PosixMutex
&);
36
PosixMutex
&
operator=
(
const
PosixMutex
&);
37
38
pthread_mutex_t
mutex_
;
39
40
friend
class
PosixCondition
;
41
42
public
:
43
PosixMutex
();
44
~PosixMutex
();
45
46
/**
47
* \brief Acquires the lock.
48
*
49
* If the mutex is busy the calling thread is blocked.
50
*/
51
void
lock
() {
52
pthread_mutex_lock(&
mutex_
);
53
}
54
55
/**
56
* \brief Releases the lock.
57
*/
58
void
unlock
() {
59
pthread_mutex_unlock(&
mutex_
);
60
}
61
62
bool
tryLock
();
63
};
64
65
/**
66
* \brief Class to simplify locking and unlocking of PosixMutex
67
*
68
* This is a convenience class that simplifies locking and unlocking
69
* PosixMutex(es) making use of the RAII idiom: the lock is acquired in the
70
* constructor and released in the destructor.
71
*/
72
class
MutexLocker
{
73
74
PosixMutex
&
mutex_
;
75
76
public
:
77
78
MutexLocker
(
PosixMutex
& mutex):
mutex_
(mutex) {
79
mutex_
.
lock
();
80
}
81
82
~MutexLocker
() {
83
mutex_
.
unlock
();
84
}
85
86
};
87
88
/**
89
* \brief Class to simplify locking and unlocking of pthread mutex.
90
*
91
* This class has the same purpose of the MutexLocker class but works
92
* with pthread mutex type instead of PosixMutex class.
93
*/
94
class
PthreadMutexLocker
{
95
96
pthread_mutex_t&
mutex_
;
97
98
public
:
99
100
PthreadMutexLocker
(pthread_mutex_t& mutex):
mutex_
(mutex) {
101
pthread_mutex_lock(&
mutex_
);
102
}
103
104
~PthreadMutexLocker
() {
105
pthread_mutex_unlock(&
mutex_
);
106
}
107
108
};
109
110
}
/* onposix */
111
112
#endif
/* POSIXMUTEX_HPP_ */
include
PosixMutex.hpp
Generated on Wed Apr 30 2014 15:27:24 for ONPOSIX by
1.8.1.2