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 ThreadEngine::ThreadEngine()
30 static void* entry_point(void* parameter)
32 /* Recommended by nenolod, signal safety on a per-thread basis */
35 sigaddset(&set, SIGPIPE);
36 pthread_sigmask(SIG_BLOCK, &set, NULL);
38 Thread* pt = static_cast<Thread*>(parameter);
44 void ThreadEngine::Start(Thread* thread)
46 ThreadData* data = new ThreadData;
49 if (pthread_create(&data->pthread_id, NULL, entry_point, thread) != 0)
53 throw CoreException("Unable to create new thread: " + std::string(strerror(errno)));
57 ThreadEngine::~ThreadEngine()
61 void ThreadData::FreeThread(Thread* thread)
63 thread->SetExitFlag();
64 pthread_join(pthread_id, NULL);
68 #include <sys/eventfd.h>
70 class ThreadSignalSocket : public EventHandler
74 ThreadSignalSocket(SocketThread* p, int newfd) : parent(p)
77 SocketEngine::AddFd(this, FD_WANT_FAST_READ | FD_WANT_NO_WRITE);
82 SocketEngine::Close(this);
90 void HandleEvent(EventType et, int errornum)
95 eventfd_read(fd, &dummy);
100 ServerInstance->GlobalCulls.AddItem(this);
105 SocketThread::SocketThread()
108 int fd = eventfd(0, EFD_NONBLOCK);
110 throw new CoreException("Could not create pipe " + std::string(strerror(errno)));
111 signal.sock = new ThreadSignalSocket(this, fd);
115 class ThreadSignalSocket : public EventHandler
117 SocketThread* parent;
120 ThreadSignalSocket(SocketThread* p, int recvfd, int sendfd) :
121 parent(p), send_fd(sendfd)
124 SocketEngine::NonBlocking(fd);
125 SocketEngine::AddFd(this, FD_WANT_FAST_READ | FD_WANT_NO_WRITE);
128 ~ThreadSignalSocket()
131 SocketEngine::Close(this);
136 static const char dummy = '*';
137 write(send_fd, &dummy, 1);
140 void HandleEvent(EventType et, int errornum)
142 if (et == EVENT_READ)
145 read(fd, dummy, 128);
150 ServerInstance->GlobalCulls.AddItem(this);
155 SocketThread::SocketThread()
160 throw new CoreException("Could not create pipe " + std::string(strerror(errno)));
161 signal.sock = new ThreadSignalSocket(this, fds[0], fds[1]);
165 void SocketThread::NotifyParent()
167 signal.sock->Notify();
170 SocketThread::~SocketThread()