]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/threadengines/threadengine_pthread.cpp
Call DelFd() and SetFd(-1) from SocketEngine::Close(EventHandler*)
[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 <signal.h>
25 #include <fcntl.h>
26
27 ThreadEngine::ThreadEngine()
28 {
29 }
30
31 static void* entry_point(void* parameter)
32 {
33         /* Recommended by nenolod, signal safety on a per-thread basis */
34         sigset_t set;
35         sigemptyset(&set);
36         sigaddset(&set, SIGPIPE);
37         pthread_sigmask(SIG_BLOCK, &set, NULL);
38
39         Thread* pt = static_cast<Thread*>(parameter);
40         pt->Run();
41         return parameter;
42 }
43
44
45 void ThreadEngine::Start(Thread* thread)
46 {
47         ThreadData* data = new ThreadData;
48         thread->state = data;
49
50         if (pthread_create(&data->pthread_id, NULL, entry_point, thread) != 0)
51         {
52                 thread->state = NULL;
53                 delete data;
54                 throw CoreException("Unable to create new thread: " + std::string(strerror(errno)));
55         }
56 }
57
58 ThreadEngine::~ThreadEngine()
59 {
60 }
61
62 void ThreadData::FreeThread(Thread* thread)
63 {
64         thread->SetExitFlag();
65         pthread_join(pthread_id, NULL);
66 }
67
68 #ifdef HAS_EVENTFD
69 #include <sys/eventfd.h>
70
71 class ThreadSignalSocket : public EventHandler
72 {
73         SocketThread* parent;
74  public:
75         ThreadSignalSocket(SocketThread* p, int newfd) : parent(p)
76         {
77                 SetFd(newfd);
78                 SocketEngine::AddFd(this, FD_WANT_FAST_READ | FD_WANT_NO_WRITE);
79         }
80
81         ~ThreadSignalSocket()
82         {
83                 SocketEngine::Close(this);
84         }
85
86         void Notify()
87         {
88                 eventfd_write(fd, 1);
89         }
90
91         void HandleEvent(EventType et, int errornum)
92         {
93                 if (et == EVENT_READ)
94                 {
95                         eventfd_t dummy;
96                         eventfd_read(fd, &dummy);
97                         parent->OnNotify();
98                 }
99                 else
100                 {
101                         ServerInstance->GlobalCulls.AddItem(this);
102                 }
103         }
104 };
105
106 SocketThread::SocketThread()
107 {
108         signal.sock = NULL;
109         int fd = eventfd(0, EFD_NONBLOCK);
110         if (fd < 0)
111                 throw new CoreException("Could not create pipe " + std::string(strerror(errno)));
112         signal.sock = new ThreadSignalSocket(this, fd);
113 }
114 #else
115
116 class ThreadSignalSocket : public EventHandler
117 {
118         SocketThread* parent;
119         int send_fd;
120  public:
121         ThreadSignalSocket(SocketThread* p, int recvfd, int sendfd) :
122                 parent(p), send_fd(sendfd)
123         {
124                 SetFd(recvfd);
125                 SocketEngine::NonBlocking(fd);
126                 SocketEngine::AddFd(this, FD_WANT_FAST_READ | FD_WANT_NO_WRITE);
127         }
128
129         ~ThreadSignalSocket()
130         {
131                 close(send_fd);
132                 SocketEngine::Close(this);
133         }
134
135         void Notify()
136         {
137                 static const char dummy = '*';
138                 write(send_fd, &dummy, 1);
139         }
140
141         void HandleEvent(EventType et, int errornum)
142         {
143                 if (et == EVENT_READ)
144                 {
145                         char dummy[128];
146                         read(fd, dummy, 128);
147                         parent->OnNotify();
148                 }
149                 else
150                 {
151                         ServerInstance->GlobalCulls.AddItem(this);
152                 }
153         }
154 };
155
156 SocketThread::SocketThread()
157 {
158         signal.sock = NULL;
159         int fds[2];
160         if (pipe(fds))
161                 throw new CoreException("Could not create pipe " + std::string(strerror(errno)));
162         signal.sock = new ThreadSignalSocket(this, fds[0], fds[1]);
163 }
164 #endif
165
166 void SocketThread::NotifyParent()
167 {
168         signal.sock->Notify();
169 }
170
171 SocketThread::~SocketThread()
172 {
173         if (signal.sock)
174         {
175                 signal.sock->cull();
176                 delete signal.sock;
177         }
178 }