Phasor  01.00.10.059
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PhasorThread.h
Go to the documentation of this file.
1 // PhasorThread.h
2 #pragma once
3 
4 #include "../Common/Threads.h"
5 #include <memory>
6 #include <stdio.h>
7 #include <list>
8 
9 class PhasorThread;
10 
12 {
13  DEST_NONE = 0,
16 };
17 
19 {
20 private:
21  DWORD dwExpiryTime, dwDelay;
22 
23  void SetExpiry(DWORD dwDelay);
24 
25 protected:
26  PhasorThreadEvent(DWORD dwDelay);
27 
28  void Reinvoke(DWORD dwDelay, PhasorThread& thread, event_dest_t dest);
29 
30  // Reinvoke the event in the specified thread (next check)
31  void ReinvokeInMain(PhasorThread& thread, DWORD dwDelay = 0);
32  void ReinvokeInAux(PhasorThread& thread, DWORD dwDelay = 0);
33 
34 public:
35  //virtual std::unique_ptr<PhasorThreadEvent> Create(DWORD dwDelayMilliseconds) = 0;
36  virtual ~PhasorThreadEvent();
37 
38  DWORD GetExpiryTime() const;
39  bool ready(DWORD dwCurTicks) const;
40 
41  // Event is invoked in the aux thread
42  virtual void OnEventAux(PhasorThread& thread) {};
43 
44  // Event is invoked in the main thread
45  virtual void OnEventMain(PhasorThread& thread) {};
46 };
47 
48 // This class implements Phasor's worker (auxillary) thread.
49 class PhasorThread : public Thread
50 {
51 private:
52  DWORD dwPhasorThreadId, dwMainThreadId;
53  typedef std::list<std::shared_ptr<PhasorThreadEvent>> eventlist_t;
54  eventlist_t auxEvents, mainEvents;
55  CRITICAL_SECTION cs;
56 
57  // This variable can be set by events if they want to be raised again.
58  event_dest_t reinvoke_in;
59 
60  // The event currently being processed wants to be reinvoked
61  void SetReinvoke(event_dest_t dest);
62 
63  // Adds an event to the specified list
64  DWORD AddEvent(event_dest_t dest, std::shared_ptr<PhasorThreadEvent>& e);
65 
66  // Processes any required events in the specified thread. A lock for
67  // the lists should have been obtained before calling.
68  void ProcessEventsLocked(event_dest_t dest, bool ignoretime);
69 
70 public:
71  PhasorThread();
72  virtual ~PhasorThread();
73 
74  // This will only be called from the main thread
75  virtual bool run();
76 
77  // Process events
78  void ProcessEvents(bool block=false, bool main=true, bool ignoretime=false);
79 
80  // Value returned is the event id (used in removing)
81  DWORD InvokeInMain(std::shared_ptr<PhasorThreadEvent> e);
82  DWORD InvokeInAux(std::shared_ptr<PhasorThreadEvent> e);
83 
84  // Removes the specified timer
85  void RemoveAuxEvent(DWORD id);
86 
87  // Thread entry point
88  int thread_main();
89 
90  friend class PhasorThreadEvent;
91 };