aboutsummaryrefslogtreecommitdiff
path: root/src/zenhttp/servers/iothreadpool.cpp
blob: e941606e252d84886f5285da60f1303779b221db (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright Epic Games, Inc. All Rights Reserved.

#include "iothreadpool.h"

#include <zencore/except.h>

#if ZEN_PLATFORM_WINDOWS

namespace zen {

WinIoThreadPool::WinIoThreadPool(int InThreadCount, int InMaxThreadCount)
{
	ZEN_ASSERT(InThreadCount);

	if (InMaxThreadCount < InThreadCount)
	{
		InMaxThreadCount = InThreadCount;
	}

	m_ThreadPool = CreateThreadpool(NULL);

	SetThreadpoolThreadMinimum(m_ThreadPool, InThreadCount);
	SetThreadpoolThreadMaximum(m_ThreadPool, InMaxThreadCount);

	InitializeThreadpoolEnvironment(&m_CallbackEnvironment);

	m_CleanupGroup = CreateThreadpoolCleanupGroup();

	SetThreadpoolCallbackPool(&m_CallbackEnvironment, m_ThreadPool);

	SetThreadpoolCallbackCleanupGroup(&m_CallbackEnvironment, m_CleanupGroup, NULL);
}

WinIoThreadPool::~WinIoThreadPool()
{
	// this will wait for all callbacks to complete and tear down the `CreateThreadpoolIo`
	// object and release all related objects
	CloseThreadpoolCleanupGroupMembers(m_CleanupGroup, /* cancel pending callbacks */ TRUE, nullptr);
	CloseThreadpoolCleanupGroup(m_CleanupGroup);
	CloseThreadpool(m_ThreadPool);
	DestroyThreadpoolEnvironment(&m_CallbackEnvironment);
}

void
WinIoThreadPool::CreateIocp(HANDLE IoHandle, PTP_WIN32_IO_CALLBACK Callback, void* Context, std::error_code& ErrorCode)
{
	ZEN_ASSERT(!m_ThreadPoolIo);

	m_ThreadPoolIo = CreateThreadpoolIo(IoHandle, Callback, Context, &m_CallbackEnvironment);

	if (!m_ThreadPoolIo)
	{
		ErrorCode = MakeErrorCodeFromLastError();
	}
}

}  // namespace zen

#endif