]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/threadengines/threadengine_pthread.cpp
MutexEngine -> MutexFactory, more sensible name
[user/henk/code/inspircd.git] / src / threadengines / threadengine_pthread.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *          the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include "threadengines/threadengine_pthread.h"
16 #include <pthread.h>
17
18 pthread_mutex_t MyMutex = PTHREAD_MUTEX_INITIALIZER;
19
20 PThreadEngine::PThreadEngine(InspIRCd* Instance) : ThreadEngine(Instance)
21 {
22 }
23
24 void PThreadEngine::Create(Thread* thread_to_init)
25 {
26         pthread_attr_t attribs;
27         pthread_attr_init(&attribs);
28         pthread_attr_setdetachstate(&attribs, PTHREAD_CREATE_JOINABLE);
29         pthread_t* MyPThread = new pthread_t;
30
31         /* Create a thread in a mutex. This prevents whacking the member value NewThread,
32          * and also prevents recursive creation of threads by mistake (instead, the thread
33          * will just deadlock itself)
34          */
35         Mutex(true);
36
37         if (pthread_create(MyPThread, &attribs, PThreadEngine::Entry, (void*)this) != 0)
38         {
39                 delete MyPThread;
40                 Mutex(false);
41                 throw CoreException("Unable to create new PThreadEngine: " + std::string(strerror(errno)));
42         }
43
44         pthread_attr_destroy(&attribs);
45
46         NewThread = thread_to_init;
47         NewThread->Creator = this;
48         NewThread->Extend("pthread", MyPThread);
49
50         /* Always unset a mutex if you set it */
51         Mutex(false);
52
53         /* Wait for the PThreadEngine::Run method to take a copy of the
54          * pointer and clear this member value
55          */
56         while (NewThread)
57                 usleep(1000);
58 }
59
60 PThreadEngine::~PThreadEngine()
61 {
62 }
63
64 void PThreadEngine::Run()
65 {
66         /* Take a copy of the member value, then clear it. Do this
67          * in a mutex so that we can be sure nothing else is looking
68          * at it.
69          */
70         Mutex(true);
71         Thread* nt = NewThread;
72         NewThread = NULL;
73         Mutex(false);
74         /* Now we have our own safe copy, call the object on it */
75         nt->Run();
76 }
77
78 bool PThreadEngine::Mutex(bool enable)
79 {
80         if (enable)
81                 pthread_mutex_lock(&MyMutex);
82         else
83                 pthread_mutex_unlock(&MyMutex);
84
85         return false;
86 }
87
88 void* PThreadEngine::Entry(void* parameter)
89 {
90         ThreadEngine * pt = (ThreadEngine*)parameter;
91         pt->Run();
92         return NULL;
93 }
94
95 void PThreadEngine::FreeThread(Thread* thread)
96 {
97         pthread_t* pthread = NULL;
98         if (thread->GetExt("pthread", pthread))
99         {
100                 thread->SetExitFlag();
101                 int rc;
102                 void* status;
103                 rc = pthread_join(*pthread, &status);
104                 delete pthread;
105         }
106 }
107
108 MutexFactory::MutexFactory(InspIRCd* Instance) : ServerInstance(Instance)
109 {
110 }
111
112 Mutex* MutexFactory::CreateMutex()
113 {
114         return new PosixMutex(this->ServerInstance);
115 }
116
117 PosixMutex::PosixMutex(InspIRCd* Instance) : Mutex(Instance)
118 {
119         pthread_mutex_init(&putex, NULL);
120 }
121
122 PosixMutex::~PosixMutex()
123 {
124         pthread_mutex_destroy(&putex);
125 }
126
127 void PosixMutex::Enable(bool enable)
128 {
129         if (enable)
130                 pthread_mutex_lock(&putex);
131         else
132                 pthread_mutex_unlock(&putex);
133 }