Welcome to mirror list, hosted at ThFree Co, Russian Federation.

WinPortHandle.cpp « src « WinPort - github.com/elfmz/far2l.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ce9e291c832de0989b85a51a6b5f38a739c78429 (plain)
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
60
61
62
63
64
65
66
#include <set>
#include <mutex>
#include "WinPortHandle.h"
#include "WinCompat.h"
#include "WinPort.h"

WinPortHandle::WinPortHandle()
{
}

WinPortHandle::~WinPortHandle() 
{
}

void WinPortHandle::Reference()
{
	++_refcnt;
}

void WinPortHandle::Dereference()
{
	if (0 == --_refcnt)
		OnReleased();
}

static struct WinPortHandles : std::set<WinPortHandle *>, std::mutex
{
} g_winport_handles;


HANDLE WinPortHandle_Register(WinPortHandle *wph)
{
	std::lock_guard<std::mutex> lock(g_winport_handles);
	wph->Reference();
	g_winport_handles.insert(wph);
	return (HANDLE)wph;
}

bool WinPortHandle_Deregister(HANDLE h)
{
	WinPortHandle *wph = reinterpret_cast<WinPortHandle *>(h);
	{
		std::lock_guard<std::mutex> lock(g_winport_handles);
		if (g_winport_handles.erase(wph)==0) {
			WINPORT(SetLastError)(ERROR_INVALID_HANDLE);
			return false;
		}
	}
	wph->Dereference();
	return true;
}



WinPortHandle *WinPortHandle_Reference(HANDLE h)
{
	WinPortHandle *wph = reinterpret_cast<WinPortHandle *>(h);
	{
		std::lock_guard<std::mutex> lock(g_winport_handles);
		if (g_winport_handles.find(wph)==g_winport_handles.end())
			return NULL;
		wph->Reference();
	}	
	return wph;
}