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_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)
74 SocketThread::SocketThread()
76 int listenFD = socket(AF_INET, SOCK_STREAM, 0);
78 throw CoreException("Could not create ITC pipe");
79 int connFD = socket(AF_INET, SOCK_STREAM, 0);
81 throw CoreException("Could not create ITC pipe");
83 if (!ServerInstance->BindSocket(listenFD, 0, "127.0.0.1", true))
84 throw CoreException("Could not create ITC pipe");
85 ServerInstance->SE->NonBlocking(connFD);
87 struct sockaddr_in addr;
88 socklen_t sz = sizeof(addr);
89 getsockname(listenFD, reinterpret_cast<struct sockaddr*>(&addr), &sz);
90 connect(connFD, reinterpret_cast<struct sockaddr*>(&addr), sz);
91 int nfd = accept(listenFD, reinterpret_cast<struct sockaddr*>(&addr), (int*)sz);
93 throw CoreException("Could not create ITC pipe");
94 new ThreadSignalSocket(this, nfd);
95 closesocket(listenFD);
97 ServerInstance->SE->Blocking(connFD);
98 this->signal.connFD = connFD;
101 void SocketThread::NotifyParent()
104 send(signal.connFD, &dummy, 1, 0);
107 SocketThread::~SocketThread()
109 if (signal.connFD >= 0)
111 shutdown(signal.connFD, 2);
112 closesocket(signal.connFD);