OSDN Git Service

AsyncExでロックをかけることにした
authortest <test@yahoo.co.jp>
Mon, 8 Apr 2019 11:17:49 +0000 (20:17 +0900)
committertest <test@yahoo.co.jp>
Mon, 8 Apr 2019 11:17:49 +0000 (20:17 +0900)
12 files changed:
Core/StringBuffer.cs
UWP/FooEditEngine.UWP/FooEditEngine.UWP.csproj
WPF/FooEditEngine/FooEditEngine.csproj
WPF/Test/Test.csproj
WPF/Test/app.config [new file with mode: 0644]
WPF/UnitTest/UnitTest.csproj
WPF/UnitTest/app.config [new file with mode: 0644]
Windows/FooEditEngine/FooEditEngine.csproj
Windows/Test/Properties/Resources.Designer.cs
Windows/Test/Properties/Settings.Designer.cs
Windows/Test/Test.csproj
Windows/Test/app.config [new file with mode: 0644]

index 09993df..0ab830c 100644 (file)
@@ -16,6 +16,7 @@ using System.Globalization;
 using System.Linq;
 using System.Text;
 using System.Text.RegularExpressions;
+using Nito.AsyncEx;
 using System.Threading;
 using System.Threading.Tasks;
 using Slusser.Collections.Generic;
@@ -41,7 +42,7 @@ namespace FooEditEngine
         GapBuffer<char> buf = new GapBuffer<char>();
         const int MaxSemaphoreCount = 1;
         SemaphoreSlim Semaphore = new SemaphoreSlim(MaxSemaphoreCount);
-        ReaderWriterLockSlim rwlock = new ReaderWriterLockSlim();
+        AsyncReaderWriterLock rwlock = new AsyncReaderWriterLock();
 
         public StringBuffer()
         {
@@ -77,15 +78,13 @@ namespace FooEditEngine
 
         public string ToString(int index, int length)
         {
-            this.rwlock.EnterReadLock();
-
             StringBuilder temp = new StringBuilder();
             temp.Clear();
-            for (int i = index; i < index + length; i++)
-                temp.Append(buf[i]);
-
-            this.rwlock.ExitReadLock();
-
+            using (this.rwlock.ReaderLock())
+            {
+                for (int i = index; i < index + length; i++)
+                    temp.Append(buf[i]);
+            }
             return temp.ToString();
         }
 
@@ -146,31 +145,23 @@ namespace FooEditEngine
 
         internal void Replace(GapBuffer<char> buf)
         {
-            this.rwlock.EnterWriteLock();
-
-            this.Clear();
-            this.buf = buf;
-
-            this.rwlock.ExitWriteLock();
+            using (this.rwlock.WriterLock())
+            {
+                this.Clear();
+                this.buf = buf;
+            }
 
             this.Update(this, new DocumentUpdateEventArgs(UpdateType.Replace, 0, 0, buf.Count));
         }
 
         internal void Replace(int index, int length, IEnumerable<char> chars, int count)
         {
-            this.rwlock.EnterWriteLock();
-
-            try
+            using (this.rwlock.WriterLock())
             {
                 if (length > 0)
                     this.buf.RemoveRange(index, length);
                 this.buf.InsertRange(index, chars, count);
             }
-            finally
-            {
-                this.rwlock.ExitWriteLock();
-            }
-
             this.Update(this, new DocumentUpdateEventArgs(UpdateType.Replace, index, length, count));
         }
 
@@ -189,11 +180,11 @@ namespace FooEditEngine
                 //内部形式に変換する
                 var internal_str = from s in str where s != '\r' && s != '\0' select s;
 
-                this.rwlock.EnterWriteLock();
-                //str.lengthは事前に確保しておくために使用するので影響はない
-                this.buf.InsertRange(index, internal_str, str.Length);
-
-                this.rwlock.ExitWriteLock();
+                using (await this.rwlock.WriterLockAsync())
+                {
+                    //str.lengthは事前に確保しておくために使用するので影響はない
+                    this.buf.InsertRange(index, internal_str, str.Length);
+                }
 
                 if (tokenSource != null)
                     tokenSource.Token.ThrowIfCancellationRequested();
@@ -207,9 +198,8 @@ namespace FooEditEngine
 
         internal async Task SaveAsync(TextWriter fs, CancellationTokenSource tokenSource = null)
         {
-            try
+            using(await this.rwlock.ReaderLockAsync())
             {
-                this.rwlock.EnterReadLock();
                 StringBuilder line = new StringBuilder();
                 for (int i = 0; i < this.Length; i++)
                 {
@@ -229,10 +219,6 @@ namespace FooEditEngine
                     }
                 }
             }
-            finally
-            {
-                this.rwlock.ExitReadLock();
-            }
         }
 
         internal void ReplaceRegexAll(LineToIndexTable layoutlines, Regex regex, string pattern, bool groupReplace)
@@ -241,25 +227,22 @@ namespace FooEditEngine
             {
                 int lineHeadIndex = layoutlines.GetIndexFromLineNumber(i), lineLength = layoutlines.GetLengthFromLineNumber(i);
                 int left = lineHeadIndex, right = lineHeadIndex;
-                string output = regex.Replace(layoutlines[i], (m) => {
+                string output;
+
+                output = regex.Replace(layoutlines[i], (m) => {
                     if (groupReplace)
                         return m.Result(pattern);
                     else
                         return pattern;
                 });
 
-                this.rwlock.EnterWriteLock();
-                try
+                using (this.rwlock.WriterLock())
                 {
                     //空行は削除する必要はない
                     if (lineHeadIndex < this.buf.Count)
                         this.buf.RemoveRange(lineHeadIndex, lineLength);
                     this.buf.InsertRange(lineHeadIndex, output, output.Length);
                 }
-                finally
-                {
-                    this.rwlock.ExitWriteLock();
-                }
 
                 this.Update(this, new DocumentUpdateEventArgs(UpdateType.Replace, lineHeadIndex, lineLength, output.Length, i));
             }
@@ -276,36 +259,27 @@ namespace FooEditEngine
                 int newLineLength = lineLength;
                 while ((right = ts.IndexOf(this.buf, left, lineHeadIndex + newLineLength)) != -1)
                 {
-                    this.rwlock.EnterWriteLock();
-                    try
+                    using (this.rwlock.WriterLock())
                     {
                         this.buf.RemoveRange(right, target.Length);
                         this.buf.InsertRange(right, pattern_chars, pattern.Length);
                     }
-                    finally
-                    {
-                        this.rwlock.ExitWriteLock();
-
-                    }
                     left = right + pattern.Length;
                     newLineLength += pattern.Length - target.Length;
                 }
 
-
                 this.Update(this, new DocumentUpdateEventArgs(UpdateType.Replace, lineHeadIndex, lineLength, newLineLength, i));
             }
         }
 
         internal int IndexOf(string target, int start, bool ci = false)
         {
-            this.rwlock.EnterReadLock();
-
-            TextSearch ts = new TextSearch(target, ci);
-            int patternIndex = ts.IndexOf(this.buf, start, this.buf.Count);
-
-            this.rwlock.ExitReadLock();
-
-            return patternIndex;
+            using (this.rwlock.ReaderLock())
+            {
+                TextSearch ts = new TextSearch(target, ci);
+                int patternIndex = ts.IndexOf(this.buf, start, this.buf.Count);
+                return patternIndex;
+            }
         }
 
         /// <summary>
index 2e1b287..2421c6d 100644 (file)
     <PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform">
       <Version>5.4.2</Version>
     </PackageReference>
+    <PackageReference Include="Nito.AsyncEx">
+      <Version>5.0.0</Version>
+    </PackageReference>
     <PackageReference Include="SharpDX">
       <Version>4.0.1</Version>
     </PackageReference>
index dba1589..f99d2a9 100644 (file)
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="utf-8"?>
-<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
   <PropertyGroup>
     <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
     <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
@@ -10,7 +10,7 @@
     <AppDesignerFolder>Properties</AppDesignerFolder>
     <RootNamespace>FooEditEngine.WPF</RootNamespace>
     <AssemblyName>FooEditEngine.WPF</AssemblyName>
-    <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
+    <TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
     <TargetFrameworkProfile>
     </TargetFrameworkProfile>
     <FileAlignment>512</FileAlignment>
@@ -83,6 +83,9 @@
     </ProjectReference>
   </ItemGroup>
   <ItemGroup>
+    <PackageReference Include="Nito.AsyncEx">
+      <Version>5.0.0</Version>
+    </PackageReference>
     <PackageReference Include="SharpDX.Direct2D1">
       <Version>4.0.1</Version>
     </PackageReference>
index 6a380eb..62ea303 100644 (file)
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="utf-8"?>
-<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
   <PropertyGroup>
     <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
     <Platform Condition=" '$(Platform)' == '' ">x86</Platform>
@@ -10,7 +10,7 @@
     <AppDesignerFolder>Properties</AppDesignerFolder>
     <RootNamespace>Test</RootNamespace>
     <AssemblyName>Test</AssemblyName>
-    <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
+    <TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
     <TargetFrameworkProfile>
     </TargetFrameworkProfile>
     <FileAlignment>512</FileAlignment>
diff --git a/WPF/Test/app.config b/WPF/Test/app.config
new file mode 100644 (file)
index 0000000..8a9a78f
--- /dev/null
@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="utf-8"?>
+<configuration>
+  <runtime>
+    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
+      <dependentAssembly>
+        <assemblyIdentity name="SharpDX" publicKeyToken="b4dcf0f35e5521f1" culture="neutral"/>
+        <bindingRedirect oldVersion="0.0.0.0-4.0.1.0" newVersion="4.0.1.0"/>
+      </dependentAssembly>
+      <dependentAssembly>
+        <assemblyIdentity name="SharpDX.DXGI" publicKeyToken="b4dcf0f35e5521f1" culture="neutral"/>
+        <bindingRedirect oldVersion="0.0.0.0-4.0.1.0" newVersion="4.0.1.0"/>
+      </dependentAssembly>
+    </assemblyBinding>
+  </runtime>
+<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6"/></startup></configuration>
index e6b693e..3780b5f 100644 (file)
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="utf-8"?>
-<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
   <PropertyGroup>
     <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
     <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
@@ -8,7 +8,7 @@
     <AppDesignerFolder>Properties</AppDesignerFolder>
     <RootNamespace>UnitTest</RootNamespace>
     <AssemblyName>UnitTest</AssemblyName>
-    <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
+    <TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
     <FileAlignment>512</FileAlignment>
     <ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
     <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
@@ -16,6 +16,7 @@
     <ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
     <IsCodedUITest>False</IsCodedUITest>
     <TestProjectType>UnitTest</TestProjectType>
+    <TargetFrameworkProfile />
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
     <DebugSymbols>true</DebugSymbols>
diff --git a/WPF/UnitTest/app.config b/WPF/UnitTest/app.config
new file mode 100644 (file)
index 0000000..8a9a78f
--- /dev/null
@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="utf-8"?>
+<configuration>
+  <runtime>
+    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
+      <dependentAssembly>
+        <assemblyIdentity name="SharpDX" publicKeyToken="b4dcf0f35e5521f1" culture="neutral"/>
+        <bindingRedirect oldVersion="0.0.0.0-4.0.1.0" newVersion="4.0.1.0"/>
+      </dependentAssembly>
+      <dependentAssembly>
+        <assemblyIdentity name="SharpDX.DXGI" publicKeyToken="b4dcf0f35e5521f1" culture="neutral"/>
+        <bindingRedirect oldVersion="0.0.0.0-4.0.1.0" newVersion="4.0.1.0"/>
+      </dependentAssembly>
+    </assemblyBinding>
+  </runtime>
+<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6"/></startup></configuration>
index 69166a8..b6920dc 100644 (file)
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="utf-8"?>
-<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
   <PropertyGroup>
     <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
     <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
@@ -10,7 +10,7 @@
     <AppDesignerFolder>Properties</AppDesignerFolder>
     <RootNamespace>FooEditEngine.Windows</RootNamespace>
     <AssemblyName>FooEditEngine.Windows</AssemblyName>
-    <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
+    <TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
     <FileAlignment>512</FileAlignment>
     <TargetFrameworkProfile />
   </PropertyGroup>
@@ -54,6 +54,9 @@
     <Compile Include="WinIME.cs" />
   </ItemGroup>
   <ItemGroup>
+    <PackageReference Include="Nito.AsyncEx">
+      <Version>5.0.0</Version>
+    </PackageReference>
     <PackageReference Include="SharpDX.Direct2D1">
       <Version>4.0.1</Version>
     </PackageReference>
index 216e892..9a2919b 100644 (file)
@@ -1,7 +1,7 @@
 //------------------------------------------------------------------------------
 // <auto-generated>
 //     このコードはツールによって生成されました。
-//     ランタイム バージョン:4.0.30319.17929
+//     ランタイム バージョン:4.0.30319.42000
 //
 //     このファイルへの変更は、以下の状況下で不正な動作の原因になったり、
 //     コードが再生成されるときに損失したりします。
@@ -19,7 +19,7 @@ namespace Test.Windows.Properties {
     // または Visual Studio のようなツールを使用して自動生成されました。
     // メンバーを追加または削除するには、.ResX ファイルを編集して、/str オプションと共に
     // ResGen を実行し直すか、または VS プロジェクトをビルドし直します。
-    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
+    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "15.0.0.0")]
     [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
     [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
     internal class Resources {
index 18c43a3..ac684d5 100644 (file)
@@ -1,7 +1,7 @@
 //------------------------------------------------------------------------------
 // <auto-generated>
 //     このコードはツールによって生成されました。
-//     ランタイム バージョン:4.0.30319.17929
+//     ランタイム バージョン:4.0.30319.42000
 //
 //     このファイルへの変更は、以下の状況下で不正な動作の原因になったり、
 //     コードが再生成されるときに損失したりします。
@@ -12,7 +12,7 @@ namespace Test.Windows.Properties {
     
     
     [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
-    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")]
+    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "15.7.0.0")]
     internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
         
         private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
index a692f9c..e974b10 100644 (file)
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="utf-8"?>
-<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
   <PropertyGroup>
     <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
     <Platform Condition=" '$(Platform)' == '' ">x86</Platform>
@@ -10,7 +10,7 @@
     <AppDesignerFolder>Properties</AppDesignerFolder>
     <RootNamespace>Test.Windows</RootNamespace>
     <AssemblyName>Test.Windows</AssemblyName>
-    <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
+    <TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
     <TargetFrameworkProfile>
     </TargetFrameworkProfile>
     <FileAlignment>512</FileAlignment>
diff --git a/Windows/Test/app.config b/Windows/Test/app.config
new file mode 100644 (file)
index 0000000..2e5cf12
--- /dev/null
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="utf-8"?>
+<configuration>
+  <runtime>
+    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
+      <dependentAssembly>
+        <assemblyIdentity name="SharpDX" publicKeyToken="b4dcf0f35e5521f1" culture="neutral"/>
+        <bindingRedirect oldVersion="0.0.0.0-4.0.1.0" newVersion="4.0.1.0"/>
+      </dependentAssembly>
+    </assemblyBinding>
+  </runtime>
+<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6"/></startup></configuration>