Simulant  21.12-194
A portable game engine for Windows, OSX, Linux, Dreamcast, and PSP
gl_thread_check.h
1 /* * Copyright (c) 2011-2017 Luke Benstead https://simulant-engine.appspot.com
2  *
3  * This file is part of Simulant.
4  *
5  * Simulant is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * Simulant 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 Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with Simulant. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef GL_THREAD_CHECK_H
20 #define GL_THREAD_CHECK_H
21 
22 #include <stdexcept>
23 #include <memory>
24 
25 #include "../threads/thread.h"
26 #include "../logging.h"
27 
28 namespace smlt {
29 
31  public std::runtime_error {
32 
33 public:
35  std::runtime_error("Attempted to call OpenGL from the wrong thread") {}
36 };
37 
38 class GLThreadCheck;
39 
40 extern std::shared_ptr<GLThreadCheck> GL_thread;
41 
43 public:
44  static void init() {
45  GL_thread.reset(new GLThreadCheck(thread::this_thread_id()));
46  }
47 
48  static void clean_up() {
49  GL_thread.reset();
50  }
51 
52  static void check();
53 
54  static bool is_current() {
55  if(!GL_thread) {
56  return false;
57  }
58 
59  return GL_thread->do_check(false);
60  }
61 
62  thread::ThreadID thread_id() const {
63  return render_thread_id_;
64  }
65 
66 private:
67  GLThreadCheck(thread::ThreadID render_thread);
68 
69  bool do_check(bool raise=true) {
70 
71  if(thread::this_thread_id() != render_thread_id_) {
72  if(raise) {
73  throw WrongThreadError();
74  } else {
75  return false;
76  }
77  }
78 
79  return true;
80  }
81 
82 
83  thread::ThreadID render_thread_id_;
84 };
85 
86 }
87 
88 #endif // GL_THREAD_CHECK_H
smlt::GLThreadCheck
Definition: gl_thread_check.h:42
smlt
Definition: animation.cpp:25
smlt::WrongThreadError
Definition: gl_thread_check.h:31