From e457f551bb41cdab00e7dcf295b7aec1327c8ce7 Mon Sep 17 00:00:00 2001 From: Umar Date: Tue, 28 Mar 2017 17:04:15 +0100 Subject: [PATCH] Fix DisposeQueue issue by using EventThreadCallback class Change-Id: I9266ff91c0a081333eb88283bca519f97c17826e --- plugins/dali-swig/Makefile.am | 8 +- plugins/dali-swig/SWIG/events/application-event.i | 13 +-- plugins/dali-swig/manual/cpp/callbackbase_wrap.cpp | 60 ------------ .../manual/cpp/event-thread-callback-wrap.cpp | 108 +++++++++++++++++++++ plugins/dali-swig/manual/csharp/DisposeQueue.cs | 24 +++-- .../dali-swig/manual/csharp/EventThreadCallback.cs | 75 ++++++++++++++ plugins/dali-swig/manual/csharp/ManualPINVOKE.cs | 10 +- 7 files changed, 212 insertions(+), 86 deletions(-) delete mode 100644 plugins/dali-swig/manual/cpp/callbackbase_wrap.cpp create mode 100644 plugins/dali-swig/manual/cpp/event-thread-callback-wrap.cpp create mode 100644 plugins/dali-swig/manual/csharp/EventThreadCallback.cs diff --git a/plugins/dali-swig/Makefile.am b/plugins/dali-swig/Makefile.am index 2ed3252..526fc62 100755 --- a/plugins/dali-swig/Makefile.am +++ b/plugins/dali-swig/Makefile.am @@ -23,8 +23,8 @@ if BUILD_MCS all-local: libNDalic.so NDali.dll -libNDalic.so: automatic/cpp/dali_wrap.o manual/cpp/keyboard_focus_manager_wrap.o manual/cpp/view-wrapper-impl-wrap.o manual/cpp/callbackbase_wrap.o manual/cpp/devel-property-wrap.o - g++ -shared automatic/cpp/dali_wrap.o manual/cpp/keyboard_focus_manager_wrap.o manual/cpp/view-wrapper-impl-wrap.o manual/cpp/callbackbase_wrap.o manual/cpp/devel-property-wrap.o -o libNDalic.so $(DALICORE_LIBS) $(DALIADAPTOR_LIBS) $(DALITOOLKIT_LIBS) +libNDalic.so: automatic/cpp/dali_wrap.o manual/cpp/keyboard_focus_manager_wrap.o manual/cpp/view-wrapper-impl-wrap.o manual/cpp/event-thread-callback-wrap.o manual/cpp/devel-property-wrap.o + g++ -shared automatic/cpp/dali_wrap.o manual/cpp/keyboard_focus_manager_wrap.o manual/cpp/view-wrapper-impl-wrap.o manual/cpp/event-thread-callback-wrap.o manual/cpp/devel-property-wrap.o -o libNDalic.so $(DALICORE_LIBS) $(DALIADAPTOR_LIBS) $(DALITOOLKIT_LIBS) automatic/cpp/dali_wrap.o: $(BUILT_SOURCES) g++ -c -fpic $(CXXFLAGS) $(DALICORE_CFLAGS) $(DALIADAPTOR_CFLAGS) $(DALITOOLKIT_CFLAGS) automatic/cpp/dali_wrap.cpp -o automatic/cpp/dali_wrap.o @@ -35,8 +35,8 @@ manual/cpp/keyboard_focus_manager_wrap.o: $(BUILT_SOURCES) manual/cpp/view-wrapper-impl-wrap.o: $(BUILT_SOURCES) g++ -c -fpic $(CXXFLAGS) $(DALICORE_CFLAGS) $(DALIADAPTOR_CFLAGS) $(DALITOOLKIT_CFLAGS) manual/cpp/view-wrapper-impl-wrap.cpp -o manual/cpp/view-wrapper-impl-wrap.o -manual/cpp/callbackbase_wrap.o: $(BUILT_SOURCES) - g++ -c -fpic $(CXXFLAGS) $(DALICORE_CFLAGS) $(DALIADAPTOR_CFLAGS) $(DALITOOLKIT_CFLAGS) manual/cpp/callbackbase_wrap.cpp -o manual/cpp/callbackbase_wrap.o +manual/cpp/event-thread-callback-wrap.o: $(BUILT_SOURCES) + g++ -c -fpic $(CXXFLAGS) $(DALICORE_CFLAGS) $(DALIADAPTOR_CFLAGS) $(DALITOOLKIT_CFLAGS) manual/cpp/event-thread-callback-wrap.cpp -o manual/cpp/event-thread-callback-wrap.o manual/cpp/devel-property-wrap.o: $(BUILT_SOURCES) g++ -c -fpic $(CXXFLAGS) $(DALICORE_CFLAGS) $(DALIADAPTOR_CFLAGS) $(DALITOOLKIT_CFLAGS) manual/cpp/devel-property-wrap.cpp -o manual/cpp/devel-property-wrap.o diff --git a/plugins/dali-swig/SWIG/events/application-event.i b/plugins/dali-swig/SWIG/events/application-event.i index 3db23f4..3050ae7 100644 --- a/plugins/dali-swig/SWIG/events/application-event.i +++ b/plugins/dali-swig/SWIG/events/application-event.i @@ -404,6 +404,9 @@ // Callback for Application InitSignal private void OnApplicationInit(IntPtr data) { + // Initialize DisposeQueue Singleton class. This is also required to create DisposeQueue on main thread. + DisposeQueue.Instance.Initialize(); + NUIApplicationInitEventArgs e = new NUIApplicationInitEventArgs(); // Populate all members of "e" (NUIApplicationInitEventArgs) with real data @@ -1031,16 +1034,6 @@ return ret; } - public bool AddIdle(System.Delegate func) { - System.IntPtr ip = System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate(func); - System.IntPtr ip2 = NDalicManualPINVOKE.MakeCallback(new System.Runtime.InteropServices.HandleRef(this, ip)); - - bool ret = NDalicPINVOKE.Application_AddIdle(swigCPtr, new System.Runtime.InteropServices.HandleRef(this, ip2)); - - if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve(); - return ret; - } - %} %enddef diff --git a/plugins/dali-swig/manual/cpp/callbackbase_wrap.cpp b/plugins/dali-swig/manual/cpp/callbackbase_wrap.cpp deleted file mode 100644 index 6b925d4..0000000 --- a/plugins/dali-swig/manual/cpp/callbackbase_wrap.cpp +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright (c) 2016 Samsung Electronics Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#ifndef CSHARP_CALLBACK_BASE -#define CSHARP_CALLBACK_BASE -#endif - -#include "common.h" - -#ifdef __cplusplus -extern "C" { -#endif - -SWIGEXPORT void * SWIGSTDCALL CSharp_Dali_MakeCallback(void * jarg1) { - void * jresult ; - void (*arg1)(void) = (void (*)(void)) 0 ; - Dali::CallbackBase *result = 0 ; - - arg1 = (void (*)(void))jarg1; - { - try { - result = (Dali::CallbackBase *)Dali::MakeCallback(arg1); - } catch (std::out_of_range& e) { - { - SWIG_CSharpException(SWIG_IndexError, const_cast(e.what())); return 0; - }; - } catch (std::exception& e) { - { - SWIG_CSharpException(SWIG_RuntimeError, const_cast(e.what())); return 0; - }; - } catch (...) { - { - SWIG_CSharpException(SWIG_UnknownError, "unknown error"); return 0; - }; - } - } - - jresult = (void *)result; - - return jresult; -} - -#ifdef __cplusplus -} -#endif - diff --git a/plugins/dali-swig/manual/cpp/event-thread-callback-wrap.cpp b/plugins/dali-swig/manual/cpp/event-thread-callback-wrap.cpp new file mode 100644 index 0000000..736c697 --- /dev/null +++ b/plugins/dali-swig/manual/cpp/event-thread-callback-wrap.cpp @@ -0,0 +1,108 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#ifndef CSHARP_EVENT_THREAD_CALLBACK +#define CSHARP_EVENT_THREAD_CALLBACK +#endif + +#include "common.h" +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef void (SWIGSTDCALL* SWIG_CallbackMakeCallback)(void); + +SWIGEXPORT void * SWIGSTDCALL CSharp_Dali_new_EventThreadCallback(SWIG_CallbackMakeCallback callbackOnMakeCallback) { + void * jresult ; + void (*arg1)(void) = (void (*)(void)) 0 ; + Dali::EventThreadCallback *result = 0 ; + + arg1 = (void (*)(void))callbackOnMakeCallback; + { + try { + result = (Dali::EventThreadCallback *)new Dali::EventThreadCallback((Dali::CallbackBase *)Dali::MakeCallback(arg1)); + } catch (std::out_of_range& e) { + { + SWIG_CSharpException(SWIG_IndexError, const_cast(e.what())); return 0; + }; + } catch (std::exception& e) { + { + SWIG_CSharpException(SWIG_RuntimeError, const_cast(e.what())); return 0; + }; + } catch (...) { + { + SWIG_CSharpException(SWIG_UnknownError, "unknown error"); return 0; + }; + } + } + jresult = (void *)result; + return jresult; +} + +SWIGEXPORT void SWIGSTDCALL CSharp_Dali_delete_EventThreadCallback(void * jarg1) { + Dali::EventThreadCallback *arg1 = (Dali::EventThreadCallback *) 0 ; + + arg1 = (Dali::EventThreadCallback *)jarg1; + { + try { + delete arg1; + } catch (std::out_of_range& e) { + { + SWIG_CSharpException(SWIG_IndexError, const_cast(e.what())); return ; + }; + } catch (std::exception& e) { + { + SWIG_CSharpException(SWIG_RuntimeError, const_cast(e.what())); return ; + }; + } catch (...) { + { + SWIG_CSharpException(SWIG_UnknownError, "unknown error"); return ; + }; + } + } +} + + +SWIGEXPORT void SWIGSTDCALL CSharp_Dali_EventThreadCallback_Trigger(void * jarg1) { + Dali::EventThreadCallback *arg1 = (Dali::EventThreadCallback *) 0 ; + + arg1 = (Dali::EventThreadCallback *)jarg1; + { + try { + (arg1)->Trigger(); + } catch (std::out_of_range& e) { + { + SWIG_CSharpException(SWIG_IndexError, const_cast(e.what())); return ; + }; + } catch (std::exception& e) { + { + SWIG_CSharpException(SWIG_RuntimeError, const_cast(e.what())); return ; + }; + } catch (...) { + { + SWIG_CSharpException(SWIG_UnknownError, "unknown error"); return ; + }; + } + } +} + +#ifdef __cplusplus +} +#endif //CSHARP_EVENT_THREAD_CALLBACK + diff --git a/plugins/dali-swig/manual/csharp/DisposeQueue.cs b/plugins/dali-swig/manual/csharp/DisposeQueue.cs index a62843b..35acafd 100644 --- a/plugins/dali-swig/manual/csharp/DisposeQueue.cs +++ b/plugins/dali-swig/manual/csharp/DisposeQueue.cs @@ -12,19 +12,16 @@ using System.Collections.Generic; namespace Dali { - public class DisposeQueue { - private static DisposeQueue _disposableQueue = new DisposeQueue(); + private static readonly DisposeQueue _disposableQueue = new DisposeQueue(); private List _disposables = new List(); private Object _listLock = new object(); - private delegate int ProcessDisposablesDelegate(IntPtr ptr); - private ProcessDisposablesDelegate _disposequeueProcessDisposablesDelegate; + private EventThreadCallback _eventThreadCallback; + private EventThreadCallback.CallbackDelegate _disposeQueueProcessDisposablesDelegate; private DisposeQueue() { - _disposequeueProcessDisposablesDelegate = new ProcessDisposablesDelegate(ProcessDisposables); - Application.Instance.AddIdle(_disposequeueProcessDisposablesDelegate); } ~DisposeQueue() @@ -36,17 +33,25 @@ namespace Dali get { return _disposableQueue; } } + public void Initialize() + { + _disposeQueueProcessDisposablesDelegate = new EventThreadCallback.CallbackDelegate(ProcessDisposables); + _eventThreadCallback = new EventThreadCallback(_disposeQueueProcessDisposablesDelegate); + } + public void Add(IDisposable disposable) { - lock(_listLock) + lock (_listLock) { _disposables.Add(disposable); } + + _eventThreadCallback.Trigger(); } - private int ProcessDisposables(IntPtr ptr) + private void ProcessDisposables() { - lock(_listLock) + lock (_listLock) { foreach (IDisposable disposable in _disposables) { @@ -54,7 +59,6 @@ namespace Dali } _disposables.Clear(); } - return 0; } } } diff --git a/plugins/dali-swig/manual/csharp/EventThreadCallback.cs b/plugins/dali-swig/manual/csharp/EventThreadCallback.cs new file mode 100644 index 0000000..a9434a4 --- /dev/null +++ b/plugins/dali-swig/manual/csharp/EventThreadCallback.cs @@ -0,0 +1,75 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +namespace Dali +{ + + public class EventThreadCallback : global::System.IDisposable + { + private global::System.Runtime.InteropServices.HandleRef swigCPtr; + protected bool swigCMemOwn; + public delegate void CallbackDelegate(); + + internal EventThreadCallback(global::System.IntPtr cPtr, bool cMemoryOwn) + { + swigCMemOwn = cMemoryOwn; + swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr); + } + + internal static global::System.Runtime.InteropServices.HandleRef getCPtr(EventThreadCallback obj) + { + return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr; + } + + ~EventThreadCallback() + { + Dispose(); + } + + public virtual void Dispose() + { + lock (this) + { + if (swigCPtr.Handle != global::System.IntPtr.Zero) + { + if (swigCMemOwn) + { + swigCMemOwn = false; + NDalicManualPINVOKE.delete_EventThreadCallback(swigCPtr); + } + swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero); + } + global::System.GC.SuppressFinalize(this); + } + } + + public EventThreadCallback(CallbackDelegate func) : this(NDalicManualPINVOKE.new_EventThreadCallback(func), true) + { + if (NDalicPINVOKE.SWIGPendingException.Pending) + throw NDalicPINVOKE.SWIGPendingException.Retrieve(); + } + + public void Trigger() + { + NDalicManualPINVOKE.EventThreadCallback_Trigger(swigCPtr); + if (NDalicPINVOKE.SWIGPendingException.Pending) + throw NDalicPINVOKE.SWIGPendingException.Retrieve(); + } + + } + +} diff --git a/plugins/dali-swig/manual/csharp/ManualPINVOKE.cs b/plugins/dali-swig/manual/csharp/ManualPINVOKE.cs index 6694174..a7ce0a2 100755 --- a/plugins/dali-swig/manual/csharp/ManualPINVOKE.cs +++ b/plugins/dali-swig/manual/csharp/ManualPINVOKE.cs @@ -184,8 +184,14 @@ namespace Dali [global::System.Runtime.InteropServices.DllImport("NDalic", EntryPoint="CSharp_Dali_ViewWrapperImpl_ApplyThemeStyle")] public static extern void ViewWrapperImpl_ApplyThemeStyle(global::System.Runtime.InteropServices.HandleRef jarg1); - [global::System.Runtime.InteropServices.DllImport("NDalic", EntryPoint="CSharp_Dali_MakeCallback")] - public static extern global::System.IntPtr MakeCallback(global::System.Runtime.InteropServices.HandleRef jarg1); + [global::System.Runtime.InteropServices.DllImport("NDalic", EntryPoint="CSharp_Dali_new_EventThreadCallback")] + public static extern global::System.IntPtr new_EventThreadCallback(EventThreadCallback.CallbackDelegate delegate1); + + [global::System.Runtime.InteropServices.DllImport("NDalic", EntryPoint="CSharp_Dali_delete_EventThreadCallback")] + public static extern void delete_EventThreadCallback(global::System.Runtime.InteropServices.HandleRef jarg1); + + [global::System.Runtime.InteropServices.DllImport("NDalic", EntryPoint="CSharp_Dali_EventThreadCallback_Trigger")] + public static extern void EventThreadCallback_Trigger(global::System.Runtime.InteropServices.HandleRef jarg1); [global::System.Runtime.InteropServices.DllImport("NDalic", EntryPoint="CSharp_Actor_Property_SIBLING_ORDER_get")] public static extern int Actor_Property_SIBLING_ORDER_get(); -- 2.7.4