Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
f43398f3
R
roslyn
项目概览
lwm1986
/
roslyn
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
roslyn
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
f43398f3
编写于
10月 13, 2016
作者:
C
CyrusNajmabadi
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Move to TaskCompletionSource in AsyncLazy now that we're no longer on .net 4.5
上级
42bb20e5
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
20 addition
and
60 deletion
+20
-60
src/Workspaces/Core/Portable/Utilities/AsyncLazy`1.cs
src/Workspaces/Core/Portable/Utilities/AsyncLazy`1.cs
+20
-60
未找到文件。
src/Workspaces/Core/Portable/Utilities/AsyncLazy`1.cs
浏览文件 @
f43398f3
...
...
@@ -101,7 +101,6 @@ public AsyncLazy(Func<CancellationToken, Task<T>> asynchronousComputeFunction, b
public
AsyncLazy
(
Func
<
CancellationToken
,
Task
<
T
>>
asynchronousComputeFunction
,
Func
<
CancellationToken
,
T
>
synchronousComputeFunction
,
bool
cacheResult
)
{
Contract
.
ThrowIfNull
(
asynchronousComputeFunction
);
_asynchronousComputeFunction
=
asynchronousComputeFunction
;
_synchronousComputeFunction
=
synchronousComputeFunction
;
_cacheResult
=
cacheResult
;
...
...
@@ -262,10 +261,7 @@ public override T GetValue(CancellationToken cancellationToken)
catch
(
Exception
ex
)
{
// We faulted for some unknown reason. We should simply fault everything.
TaskCompletionSource
<
T
>
tcs
=
new
TaskCompletionSource
<
T
>();
tcs
.
SetException
(
ex
);
CompleteWithTask
(
tcs
.
Task
,
CancellationToken
.
None
);
CompleteWithTask
(
Task
.
FromException
<
T
>(
ex
),
CancellationToken
.
None
);
throw
;
}
...
...
@@ -379,11 +375,7 @@ private void StartAsynchronousComputation(AsynchronousComputationToStart computa
//
// Note: we want to do this before we do the .ContinueWith below. That way,
// when the async call to CompleteWithTask runs, it sees that we've already
// completed and can bail immediately. If we were to do this after we
// kicked off the async work, then we'd have the chance that both would
// run concurrently and we'd have a higher change of hitting the race condition
// of calling AsyncMethodBuilder.SetResult simultaneously (and thus having
// the InvalidOperationException that we have to ignore).
// completed and can bail immediately.
if
(
requestToCompleteSynchronously
!=
null
&&
task
.
IsCompleted
)
{
using
(
TakeLock
(
CancellationToken
.
None
))
...
...
@@ -533,25 +525,15 @@ private sealed class Request
private
CancellationToken
_cancellationToken
;
private
CancellationTokenRegistration
_cancellationTokenRegistration
;
// We use a AsyncTaskMethodBuilder so we have the ability to cancel the task with a given cancellation token
// TODO: remove this once we're on .NET 4.6 and can move back to using TaskCompletionSource.
// WARNING: this is a mutable struct, and thus cannot be made readonly
private
AsyncTaskMethodBuilder
<
T
>
_taskBuilder
;
private
TaskCompletionSource
<
T
>
_taskCompletionSource
;
public
Request
()
{
// .Task on AsyncTaskMethodBuilder is lazily created in a non-synchronized way, so we must request it
// once before we start doing fancy stuff
var
ignored
=
_taskBuilder
.
Task
;
_taskCompletionSource
=
new
TaskCompletionSource
<
T
>();
}
public
Task
<
T
>
Task
{
get
{
return
_taskBuilder
.
Task
;
}
}
public
Task
<
T
>
Task
=>
_taskCompletionSource
.
Task
;
public
void
RegisterForCancellation
(
Action
<
object
>
callback
,
CancellationToken
cancellationToken
)
{
...
...
@@ -575,31 +557,24 @@ public void CompleteFromTaskSynchronously(Task<T> task)
// is already completed. The belief is that the race is somewhere between rare to impossible, and
// so we'll do a quick check to see if the task is already completed or otherwise just give it a shot
// and catch it if it fails
if
(
_taskBuilder
.
Task
.
IsCompleted
)
if
(
this
.
Task
.
IsCompleted
)
{
return
;
}
try
// As an optimization, we'll cancel the request even we did get a value for it.
// That way things abort sooner.
if
(
task
.
IsCanceled
||
_cancellationToken
.
IsCancellationRequested
)
{
// As an optimization, we'll cancel the request even we did get a value for it.
// That way things abort sooner.
if
(
task
.
IsCanceled
||
_cancellationToken
.
IsCancellationRequested
)
{
CancelSynchronously
();
}
else
if
(
task
.
IsFaulted
)
{
_taskBuilder
.
SetException
(
task
.
Exception
);
}
else
{
_taskBuilder
.
SetResult
(
task
.
Result
);
}
CancelSynchronously
();
}
catch
(
InvalidOperationException
)
else
if
(
task
.
IsFaulted
)
{
// Something else beat us to setting the state, so bail
_taskCompletionSource
.
TrySetException
(
task
.
Exception
);
}
else
{
_taskCompletionSource
.
TrySetResult
(
task
.
Result
);
}
_cancellationTokenRegistration
.
Dispose
();
...
...
@@ -609,29 +584,14 @@ public void CancelAsynchronously()
{
// Since there could be synchronous continuations on the TaskCancellationSource, we queue this to the threadpool
// to avoid inline running of other operations.
System
.
Threading
.
Tasks
.
Task
.
Factory
.
StartNew
(
CancelSynchronously
,
CancellationToken
.
None
,
TaskCreationOptions
.
None
,
TaskScheduler
.
Default
);
System
.
Threading
.
Tasks
.
Task
.
Factory
.
StartNew
(
CancelSynchronously
,
CancellationToken
.
None
,
TaskCreationOptions
.
None
,
TaskScheduler
.
Default
);
}
private
void
CancelSynchronously
()
{
// AsyncTaskMethodBuilder doesn't give us Try* methods, and the Set methods may throw if the task
// is already completed. The belief is that the race is somewhere between rare to impossible, and
// so we'll do a quick check to see if the task is already completed or otherwise just give it a shot
// and catch it if it fails
if
(
_taskBuilder
.
Task
.
IsCompleted
)
{
return
;
}
try
{
_taskBuilder
.
SetException
(
new
OperationCanceledException
(
_cancellationToken
));
}
catch
(
InvalidOperationException
)
{
// Something else beat us to setting the state, so bail
}
_taskCompletionSource
.
TrySetCanceled
(
_cancellationToken
);
}
}
}
}
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录