OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4InteractiveThread.h
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
5  * Copyright (c) 2009-2016, The OpenClonk Team and contributors
6  *
7  * Distributed under the terms of the ISC license; see accompanying file
8  * "COPYING" for details.
9  *
10  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
11  * See accompanying file "TRADEMARK" for details.
12  *
13  * To redistribute this file separately, substitute the full license texts
14  * for the above references.
15  */
16 #ifndef C4INTERACTIVETHREAD_H
17 #define C4INTERACTIVETHREAD_H
18 
19 #include "platform/StdScheduler.h"
20 #include "platform/StdSync.h"
21 
22 // Event types
24 {
25  Ev_None = 0,
26 
28 
33 
35 
38 
40 
44 
46 };
47 
49 {
50 private:
51  class C4InteractiveThread *pNotify;
52 public:
53  void SetNotify(class C4InteractiveThread *pnNotify) { pNotify = pnNotify; }
54  bool Execute(int iTimeout, pollfd * readyfds) override;
55 };
56 
57 // Collects StdSchedulerProc objects and executes them in a separate thread
58 // Provides an event queue for the procs to communicate with the main thread
60 {
61 public:
64 
65  // Event callback interface
66  class Callback
67  {
68  public:
69  virtual void OnThreadEvent(C4InteractiveEventType eEvent, void *pEventData) = 0;
70  virtual ~Callback() = default;
71  };
72 
73 private:
74 
75  // the thread itself
76  StdSchedulerThread Scheduler;
77 
78  // event queue (signals to main thread)
79  struct Event
80  {
82  void *Data;
83 #ifdef _DEBUG
84  C4TimeMilliseconds Time;
85 #endif
86  Event *Next;
87  };
88  Event *pFirstEvent, *pLastEvent;
89  CStdCSec EventPushCSec, EventPopCSec;
90 
91  // callback objects for events of special types
92  Callback *pCallbacks[Ev_Last + 1];
93 
94  // proc that is added to the main thread to receive messages from our thread
96 
97 public:
98 
99  // process management
100  bool AddProc(StdSchedulerProc *pProc);
101  void RemoveProc(StdSchedulerProc *pProc);
102 
103  // event queue
104  bool PushEvent(C4InteractiveEventType eEventType, void *pData = nullptr);
105  void ProcessEvents(); // by main thread
106 
107  // special events
108  bool ThreadLog(const char *szMessage, ...) GNUC_FORMAT_ATTRIBUTE_O;
109  bool ThreadLogFatal(const char *szMessage, ...) GNUC_FORMAT_ATTRIBUTE_O;
110  bool ThreadLogS(const char *szMessage, ...) GNUC_FORMAT_ATTRIBUTE_O;
111  bool ThreadLogDebug(const char *szMessage, ...) GNUC_FORMAT_ATTRIBUTE_O;
112 
113  template<typename RType = void, typename Functor>
114  bool ThreadPostAsync(Functor function)
115  {
116  return PushEvent(Ev_Function, new std::function<RType()>(function));
117  }
118 
119  // event handlers
120  void SetCallback(C4InteractiveEventType eEvent, Callback *pnNetworkCallback)
121  { pCallbacks[eEvent] = pnNetworkCallback; }
122  void ClearCallback(C4InteractiveEventType eEvent, Callback *pnNetworkCallback)
123  { if (pCallbacks[eEvent] == pnNetworkCallback) pCallbacks[eEvent] = nullptr; }
124 
125 private:
126  bool PopEvent(C4InteractiveEventType *pEventType, void **ppData); // by main thread
127 
128 };
129 
130 #endif // C4INTERACTIVETHREAD_H
virtual void OnThreadEvent(C4InteractiveEventType eEvent, void *pEventData)=0
bool ThreadLogS(const char *szMessage,...) GNUC_FORMAT_ATTRIBUTE_O
bool ThreadPostAsync(Functor function)
bool ThreadLogFatal(const char *szMessage,...) GNUC_FORMAT_ATTRIBUTE_O
bool PushEvent(C4InteractiveEventType eEventType, void *pData=nullptr)
void ClearCallback(C4InteractiveEventType eEvent, Callback *pnNetworkCallback)
bool ThreadLog(const char *szMessage,...) GNUC_FORMAT_ATTRIBUTE_O
#define GNUC_FORMAT_ATTRIBUTE_O
virtual ~Callback()=default
C4InteractiveEventType
bool Execute(int iTimeout, pollfd *readyfds) override
bool AddProc(StdSchedulerProc *pProc)
void RemoveProc(StdSchedulerProc *pProc)
void SetCallback(C4InteractiveEventType eEvent, Callback *pnNetworkCallback)
bool ThreadLogDebug(const char *szMessage,...) GNUC_FORMAT_ATTRIBUTE_O
void SetNotify(class C4InteractiveThread *pnNotify)