Skip to content

Instantly share code, notes, and snippets.

@bojanrajkovic
Created June 19, 2012 22:58

Revisions

  1. bojanrajkovic revised this gist Jun 19, 2012. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions output.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    bojanrajkovic@dynamic-033 ~/C/O/G/G/b/Debug $ mono --debug GCDTest.exe
    Hello, world.
    Is thread pool thread: False
    Dispatch queue: org.mono.tpl_dispatch_queue

    Hello, world from non-GCD task.
    Is thread pool thread: True
    Dispatch queue: com.apple.root.default-overcommit-priority
  2. bojanrajkovic revised this gist Jun 19, 2012. 1 changed file with 9 additions and 1 deletion.
    10 changes: 9 additions & 1 deletion GCDTest.cs
    Original file line number Diff line number Diff line change
    @@ -19,12 +19,20 @@ public static void Main (string[] args)
    Console.WriteLine ("Is thread pool thread: {0}", Thread.CurrentThread.IsThreadPoolThread);
    Console.WriteLine ("Dispatch queue: {0}", DispatchQueue.CurrentQueue.Label);
    }).Wait (Timeout.Infinite);

    Console.WriteLine ();

    Task.Factory.StartNew (() => {
    Console.WriteLine ("Hello, world from non-GCD task.");
    Console.WriteLine ("Is thread pool thread: {0}", Thread.CurrentThread.IsThreadPoolThread);
    Console.WriteLine ("Dispatch queue: {0}", DispatchQueue.CurrentQueue.Label);
    }).Wait (Timeout.Infinite);
    }
    }

    public class GrandCentralDispatchTaskScheduler : TaskScheduler
    {
    DispatchQueue gcdDispatchQueue = DispatchQueue.DefaultGlobalQueue;
    static readonly DispatchQueue gcdDispatchQueue = new DispatchQueue ("org.mono.tpl_dispatch_queue");

    protected override System.Collections.Generic.IEnumerable<Task> GetScheduledTasks ()
    {
  3. bojanrajkovic revised this gist Jun 19, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion GCDTest.cs
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@

    using System;
    using System.Threading.Tasks;
    using MonoMac.CoreFoundation;
    @@ -18,6 +17,7 @@ public static void Main (string[] args)
    tf.StartNew (() => {
    Console.WriteLine ("Hello, world.");
    Console.WriteLine ("Is thread pool thread: {0}", Thread.CurrentThread.IsThreadPoolThread);
    Console.WriteLine ("Dispatch queue: {0}", DispatchQueue.CurrentQueue.Label);
    }).Wait (Timeout.Infinite);
    }
    }
  4. bojanrajkovic created this gist Jun 19, 2012.
    55 changes: 55 additions & 0 deletions GCDTest.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@

    using System;
    using System.Threading.Tasks;
    using MonoMac.CoreFoundation;
    using MonoMac.AppKit;
    using System.Threading;

    namespace GCDTest
    {
    class MainClass
    {
    public static void Main (string[] args)
    {
    NSApplication.Init ();

    var gcds = new GrandCentralDispatchTaskScheduler ();
    var tf = new TaskFactory (gcds);
    tf.StartNew (() => {
    Console.WriteLine ("Hello, world.");
    Console.WriteLine ("Is thread pool thread: {0}", Thread.CurrentThread.IsThreadPoolThread);
    }).Wait (Timeout.Infinite);
    }
    }

    public class GrandCentralDispatchTaskScheduler : TaskScheduler
    {
    DispatchQueue gcdDispatchQueue = DispatchQueue.DefaultGlobalQueue;

    protected override System.Collections.Generic.IEnumerable<Task> GetScheduledTasks ()
    {
    throw new NotImplementedException ();
    }

    protected override void QueueTask (Task task)
    {
    gcdDispatchQueue.DispatchAsync (delegate {
    TryExecuteTaskInline (task, false);
    });
    }

    // TODO: Tasks cannot be dequeued.
    protected override bool TryDequeue (Task task)
    {
    return false;
    }

    protected override bool TryExecuteTaskInline (Task task, bool taskWasPreviouslyQueued)
    {
    if (taskWasPreviouslyQueued && !TryDequeue (task))
    return false;

    return TryExecuteTask (task);
    }
    }
    }