EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Threading.cpp
Go to the documentation of this file.
1 
17 #include "Threading.h"
18 #include "log/LogNew.h"
19 #include "log/logsys.h"
20 
21 
23 : buf(nullptr),
24  tv(timeval()),
25  nfds(0),
26  sleepTime(0),
27  bufferLen(0)
28 {
29 }
30 
32 {
33  m_threads.clear();
34 }
35 
37  nfds = 1;
38  bufferLen = 64 * 1024; // 64kbyte recieve buffer, up from default of 8k
39  sleepTime = 10; //sConfig.rates.NetSleepTime
40  tv.tv_sec = 0;
41  tv.tv_usec = 0;
42  m_threads.clear();
43  sLog.Blue( " Threading", "Threading System Initialized.");
44 }
45 
47  while (true) {
48  Process();
50  }
51 }
52 
55  /* reset timeouts because select() reset them */
56  tv.tv_sec = 0;
57  tv.tv_usec = 0;
58  FD_ZERO(&rSoc);
59  FD_ZERO(&wSoc);
60  FD_SET(0, &rSoc);
61  FD_SET(0, &wSoc);
62  int status = select(nfds, &rSoc, &wSoc, nullptr, &tv);
63  if (status == 0) /* nothing ready yet */
64  return;
65  else if (status == -1) { /* error */
66  _log(THREAD__ERROR, "Process() - select() returned: %s", strerror(status));
67  return;
68  }
69  if (FD_ISSET(nfds, &rSoc)) {
70  buf = (char*) malloc (bufferLen);
71  /* read from socket here */
72  free(buf);
73  }
74  if (FD_ISSET(nfds, &wSoc)) {
75  buf = (char*) malloc (bufferLen);
76  /* write to socket here */
77  free(buf);
78  }
79 
80 }
82  /* when adding a new socket, we must add it to the read and write sets, with it's fd# */
83  FD_SET(soc, &rSoc);
84  FD_SET(soc, &wSoc);
85  ++nfds;
86 }
87 
88 void Threading::CreateThread(void *(*start_routine) (void *), void *args) {
89  pthread_t thread;
90  int status = pthread_create( &thread, nullptr, start_routine, args);
91  if (status) {
92  _log(THREAD__ERROR, "CreateThread() - Error Creating new thread: %s", strerror(errno));
93  } else {
94  _log(THREAD__INFO, "CreateThread() - Creating new threadID 0x%X", thread);
95  AddThread(thread);
96  pthread_detach(thread);
97  }
98 }
99 
100 void Threading::AddThread(pthread_t thread) {
101  m_threads.push_back(thread);
102  _log(THREAD__INFO, "AddThread() - Added thread ID 0x%X", thread);
103 }
104 
105 void Threading::RemoveThread(pthread_t thread) {
106  for (std::vector<pthread_t>::iterator cur = m_threads.begin(); cur != m_threads.end(); ++cur) {
107  if ((*cur) == thread) {
108  _log(THREAD__INFO, "RemoveThread() called for thread ID 0x%X", thread);
109  m_threads.erase(cur);
110  return;
111  }
112  }
113  _log(THREAD__ERROR, "RemoveThread() - Called on unregistered threadID 0x%X", thread);
114 }
115 
117  for (auto cur : m_threads)
118  sLog.Warning( " ", "ThreadID 0x%X", cur );
119 }
120 
122  if (!m_threads.size()) {
123  _log(THREAD__MESSAGE, "EndThreads() - There are no active threads.");
124  return;
125  }
126  _log(THREAD__MESSAGE, "EndThreads() - Joining %u currently active threads.", m_threads.size());
127  std::vector<pthread_t>::iterator cur = m_threads.begin();
128  while ((cur != m_threads.end())) {
129  _log(THREAD__TRACE, "EndThreads() - Removing threadID 0x%X", (*cur));
130  cur = m_threads.erase(cur);
131  }
132  m_threads.clear();
133 }
struct timeval tv
Definition: Threading.h:39
uint32 bufferLen
Definition: Threading.h:43
#define _log(type, fmt,...)
Definition: logsys.h:124
std::vector< pthread_t > m_threads
Definition: Threading.h:46
uint8 sleepTime
Definition: Threading.h:42
void Initialize()
Definition: Threading.cpp:36
* args
#define sLog
Evaluates to a NewLog instance.
Definition: LogNew.h:250
char * buf
Definition: Threading.h:37
void RunLoop()
Definition: Threading.cpp:46
void RemoveThread(pthread_t thread)
Definition: Threading.cpp:105
fd_set wSoc
Definition: Threading.h:38
void ListThreads()
Definition: Threading.cpp:116
int SOCKET
Definition: eve-compat.h:127
void Sleep(uint32 x)
Definition: eve-compat.cpp:32
void CreateThread(void *(*start_routine)(void *), void *args)
Definition: Threading.cpp:88
void AddThread(pthread_t thread)
Definition: Threading.cpp:100
fd_set rSoc
Definition: Threading.h:38
void Process()
Definition: Threading.cpp:54
uint8 nfds
Definition: Threading.h:41
void AddSocket(SOCKET soc)
Definition: Threading.cpp:81
void EndThreads()
Definition: Threading.cpp:121