dev
Revision | 6d40a276c6d9f663e282667080868ad3e5f1ab10 (tree) |
---|---|
Time | 2013-04-10 09:56:09 |
Author | Kimura Youichi <kim.upsilon@bucy...> |
Commiter | Kimura Youichi |
Revert "ImageCacheの排他制御を改善"
This reverts commit f3d6eb2556e7739b167f1f4fd79251b1bb660ac8.
LRUCacheDictionaryは参照もスレッドセーフではないため。
@@ -45,7 +45,7 @@ namespace OpenTween | ||
45 | 45 | /// <summary> |
46 | 46 | /// innerDictionary の排他制御のためのロックオブジェクト |
47 | 47 | /// </summary> |
48 | - private ReaderWriterLockSlim lockObject = new ReaderWriterLockSlim(); | |
48 | + private object lockObject = new object(); | |
49 | 49 | |
50 | 50 | /// <summary> |
51 | 51 | /// オブジェクトが破棄された否か |
@@ -54,13 +54,11 @@ namespace OpenTween | ||
54 | 54 | |
55 | 55 | public ImageCache() |
56 | 56 | { |
57 | - this.lockObject.EnterWriteLock(); | |
58 | - try | |
57 | + lock (this.lockObject) | |
59 | 58 | { |
60 | 59 | this.innerDictionary = new LRUCacheDictionary<string, Task<MemoryImage>>(trimLimit: 300, autoTrimCount: 100); |
61 | 60 | |
62 | - this.innerDictionary.CacheRemoved += (s, e) => | |
63 | - { | |
61 | + this.innerDictionary.CacheRemoved += (s, e) => { | |
64 | 62 | // まだ参照されている場合もあるのでDisposeはファイナライザ任せ |
65 | 63 | |
66 | 64 | this.CacheRemoveCount++; |
@@ -68,7 +66,6 @@ namespace OpenTween | ||
68 | 66 | |
69 | 67 | this.cancelTokenSource = new CancellationTokenSource(); |
70 | 68 | } |
71 | - finally { this.lockObject.ExitWriteLock(); } | |
72 | 69 | } |
73 | 70 | |
74 | 71 | /// <summary> |
@@ -98,10 +95,12 @@ namespace OpenTween | ||
98 | 95 | return Task.Factory.StartNew(() => |
99 | 96 | { |
100 | 97 | Task<MemoryImage> cachedImageTask = null; |
101 | - this.lockObject.EnterUpgradeableReadLock(); | |
102 | - try | |
98 | + lock (this.lockObject) | |
103 | 99 | { |
104 | - if (!force && this.innerDictionary.ContainsKey(address) && !this.innerDictionary[address].IsFaulted) | |
100 | + if (force) | |
101 | + this.innerDictionary.Remove(address); | |
102 | + | |
103 | + if (this.innerDictionary.ContainsKey(address) && !this.innerDictionary[address].IsFaulted) | |
105 | 104 | cachedImageTask = this.innerDictionary[address]; |
106 | 105 | |
107 | 106 | if (cachedImageTask != null) |
@@ -109,41 +108,34 @@ namespace OpenTween | ||
109 | 108 | |
110 | 109 | cancelToken.ThrowIfCancellationRequested(); |
111 | 110 | |
112 | - this.lockObject.EnterWriteLock(); | |
113 | - try | |
111 | + using (var client = new OTWebClient() { Timeout = 10000 }) | |
114 | 112 | { |
115 | - using (var client = new OTWebClient() { Timeout = 10000 }) | |
113 | + var imageTask = client.DownloadDataAsync(new Uri(address), cancelToken).ContinueWith(t => | |
116 | 114 | { |
117 | - var imageTask = client.DownloadDataAsync(new Uri(address), cancelToken).ContinueWith(t => | |
115 | + MemoryImage image = null; | |
116 | + if (t.Status == TaskStatus.RanToCompletion) | |
118 | 117 | { |
119 | - MemoryImage image = null; | |
120 | - if (t.Status == TaskStatus.RanToCompletion) | |
121 | - { | |
122 | - image = MemoryImage.CopyFromBytes(t.Result); | |
123 | - } | |
118 | + image = MemoryImage.CopyFromBytes(t.Result); | |
119 | + } | |
124 | 120 | |
125 | - if (t.Exception != null) | |
126 | - t.Exception.Handle(e => e is WebException); | |
121 | + if (t.Exception != null) | |
122 | + t.Exception.Handle(e => e is WebException); | |
127 | 123 | |
128 | - // FIXME: MemoryImage.Dispose() が正しいタイミングで呼ばれるように修正すべき | |
129 | - return image; | |
130 | - }, cancelToken); | |
124 | + // FIXME: MemoryImage.Dispose() が正しいタイミングで呼ばれるように修正すべき | |
125 | + return image; | |
126 | + }, cancelToken); | |
131 | 127 | |
132 | - this.innerDictionary[address] = imageTask; | |
128 | + this.innerDictionary[address] = imageTask; | |
133 | 129 | |
134 | - return imageTask; | |
135 | - } | |
130 | + return imageTask; | |
136 | 131 | } |
137 | - finally { this.lockObject.ExitWriteLock(); } | |
138 | 132 | } |
139 | - finally { this.lockObject.ExitUpgradeableReadLock(); } | |
140 | 133 | }, cancelToken).Unwrap(); |
141 | 134 | } |
142 | 135 | |
143 | 136 | public MemoryImage TryGetFromCache(string address) |
144 | 137 | { |
145 | - this.lockObject.EnterReadLock(); | |
146 | - try | |
138 | + lock (this.lockObject) | |
147 | 139 | { |
148 | 140 | if (!this.innerDictionary.ContainsKey(address)) |
149 | 141 | return null; |
@@ -154,18 +146,15 @@ namespace OpenTween | ||
154 | 146 | |
155 | 147 | return imageTask.Result; |
156 | 148 | } |
157 | - finally { this.lockObject.ExitReadLock(); } | |
158 | 149 | } |
159 | 150 | |
160 | 151 | public void CancelAsync() |
161 | 152 | { |
162 | - this.lockObject.EnterWriteLock(); | |
163 | - try | |
153 | + lock (this.lockObject) | |
164 | 154 | { |
165 | 155 | this.cancelTokenSource.Cancel(); |
166 | 156 | this.cancelTokenSource = new CancellationTokenSource(); |
167 | 157 | } |
168 | - finally { this.lockObject.ExitWriteLock(); } | |
169 | 158 | } |
170 | 159 | |
171 | 160 | protected virtual void Dispose(bool disposing) |
@@ -176,8 +165,7 @@ namespace OpenTween | ||
176 | 165 | { |
177 | 166 | this.CancelAsync(); |
178 | 167 | |
179 | - this.lockObject.EnterWriteLock(); | |
180 | - try | |
168 | + lock (this.lockObject) | |
181 | 169 | { |
182 | 170 | foreach (var item in this.innerDictionary) |
183 | 171 | { |
@@ -189,9 +177,6 @@ namespace OpenTween | ||
189 | 177 | this.innerDictionary.Clear(); |
190 | 178 | this.cancelTokenSource.Dispose(); |
191 | 179 | } |
192 | - finally { this.lockObject.ExitWriteLock(); } | |
193 | - | |
194 | - this.lockObject.Dispose(); | |
195 | 180 | } |
196 | 181 | } |
197 | 182 |
@@ -2,7 +2,6 @@ | ||
2 | 2 | |
3 | 3 | ==== Ver 1.1.0-beta1(2013/xx/xx) |
4 | 4 | * NEW: タブの表示位置を画面上部に変更可能に (thx @aokomoriuta!) |
5 | - * CHG: アイコン読み込み処理の速度改善 | |
6 | 5 | * FIX: スペースが含まれているURLをブラウザで開こうとするとURLが分断されて複数のタブが開いてしまう問題を修正 (thx @5px!) |
7 | 6 | * FIX: 画面更新時にInvalidOperationExceptionのエラーが発生する不具合を修正 |
8 | 7 |