Oxygine  1
2g game engine
ThreadDispatcher.h
1 #pragma once
2 #include "oxygine-include.h"
3 #include <vector>
4 #include <functional>
5 
6 #if defined(_WIN32) && !defined(__MINGW32__)
7 typedef struct pthread_mutex_t_* pthread_mutex_t;
8 typedef struct pthread_cond_t_* pthread_cond_t;
9 #else
10 # include "pthread.h"
11 #endif
12 
13 namespace oxygine
14 {
16  {
17  public:
18  MutexPthreadLock(pthread_mutex_t& m, bool lock = true);
20 
21  protected:
22  pthread_mutex_t& _mutex;
23  bool _locked;
24  };
25  /*
26  class TDMessage
27  {
28  public:
29  TDMessage& withMSGID(int id) { msgid = id; return *this; }
30  TDMessage& withArgs(void* Arg1, void* Arg2 = 0) { arg1 = Arg1; arg2 = Arg2; return *this; }
31  TDMessage& withHighPriority() { hp = true; return *this; }
32  TDMessage& withReply() { reply = true; return *this; }
33 
34  int msgid;
35  void* arg1;
36  void* arg2;
37  bool hp;
38  bool reply;
39 
40  protected:
41  };*/
42 
43 
46  {
47  public:
48  struct message;
49  typedef void (*callback)(const message& m);
50  struct message
51  {
52  message(): msgid(0), arg1(0), arg2(0), cb(0), cbData(0), _id(0), need_reply(false) {}
53 
54  int msgid;
55  void* arg1;
56  void* arg2;
57 
58  callback cb;
59  void* cbData;
60 #ifndef __S3E__
61  std::function< void() > cbFunction;
62 #endif
63 
64  unsigned int _id;
65  bool need_reply;
66  };
67 
68  struct peekMessage: public message
69  {
70  peekMessage() : num(-1) {}
71  int num;
72  };
73 
74 
77 
78  bool empty();
79  size_t size();
80 
81 
82  //blocking
83  void wait();
84  //blocking
85  void get(message& ev);
86 
87 
88  bool peek(peekMessage& ev, bool del);
89  void clear();
90 
91  //blocking, sends message and waiting reply from other thread
92  void* send(int msgid, void* arg1, void* arg2);
93  //blocking, sends callback and waiting until it is done
94  void* sendCallback(void* arg1, void* arg2, callback cb, void* cbData, bool highPriority = false);
95 #ifndef __S3E__
96  //blocking, sends callback and waiting until it is done
97  void sendCallback(const std::function<void()>&);
98 #endif
99 
100 
101 
102  //async, sends post message
103  void post(int msgid, void* arg1, void* arg2);
104  //async, sends post callback
105  void postCallback(int msgid, void* arg1, void* arg2, callback cb, void* cbData);
106  void postCallback(void* arg1, void* arg2, callback cb, void* cbData);
107 #ifndef __S3E__
108  //async, sends post callback
109  void postCallback(const std::function<void()>&);
110 #endif
111  //remove scheduled postCallback
112  void removeCallback(int msgid, callback cb, void* cbData);
113 
114  void reply(void* val);
115 
116  //void post(TDMessage&);
117  //void send(TDMessage&);
118 
119  std::vector<message>& lockMessages();
120  void unlockMessages();
121 
122  private:
123  void _waitMessage();
124  void _waitReply(int id);
125 
126  void _runCallbacks();
127 
128  void _pushMessage(message&);
129  void _pushMessageWaitReply(message&, bool highPriority = false);
130  void _popMessage(message&);
131  void _popMessageNoCB(message&);
132  void _replyLast(void* val);
133 
134 
135  pthread_mutex_t _mutex;
136  pthread_cond_t _cond;
137 
138  typedef std::vector<message> messages;
139  messages _events;
140  message _last;
141 
142  void* _result;
143  unsigned int _id;
144  int _replyingTo;
145  };
146 
147 
149 }
Definition: ThreadDispatcher.h:15
Definition: ThreadDispatcher.h:45
–oxgl-end–!
Definition: Actor.h:14
Definition: ThreadDispatcher.h:50
Definition: ThreadDispatcher.h:68