Commit manual merge for dali-csharp.
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / DisposeQueue.cs
1 // Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
2 // PROPRIETARY/CONFIDENTIAL
3 // This software is the confidential and proprietary
4 // information of SAMSUNG ELECTRONICS ("Confidential Information"). You shall
5 // not disclose such Confidential Information and shall use it only in
6 // accordance with the terms of the license agreement you entered into with
7 // SAMSUNG ELECTRONICS.
8 //
9
10 using System;
11 using System.Collections.Generic;
12
13 namespace Tizen.NUI
14 {
15
16     internal class DisposeQueue
17     {
18         private static readonly DisposeQueue _disposableQueue = new DisposeQueue();
19         private List<IDisposable> _disposables = new List<IDisposable>();
20         private System.Object _listLock = new object();
21         private EventThreadCallback _eventThreadCallback;
22         private EventThreadCallback.CallbackDelegate _disposeQueueProcessDisposablesDelegate;
23
24         private DisposeQueue()
25         {
26         }
27
28         ~DisposeQueue()
29         {
30         }
31
32         public static DisposeQueue Instance
33         {
34             get { return _disposableQueue; }
35         }
36
37         public void Initialize()
38         {
39             _disposeQueueProcessDisposablesDelegate = new EventThreadCallback.CallbackDelegate(ProcessDisposables);
40             _eventThreadCallback = new EventThreadCallback(_disposeQueueProcessDisposablesDelegate);
41         }
42
43         public void Add(IDisposable disposable)
44         {
45             lock(_listLock)
46             {
47                 _disposables.Add(disposable);
48             }
49
50             if (_eventThreadCallback != null)
51             {
52                 _eventThreadCallback.Trigger();
53             }
54         }
55
56         private void ProcessDisposables()
57         {
58             lock(_listLock)
59             {
60                 foreach (IDisposable disposable in _disposables)
61                 {
62                     disposable.Dispose();
63                 }
64                 _disposables.Clear();
65             }
66         }
67     }
68 }