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 EventHandler
68 ThreadSignalSocket(SocketThread* p, int newfd) : parent(p)
71 ServerInstance->SE->AddFd(this, FD_WANT_FAST_READ | FD_WANT_NO_WRITE);
83 void HandleEvent(EventType et, int errornum)
88 eventfd_read(fd, &dummy);
93 ServerInstance->GlobalCulls.AddItem(this);
98 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 ServerInstance->SE->NonBlocking(fd);
117 ServerInstance->SE->AddFd(this, FD_WANT_FAST_READ | FD_WANT_NO_WRITE);
120 ~ThreadSignalSocket()
127 static const char dummy = '*';
128 write(send_fd, &dummy, 1);
131 void HandleEvent(EventType et, int errornum)
133 if (et == EVENT_READ)
136 read(fd, dummy, 128);
141 ServerInstance->GlobalCulls.AddItem(this);
146 SocketThread::SocketThread()
150 throw new CoreException("Could not create pipe " + std::string(strerror(errno)));
151 signal.sock = new ThreadSignalSocket(this, fds[0], fds[1]);
155 void SocketThread::NotifyParent()
157 signal.sock->Notify();
160 SocketThread::~SocketThread()