Anope IRC Services  Version 2.0
threadengine.cpp
Go to the documentation of this file.
1 /*
2  *
3  * (C) 2003-2014 Anope Team
4  * Contact us at team@anope.org
5  *
6  * Please read COPYING and README for further details.
7  *
8  * Based on the original code of Epona by Lara.
9  * Based on the original code of Services by Andy Church.
10  *
11  */
12 
13 #include "services.h"
14 #include "threadengine.h"
15 #include "anope.h"
16 
17 #ifndef _WIN32
18 #include <pthread.h>
19 #endif
20 
22 {
23  /* Threadengine attributes used by this thread engine */
24  static pthread_attr_t attr;
25  static bool inited = false;
26 
27  if (inited == false)
28  {
29  if (pthread_attr_init(&attr))
30  throw CoreException("Error calling pthread_attr_init");
32  throw CoreException("Unable to mark threads as joinable");
33  inited = true;
34  }
35 
36  return &attr;
37 }
38 
39 static void *entry_point(void *parameter)
40 {
41  Thread *thread = static_cast<Thread *>(parameter);
42  thread->Run();
43  thread->SetExitState();
44  pthread_exit(0);
45  return NULL;
46 }
47 
48 Thread::Thread() : exit(false)
49 {
50 }
51 
53 {
54 }
55 
57 {
58  this->SetExitState();
59  pthread_join(handle, NULL);
60 }
61 
63 {
64  this->Notify();
65  exit = true;
66 }
67 
69 {
70  this->SetExitState();
71  pthread_exit(0);
72 }
73 
75 {
76  if (pthread_create(&this->handle, get_engine_attr(), entry_point, this))
77  {
78  this->flags[SF_DEAD] = true;
79  throw CoreException("Unable to create thread: " + Anope::LastError());
80  }
81 }
82 
84 {
85  return exit;
86 }
87 
89 {
90  this->Join();
91  this->flags[SF_DEAD] = true;
92 }
93 
95 {
96  pthread_mutex_init(&mutex, NULL);
97 }
98 
100 {
102 }
103 
105 {
107 }
108 
110 {
112 }
113 
115 {
116  return pthread_mutex_trylock(&mutex) == 0;
117 }
118 
120 {
121  pthread_cond_init(&cond, NULL);
122 }
123 
125 {
127 }
128 
130 {
132 }
133 
135 {
137 }
int pthread_attr_init(pthread_attr_t *)
Definition: pthread.cpp:24
void Join()
std::bitset< SF_SIZE > flags
Definition: sockets.h:203
virtual void Run()=0
CoreExport const string LastError()
Definition: misc.cpp:606
bool GetExitState() const
static void * entry_point(void *parameter)
void Exit()
void OnNotify()
int pthread_mutex_unlock(pthread_mutex_t *mutex)
Definition: pthread.cpp:88
void Unlock()
void Start()
bool TryLock()
int pthread_cond_destroy(pthread_cond_t *cond)
Definition: pthread.cpp:102
void SetExitState()
void pthread_exit(int i)
Definition: pthread.cpp:60
void Wakeup()
int pthread_attr_setdetachstate(pthread_attr_t *, int)
Definition: pthread.cpp:30
void Notify()
Definition: pipeengine.cpp:77
pthread_mutex_t mutex
Definition: threadengine.h:71
pthread_cond_t cond
Definition: threadengine.h:101
int pthread_attr_t
Definition: pthread.h:13
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
Definition: pthread.cpp:112
void Lock()
pthread_t handle
Definition: threadengine.h:27
bool exit
Definition: threadengine.h:23
virtual ~Thread()
static pthread_attr_t * get_engine_attr()
int pthread_cond_signal(pthread_cond_t *cond)
Definition: pthread.cpp:107
int pthread_mutex_lock(pthread_mutex_t *mutex)
Definition: pthread.cpp:77
int pthread_join(pthread_t thread, void **)
Definition: pthread.cpp:52
int pthread_create(pthread_t *thread, const pthread_attr_t *, void *(*entry)(void *), void *param)
Definition: pthread.cpp:36
int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *)
Definition: pthread.cpp:65
int pthread_mutex_trylock(pthread_mutex_t *mutex)
Definition: pthread.cpp:83
int pthread_mutex_destroy(pthread_mutex_t *mutex)
Definition: pthread.cpp:71
#define PTHREAD_CREATE_JOINABLE
Definition: pthread.h:17
int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *)
Definition: pthread.cpp:94