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"
27 ThreadEngine::ThreadEngine()
31 static void* entry_point(void* parameter)
33 /* Recommended by nenolod, signal safety on a per-thread basis */
36 sigaddset(&set, SIGPIPE);
37 pthread_sigmask(SIG_BLOCK, &set, NULL);
39 Thread* pt = static_cast<Thread*>(parameter);
45 void ThreadEngine::Start(Thread* thread)
47 ThreadData* data = new ThreadData;
50 if (pthread_create(&data->pthread_id, NULL, entry_point, thread) != 0)
54 throw CoreException("Unable to create new thread: " + std::string(strerror(errno)));
58 ThreadEngine::~ThreadEngine()
62 void ThreadData::FreeThread(Thread* thread)
64 thread->SetExitFlag();
65 pthread_join(pthread_id, NULL);
69 #include <sys/eventfd.h>
71 class ThreadSignalSocket : public EventHandler
75 ThreadSignalSocket(SocketThread* p, int newfd) : parent(p)
78 SocketEngine::AddFd(this, FD_WANT_FAST_READ | FD_WANT_NO_WRITE);
83 SocketEngine::Close(this);
91 void HandleEvent(EventType et, int errornum)
96 eventfd_read(fd, &dummy);
101 ServerInstance->GlobalCulls.AddItem(this);
106 SocketThread::SocketThread()
109 int fd = eventfd(0, EFD_NONBLOCK);
111 throw new CoreException("Could not create pipe " + std::string(strerror(errno)));
112 signal.sock = new ThreadSignalSocket(this, fd);
116 class ThreadSignalSocket : public EventHandler
118 SocketThread* parent;
121 ThreadSignalSocket(SocketThread* p, int recvfd, int sendfd) :
122 parent(p), send_fd(sendfd)
125 SocketEngine::NonBlocking(fd);
126 SocketEngine::AddFd(this, FD_WANT_FAST_READ | FD_WANT_NO_WRITE);
129 ~ThreadSignalSocket()
132 SocketEngine::Close(this);
137 static const char dummy = '*';
138 write(send_fd, &dummy, 1);
141 void HandleEvent(EventType et, int errornum)
143 if (et == EVENT_READ)
146 read(fd, dummy, 128);
151 ServerInstance->GlobalCulls.AddItem(this);
156 SocketThread::SocketThread()
161 throw new CoreException("Could not create pipe " + std::string(strerror(errno)));
162 signal.sock = new ThreadSignalSocket(this, fds[0], fds[1]);
166 void SocketThread::NotifyParent()
168 signal.sock->Notify();
171 SocketThread::~SocketThread()