2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using System.Collections.Concurrent;
19 using System.Threading;
21 namespace Tizen.Applications
24 /// Provides a synchronization context for the Tizen application model.
26 /// <since_tizen> 3 </since_tizen>
27 public class TizenSynchronizationContext : SynchronizationContext
30 /// Initilizes a new TizenSynchronizationContext and install into the current thread.
35 /// SetSynchronizationContext(new TizenSynchronizationContext());
38 /// <since_tizen> 3 </since_tizen>
39 public static void Initialize()
41 SetSynchronizationContext(new TizenSynchronizationContext());
45 /// Dispatches an asynchronous message to a Tizen main loop.
47 /// <param name="d"><see cref="System.Threading.SendOrPostCallback"/>The SendOrPostCallback delegate to call.</param>
48 /// <param name="state"><see cref="System.Object"/>The object passed to the delegate.</param>
50 /// The post method starts an asynchronous request to post a message.</remarks>
51 /// <since_tizen> 3 </since_tizen>
52 public override void Post(SendOrPostCallback d, object state)
54 GSourceManager.Post(() =>
61 /// Dispatches a synchronous message to a Tizen main loop.
63 /// <param name="d"><see cref="System.Threading.SendOrPostCallback"/>The SendOrPostCallback delegate to call.</param>
64 /// <param name="state"><see cref="System.Object"/>The object passed to the delegate.</param>
66 /// The send method starts a synchronous request to send a message.</remarks>
67 /// <since_tizen> 3 </since_tizen>
68 public override void Send(SendOrPostCallback d, object state)
70 var mre = new ManualResetEvent(false);
72 GSourceManager.Post(() =>
94 private static class GSourceManager
96 private static Interop.Glib.GSourceFunc _wrapperHandler;
97 private static Object _transactionLock;
98 private static ConcurrentDictionary<int, Action> _handlerMap;
99 private static int _transactionId;
101 static GSourceManager()
103 _wrapperHandler = new Interop.Glib.GSourceFunc(Handler);
104 _transactionLock = new Object();
105 _handlerMap = new ConcurrentDictionary<int, Action>();
109 public static void Post(Action action)
112 lock (_transactionLock)
114 id = _transactionId++;
116 _handlerMap.TryAdd(id, action);
117 Interop.Glib.IdleAdd(_wrapperHandler, (IntPtr)id);
120 private static bool Handler(IntPtr userData)
122 int key = (int)userData;
123 if (_handlerMap.ContainsKey(key))
126 _handlerMap.TryRemove(key, out action);