File: instance_data.cpp

package info (click to toggle)
ghostscript 10.0.0~dfsg-11%2Bdeb12u7
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 98,252 kB
  • sloc: ansic: 891,828; python: 7,649; cpp: 6,493; cs: 6,209; sh: 6,043; java: 4,028; perl: 2,372; tcl: 1,639; makefile: 521; awk: 66; javascript: 43; yacc: 18
file content (64 lines) | stat: -rw-r--r-- 1,099 bytes parent folder | download | duplicates (6)
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
#include "instance_data.h"

#include <unordered_map>
#include <assert.h>
#include <mutex>

#if !defined(GSJNI_NO_MT)

static std::unordered_map<void *, GSInstanceData *> g_inst2Data; // instance -> data
static std::mutex g_mtx;

#else

static GSInstanceData g_global; // Global instance for faster multithreading

#endif

GSInstanceData *putInstanceData(GSInstanceData *data)
{
#if !defined(GSJNI_NO_MT)
	g_mtx.lock();

	assert(g_inst2Data.find(data->instance) == g_inst2Data.end());

	g_inst2Data[data->instance] = data;

	g_mtx.unlock();

	return data;

#else
	delete data;
	return &g_global;
#endif
}

GSInstanceData *findDataFromInstance(void *instance)
{
#if !defined(GSJNI_NO_MT)
	g_mtx.lock();

	auto it = g_inst2Data.find(instance);
	GSInstanceData *result = it == g_inst2Data.end() ? NULL : it->second;

	g_mtx.unlock();

	return result;
#else
	return &g_global;
#endif
}

void deleteDataFromInstance(void *instance)
{
#if !defined(GSJNI_NO_MT)
	g_mtx.lock();

	auto i2dit = g_inst2Data.find(instance);
	if (i2dit != g_inst2Data.end())
		g_inst2Data.erase(i2dit);

	g_mtx.unlock();
#endif
}