EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Threading Class Reference

#include "Threading.h"

Inheritance diagram for Threading:
Collaboration diagram for Threading:

Public Member Functions

uint8 Count ()
 
Threading.cpp

lightweight thread pool specifically for EvEmu Sockets this code is very basic and very specific. : Allan

Date
: 05 March 2016
 Threading ()
 
 ~Threading ()
 
void Initialize ()
 
void RunLoop ()
 
void Process ()
 
void AddSocket (SOCKET soc)
 
void AddThread (pthread_t thread)
 
void RemoveThread (pthread_t thread)
 
void CreateThread (void *(*start_routine)(void *), void *args)
 
void EndThreads ()
 
void ListThreads ()
 
- Public Member Functions inherited from Singleton< Threading >
 Singleton ()
 Primary constructor. More...
 

Protected Attributes

char * buf
 
fd_set rSoc
 
fd_set wSoc
 
struct timeval tv
 
uint8 nfds
 
uint8 sleepTime
 
uint32 bufferLen
 

Private Attributes

std::vector< pthread_t > m_threads
 

Additional Inherited Members

- Static Public Member Functions inherited from Singleton< Threading >
static Threadingget ()
 
- Static Protected Attributes inherited from Singleton< Threading >
static std::shared_ptr< ThreadingmInstance
 

Detailed Description

Definition at line 16 of file Threading.h.

Constructor & Destructor Documentation

Threading::Threading ( )
Todo:
the entire concurrency idea needs work throughout evemu. currently, it uses posix threads, boost, and shared_pointer. they all need updating
Note
image server uses boost::asio::thread and has no problems....leave for now, and may leave as is.

update this to use thread pools and strictly for network sockets

Definition at line 22 of file Threading.cpp.

23 : buf(nullptr),
24  tv(timeval()),
25  nfds(0),
26  sleepTime(0),
27  bufferLen(0)
28 {
29 }
struct timeval tv
Definition: Threading.h:39
uint32 bufferLen
Definition: Threading.h:43
uint8 sleepTime
Definition: Threading.h:42
char * buf
Definition: Threading.h:37
uint8 nfds
Definition: Threading.h:41
Threading::~Threading ( )

Definition at line 31 of file Threading.cpp.

References m_threads.

32 {
33  m_threads.clear();
34 }
std::vector< pthread_t > m_threads
Definition: Threading.h:46

Member Function Documentation

void Threading::AddSocket ( SOCKET  soc)

Definition at line 81 of file Threading.cpp.

References nfds, rSoc, and wSoc.

81  {
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 }
fd_set wSoc
Definition: Threading.h:38
fd_set rSoc
Definition: Threading.h:38
uint8 nfds
Definition: Threading.h:41
void Threading::AddThread ( pthread_t  thread)

Definition at line 100 of file Threading.cpp.

References _log, and m_threads.

Referenced by CreateThread().

100  {
101  m_threads.push_back(thread);
102  _log(THREAD__INFO, "AddThread() - Added thread ID 0x%X", thread);
103 }
#define _log(type, fmt,...)
Definition: logsys.h:124
std::vector< pthread_t > m_threads
Definition: Threading.h:46

Here is the caller graph for this function:

uint8 Threading::Count ( )
inline

Definition at line 34 of file Threading.h.

References m_threads.

34 { return (uint8)(m_threads.size()); }
unsigned __int8 uint8
Definition: eve-compat.h:46
std::vector< pthread_t > m_threads
Definition: Threading.h:46
void Threading::CreateThread ( void *(*)(void *)  start_routine,
void *  args 
)

Definition at line 88 of file Threading.cpp.

References _log, and AddThread().

88  {
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 }
#define _log(type, fmt,...)
Definition: logsys.h:124
* args
void AddThread(pthread_t thread)
Definition: Threading.cpp:100

Here is the call graph for this function:

void Threading::EndThreads ( )

Definition at line 121 of file Threading.cpp.

References _log, and m_threads.

121  {
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 }
#define _log(type, fmt,...)
Definition: logsys.h:124
std::vector< pthread_t > m_threads
Definition: Threading.h:46
void Threading::Initialize ( )

Definition at line 36 of file Threading.cpp.

References bufferLen, m_threads, nfds, sleepTime, sLog, and tv.

36  {
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 }
struct timeval tv
Definition: Threading.h:39
uint32 bufferLen
Definition: Threading.h:43
std::vector< pthread_t > m_threads
Definition: Threading.h:46
uint8 sleepTime
Definition: Threading.h:42
#define sLog
Evaluates to a NewLog instance.
Definition: LogNew.h:250
uint8 nfds
Definition: Threading.h:41
void Threading::ListThreads ( )

Definition at line 116 of file Threading.cpp.

References m_threads, and sLog.

116  {
117  for (auto cur : m_threads)
118  sLog.Warning( " ", "ThreadID 0x%X", cur );
119 }
std::vector< pthread_t > m_threads
Definition: Threading.h:46
#define sLog
Evaluates to a NewLog instance.
Definition: LogNew.h:250
void Threading::Process ( )
Todo:
begin basic thread pool for processing sockets, using select()

Definition at line 54 of file Threading.cpp.

References _log, buf, bufferLen, nfds, rSoc, tv, and wSoc.

Referenced by RunLoop().

54  {
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 }
struct timeval tv
Definition: Threading.h:39
uint32 bufferLen
Definition: Threading.h:43
#define _log(type, fmt,...)
Definition: logsys.h:124
char * buf
Definition: Threading.h:37
fd_set wSoc
Definition: Threading.h:38
fd_set rSoc
Definition: Threading.h:38
uint8 nfds
Definition: Threading.h:41

Here is the caller graph for this function:

void Threading::RemoveThread ( pthread_t  thread)

Definition at line 105 of file Threading.cpp.

References _log, and m_threads.

105  {
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 }
#define _log(type, fmt,...)
Definition: logsys.h:124
std::vector< pthread_t > m_threads
Definition: Threading.h:46
void Threading::RunLoop ( )

Definition at line 46 of file Threading.cpp.

References Process(), Sleep(), and sleepTime.

46  {
47  while (true) {
48  Process();
50  }
51 }
uint8 sleepTime
Definition: Threading.h:42
void Sleep(uint32 x)
Definition: eve-compat.cpp:32
void Process()
Definition: Threading.cpp:54

Here is the call graph for this function:

Member Data Documentation

char* Threading::buf
protected

Definition at line 37 of file Threading.h.

Referenced by Process().

uint32 Threading::bufferLen
protected

Definition at line 43 of file Threading.h.

Referenced by Initialize(), and Process().

std::vector<pthread_t> Threading::m_threads
private

Definition at line 46 of file Threading.h.

Referenced by AddThread(), Count(), EndThreads(), Initialize(), ListThreads(), RemoveThread(), and ~Threading().

uint8 Threading::nfds
protected

Definition at line 41 of file Threading.h.

Referenced by AddSocket(), Initialize(), and Process().

fd_set Threading::rSoc
protected

Definition at line 38 of file Threading.h.

Referenced by AddSocket(), and Process().

uint8 Threading::sleepTime
protected

Definition at line 42 of file Threading.h.

Referenced by Initialize(), and RunLoop().

struct timeval Threading::tv
protected

Definition at line 39 of file Threading.h.

Referenced by Initialize(), and Process().

fd_set Threading::wSoc
protected

Definition at line 38 of file Threading.h.

Referenced by AddSocket(), and Process().


The documentation for this class was generated from the following files: