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 ThreadData* data = new ThreadData;
45 if (pthread_create(&data->pthread_id, NULL, entry_point, thread) != 0)
49 throw CoreException("Unable to create new thread: " + std::string(strerror(errno)));
53 void ThreadData::FreeThread(Thread* thread)
55 thread->SetExitFlag();
56 pthread_join(pthread_id, NULL);
60 #include <sys/eventfd.h>
62 class ThreadSignalSocket : public EventHandler
66 ThreadSignalSocket(SocketThread* p, int newfd) : parent(p)
69 SocketEngine::AddFd(this, FD_WANT_FAST_READ | FD_WANT_NO_WRITE);
74 SocketEngine::Close(this);
82 void HandleEvent(EventType et, int errornum)
87 eventfd_read(fd, &dummy);
92 ServerInstance->GlobalCulls.AddItem(this);
97 SocketThread::SocketThread()
100 int fd = eventfd(0, EFD_NONBLOCK);
102 throw new CoreException("Could not create pipe " + std::string(strerror(errno)));
103 signal.sock = new ThreadSignalSocket(this, fd);
107 class ThreadSignalSocket : public EventHandler
109 SocketThread* parent;
112 ThreadSignalSocket(SocketThread* p, int recvfd, int sendfd) :
113 parent(p), send_fd(sendfd)
116 SocketEngine::NonBlocking(fd);
117 SocketEngine::AddFd(this, FD_WANT_FAST_READ | FD_WANT_NO_WRITE);
120 ~ThreadSignalSocket()
123 SocketEngine::Close(this);
128 static const char dummy = '*';
129 write(send_fd, &dummy, 1);
132 void HandleEvent(EventType et, int errornum)
134 if (et == EVENT_READ)
137 read(fd, dummy, 128);
142 ServerInstance->GlobalCulls.AddItem(this);
147 SocketThread::SocketThread()
152 throw new CoreException("Could not create pipe " + std::string(strerror(errno)));
153 signal.sock = new ThreadSignalSocket(this, fds[0], fds[1]);
157 void SocketThread::NotifyParent()
159 signal.sock->Notify();
162 SocketThread::~SocketThread()