2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2013-2015 Attila Molnar <attilamolnar@hush.com>
5 * Copyright (C) 2012 Robby <robby@chatbelgie.be>
6 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
7 * Copyright (C) 2008, 2010 Craig Edwards <brain@inspircd.org>
9 * This file is part of InspIRCd. InspIRCd is free software: you can
10 * redistribute it and/or modify it under the terms of the GNU General Public
11 * License as published by the Free Software Foundation, version 2.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "threadengines/threadengine_pthread.h"
28 static void* entry_point(void* parameter)
30 /* Recommended by nenolod, signal safety on a per-thread basis */
33 sigaddset(&set, SIGPIPE);
34 pthread_sigmask(SIG_BLOCK, &set, NULL);
36 Thread* pt = static_cast<Thread*>(parameter);
42 void ThreadEngine::Start(Thread* thread)
44 if (pthread_create(&thread->state.pthread_id, NULL, entry_point, thread) != 0)
45 throw CoreException("Unable to create new thread: " + std::string(strerror(errno)));
48 void ThreadEngine::Stop(Thread* thread)
50 thread->SetExitFlag();
51 pthread_join(thread->state.pthread_id, NULL);
55 #include <sys/eventfd.h>
57 class ThreadSignalSocket : public EventHandler
61 ThreadSignalSocket(SocketThread* p, int newfd) : parent(p)
64 SocketEngine::AddFd(this, FD_WANT_FAST_READ | FD_WANT_NO_WRITE);
69 SocketEngine::Close(this);
77 void OnEventHandlerRead() CXX11_OVERRIDE
80 eventfd_read(fd, &dummy);
84 void OnEventHandlerWrite() CXX11_OVERRIDE
86 ServerInstance->GlobalCulls.AddItem(this);
89 void OnEventHandlerError(int errcode) CXX11_OVERRIDE
91 ThreadSignalSocket::OnEventHandlerWrite();
95 SocketThread::SocketThread()
98 int fd = eventfd(0, EFD_NONBLOCK);
100 throw CoreException("Could not create pipe " + std::string(strerror(errno)));
101 signal.sock = new ThreadSignalSocket(this, fd);
105 class ThreadSignalSocket : public EventHandler
107 SocketThread* parent;
110 ThreadSignalSocket(SocketThread* p, int recvfd, int sendfd) :
111 parent(p), send_fd(sendfd)
114 SocketEngine::NonBlocking(fd);
115 SocketEngine::AddFd(this, FD_WANT_FAST_READ | FD_WANT_NO_WRITE);
118 ~ThreadSignalSocket()
121 SocketEngine::Close(this);
126 static const char dummy = '*';
127 write(send_fd, &dummy, 1);
130 void OnEventHandlerRead() CXX11_OVERRIDE
133 read(fd, dummy, 128);
137 void OnEventHandlerWrite() CXX11_OVERRIDE
139 ServerInstance->GlobalCulls.AddItem(this);
142 void OnEventHandlerError(int errcode) CXX11_OVERRIDE
144 ThreadSignalSocket::OnEventHandlerWrite();
148 SocketThread::SocketThread()
153 throw CoreException("Could not create pipe " + std::string(strerror(errno)));
154 signal.sock = new ThreadSignalSocket(this, fds[0], fds[1]);
158 void SocketThread::NotifyParent()
160 signal.sock->Notify();
163 SocketThread::~SocketThread()