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(InspIRCd* Instance)
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, InspIRCd* SI, int newfd) :
69 BufferedSocket(SI, newfd, const_cast<char*>("0.0.0.0")), parent(p) {}
80 virtual bool OnDataReady()
83 if (eventfd_read(fd, &data))
90 SocketThread::SocketThread(InspIRCd* SI)
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, SI, fd);
99 class ThreadSignalSocket : public BufferedSocket
101 SocketThread* parent;
104 ThreadSignalSocket(SocketThread* p, InspIRCd* SI, int recvfd, int sendfd) :
105 BufferedSocket(SI, recvfd, const_cast<char*>("0.0.0.0")), parent(p), send_fd(sendfd) {}
107 ~ThreadSignalSocket()
115 write(send_fd, &dummy, 1);
118 virtual bool OnDataReady()
121 if (read(this->fd, &data, 1) <= 0)
128 SocketThread::SocketThread(InspIRCd* SI)
132 throw new CoreException("Could not create pipe " + std::string(strerror(errno)));
133 signal.sock = new ThreadSignalSocket(this, SI, fds[0], fds[1]);
137 void SocketThread::NotifyParent()
139 signal.sock->Notify();
142 SocketThread::~SocketThread()