Mana
Loading...
Searching...
No Matches
mutex.h
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2008-2009 The Mana World Development Team
4 * Copyright (C) 2009-2012 The Mana Developers
5 *
6 * This file is part of The Mana Client.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#pragma once
23
24#include "log.h"
25
26#include <SDL_thread.h>
27
32class Mutex
33{
34public:
35 Mutex();
36 ~Mutex();
37 Mutex(Mutex&&) = delete; // prevent moving
38 Mutex(const Mutex&) = delete; // prevent copying
39 Mutex& operator=(const Mutex&) = delete;
40
41 void lock();
42 void unlock();
43
44private:
45 SDL_mutex *mMutex;
46};
47
52{
53public:
54 MutexLocker(Mutex *mutex);
56 MutexLocker(const MutexLocker&) = delete; // prevent copying
59
60private:
62};
63
64
66{
67 mMutex = SDL_CreateMutex();
68}
69
71{
72 SDL_DestroyMutex(mMutex);
73}
74
75inline void Mutex::lock()
76{
77 if (SDL_mutexP(mMutex) == -1)
78 Log::info("Mutex locking failed: %s", SDL_GetError());
79}
80
81inline void Mutex::unlock()
82{
83 if (SDL_mutexV(mMutex) == -1)
84 Log::info("Mutex unlocking failed: %s", SDL_GetError());
85}
86
87
89 mMutex(mutex)
90{
91 mMutex->lock();
92}
93
95 mMutex(rhs.mMutex)
96{
97 rhs.mMutex = nullptr;
98}
99
101{
102 if (mMutex)
103 mMutex->unlock();
104}
105
109template <typename T>
111{
112 class Locked : private MutexLocker
113 {
114 public:
115 Locked(T &data, Mutex &mutex)
116 : MutexLocker(&mutex)
117 , mData(data)
118 {}
119
120 Locked(Locked&& rhs) = delete;
121 Locked(const Locked&) = delete;
122 Locked& operator=(const Locked&) = delete;
123 Locked& operator=(Locked&&) = delete;
124
125 T &operator*() const { return mData; }
126 T *operator->() const { return &mData; }
127
128 private:
130 };
131
132public:
133 ThreadSafe() = default;
134 ThreadSafe(const T &data)
135 : mData(data)
136 {}
137
138 Locked lock() { return { mData, mMutex }; }
139
140private:
143};
A convenience class for locking a mutex.
Definition mutex.h:52
Mutex * mMutex
Definition mutex.h:61
~MutexLocker()
Definition mutex.h:100
MutexLocker & operator=(const MutexLocker &)=delete
MutexLocker(Mutex *mutex)
Definition mutex.h:88
MutexLocker(const MutexLocker &)=delete
A mutex provides mutual exclusion of access to certain data that is accessed by multiple threads.
Definition mutex.h:33
Mutex(const Mutex &)=delete
Mutex & operator=(const Mutex &)=delete
Mutex(Mutex &&)=delete
void unlock()
Definition mutex.h:81
Mutex()
Definition mutex.h:65
SDL_mutex * mMutex
Definition mutex.h:45
~Mutex()
Definition mutex.h:70
void lock()
Definition mutex.h:75
Locked(Locked &&rhs)=delete
T & operator*() const
Definition mutex.h:125
Locked(T &data, Mutex &mutex)
Definition mutex.h:115
T * operator->() const
Definition mutex.h:126
Locked & operator=(Locked &&)=delete
Locked & operator=(const Locked &)=delete
Locked(const Locked &)=delete
A template class for wrapping data that is accessed by multiple threads.
Definition mutex.h:111
ThreadSafe()=default
ThreadSafe(const T &data)
Definition mutex.h:134
Mutex mMutex
Definition mutex.h:142
Locked lock()
Definition mutex.h:138
void info(const char *log_text,...) LOG_PRINTF_ATTR