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
}
|