2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
7 * This file is part of InspIRCd. InspIRCd is free software: you can
8 * redistribute it and/or modify it under the terms of the GNU General Public
9 * License as published by the Free Software Foundation, version 2.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "threadengines/threadengine_win32.h"
24 ThreadEngine::ThreadEngine()
28 void ThreadEngine::Start(Thread* thread)
30 ThreadData* data = new ThreadData;
34 data->handle = CreateThread(NULL,0,ThreadEngine::Entry,thread,0,&ThreadId);
36 if (data->handle == NULL)
40 throw CoreException(std::string("Unable to create new thread: ") + dlerror());
44 ThreadEngine::~ThreadEngine()
48 DWORD WINAPI ThreadEngine::Entry(void* parameter)
50 Thread* pt = static_cast<Thread*>(parameter);
55 void ThreadData::FreeThread(Thread* thread)
57 thread->SetExitFlag();
58 WaitForSingleObject(handle,INFINITE);
61 class ThreadSignalSocket : public BufferedSocket
65 ThreadSignalSocket(SocketThread* t, int newfd)
66 : BufferedSocket(newfd), parent(t)
76 void OnError(BufferedSocketError)
78 ServerInstance->GlobalCulls.AddItem(this);
82 SocketThread::SocketThread()
84 int listenFD = socket(AF_INET, SOCK_STREAM, 0);
86 throw CoreException("Could not create ITC pipe");
87 int connFD = socket(AF_INET, SOCK_STREAM, 0);
89 throw CoreException("Could not create ITC pipe");
91 if (!ServerInstance->BindSocket(listenFD, 0, "127.0.0.1", true))
92 throw CoreException("Could not create ITC pipe");
93 ServerInstance->SE->NonBlocking(connFD);
95 struct sockaddr_in addr;
96 socklen_t sz = sizeof(addr);
97 getsockname(listenFD, reinterpret_cast<struct sockaddr*>(&addr), &sz);
98 connect(connFD, reinterpret_cast<struct sockaddr*>(&addr), sz);
99 ServerInstance->SE->Blocking(listenFD);
100 int nfd = accept(listenFD, reinterpret_cast<struct sockaddr*>(&addr), &sz);
102 throw CoreException("Could not create ITC pipe");
103 new ThreadSignalSocket(this, nfd);
104 closesocket(listenFD);
106 ServerInstance->SE->Blocking(connFD);
107 this->signal.connFD = connFD;
110 void SocketThread::NotifyParent()
113 send(signal.connFD, &dummy, 1, 0);
116 SocketThread::~SocketThread()
118 if (signal.connFD >= 0)
120 shutdown(signal.connFD, 2);
121 closesocket(signal.connFD);