123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Text;
- using System.Threading;
- namespace UniRx
- {
- // Scheduler Extension
- public static partial class Scheduler
- {
- // configurable defaults
- public static class DefaultSchedulers
- {
- static IScheduler constantTime;
- public static IScheduler ConstantTimeOperations
- {
- get
- {
- return constantTime ?? (constantTime = Scheduler.Immediate);
- }
- set
- {
- constantTime = value;
- }
- }
- static IScheduler tailRecursion;
- public static IScheduler TailRecursion
- {
- get
- {
- return tailRecursion ?? (tailRecursion = Scheduler.Immediate);
- }
- set
- {
- tailRecursion = value;
- }
- }
- static IScheduler iteration;
- public static IScheduler Iteration
- {
- get
- {
- return iteration ?? (iteration = Scheduler.CurrentThread);
- }
- set
- {
- iteration = value;
- }
- }
- static IScheduler timeBasedOperations;
- public static IScheduler TimeBasedOperations
- {
- get
- {
- #if UniRxLibrary
- return timeBasedOperations ?? (timeBasedOperations = Scheduler.ThreadPool);
- #else
- return timeBasedOperations ?? (timeBasedOperations = Scheduler.MainThread); // MainThread as default for TimeBased Operation
- #endif
- }
- set
- {
- timeBasedOperations = value;
- }
- }
- static IScheduler asyncConversions;
- public static IScheduler AsyncConversions
- {
- get
- {
- #if WEB_GL
- // WebGL does not support threadpool
- return asyncConversions ?? (asyncConversions = Scheduler.MainThread);
- #else
- return asyncConversions ?? (asyncConversions = Scheduler.ThreadPool);
- #endif
- }
- set
- {
- asyncConversions = value;
- }
- }
- public static void SetDotNetCompatible()
- {
- ConstantTimeOperations = Scheduler.Immediate;
- TailRecursion = Scheduler.Immediate;
- Iteration = Scheduler.CurrentThread;
- TimeBasedOperations = Scheduler.ThreadPool;
- AsyncConversions = Scheduler.ThreadPool;
- }
- }
- // utils
- public static DateTimeOffset Now
- {
- get { return DateTimeOffset.UtcNow; }
- }
- public static TimeSpan Normalize(TimeSpan timeSpan)
- {
- return timeSpan >= TimeSpan.Zero ? timeSpan : TimeSpan.Zero;
- }
- public static IDisposable Schedule(this IScheduler scheduler, DateTimeOffset dueTime, Action action)
- {
- return scheduler.Schedule(dueTime - scheduler.Now, action);
- }
- public static IDisposable Schedule(this IScheduler scheduler, Action<Action> action)
- {
- // InvokeRec1
- var group = new CompositeDisposable(1);
- var gate = new object();
- Action recursiveAction = null;
- recursiveAction = () => action(() =>
- {
- var isAdded = false;
- var isDone = false;
- var d = default(IDisposable);
- d = scheduler.Schedule(() =>
- {
- lock (gate)
- {
- if (isAdded)
- group.Remove(d);
- else
- isDone = true;
- }
- recursiveAction();
- });
- lock (gate)
- {
- if (!isDone)
- {
- group.Add(d);
- isAdded = true;
- }
- }
- });
- group.Add(scheduler.Schedule(recursiveAction));
- return group;
- }
- public static IDisposable Schedule(this IScheduler scheduler, TimeSpan dueTime, Action<Action<TimeSpan>> action)
- {
- // InvokeRec2
- var group = new CompositeDisposable(1);
- var gate = new object();
- Action recursiveAction = null;
- recursiveAction = () => action(dt =>
- {
- var isAdded = false;
- var isDone = false;
- var d = default(IDisposable);
- d = scheduler.Schedule(dt, () =>
- {
- lock (gate)
- {
- if (isAdded)
- group.Remove(d);
- else
- isDone = true;
- }
- recursiveAction();
- });
- lock (gate)
- {
- if (!isDone)
- {
- group.Add(d);
- isAdded = true;
- }
- }
- });
- group.Add(scheduler.Schedule(dueTime, recursiveAction));
- return group;
- }
- public static IDisposable Schedule(this IScheduler scheduler, DateTimeOffset dueTime, Action<Action<DateTimeOffset>> action)
- {
- // InvokeRec3
- var group = new CompositeDisposable(1);
- var gate = new object();
- Action recursiveAction = null;
- recursiveAction = () => action(dt =>
- {
- var isAdded = false;
- var isDone = false;
- var d = default(IDisposable);
- d = scheduler.Schedule(dt, () =>
- {
- lock (gate)
- {
- if (isAdded)
- group.Remove(d);
- else
- isDone = true;
- }
- recursiveAction();
- });
- lock (gate)
- {
- if (!isDone)
- {
- group.Add(d);
- isAdded = true;
- }
- }
- });
- group.Add(scheduler.Schedule(dueTime, recursiveAction));
- return group;
- }
- }
- }
|