thread.h
1 /*************************************************************************/
2 /* thread.h */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* http://www.godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
9 /* */
10 /* Permission is hereby granted, free of charge, to any person obtaining */
11 /* a copy of this software and associated documentation files (the */
12 /* "Software"), to deal in the Software without restriction, including */
13 /* without limitation the rights to use, copy, modify, merge, publish, */
14 /* distribute, sublicense, and/or sell copies of the Software, and to */
15 /* permit persons to whom the Software is furnished to do so, subject to */
16 /* the following conditions: */
17 /* */
18 /* The above copyright notice and this permission notice shall be */
19 /* included in all copies or substantial portions of the Software. */
20 /* */
21 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
22 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
23 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
24 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
25 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
26 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
27 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
28 /*************************************************************************/
29 #ifndef THREAD_H
30 #define THREAD_H
31 
32 
33 #include "typedefs.h"
38 #include "ustring.h"
39 
40 typedef void (*ThreadCreateCallback)(void *p_userdata);
41 
42 class Thread {
43 public:
44 
45  enum Priority {
46 
47  PRIORITY_LOW,
48  PRIORITY_NORMAL,
49  PRIORITY_HIGH
50  };
51 
52  struct Settings {
53 
54  Priority priority;
55  Settings() { priority=PRIORITY_NORMAL; }
56  };
57 
58 
59 
60  typedef uint64_t ID;
61 
62 protected:
63  static Thread* (*create_func)(ThreadCreateCallback p_callback,void *,const Settings&);
64  static ID (*get_thread_ID_func)();
65  static void (*wait_to_finish_func)(Thread*);
66  static Error (*set_name_func)(const String&);
67 
68  friend class Main;
69 
70  static ID _main_thread_id;
71 
72 
73  Thread();
74 public:
75 
76 
77  virtual ID get_ID() const=0;
78 
79  static Error set_name(const String &p_name);
80  _FORCE_INLINE_ static ID get_main_ID() { return _main_thread_id; }
81  static ID get_caller_ID();
82  static void wait_to_finish(Thread *p_thread);
83  static Thread * create(ThreadCreateCallback p_callback,void * p_user,const Settings& p_settings=Settings());
84 
85 
86  virtual ~Thread();
87 
88 };
89 
90 #endif
91 
static ID get_caller_ID()
get the ID of the caller function ID
Definition: thread.cpp:39
static _FORCE_INLINE_ ID get_main_ID()
get the ID of the main thread
Definition: thread.h:80
static Thread * create(ThreadCreateCallback p_callback, void *p_user, const Settings &p_settings=Settings())
Static function to create a thread, will call p_callback.
Definition: thread.cpp:46
Definition: thread.h:42
static void wait_to_finish(Thread *p_thread)
waits until thread is finished, and deallocates it.
Definition: thread.cpp:55
Definition: main.h:40
Definition: thread.h:52
Definition: ustring.h:64