HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NET_WebWorkerThread.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: NET_WebWorkerThread.h
7  *
8  * COMMENTS:
9  *
10  */
11 
12 #ifndef __NET_WEBWORKERTHREAD_H__
13 #define __NET_WEBWORKERTHREAD_H__
14 
15 #include "NET_API.h"
16 
17 #include "NET_WebEndpoint.h"
18 #include "NET_WebStatsManager.h"
19 
20 #include <UT/UT_Array.h>
21 #include <UT/UT_Condition.h>
22 #include <UT/UT_Debug.h>
23 #include <UT/UT_Lock.h>
24 #include <UT/UT_SysClone.h>
25 #include <UT/UT_UniquePtr.h>
26 
27 #include <SYS/SYS_Inline.h>
28 
29 #include <atomic>
30 #include <functional>
31 
32 class UT_ThreadFarm;
33 class UT_Thread;
34 
35 class NET_API NET_WebWorkerThread
36 {
37 public:
38  enum State
39  {
43  kPaused
44  };
45 
48 
49  NET_WebWorkerThread(const NET_WebWorkerThread &) = delete;
50  NET_WebWorkerThread &operator=(const NET_WebWorkerThread &) = delete;
51 
53  NET_WebWorkerThread &operator=(NET_WebWorkerThread &&) = delete;
54 
55  void startThread();
56  void stop();
57  void pause();
58  void resume();
59 
60  SYS_FORCE_INLINE bool isRunning() const { return myState == kRunning; }
61  SYS_FORCE_INLINE bool isPaused() const { return myState == kPaused; }
62  SYS_FORCE_INLINE State state() const { return myState; }
63 
65  {
66  myStatsManager.addCurrentQueueSize(1);
67  {
68  UT_AutoLock lock(myWorkLock);
69  myQueue.emplace_back(data);
70  }
71  // Notify our thread theres data to handle
72  notifyReady();
73  }
74 
75 private:
76  static void *processEvents(void *arg);
77 
78  SYS_FORCE_INLINE void setState(State state) { myState = state; }
79 
80  SYS_FORCE_INLINE bool isWorking() const
81  {
82  return myIsWorking.load(std::memory_order_seq_cst);
83  }
84  SYS_FORCE_INLINE void setIsWorking(bool is_working)
85  {
86  myIsWorking.store(is_working, std::memory_order_seq_cst);
87  }
88 
89  bool internalWaitForEvents();
90  void internalProcessEvents();
91  void notifyReady();
92  NET_WebEndpointPtr getNextWorkItem();
93 
94  std::atomic_bool myIsWorking;
95 
97 
98  State myState;
99 
100  UT_Condition myCondition;
101  UT_Lock myWorkLock;
102  bool myReady = false;
103 
104  NET_WebStatsManager &myStatsManager;
105 
106  // The thread farm this worker belongs to and the thread its executing on
107  UT_ThreadFarm &myFarm;
108  UT_Thread *myThread;
109 
110  unsigned int myMaxEventsToProcess = 1000;
111 };
112 
113 #endif // __NET_WEBWORKERTHREAD_H__
114 
SYS_FORCE_INLINE State state() const
UT_SharedPtr< NET_WebEndpoint > NET_WebEndpointPtr
internal::named_arg< T, char > arg(string_view name, const T &arg)
Definition: core.h:1393
Condition synchronization primitive.
Definition: UT_Condition.h:25
GLint GLenum GLsizei GLint GLsizei const void * data
Definition: glew.h:1379
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
Simple holder for statistics relating to the web server.
void runAsync(NET_WebEndpointPtr data)
SYS_FORCE_INLINE bool isPaused() const
SYS_FORCE_INLINE bool isRunning() const
void pause(int delay)
Definition: thread.h:134