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_win32.h"
17 ThreadEngine::ThreadEngine()
21 void ThreadEngine::Start(Thread* thread)
23 ThreadData* data = new ThreadData;
27 data->handle = CreateThread(NULL,0,ThreadEngine::Entry,thread,0,&ThreadId);
29 if (data->handle == NULL)
33 throw CoreException(std::string("Unable to create new thread: ") + dlerror());
37 ThreadEngine::~ThreadEngine()
41 DWORD WINAPI ThreadEngine::Entry(void* parameter)
43 Thread* pt = static_cast<Thread*>(parameter);
48 void ThreadData::FreeThread(Thread* thread)
50 thread->SetExitFlag();
51 WaitForSingleObject(handle,INFINITE);
54 class ThreadSignalSocket : public BufferedSocket
58 ThreadSignalSocket(SocketThread* t, int newfd)
59 : BufferedSocket(newfd), parent(t)
69 void OnError(BufferedSocketError)
71 ServerInstance->GlobalCulls.AddItem(this);
75 SocketThread::SocketThread()
77 int listenFD = socket(AF_INET, SOCK_STREAM, 0);
79 throw CoreException("Could not create ITC pipe");
80 int connFD = socket(AF_INET, SOCK_STREAM, 0);
82 throw CoreException("Could not create ITC pipe");
84 if (!ServerInstance->BindSocket(listenFD, 0, "127.0.0.1", true))
85 throw CoreException("Could not create ITC pipe");
86 ServerInstance->SE->NonBlocking(connFD);
88 struct sockaddr_in addr;
89 socklen_t sz = sizeof(addr);
90 getsockname(listenFD, reinterpret_cast<struct sockaddr*>(&addr), &sz);
91 connect(connFD, reinterpret_cast<struct sockaddr*>(&addr), sz);
92 int nfd = accept(listenFD, reinterpret_cast<struct sockaddr*>(&addr), (int*)sz);
94 throw CoreException("Could not create ITC pipe");
95 new ThreadSignalSocket(this, nfd);
96 closesocket(listenFD);
98 ServerInstance->SE->Blocking(connFD);
99 this->signal.connFD = connFD;
102 void SocketThread::NotifyParent()
105 send(signal.connFD, &dummy, 1, 0);
108 SocketThread::~SocketThread()
110 if (signal.connFD >= 0)
112 shutdown(signal.connFD, 2);
113 closesocket(signal.connFD);