]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/threadengines/threadengine_pthread.cpp
Add the fakederef class
[user/henk/code/inspircd.git] / src / threadengines / threadengine_pthread.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
6  *
7  * This file is part of InspIRCd.  InspIRCd is free software: you can
8  * redistribute it and/or modify it under the terms of the GNU General Public
9  * License as published by the Free Software Foundation, version 2.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20
21 #include "inspircd.h"
22 #include "threadengines/threadengine_pthread.h"
23 #include <pthread.h>
24 #include <fcntl.h>
25
26 ThreadEngine::ThreadEngine()
27 {
28 }
29
30 static void* entry_point(void* parameter)
31 {
32         /* Recommended by nenolod, signal safety on a per-thread basis */
33         sigset_t set;
34         sigemptyset(&set);
35         sigaddset(&set, SIGPIPE);
36         pthread_sigmask(SIG_BLOCK, &set, NULL);
37
38         Thread* pt = static_cast<Thread*>(parameter);
39         pt->Run();
40         return parameter;
41 }
42
43
44 void ThreadEngine::Start(Thread* thread)
45 {
46         ThreadData* data = new ThreadData;
47         thread->state = data;
48
49         if (pthread_create(&data->pthread_id, NULL, entry_point, thread) != 0)
50         {
51                 thread->state = NULL;
52                 delete data;
53                 throw CoreException("Unable to create new thread: " + std::string(strerror(errno)));
54         }
55 }
56
57 ThreadEngine::~ThreadEngine()
58 {
59 }
60
61 void ThreadData::FreeThread(Thread* thread)
62 {
63         thread->SetExitFlag();
64         pthread_join(pthread_id, NULL);
65 }
66
67 #ifdef HAS_EVENTFD
68 #include <sys/eventfd.h>
69
70 class ThreadSignalSocket : public EventHandler
71 {
72         SocketThread* parent;
73  public:
74         ThreadSignalSocket(SocketThread* p, int newfd) : parent(p)
75         {
76                 SetFd(newfd);
77                 SocketEngine::AddFd(this, FD_WANT_FAST_READ | FD_WANT_NO_WRITE);
78         }
79
80         ~ThreadSignalSocket()
81         {
82                 SocketEngine::Close(this);
83         }
84
85         void Notify()
86         {
87                 eventfd_write(fd, 1);
88         }
89
90         void HandleEvent(EventType et, int errornum)
91         {
92                 if (et == EVENT_READ)
93                 {
94                         eventfd_t dummy;
95                         eventfd_read(fd, &dummy);
96                         parent->OnNotify();
97                 }
98                 else
99                 {
100                         ServerInstance->GlobalCulls.AddItem(this);
101                 }
102         }
103 };
104
105 SocketThread::SocketThread()
106 {
107         signal.sock = NULL;
108         int fd = eventfd(0, EFD_NONBLOCK);
109         if (fd < 0)
110                 throw new CoreException("Could not create pipe " + std::string(strerror(errno)));
111         signal.sock = new ThreadSignalSocket(this, fd);
112 }
113 #else
114
115 class ThreadSignalSocket : public EventHandler
116 {
117         SocketThread* parent;
118         int send_fd;
119  public:
120         ThreadSignalSocket(SocketThread* p, int recvfd, int sendfd) :
121                 parent(p), send_fd(sendfd)
122         {
123                 SetFd(recvfd);
124                 SocketEngine::NonBlocking(fd);
125                 SocketEngine::AddFd(this, FD_WANT_FAST_READ | FD_WANT_NO_WRITE);
126         }
127
128         ~ThreadSignalSocket()
129         {
130                 close(send_fd);
131                 SocketEngine::Close(this);
132         }
133
134         void Notify()
135         {
136                 static const char dummy = '*';
137                 write(send_fd, &dummy, 1);
138         }
139
140         void HandleEvent(EventType et, int errornum)
141         {
142                 if (et == EVENT_READ)
143                 {
144                         char dummy[128];
145                         read(fd, dummy, 128);
146                         parent->OnNotify();
147                 }
148                 else
149                 {
150                         ServerInstance->GlobalCulls.AddItem(this);
151                 }
152         }
153 };
154
155 SocketThread::SocketThread()
156 {
157         signal.sock = NULL;
158         int fds[2];
159         if (pipe(fds))
160                 throw new CoreException("Could not create pipe " + std::string(strerror(errno)));
161         signal.sock = new ThreadSignalSocket(this, fds[0], fds[1]);
162 }
163 #endif
164
165 void SocketThread::NotifyParent()
166 {
167         signal.sock->Notify();
168 }
169
170 SocketThread::~SocketThread()
171 {
172         if (signal.sock)
173         {
174                 signal.sock->cull();
175                 delete signal.sock;
176         }
177 }