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 ServerInstance->SE->AddFd(this, FD_WANT_FAST_READ | FD_WANT_NO_WRITE);
83 ServerInstance->SE->DelFd(this);
84 ServerInstance->SE->Close(GetFd());
92 void HandleEvent(EventType et, int errornum)
97 eventfd_read(fd, &dummy);
102 ServerInstance->GlobalCulls.AddItem(this);
107 SocketThread::SocketThread()
110 int fd = eventfd(0, EFD_NONBLOCK);
112 throw CoreException("Could not create pipe " + std::string(strerror(errno)));
113 signal.sock = new ThreadSignalSocket(this, fd);
117 class ThreadSignalSocket : public EventHandler
119 SocketThread* parent;
122 ThreadSignalSocket(SocketThread* p, int recvfd, int sendfd) :
123 parent(p), send_fd(sendfd)
126 ServerInstance->SE->NonBlocking(fd);
127 ServerInstance->SE->AddFd(this, FD_WANT_FAST_READ | FD_WANT_NO_WRITE);
130 ~ThreadSignalSocket()
133 ServerInstance->SE->DelFd(this);
134 ServerInstance->SE->Close(GetFd());
139 static const char dummy = '*';
140 write(send_fd, &dummy, 1);
143 void HandleEvent(EventType et, int errornum)
145 if (et == EVENT_READ)
148 read(fd, dummy, 128);
153 ServerInstance->GlobalCulls.AddItem(this);
158 SocketThread::SocketThread()
163 throw CoreException("Could not create pipe " + std::string(strerror(errno)));
164 signal.sock = new ThreadSignalSocket(this, fds[0], fds[1]);
168 void SocketThread::NotifyParent()
170 signal.sock->Notify();
173 SocketThread::~SocketThread()