Skip to content

Commit f982b7d

Browse files
committed
Fix Scan Concurenncy and refactoring
1 parent 61699d9 commit f982b7d

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

src/Mapster/TypeAdapterConfig/BaseTypeAdapterConfigDecorator.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public BaseTypeAdapterConfigDecorator(ITypeAdapterConfig config, bool IsGlobal =
4343

4444
public AutoResetEvent Configure => _Config.Configure;
4545

46+
public AutoResetEvent ApplySync => _Config.ApplySync;
47+
4648
public void AddRule(TypeAdapterRule rule)
4749
{
4850
_Config.AddRule(rule);

src/Mapster/TypeAdapterConfig/ITypeAdapterConfig.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ public interface ITypeAdapterConfig
2727

2828
bool ConcurrencyEnvironment { get; }
2929
AutoResetEvent Configure { get;}
30-
30+
AutoResetEvent ApplySync { get; }
31+
3132
void Apply(IEnumerable<IRegister> registers);
3233
void Clear();
3334
ITypeAdapterConfig Clone();

src/Mapster/TypeAdapterConfig/TypeAdapterConfig.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System;
44
using System.Collections.Concurrent;
55
using System.Collections.Generic;
6+
using System.Diagnostics;
67
using System.Linq;
78
using System.Linq.Expressions;
89
using System.Runtime.CompilerServices;
@@ -33,7 +34,10 @@ public class TypeAdapterConfig : ITypeAdapterConfig, IConfigConcurrency
3334

3435
[AdaptIgnore]
3536
public AutoResetEvent Configure { get; private set; }
36-
37+
38+
[AdaptIgnore]
39+
public AutoResetEvent ApplySync { get; private set; }
40+
3741
internal TypeAdapterConfig(bool IsGlobal) : this()
3842
{
3943
IsGlobalSettings = IsGlobal;
@@ -42,6 +46,7 @@ internal TypeAdapterConfig(bool IsGlobal) : this()
4246
public TypeAdapterConfig()
4347
{
4448
Configure = new(true);
49+
ApplySync = new(true);
4550
Rules = TypeAdapterConfigFactory.RulesTemplate.ToList();
4651
var settings = new TypeAdapterSettings();
4752
ConfigCompile = new ConfigCompileStorage(this);
@@ -85,6 +90,11 @@ public LambdaExpression CreateMapExpression(TypeTuple tuple, MapType mapType)
8590
{
8691
Configure.WaitOne(-1);
8792
}
93+
if(this is IConfigConcurrency config)
94+
{
95+
if (config.IsScanConcurrency)
96+
ApplySync.WaitOne(-1);
97+
}
8898

8999
var context = new CompileContext(this);
90100
context.Running.Add(tuple);
@@ -105,6 +115,7 @@ public LambdaExpression CreateMapExpression(TypeTuple tuple, MapType mapType)
105115
finally
106116
{
107117
Configure.Set();
118+
ApplySync.Set();
108119

109120
if (fork != null)
110121
context.Configs.Pop();
@@ -175,10 +186,14 @@ public void CompileProjection(Type sourceType, Type destinationType)
175186
/// <param name="registers">collection of IRegister interface to apply mapping.</param>
176187
public void Apply(IEnumerable<IRegister> registers)
177188
{
189+
ApplySync.WaitOne(-1, false);
190+
178191
foreach (IRegister register in registers)
179192
{
180193
register.Register(this);
181194
}
195+
196+
ApplySync.Set();
182197
}
183198

184199
/// <summary>

0 commit comments

Comments
 (0)