2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
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.
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
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/>.
22 #include "threadengines/threadengine_pthread.h"
26 static void* entry_point(void* parameter)
28 /* Recommended by nenolod, signal safety on a per-thread basis */
31 sigaddset(&set, SIGPIPE);
32 pthread_sigmask(SIG_BLOCK, &set, NULL);
34 Thread* pt = static_cast<Thread*>(parameter);
40 void ThreadEngine::Start(Thread* thread)
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)));
46 void ThreadEngine::Stop(Thread* thread)
48 thread->SetExitFlag();
49 pthread_join(thread->state.pthread_id, NULL);
53 #include <sys/eventfd.h>
55 class ThreadSignalSocket : public EventHandler
59 ThreadSignalSocket(SocketThread* p, int newfd) : parent(p)
62 SocketEngine::AddFd(this, FD_WANT_FAST_READ | FD_WANT_NO_WRITE);
67 SocketEngine::Close(this);
75 void OnEventHandlerRead() CXX11_OVERRIDE
78 eventfd_read(fd, &dummy);
82 void OnEventHandlerWrite() CXX11_OVERRIDE
84 ServerInstance->GlobalCulls.AddItem(this);
87 void OnEventHandlerError(int errcode) CXX11_OVERRIDE
89 ThreadSignalSocket::OnEventHandlerWrite();
93 SocketThread::SocketThread()
96 int fd = eventfd(0, EFD_NONBLOCK);
98 throw CoreException("Could not create pipe " + std::string(strerror(errno)));
99 signal.sock = new ThreadSignalSocket(this, fd);
103 class ThreadSignalSocket : public EventHandler
105 SocketThread* parent;
108 ThreadSignalSocket(SocketThread* p, int recvfd, int sendfd) :
109 parent(p), send_fd(sendfd)
112 SocketEngine::NonBlocking(fd);
113 SocketEngine::AddFd(this, FD_WANT_FAST_READ | FD_WANT_NO_WRITE);
116 ~ThreadSignalSocket()
119 SocketEngine::Close(this);
124 static const char dummy = '*';
125 write(send_fd, &dummy, 1);
128 void OnEventHandlerRead() CXX11_OVERRIDE
131 read(fd, dummy, 128);
135 void OnEventHandlerWrite() CXX11_OVERRIDE
137 ServerInstance->GlobalCulls.AddItem(this);
140 void OnEventHandlerError(int errcode) CXX11_OVERRIDE
142 ThreadSignalSocket::OnEventHandlerWrite();
146 SocketThread::SocketThread()
151 throw CoreException("Could not create pipe " + std::string(strerror(errno)));
152 signal.sock = new ThreadSignalSocket(this, fds[0], fds[1]);
156 void SocketThread::NotifyParent()
158 signal.sock->Notify();
161 SocketThread::~SocketThread()