Custom TaskScheduler, SynchronizationContext?

async is waiting for support inside the participants

I am porting Akka lib actor to .NET ( https://github.com/rogeralsing/Pigeon ) I want to add async / await support inside my members.

This gives me some problems right now, because if I use the default scheduler, the wait for the continuation will be executed without respect for the boundaries of the concurrency participants. Meaning, continuation can be performed when the actor processes the message, as this will lead to the fact that two threads access the internal state of the actors at the same time.

If I could somehow plan tasks for my own member users and complete the task in the mailbox run, this would solve the problem.

 public class SomeActor : UntypedActor
 {
      protected override OnReceive(object message)
      {
           if (message is SomeMessage)
           {
              DoAsyncStuff();
           }
      }

      private async void DoAsyncStuff()
      {
          var res = await something..
          //this code will not respect the actor concurrency boundary
          //since it can run at the same time as OnReceive
          Console.WriteLine(res);
      }
 }

, , , . , . - :

public class ActorTaskScheduler : TaskScheduler
{
    protected override void QueueTask(Task task)
    {            
        var self = ActorCell.Current.Self;
        self.Tell(new ActorTask(task), ActorRef.NoSender);
    }

ActorTask . .

, . TaskScheduler? ? , , , .

?

+3
1

TaskScheduler?

"" - , . , "".

, , , .

-? .

, ConcurrentExclusiveSchedulerPair.ExclusiveScheduler. , ; , ExclusiveScheduler .

"" , , , . , . , , thread statics, , - Current, SynchronizationContext TaskScheduler.

+7

All Articles