]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/threadengines/threadengine_pthread.cpp
9fc9cc4b4f0a5b3d39ec17fbadd8a4c4e85f16da
[user/henk/code/inspircd.git] / src / threadengines / threadengine_pthread.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/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 #include <signal.h>
18
19 PThreadEngine::PThreadEngine(InspIRCd* Instance) : ThreadEngine(Instance)
20 {
21 }
22
23 static void* entry_point(void* parameter)
24 {
25         /* Recommended by nenolod, signal safety on a per-thread basis */
26         sigset_t set;
27         sigemptyset(&set);
28         sigaddset(&set, SIGPIPE);
29         pthread_sigmask(SIG_BLOCK, &set, NULL);
30
31         Thread* pt = reinterpret_cast<Thread*>(parameter);
32         pt->Run();
33         return parameter;
34 }
35
36
37 void PThreadEngine::Start(Thread* thread)
38 {
39         PThreadData* data = new PThreadData;
40         thread->state = data;
41
42         if (pthread_create(&data->pthread_id, NULL, entry_point, thread) != 0)
43         {
44                 thread->state = NULL;
45                 delete data;
46                 throw CoreException("Unable to create new PThreadEngine: " + std::string(strerror(errno)));
47         }
48 }
49
50 PThreadEngine::~PThreadEngine()
51 {
52 }
53
54 void PThreadData::FreeThread(Thread* thread)
55 {
56         thread->SetExitFlag(true);
57         pthread_join(pthread_id, NULL);
58 }
59
60 MutexFactory::MutexFactory(InspIRCd* Instance) : ServerInstance(Instance)
61 {
62 }
63
64 Mutex* MutexFactory::CreateMutex()
65 {
66         return new PosixMutex();
67 }
68
69 PosixMutex::PosixMutex() : Mutex()
70 {
71         pthread_mutex_init(&putex, NULL);
72 }
73
74 PosixMutex::~PosixMutex()
75 {
76         pthread_mutex_destroy(&putex);
77 }
78
79 void PosixMutex::Enable(bool enable)
80 {
81         if (enable)
82                 pthread_mutex_lock(&putex);
83         else
84                 pthread_mutex_unlock(&putex);
85 }