1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
15 #include "threadengines/threadengine_pthread.h"
20 ThreadEngine::ThreadEngine()
24 static void* entry_point(void* parameter)
26 /* Recommended by nenolod, signal safety on a per-thread basis */
29 sigaddset(&set, SIGPIPE);
30 pthread_sigmask(SIG_BLOCK, &set, NULL);
32 Thread* pt = static_cast<Thread*>(parameter);
38 void ThreadEngine::Start(Thread* thread)
40 ThreadData* data = new ThreadData;
43 if (pthread_create(&data->pthread_id, NULL, entry_point, thread) != 0)
47 throw CoreException("Unable to create new thread: " + std::string(strerror(errno)));
51 ThreadEngine::~ThreadEngine()
55 void ThreadData::FreeThread(Thread* thread)
57 thread->SetExitFlag();
58 pthread_join(pthread_id, NULL);
62 #include <sys/eventfd.h>
64 class ThreadSignalSocket : public BufferedSocket
68 ThreadSignalSocket(SocketThread* p, int newfd) : BufferedSocket(newfd), parent(p) {}
85 void OnError(BufferedSocketError)
90 SocketThread::SocketThread()
92 int fd = eventfd(0, O_NONBLOCK);
94 throw new CoreException("Could not create pipe " + std::string(strerror(errno)));
95 signal.sock = new ThreadSignalSocket(this, fd);
99 class ThreadSignalSocket : public BufferedSocket
101 SocketThread* parent;
104 ThreadSignalSocket(SocketThread* p, int recvfd, int sendfd) :
105 BufferedSocket(recvfd), parent(p), send_fd(sendfd) {}
107 ~ThreadSignalSocket()
115 write(send_fd, &dummy, 1);
124 void OnError(BufferedSocketError)
129 SocketThread::SocketThread()
133 throw new CoreException("Could not create pipe " + std::string(strerror(errno)));
134 signal.sock = new ThreadSignalSocket(this, fds[0], fds[1]);
138 void SocketThread::NotifyParent()
140 signal.sock->Notify();
143 SocketThread::~SocketThread()