Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
MicroThreadSynchronizationContext.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Threading;
5 
6 namespace SiliconStudio.Core.MicroThreading
7 {
9  {
10  internal MicroThread MicroThread;
11 
13  {
14  this.MicroThread = microThread;
15  }
16 
18  {
19  return this;
20  }
21 
22  public override void Post(SendOrPostCallback d, object state)
23  {
24  // There is two case:
25  // 1/ We are either in normal MicroThread inside Scheduler.Step() (CurrentThread test),
26  // in which case we will directly execute the callback to avoid further processing from scheduler.
27  // Also, note that Wait() sends us event that are supposed to come back into scheduler.
28  // Note: As it will end up on the callstack, it might be better to Schedule it instead (to avoid overflow)?
29  // 2/ Otherwise, we just received an external task continuation (i.e. TaskEx.Sleep()), or a microthread triggering another,
30  // so schedule it so that it comes back in our regular scheduler.
32  {
33  d(state);
34  }
35  else if (MicroThread.State == MicroThreadState.Completed)
36  {
37  throw new InvalidOperationException("MicroThread is already completed but still posting continuations.");
38  }
39  else
40  {
41  MicroThread.ScheduleContinuation(MicroThread.ScheduleMode, d, state);
42  }
43  }
44  }
45 }
Represents an execution context managed by a Scheduler, that can cooperatively yield execution to ano...
Definition: MicroThread.cs:16
MicroThread RunningMicroThread
Gets the current running micro thread in this scheduler through Run.
Definition: Scheduler.cs:51
MicroThreadState State
Gets the state of this MicroThread.
Definition: MicroThread.cs:126
Scheduler Scheduler
Gets the scheduler associated with this MicroThread.
Definition: MicroThread.cs:120