Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
ruanhaishen
NLua
Commits
8095cc63
Commit
8095cc63
authored
Mar 03, 2016
by
Kalin Toshev
Committed by
Kalin Toshev
Mar 04, 2016
Browse files
Made ObjectTranslatorPool thread-safe
parent
106b34ce
Changes
2
Hide whitespace changes
Inline
Side-by-side
Core/NLua/NLua.Net35.csproj
View file @
8095cc63
...
@@ -18,7 +18,7 @@
...
@@ -18,7 +18,7 @@
<DebugType>
full
</DebugType>
<DebugType>
full
</DebugType>
<Optimize>
False
</Optimize>
<Optimize>
False
</Optimize>
<OutputPath>
..\..\Run\Debug\net35\
</OutputPath>
<OutputPath>
..\..\Run\Debug\net35\
</OutputPath>
<DefineConstants>
DEBUG
</DefineConstants>
<DefineConstants>
DEBUG
;NET_3_5
</DefineConstants>
<ErrorReport>
prompt
</ErrorReport>
<ErrorReport>
prompt
</ErrorReport>
<WarningLevel>
4
</WarningLevel>
<WarningLevel>
4
</WarningLevel>
<TreatWarningsAsErrors>
true
</TreatWarningsAsErrors>
<TreatWarningsAsErrors>
true
</TreatWarningsAsErrors>
...
@@ -27,7 +27,7 @@
...
@@ -27,7 +27,7 @@
<DebugType>
none
</DebugType>
<DebugType>
none
</DebugType>
<Optimize>
True
</Optimize>
<Optimize>
True
</Optimize>
<OutputPath>
..\..\Run\Release\net35\
</OutputPath>
<OutputPath>
..\..\Run\Release\net35\
</OutputPath>
<DefineConstants>
RELEASE
</DefineConstants>
<DefineConstants>
RELEASE
;NET_3_5
</DefineConstants>
<ErrorReport>
prompt
</ErrorReport>
<ErrorReport>
prompt
</ErrorReport>
<WarningLevel>
4
</WarningLevel>
<WarningLevel>
4
</WarningLevel>
<PlatformTarget>
AnyCPU
</PlatformTarget>
<PlatformTarget>
AnyCPU
</PlatformTarget>
...
@@ -35,7 +35,7 @@
...
@@ -35,7 +35,7 @@
<PropertyGroup
Condition=
"'$(Configuration)|$(Platform)' == 'DebugKopiLua|AnyCPU'"
>
<PropertyGroup
Condition=
"'$(Configuration)|$(Platform)' == 'DebugKopiLua|AnyCPU'"
>
<DebugSymbols>
true
</DebugSymbols>
<DebugSymbols>
true
</DebugSymbols>
<OutputPath>
bin\DebugKopiLua\
</OutputPath>
<OutputPath>
bin\DebugKopiLua\
</OutputPath>
<DefineConstants>
DEBUG;USE_KOPILUA
</DefineConstants>
<DefineConstants>
DEBUG;
NET_3_5;
USE_KOPILUA
</DefineConstants>
<TreatWarningsAsErrors>
true
</TreatWarningsAsErrors>
<TreatWarningsAsErrors>
true
</TreatWarningsAsErrors>
<DebugType>
full
</DebugType>
<DebugType>
full
</DebugType>
<PlatformTarget>
AnyCPU
</PlatformTarget>
<PlatformTarget>
AnyCPU
</PlatformTarget>
...
@@ -44,7 +44,7 @@
...
@@ -44,7 +44,7 @@
</PropertyGroup>
</PropertyGroup>
<PropertyGroup
Condition=
"'$(Configuration)|$(Platform)' == 'ReleaseKopiLua|AnyCPU'"
>
<PropertyGroup
Condition=
"'$(Configuration)|$(Platform)' == 'ReleaseKopiLua|AnyCPU'"
>
<OutputPath>
bin\ReleaseKopiLua\
</OutputPath>
<OutputPath>
bin\ReleaseKopiLua\
</OutputPath>
<DefineConstants>
USE_KOPILUA
</DefineConstants>
<DefineConstants>
NET_3_5;
USE_KOPILUA
</DefineConstants>
</PropertyGroup>
</PropertyGroup>
<PropertyGroup>
<PropertyGroup>
<SignAssembly>
true
</SignAssembly>
<SignAssembly>
true
</SignAssembly>
...
@@ -123,4 +123,4 @@
...
@@ -123,4 +123,4 @@
<ItemGroup>
<ItemGroup>
<None
Include=
"key.snk"
/>
<None
Include=
"key.snk"
/>
</ItemGroup>
</ItemGroup>
</Project>
</Project>
\ No newline at end of file
Core/NLua/ObjectTranslatorPool.cs
View file @
8095cc63
using
System
;
using
System
;
#if WINDOWS_PHONE || NET_3_5
using
System.Collections.Generic
;
using
System.Collections.Generic
;
#else
using
System.Collections.Concurrent
;
#endif
/*
/*
* This file is part of NLua.
* This file is part of NLua.
...
@@ -27,19 +31,23 @@ using System.Collections.Generic;
...
@@ -27,19 +31,23 @@ using System.Collections.Generic;
namespace
NLua
namespace
NLua
{
{
#
if
USE_KOPILUA
#if USE_KOPILUA
using
LuaCore
=
KopiLua
.
Lua
;
using
LuaCore
=
KopiLua
.
Lua
;
using
LuaState
=
KopiLua
.
LuaState
;
using
LuaState
=
KopiLua
.
LuaState
;
#
else
#else
using
LuaCore
=
KeraLua
.
Lua
;
using
LuaCore
=
KeraLua
.
Lua
;
using
LuaState
=
KeraLua
.
LuaState
;
using
LuaState
=
KeraLua
.
LuaState
;
#
endif
#endif
internal
class
ObjectTranslatorPool
internal
class
ObjectTranslatorPool
{
{
private
static
volatile
ObjectTranslatorPool
instance
=
new
ObjectTranslatorPool
();
private
static
volatile
ObjectTranslatorPool
instance
=
new
ObjectTranslatorPool
();
#if WINDOWS_PHONE || NET_3_5
private
Dictionary
<
LuaState
,
ObjectTranslator
>
translators
=
new
Dictionary
<
LuaState
,
ObjectTranslator
>();
private
Dictionary
<
LuaState
,
ObjectTranslator
>
translators
=
new
Dictionary
<
LuaState
,
ObjectTranslator
>();
#else
private
ConcurrentDictionary
<
LuaState
,
ObjectTranslator
>
translators
=
new
ConcurrentDictionary
<
LuaState
,
ObjectTranslator
>();
#endif
public
static
ObjectTranslatorPool
Instance
public
static
ObjectTranslatorPool
Instance
{
{
get
get
...
@@ -54,28 +62,51 @@ namespace NLua
...
@@ -54,28 +62,51 @@ namespace NLua
public
void
Add
(
LuaState
luaState
,
ObjectTranslator
translator
)
public
void
Add
(
LuaState
luaState
,
ObjectTranslator
translator
)
{
{
translators
.
Add
(
luaState
,
translator
);
#if WINDOWS_PHONE || NET_3_5
lock
(
translators
)
translators
.
Add
(
luaState
,
translator
);
#else
if
(!
translators
.
TryAdd
(
luaState
,
translator
))
throw
new
ArgumentException
(
"An item with the same key has already been added. "
,
"luaState"
);
#endif
}
}
public
ObjectTranslator
Find
(
LuaState
luaState
)
public
ObjectTranslator
Find
(
LuaState
luaState
)
{
{
if
(
translators
.
ContainsKey
(
luaState
))
#if WINDOWS_PHONE || NET_3_5
return
translators
[
luaState
];
lock
(
translators
)
{
#endif
ObjectTranslator
translator
;
LuaState
main
=
LuaCore
.
LuaNetGetMainState
(
luaState
);
if
(!
translators
.
TryGetValue
(
luaState
,
out
translator
))
{
LuaState
main
=
LuaCore
.
LuaNetGetMainState
(
luaState
);
if
(
translators
.
ContainsKey
(
main
))
if
(!
translators
.
TryGetValue
(
main
,
out
translator
))
return
translators
[
main
];
translator
=
null
;
}
return
null
;
return
translator
;
#if WINDOWS_PHONE || NET_3_5
}
#endif
}
}
public
void
Remove
(
LuaState
luaState
)
public
void
Remove
(
LuaState
luaState
)
{
{
if
(!
translators
.
ContainsKey
(
luaState
))
#if WINDOWS_PHONE || NET_3_5
return
;
lock
(
translators
)
{
if
(!
translators
.
ContainsKey
(
luaState
))
return
;
translators
.
Remove
(
luaState
);
translators
.
Remove
(
luaState
);
}
#else
ObjectTranslator
translator
;
translators
.
TryRemove
(
luaState
,
out
translator
);
#endif
}
}
}
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment