FiberSchedulerSynchronizationContext.cs
Go to the documentation of this file.
1 /*
2 
3 Author: Aaron Oneal, http://aarononeal.info
4 
5 Copyright (c) 2012 Spicy Pixel, http://spicypixel.com
6 
7 Permission is hereby granted, free of charge, to any person obtaining
8 a copy of this software and associated documentation files (the
9 "Software"), to deal in the Software without restriction, including
10 without limitation the rights to use, copy, modify, merge, publish,
11 distribute, sublicense, and/or sell copies of the Software, and to
12 permit persons to whom the Software is furnished to do so, subject to
13 the following conditions:
14 
15 The above copyright notice and this permission notice shall be
16 included in all copies or substantial portions of the Software.
17 
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 
26 */
27 using System;
28 using System.Threading;
29 
30 namespace SpicyPixel.Threading
31 {
37  public class FiberSchedulerSynchronizationContext : SynchronizationContext
38  {
39  FiberScheduler scheduler;
40 
48  {
49  this.scheduler = scheduler;
50  }
51 
65  public override void Post (SendOrPostCallback d, object state)
66  {
67  // The scheduler may choose to inline this if the Post
68  // is executed from the scheduler thread.
69  Fiber.Factory.StartNew(() => {
70  d(state);
71  }, scheduler);
72  }
73 
89  public override void Send (SendOrPostCallback d, object state)
90  {
91  // Force inlining if the threads match.
92  if(scheduler.SchedulerThread == Thread.CurrentThread)
93  {
94  d(state);
95  return;
96  }
97 
98  // FIXME: This could block indefinitely if the scheduler goes down
99  // before executing the task. Need another wait handle here or
100  // better approach. Maybe add a WaitHandle to the fiber itself or
101  // add Join().
102 
103  // The threads don't match, so queue the action
104  // and wait for it to complete.
105  ManualResetEvent wait = new ManualResetEvent(false);
106  Fiber.Factory.StartNew(() => {
107  d(state);
108  wait.Set();
109  }, scheduler);
110  wait.WaitOne();
111  }
112  }
113 }
Schedules fibers for execution.
Fiber StartNew(IEnumerator coroutine)
Start executing a new fiber using the default scheduler on the thread.
static FiberFactory Factory
Gets the default factory for creating fibers.
Definition: Fiber.cs:120
A Fiber is a lightweight means of scheduling work that enables multiple units of processing to execut...
override void Send(SendOrPostCallback d, object state)
Dispatches an synchronous message to a synchronization context (the FiberScheduler).
Thread SchedulerThread
Gets the thread the scheduler is running on.
Fiber scheduler synchronization context to support task synchronization across schedulers or other sy...
FiberSchedulerSynchronizationContext(FiberScheduler scheduler)
Initializes a new instance of the SpicyPixel.Threading.FiberSchedulerSynchronizationContext class...
override void Post(SendOrPostCallback d, object state)
Dispatches an asynchronous message to a synchronization context (the FiberScheduler).