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