Skip to content

Instantly share code, notes, and snippets.

namespace RStein.Async.Schedulers
{
public class IoSchedulerThreadServiceFlags
{
public IoSchedulerThreadServiceFlags()
{
ResetData();
}
public bool IsServiceThread
public class IoServiceScheduler : TaskSchedulerBase, IAsioTaskService
{
public const int REQUIRED_WORK_CANCEL_TOKEN_VALUE = 1;
public const int POLLONE_RUNONE_MAX_TASKS = 1;
public const int UNLIMITED_MAX_TASKS = -1;
private readonly ThreadLocal<IoSchedulerThreadServiceFlags> m_isServiceThreadFlags;
private readonly CancellationTokenSource m_stopCancelTokenSource;
private readonly BlockingCollection<Task> m_tasks;
private readonly object m_workLockObject;
using System;
using System.Threading.Tasks;
namespace RStein.Async.Schedulers
{
public interface IAsioTaskService : IDisposable
{
Task Dispatch(Action action);
Task Dispatch(Func<Task> function);
[TestMethod]
[ExpectedException(typeof (ObjectDisposedException))]
public void QueueTask_When_TaskScheduler_Disposed_Then_Throws_ObjectDisposedException()
{
var dummyTask = new Task(() => {});
Scheduler.Dispose();
Scheduler.QueueTask(dummyTask);
}
[TestMethod]
public async Task WithTaskFactory_When_Tasks_Are_Queued_Then_All_Tasks_Are_Executed()
{
const int NUMBER_OF_TASKS = 8096;
int numberOfTasksExecuted = 0;
var tasks = Enumerable.Range(0, NUMBER_OF_TASKS)
.Select(_ => TestTaskFactory.StartNew(() => Interlocked.Increment(ref numberOfTasksExecuted))).ToArray();
[TestMethod]
public async Task WithTaskFactory_When_One_Task_Is_Queued_Then_Task_is_Executed()
{
bool wasTaskExecuted = false;
await TestTaskFactory.StartNew(() => wasTaskExecuted = true);
Assert.IsTrue(wasTaskExecuted);
}
protected override ITaskScheduler Scheduler
{
get
{
return m_scheduler;
}
}
protected override IProxyScheduler ProxyScheduler
{
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace RStein.Async.Schedulers
{
public class CurrentThreadScheduler : TaskSchedulerBase
{
private const int MAXIMUM_CONCURRENCY_LEVEL = 1;
public override int MaximumConcurrencyLevel
@renestein
renestein / gist:135209c69de14111fcb4
Created May 26, 2014 10:08
TaskSchedulerBase.cs
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
namespace RStein.Async.Schedulers
{
public abstract class TaskSchedulerBase : ITaskScheduler
{
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace RStein.Async.Schedulers
{
public interface ITaskScheduler : IDisposable
{
int MaximumConcurrencyLevel
{