1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 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)
87 ServerInstance->GlobalCulls.AddItem(this);
91 SocketThread::SocketThread()
93 int fd = eventfd(0, O_NONBLOCK);
95 throw new CoreException("Could not create pipe " + std::string(strerror(errno)));
96 signal.sock = new ThreadSignalSocket(this, fd);
100 class ThreadSignalSocket : public BufferedSocket
102 SocketThread* parent;
105 ThreadSignalSocket(SocketThread* p, int recvfd, int sendfd) :
106 BufferedSocket(recvfd), parent(p), send_fd(sendfd) {}
108 ~ThreadSignalSocket()
116 write(send_fd, &dummy, 1);
125 void OnError(BufferedSocketError)
127 ServerInstance->GlobalCulls.AddItem(this);
131 SocketThread::SocketThread()
135 throw new CoreException("Could not create pipe " + std::string(strerror(errno)));
136 signal.sock = new ThreadSignalSocket(this, fds[0], fds[1]);
140 void SocketThread::NotifyParent()
142 signal.sock->Notify();
145 SocketThread::~SocketThread()