summaryrefslogtreecommitdiff
path: root/modules/mono/glue/cs_files/GodotSynchronizationContext.cs
blob: eb4d0bed1cc0cd86aec40a6fc03dfc04ff546a6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;

namespace Godot
{
	public class GodotSynchronizationContext : SynchronizationContext
	{
		private readonly BlockingCollection<KeyValuePair<SendOrPostCallback, object>> queue = new BlockingCollection<KeyValuePair<SendOrPostCallback, object>>();

		public override void Post(SendOrPostCallback d, object state)
		{
			queue.Add(new KeyValuePair<SendOrPostCallback, object>(d, state));
		}

		public void ExecutePendingContinuations()
		{
			KeyValuePair<SendOrPostCallback, object> workItem;
			while (queue.TryTake(out workItem))
			{
				workItem.Key(workItem.Value);
			}
		}
	}
}