From 8d11825dae79b0c2783bc5b786c21c1d2b192f49 Mon Sep 17 00:00:00 2001 From: test Date: Tue, 5 Feb 2019 21:03:51 +0900 Subject: [PATCH] =?utf8?q?Revert=20"=E3=82=BB=E3=83=9E=E3=83=95=E3=82=A9?= =?utf8?q?=E3=83=BC=E3=81=A0=E3=81=A8=E3=83=87=E3=83=83=E3=83=89=E3=83=AD?= =?utf8?q?=E3=83=83=E3=82=AF=E3=82=92=E8=B5=B7=E3=81=93=E3=81=99=E3=81=93?= =?utf8?q?=E3=81=A8=E3=81=8C=E3=81=82=E3=82=8B=E3=81=AE=E3=81=A7=E3=83=AA?= =?utf8?q?=E3=83=BC=E3=83=80=E3=83=BC=E3=83=A9=E3=82=A4=E3=82=BF=E3=83=BC?= =?utf8?q?=E3=83=AD=E3=83=83=E3=82=AB=E3=83=BC=E3=81=AB=E5=A4=89=E6=9B=B4?= =?utf8?q?=E3=81=97=E3=81=9F"?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This reverts commit 182f1aef06140e95191f3ad12a30e9cad446c028. --- Core/Document.cs | 23 ++++++++++++-- Core/StringBuffer.cs | 84 ++++++++++++++++++++++++---------------------------- 2 files changed, 60 insertions(+), 47 deletions(-) diff --git a/Core/Document.cs b/Core/Document.cs index ed6fdf3..0a42405 100644 --- a/Core/Document.cs +++ b/Core/Document.cs @@ -1160,11 +1160,30 @@ namespace FooEditEngine { try { - await this.buffer.SaveAsync(fs, tokenSource); + await this.buffer.LockAsync().ConfigureAwait(false); + StringBuilder line = new StringBuilder(); + for (int i = 0; i < this.Length; i++) + { + char c = this[i]; + line.Append(c); + if (c == Document.NewLine || i == this.Length - 1) + { + string str = line.ToString(); + str = str.Replace(Document.NewLine.ToString(), fs.NewLine); + await fs.WriteAsync(str).ConfigureAwait(false); + line.Clear(); + if (tokenSource != null) + tokenSource.Token.ThrowIfCancellationRequested(); +#if TEST_ASYNC + System.Threading.Thread.Sleep(10); +#endif + } + } + this.Dirty = false; } finally { - this.Dirty = false; + this.buffer.UnLock(); } } diff --git a/Core/StringBuffer.cs b/Core/StringBuffer.cs index 57aaacd..0fdc448 100644 --- a/Core/StringBuffer.cs +++ b/Core/StringBuffer.cs @@ -41,7 +41,6 @@ namespace FooEditEngine GapBuffer buf = new GapBuffer(); const int MaxSemaphoreCount = 1; SemaphoreSlim Semaphore = new SemaphoreSlim(MaxSemaphoreCount); - ReaderWriterLockSlim rwlock = new ReaderWriterLockSlim(); public StringBuffer() { @@ -59,6 +58,31 @@ namespace FooEditEngine } } + /// + /// ロックを解除します + /// + public void UnLock() + { + this.Semaphore.Release(); + } + + /// + /// ロックします + /// + public void Lock() + { + this.Semaphore.Wait(); + } + + /// + /// ロックします + /// + /// Taskオブジェクト + public Task LockAsync() + { + return this.Semaphore.WaitAsync(); + } + public StringBuffer(StringBuffer buffer) : this() { @@ -77,14 +101,14 @@ namespace FooEditEngine public string ToString(int index, int length) { - this.rwlock.EnterReadLock(); + this.Lock(); StringBuilder temp = new StringBuilder(); temp.Clear(); for (int i = index; i < index + length; i++) temp.Append(buf[i]); - this.rwlock.ExitReadLock(); + this.UnLock(); return temp.ToString(); } @@ -146,19 +170,19 @@ namespace FooEditEngine internal void Replace(GapBuffer buf) { - this.rwlock.EnterWriteLock(); + this.Lock(); this.Clear(); this.buf = buf; - this.rwlock.ExitWriteLock(); + this.UnLock(); this.Update(this, new DocumentUpdateEventArgs(UpdateType.Replace, 0, 0, buf.Count)); } internal void Replace(int index, int length, IEnumerable chars,int count) { - this.rwlock.EnterWriteLock(); + this.Lock(); try{ if (length > 0) @@ -167,7 +191,7 @@ namespace FooEditEngine } finally { - this.rwlock.ExitWriteLock(); + this.UnLock(); } this.Update(this, new DocumentUpdateEventArgs(UpdateType.Replace, index, length, count)); @@ -188,11 +212,11 @@ namespace FooEditEngine //内部形式に変換する var internal_str = from s in str where s != '\r' && s != '\0' select s; - this.rwlock.EnterWriteLock(); + await this.LockAsync().ConfigureAwait(false); //str.lengthは事前に確保しておくために使用するので影響はない this.buf.InsertRange(index, internal_str, str.Length); - this.rwlock.ExitWriteLock(); + this.UnLock(); if (tokenSource != null) tokenSource.Token.ThrowIfCancellationRequested(); @@ -204,36 +228,6 @@ namespace FooEditEngine } while (readCount > 0); } - internal async Task SaveAsync(TextWriter fs,CancellationTokenSource tokenSource=null) - { - try - { - this.rwlock.EnterWriteLock(); - StringBuilder line = new StringBuilder(); - for (int i = 0; i < this.Length; i++) - { - char c = this[i]; - line.Append(c); - if (c == Document.NewLine || i == this.Length - 1) - { - string str = line.ToString(); - str = str.Replace(Document.NewLine.ToString(), fs.NewLine); - await fs.WriteAsync(str).ConfigureAwait(false); - line.Clear(); - if (tokenSource != null) - tokenSource.Token.ThrowIfCancellationRequested(); -#if TEST_ASYNC - System.Threading.Thread.Sleep(10); -#endif - } - } - } - finally - { - this.rwlock.ExitWriteLock(); - } - } - internal void ReplaceRegexAll(LineToIndexTable layoutlines, Regex regex, string pattern, bool groupReplace) { for (int i = 0; i < layoutlines.Count; i++) @@ -247,7 +241,7 @@ namespace FooEditEngine return pattern; }); - this.rwlock.EnterWriteLock(); + this.Lock(); try { //空行は削除する必要はない @@ -257,7 +251,7 @@ namespace FooEditEngine } finally { - this.rwlock.ExitWriteLock(); + this.UnLock(); } this.Update(this, new DocumentUpdateEventArgs(UpdateType.Replace, lineHeadIndex, lineLength, output.Length, i)); @@ -275,7 +269,7 @@ namespace FooEditEngine int newLineLength = lineLength; while ((right = ts.IndexOf(this.buf, left, lineHeadIndex + newLineLength)) != -1) { - this.rwlock.EnterWriteLock(); + this.Lock(); try { this.buf.RemoveRange(right, target.Length); @@ -283,7 +277,7 @@ namespace FooEditEngine } finally { - this.rwlock.ExitWriteLock(); + this.UnLock(); } left = right + pattern.Length; @@ -297,12 +291,12 @@ namespace FooEditEngine internal int IndexOf(string target, int start,bool ci = false) { - this.rwlock.EnterReadLock(); + this.Lock(); TextSearch ts = new TextSearch(target,ci); int patternIndex = ts.IndexOf(this.buf, start,this.buf.Count); - this.rwlock.ExitReadLock(); + this.UnLock(); return patternIndex; } -- 2.11.0