Nebula
Loading...
Searching...
No Matches
posixcriticalsection.h
Go to the documentation of this file.
1#pragma once
2#ifndef THREADING_POSIXCRITICALSECTION_H
3#define THREADING_POSIXCRITICALSECTION_H
4//------------------------------------------------------------------------------
17#include "core/types.h"
18#include <pthread.h>
19
20//------------------------------------------------------------------------------
21namespace Posix
22{
24{
25public:
33 void Enter() const;
35 void Leave() const;
36private:
37 pthread_mutex_t* mutex;
38};
39
40//------------------------------------------------------------------------------
43inline
45 mutex(0)
46{
47 this->mutex = new pthread_mutex_t;
48 pthread_mutexattr_t attr;
49 pthread_mutexattr_init(&attr);
50 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
51 pthread_mutex_init(this->mutex, &attr);
52 pthread_mutexattr_destroy(&attr);
53}
54
55//------------------------------------------------------------------------------
58inline
60{
61 pthread_mutex_destroy(this->mutex);
62 delete this->mutex;
63 this->mutex = 0;
64}
65
66//------------------------------------------------------------------------------
69inline void
71{
72 if (this->mutex)
73 {
74 pthread_mutex_destroy(this->mutex);
75 delete this->mutex;
76 }
77 this->mutex = rhs.mutex;
78 rhs.mutex = nullptr;
79}
80
81//------------------------------------------------------------------------------
84inline
85void
87{
88 pthread_mutex_lock(this->mutex);
89}
90
91//------------------------------------------------------------------------------
94inline
95void
97{
98 pthread_mutex_unlock(this->mutex);
99}
100
101};
102//------------------------------------------------------------------------------
103#endif
Posix-implementation of critical section.
Definition posixcriticalsection.h:24
PosixCriticalSection()
constructor
Definition posixcriticalsection.h:44
void Leave() const
leave the critical section
Definition posixcriticalsection.h:96
pthread_mutex_t * mutex
Definition posixcriticalsection.h:37
void Enter() const
enter the critical section
Definition posixcriticalsection.h:86
void operator=(PosixCriticalSection &&rhs)
copy assignment
Definition posixcriticalsection.h:70
~PosixCriticalSection()
destructor
Definition posixcriticalsection.h:59
Posix implemention of a read-many write-few lock.
Definition posixsysfunc.cc:21