xoreos  0.0.5
threads.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
27 #include <cassert>
28 
29 #include "src/common/fallthrough.h"
31 #include <SDL_thread.h>
33 
34 #include "src/common/types.h"
35 #include "src/common/error.h"
36 #include "src/common/threads.h"
37 
38 static bool threadsInited = false;
39 static SDL_threadID threadsMainID;
40 
41 namespace Common {
42 
43 void initThreads() {
44  assert(!threadsInited);
45 
46  threadsInited = true;
47  threadsMainID = SDL_ThreadID();
48 }
49 
50 bool initedThreads() {
51  return threadsInited;
52 }
53 
54 bool isMainThread() {
55  assert(threadsInited);
56 
57  return SDL_ThreadID() == threadsMainID;
58 }
59 
61  if (!isMainThread())
62  throw Exception("Unsafe function called in non-main thread");
63 }
64 
65 } // End of namespace Common
START_IGNORE_IMPLICIT_FALLTHROUGH static STOP_IGNORE_IMPLICIT_FALLTHROUGH bool threadsInited
Definition: threads.cpp:38
Definition: 2dafile.h:39
#define START_IGNORE_IMPLICIT_FALLTHROUGH
Definition: fallthrough.h:79
bool isMainThread()
Returns true if called from the main thread, false otherwise.
Definition: threads.cpp:54
Basic exceptions to throw.
Threading system helpers.
void initThreads()
Initialize the global threading system.
Definition: threads.cpp:43
Low-level type definitions to handle fixed width types portably.
StackException Exception
Definition: error.h:59
#define STOP_IGNORE_IMPLICIT_FALLTHROUGH
Definition: fallthrough.h:80
bool initedThreads()
Was the global threading system initialized?
Definition: threads.cpp:50
void enforceMainThread()
Throws an Exception if called from a non-main thread.
Definition: threads.cpp:60
static SDL_threadID threadsMainID
Definition: threads.cpp:39
Compiler-specific defines to mark an implicit switch-case fallthrough.