private void InvokeAndComplete (Action<T> act, TaskCompletionSource<byte> promise)
{
try {
act (internalValue);
} finally {
// some value we will ignore
promise.SetResult (1);
}
}
public Task Alter (Action<T> alterAction)
{
TaskCompletionSource<byte> promise = new TaskCompletionSource<byte> ();
Task localLastTask;
do {
// Not sure if Volatile.Read is necessary
localLastTask = Volatile.Read (ref lastTask);
} while (Interlocked.CompareExchange (ref lastTask, promise.Task, localLastTask) != localLastTask);
// only this thread has a reference to the object referenced by localLastTask
return localLastTask.ContinueWith ((currentTask) => {
InvokeAndComplete (alterAction, promise);
}, taskScheduler);
}
private void InvokeAndComplete (Action<T> act, TaskCompletionSource<byte> promise)
{
try {
act (internalValue);
} finally {
// some value we will ignore
promise.SetResult (1);
}
}
public Task Alter (Action<T> alterAction)
{
TaskCompletionSource<byte> promise = new TaskCompletionSource<byte> ();
Task localLastTask;
do {
// Not sure if Volatile.Read is necessary
localLastTask = Volatile.Read (ref lastTask);
} while (Interlocked.CompareExchange (ref lastTask, promise.Task, localLastTask) != localLastTask);
return localLastTask.ContinueWith ((currentTask) => {
InvokeAndComplete (alterAction, promise);
}, taskScheduler);
}
private void InvokeAndComplete (Action<T> act, TaskCompletionSource<byte> promise)
{
try {
act (internalValue);
} finally {
// some value we will ignore
promise.SetResult (1);
}
}
public Task Alter (Action<T> alterAction)
{
TaskCompletionSource<byte> promise = new TaskCompletionSource<byte> ();
Task localLastTask;
do {
// Not sure if Volatile.Read is necessary
localLastTask = Volatile.Read (ref lastTask);
} while (Interlocked.CompareExchange (ref lastTask, promise.Task, localLastTask) != localLastTask);
// only this thread has a reference to the object referenced by localLastTask
return localLastTask.ContinueWith ((currentTask) => {
InvokeAndComplete (alterAction, promise);
}, taskScheduler);
}
Doing this now
private void InvokeAndComplete (Action<T> act, TaskCompletionSource<byte> promise)
{
try {
act (internalValue);
} finally {
// some value we will ignore
promise.SetResult (1);
}
}
public Task Alter (Action<T> alterAction)
{
TaskCompletionSource<byte> promise = new TaskCompletionSource<byte> ();
Task localLastTask;
do {
// Not sure if Volatile.Read is necessary
localLastTask = Volatile.Read (ref lastTask);
} while (Interlocked.CompareExchange (ref lastTask, promise.Task, localLastTask) != localLastTask);
return localLastTask.ContinueWith ((currentTask) => {
InvokeAndComplete (alterAction, promise);
}, taskScheduler);
}
Doing this now
private void InvokeAndComplete (Action<T> act, TaskCompletionSource<byte> promise)
{
try {
act (internalValue);
} finally {
// some value we will ignore
promise.SetResult (1);
}
}
public Task Alter (Action<T> alterAction)
{
TaskCompletionSource<byte> promise = new TaskCompletionSource<byte> ();
Task localLastTask;
do {
// Not sure if Volatile.Read is necessary
localLastTask = Volatile.Read (ref lastTask);
} while (Interlocked.CompareExchange (ref lastTask, promise.Task, localLastTask) != localLastTask);
return localLastTask.ContinueWith ((currentTask) => {
InvokeAndComplete (alterAction, promise);
}, taskScheduler);
}
- I'd like to be able to
ContinueWith
theTask task
directly, instead of having to start it within a different submittedAction
. I feel like I need to create the newTask
so that I can atomically exchange thelastTask
field's value. - Lambda capturing of local variables, partly alleviated with state objects where possible.
- How can I keep a reference to the currently executing
Task
, so I can short-circuit execution? Or should I use a poison pill to signal termination and not chain any new tasks? Interlocked
for atomicity. Should I use aConcurrentQueue
? How would I manage not having a dedicated thread and only running oneTask
at a time with aConcurrentQueue
?- Is a
Volatile.Read
oflastTask
necessary forlocalCurrent = lastTask;
?
- I'd like to be able to
ContinueWith
theTask task
directly, instead of having to start it within a different submittedAction
. I feel like I need to create the newTask
so that I can atomically exchange thelastTask
field's value. - Lambda capturing of local variables, partly alleviated with state objects where possible.
- How can I keep a reference to the currently executing
Task
, so I can short-circuit execution? Or should I use a poison pill to signal termination and not chain any new tasks? Interlocked
for atomicity. Should I use aConcurrentQueue
? How would I manage not having a dedicated thread and only running oneTask
at a time with aConcurrentQueue
?
- I'd like to be able to
ContinueWith
theTask task
directly, instead of having to start it within a different submittedAction
. I feel like I need to create the newTask
so that I can atomically exchange thelastTask
field's value. - Lambda capturing of local variables, partly alleviated with state objects where possible.
- How can I keep a reference to the currently executing
Task
, so I can short-circuit execution? Or should I use a poison pill to signal termination and not chain any new tasks? Interlocked
for atomicity. Should I use aConcurrentQueue
? How would I manage not having a dedicated thread and only running oneTask
at a time with aConcurrentQueue
?- Is a
Volatile.Read
oflastTask
necessary forlocalCurrent = lastTask;
?
Loading
lang-cs