GameApi 28
Library used by GameApi Builder tool
Loading...
Searching...
No Matches
chaiscript_threading.hpp
Go to the documentation of this file.
1// This file is distributed under the BSD License.
2// See "license.txt" for details.
3// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
4// Copyright 2009-2017, Jason Turner (jason@emptycrate.com)
5// http://www.chaiscript.com
6
7// This is an open source non-commercial project. Dear PVS-Studio, please check it.
8// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
9
10
11#ifndef CHAISCRIPT_THREADING_HPP_
12#define CHAISCRIPT_THREADING_HPP_
13
14
15#include <unordered_map>
16
17#ifndef CHAISCRIPT_NO_THREADS
18#include <thread>
19#include <mutex>
20#else
21#ifndef CHAISCRIPT_NO_THREADS_WARNING
22#pragma message ("ChaiScript is compiling without thread safety.")
23#endif
24#endif
25
27
35
36namespace chaiscript
37{
38 namespace detail
39 {
43 namespace threading
44 {
45
46#ifndef CHAISCRIPT_NO_THREADS
47
48 template<typename T>
49 using unique_lock = std::unique_lock<T>;
50
51 template<typename T>
52 using shared_lock = std::unique_lock<T>;
53
54 template<typename T>
55 using lock_guard = std::lock_guard<T>;
56
57
58 using shared_mutex = std::mutex;
59
60 using std::mutex;
61
62 using std::recursive_mutex;
63
66 template<typename T>
68 {
69 public:
70 Thread_Storage() = default;
71 Thread_Storage(const Thread_Storage &) = delete;
75
77 {
78 t().erase(this);
79 }
80
81 inline const T *operator->() const
82 {
83 return &(t()[this]);
84 }
85
86 inline const T &operator*() const
87 {
88 return t()[this];
89 }
90
91 inline T *operator->()
92 {
93 return &(t()[this]);
94 }
95
96 inline T &operator*()
97 {
98 return t()[this];
99 }
100
101
102 void *m_key;
103
104 private:
105 static std::unordered_map<const void*, T> &t()
106 {
107 thread_local std::unordered_map<const void *, T> my_t;
108 return my_t;
109 }
110 };
111
112#else // threading disabled
113 template<typename T>
114 class unique_lock
115 {
116 public:
117 explicit unique_lock(T &) {}
118 void lock() {}
119 void unlock() {}
120 };
121
122 template<typename T>
123 class shared_lock
124 {
125 public:
126 explicit shared_lock(T &) {}
127 void lock() {}
128 void unlock() {}
129 };
130
131 template<typename T>
132 class lock_guard
133 {
134 public:
135 explicit lock_guard(T &) {}
136 };
137
138 class shared_mutex { };
139
140 class recursive_mutex {};
141
142
143 template<typename T>
144 class Thread_Storage
145 {
146 public:
147 explicit Thread_Storage()
148 {
149 }
150
151 inline T *operator->() const
152 {
153 return &obj;
154 }
155
156 inline T &operator*() const
157 {
158 return obj;
159 }
160
161 private:
162 mutable T obj;
163 };
164
165#endif
166 }
167 }
168}
169
170
171
172#endif
173
Definition chaiscript_threading.hpp:68
const T * operator->() const
Definition chaiscript_threading.hpp:81
Thread_Storage & operator=(Thread_Storage &&)=delete
Thread_Storage(const Thread_Storage &)=delete
T * operator->()
Definition chaiscript_threading.hpp:91
~Thread_Storage()
Definition chaiscript_threading.hpp:76
T & operator*()
Definition chaiscript_threading.hpp:96
void * m_key
Definition chaiscript_threading.hpp:102
const T & operator*() const
Definition chaiscript_threading.hpp:86
Thread_Storage & operator=(const Thread_Storage &)=delete
Definition chaiscript_threading.hpp:44
std::lock_guard< T > lock_guard
Definition chaiscript_threading.hpp:55
std::unique_lock< T > shared_lock
Definition chaiscript_threading.hpp:52
std::mutex shared_mutex
Definition chaiscript_threading.hpp:58
std::unique_lock< T > unique_lock
Definition chaiscript_threading.hpp:49
Classes and functions reserved for internal use. Items in this namespace are not supported.
Namespace chaiscript contains every API call that the average user will be concerned with.
Definition GameApi.h:42