SuperTuxKart
Loading...
Searching...
No Matches
synchronised.hpp
1//
2// SuperTuxKart - a fun racing game with go-kart
3// Copyright (C) 2010-2015 Joerg Henrichs
4//
5// This program is free software; you can redistribute it and/or
6// modify it under the terms of the GNU General Public License
7// as published by the Free Software Foundation; either version 3
8// of the License, or (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program; if not, write to the Free Software
17// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19#ifndef HEADER_SYNCHRONISED_HPP
20#define HEADER_SYNCHRONISED_HPP
21
22#include <mutex>
23
26template<typename TYPE>
28{
29private:
31 mutable std::mutex m_mutex;
33 TYPE m_data;
34public:
35 // ------------------------------------------------------------------------
38 {
39 } // Synchronised()
40
41 // ------------------------------------------------------------------------
43 Synchronised(const TYPE &v)
44 {
45 m_data = v;
46 } // Synchronised
47
48 // ------------------------------------------------------------------------
52 {
53 } // ~Synchronised
54
55 // ------------------------------------------------------------------------
59 void setAtomic(const TYPE &v)
60 {
61 std::lock_guard<std::mutex> lock(m_mutex);
62 m_data = v;
63 } // set
64
65 // ------------------------------------------------------------------------
68 TYPE getAtomic() const
69 {
70 TYPE v;
71 std::unique_lock<std::mutex> ul(m_mutex);
72 v = m_data;
73 ul.unlock();
74 return v;
75 } // get
76 // ------------------------------------------------------------------------
82 TYPE &getData()
83 {
84 return m_data;
85 } // getData
86 // ------------------------------------------------------------------------
87 const TYPE &getData() const
88 {
89 return m_data;
90 } // getData
91 // ------------------------------------------------------------------------
95 void lock() const { m_mutex.lock(); }
96 // ------------------------------------------------------------------------
99 void unlock() const { m_mutex.unlock(); }
100 // ------------------------------------------------------------------------
103 std::unique_lock<std::mutex> acquireMutex() const
104 { return std::unique_lock<std::mutex>(m_mutex); }
105private:
106 // Make sure that no actual copying is taking place
107 // ------------------------------------------------------------------------
108 void operator=(const Synchronised<TYPE>& v) {}
109};
110
111#endif
A variable that is automatically synchronised using pthreads mutex.
Definition: synchronised.hpp:28
TYPE & getData()
Returns a reference to the original data file.
Definition: synchronised.hpp:82
void lock() const
Locks the mutex.
Definition: synchronised.hpp:95
void setAtomic(const TYPE &v)
Sets the value of this variable using a mutex.
Definition: synchronised.hpp:59
Synchronised()
Initialise the data and the mutex with default constructors.
Definition: synchronised.hpp:37
Synchronised(const TYPE &v)
Initialise the data and the mutex.
Definition: synchronised.hpp:43
void unlock() const
Unlocks the mutex.
Definition: synchronised.hpp:99
std::mutex m_mutex
The mutex to protect this variable with.
Definition: synchronised.hpp:31
TYPE m_data
The actual data to be used.
Definition: synchronised.hpp:33
std::unique_lock< std::mutex > acquireMutex() const
Gives unique_lock to the mutex, which can then be used by std::condition_variable wait.
Definition: synchronised.hpp:103
TYPE getAtomic() const
Returns a copy of this variable.
Definition: synchronised.hpp:68
~Synchronised()
Destroy this mutex.
Definition: synchronised.hpp:51