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 @@
<DebugType>
full
</DebugType>
<Optimize>
False
</Optimize>
<OutputPath>
..\..\Run\Debug\net35\
</OutputPath>
<DefineConstants>
DEBUG
</DefineConstants>
<DefineConstants>
DEBUG
;NET_3_5
</DefineConstants>
<ErrorReport>
prompt
</ErrorReport>
<WarningLevel>
4
</WarningLevel>
<TreatWarningsAsErrors>
true
</TreatWarningsAsErrors>
...
...
@@ -27,7 +27,7 @@
<DebugType>
none
</DebugType>
<Optimize>
True
</Optimize>
<OutputPath>
..\..\Run\Release\net35\
</OutputPath>
<DefineConstants>
RELEASE
</DefineConstants>
<DefineConstants>
RELEASE
;NET_3_5
</DefineConstants>
<ErrorReport>
prompt
</ErrorReport>
<WarningLevel>
4
</WarningLevel>
<PlatformTarget>
AnyCPU
</PlatformTarget>
...
...
@@ -35,7 +35,7 @@
<PropertyGroup
Condition=
"'$(Configuration)|$(Platform)' == 'DebugKopiLua|AnyCPU'"
>
<DebugSymbols>
true
</DebugSymbols>
<OutputPath>
bin\DebugKopiLua\
</OutputPath>
<DefineConstants>
DEBUG;USE_KOPILUA
</DefineConstants>
<DefineConstants>
DEBUG;
NET_3_5;
USE_KOPILUA
</DefineConstants>
<TreatWarningsAsErrors>
true
</TreatWarningsAsErrors>
<DebugType>
full
</DebugType>
<PlatformTarget>
AnyCPU
</PlatformTarget>
...
...
@@ -44,7 +44,7 @@
</PropertyGroup>
<PropertyGroup
Condition=
"'$(Configuration)|$(Platform)' == 'ReleaseKopiLua|AnyCPU'"
>
<OutputPath>
bin\ReleaseKopiLua\
</OutputPath>
<DefineConstants>
USE_KOPILUA
</DefineConstants>
<DefineConstants>
NET_3_5;
USE_KOPILUA
</DefineConstants>
</PropertyGroup>
<PropertyGroup>
<SignAssembly>
true
</SignAssembly>
...
...
@@ -123,4 +123,4 @@
<ItemGroup>
<None
Include=
"key.snk"
/>
</ItemGroup>
</Project>
\ No newline at end of file
</Project>
Core/NLua/ObjectTranslatorPool.cs
View file @
8095cc63
using
System
;
#if WINDOWS_PHONE || NET_3_5
using
System.Collections.Generic
;
#else
using
System.Collections.Concurrent
;
#endif
/*
* This file is part of NLua.
...
...
@@ -27,19 +31,23 @@ using System.Collections.Generic;
namespace
NLua
{
#
if
USE_KOPILUA
#if USE_KOPILUA
using
LuaCore
=
KopiLua
.
Lua
;
using
LuaState
=
KopiLua
.
LuaState
;
#
else
#else
using
LuaCore
=
KeraLua
.
Lua
;
using
LuaState
=
KeraLua
.
LuaState
;
#
endif
#endif
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
>();
#else
private
ConcurrentDictionary
<
LuaState
,
ObjectTranslator
>
translators
=
new
ConcurrentDictionary
<
LuaState
,
ObjectTranslator
>();
#endif
public
static
ObjectTranslatorPool
Instance
{
get
...
...
@@ -54,28 +62,51 @@ namespace NLua
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
)
{
if
(
translators
.
ContainsKey
(
luaState
))
return
translators
[
luaState
];
#if WINDOWS_PHONE || NET_3_5
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
))
return
translators
[
main
];
if
(!
translators
.
TryGetValue
(
main
,
out
translator
))
translator
=
null
;
}
return
null
;
return
translator
;
#if WINDOWS_PHONE || NET_3_5
}
#endif
}
public
void
Remove
(
LuaState
luaState
)
{
if
(!
translators
.
ContainsKey
(
luaState
))
return
;
#if WINDOWS_PHONE || NET_3_5
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