]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/threadengines/threadengine_pthread.cpp
Merge pull request #131 from attilamolnar/insp20+hideroperwhofix
[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                 ServerInstance->SE->AddFd(this, FD_WANT_FAST_READ | FD_WANT_NO_WRITE);
79         }
80
81         ~ThreadSignalSocket()
82         {
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         int fd = eventfd(0, EFD_NONBLOCK);
108         if (fd < 0)
109                 throw new CoreException("Could not create pipe " + std::string(strerror(errno)));
110         signal.sock = new ThreadSignalSocket(this, fd);
111 }
112 #else
113
114 class ThreadSignalSocket : public EventHandler
115 {
116         SocketThread* parent;
117         int send_fd;
118  public:
119         ThreadSignalSocket(SocketThread* p, int recvfd, int sendfd) :
120                 parent(p), send_fd(sendfd)
121         {
122                 SetFd(recvfd);
123                 ServerInstance->SE->NonBlocking(fd);
124                 ServerInstance->SE->AddFd(this, FD_WANT_FAST_READ | FD_WANT_NO_WRITE);
125         }
126
127         ~ThreadSignalSocket()
128         {
129                 close(send_fd);
130         }
131
132         void Notify()
133         {
134                 static const char dummy = '*';
135                 write(send_fd, &dummy, 1);
136         }
137
138         void HandleEvent(EventType et, int errornum)
139         {
140                 if (et == EVENT_READ)
141                 {
142                         char dummy[128];
143                         read(fd, dummy, 128);
144                         parent->OnNotify();
145                 }
146                 else
147                 {
148                         ServerInstance->GlobalCulls.AddItem(this);
149                 }
150         }
151 };
152
153 SocketThread::SocketThread()
154 {
155         int fds[2];
156         if (pipe(fds))
157                 throw new CoreException("Could not create pipe " + std::string(strerror(errno)));
158         signal.sock = new ThreadSignalSocket(this, fds[0], fds[1]);
159 }
160 #endif
161
162 void SocketThread::NotifyParent()
163 {
164         signal.sock->Notify();
165 }
166
167 SocketThread::~SocketThread()
168 {
169 }