tizen 2.4 release
[framework/web/wrt-commons.git] / modules / event / include / dpl / event / event_support.h
old mode 100755 (executable)
new mode 100644 (file)
similarity index 85%
rename from modules_wearable/event/include/dpl/event/event_support.h
rename to modules/event/include/dpl/event/event_support.h
index c8c0539..3938a03
 #ifndef DPL_EVENT_SUPPORT_H
 #define DPL_EVENT_SUPPORT_H
 
+#include <atomic>
 #include <functional>
 #include <list>
 #include <map>
 #include <memory>
+#include <mutex>
 #include <type_traits>
 #include <vector>
 
 #include <dpl/exception.h>
 #include <dpl/thread.h>
 #include <dpl/assert.h>
-#include <dpl/atomic.h>
-#include <dpl/mutex.h>
 #include <dpl/foreach.h>
-#include <dpl/log/log.h>
+#include <dpl/log/wrt_log.h>
 
 namespace DPL {
 namespace Event {
@@ -103,20 +103,20 @@ class EventSupport :
     DelegateList m_delegateList;
 
     // Event support operation mutex
-    Mutex m_listenerDelegateMutex;
+    std::mutex m_listenerDelegateMutex;
 
     // Dedicated instance of thread event dispatcher
     ThreadEventDispatcher m_threadEventDispatcher;
 
     // Guard destruction of event support in event handler
-    Atomic m_guardedCallInProgress;
+    std::atomic<int> m_guardedCallInProgress;
 
     // Events created by this support
     typedef std::list<GenericEventCallType *> EventCallList;
     EventCallList m_eventsList;
 
     // Events list mutex
-    Mutex m_eventListMutex;
+    std::mutex m_eventListMutex;
 
   public:
     class EventSupportData
@@ -136,7 +136,7 @@ class EventSupport :
         //      framework/thread's event queue
         WaitableEvent *m_synchronization;
 
-        Mutex m_dataMutex;
+        std::mutex m_dataMutex;
 
       public:
         EventSupportData(EventSupportType *support,
@@ -149,10 +149,10 @@ class EventSupport :
 
         ~EventSupportData()
         {
-            Mutex::ScopedLock lock(&m_dataMutex);
+            std::lock_guard<std::mutex> lock(m_dataMutex);
 
             if (!m_eventSupport) {
-                LogPedantic("EventSupport for this call does not exist");
+                WrtLogD("EventSupport for this call does not exist");
                 return;
             }
 
@@ -171,7 +171,7 @@ class EventSupport :
                             DelegateType delegate)
         {
             {
-                Mutex::ScopedLock lock(&m_dataMutex);
+                std::lock_guard<std::mutex> lock(m_dataMutex);
 
                 if (m_eventSupport != NULL) {
                     (*m_eventSupport.*m_method)(event,
@@ -179,7 +179,7 @@ class EventSupport :
                                                 delegate,
                                                 m_synchronization);
                 } else {
-                    LogPedantic("EventSupport for this call does not "
+                    WrtLogD("EventSupport for this call does not "
                                 "exist anymore. Ignored.");
                 }
 
@@ -193,9 +193,7 @@ class EventSupport :
 
         void Reset()
         {
-            LogPedantic("Reseting my EventSupport");
-
-            Mutex::ScopedLock lock(&m_dataMutex);
+            std::lock_guard<std::mutex> lock(m_dataMutex);
             m_eventSupport = NULL;
         }
     };
@@ -206,9 +204,7 @@ class EventSupport :
                                             DelegateType delegate,
                                             WaitableEvent *waitableEvent)
     {
-        LogPedantic("Create and Register EventCall in EventSupport");
-
-        Mutex::ScopedLock lock(&m_eventListMutex);
+        std::lock_guard<std::mutex> lock(m_eventListMutex);
 
         EventSupportDataPtr supportData =
             new EventSupportData(
@@ -230,9 +226,7 @@ class EventSupport :
 
     void RemoveEventCall(typename EventCallList::iterator eventIterator)
     {
-        Mutex::ScopedLock lock(&m_eventListMutex);
-
-        LogPedantic("Removing event call from EventSupport");
+        std::lock_guard<std::mutex> lock(m_eventListMutex);
 
         m_eventsList.erase(eventIterator);
     }
@@ -241,8 +235,6 @@ class EventSupport :
     void GuardedEventCall(const EventType &event,
                           EventListenerType *eventListener)
     {
-        LogPedantic("Guarded event listener call...");
-
         ++m_guardedCallInProgress;
 
         UNHANDLED_EXCEPTION_HANDLER_BEGIN
@@ -252,16 +244,12 @@ class EventSupport :
         UNHANDLED_EXCEPTION_HANDLER_END
 
         -- m_guardedCallInProgress;
-
-        LogPedantic("Guarded event listener finished");
     }
 
     // Note: Reentrant metod
     void GuardedEventCall(const EventType &event,
                           DelegateType delegate)
     {
-        LogPedantic("Guarded delegate call...");
-
         ++m_guardedCallInProgress;
 
         UNHANDLED_EXCEPTION_HANDLER_BEGIN
@@ -271,8 +259,6 @@ class EventSupport :
         UNHANDLED_EXCEPTION_HANDLER_END
 
         -- m_guardedCallInProgress;
-
-        LogPedantic("Guarded delegate call finished");
     }
 
     void ReceiveAbstractEventCall(const EventType &event,
@@ -280,19 +266,17 @@ class EventSupport :
                                   DelegateType delegate,
                                   WaitableEvent *synchronization)
     {
-        LogPedantic("Received abstract event call method");
-
         Thread *targetThread;
 
         // Listener might have been removed, ensure that it still exits
         if (eventListener != NULL) {
-            Mutex::ScopedLock lock(&m_listenerDelegateMutex);
+            std::lock_guard<std::mutex> lock(m_listenerDelegateMutex);
 
             typename EventListenerList::iterator iterator =
                 m_eventListenerList.find(eventListener);
 
             if (iterator == m_eventListenerList.end()) {
-                LogPedantic("Abstract event call listener disappeared."
+                WrtLogD("Abstract event call listener disappeared."
                             "Event ignored.");
 
                 // Even though, synchronize caller if needed
@@ -307,13 +291,13 @@ class EventSupport :
             targetThread = iterator->second;
         } else {
             // Delegate might have been removed, ensure that it still exits
-            Mutex::ScopedLock lock(&m_listenerDelegateMutex);
+            std::lock_guard<std::mutex> lock(m_listenerDelegateMutex);
 
             typename DelegateList::iterator iterator =
                 m_delegateList.find(delegate);
 
             if (iterator == m_delegateList.end()) {
-                LogPedantic("Abstract event call delegate disappeared."
+                WrtLogD("Abstract event call delegate disappeared."
                             "Event ignored.");
 
                 // Even though, synchronize caller if needed
@@ -330,21 +314,21 @@ class EventSupport :
 
         // Ensure that we are now in proper thread now
         if (targetThread != Thread::GetCurrentThread()) {
-            LogPedantic("Detected event dispatching ping-pong scenario");
+            WrtLogD("Detected event dispatching ping-pong scenario");
 
             // Retry if it was not synchronized
             if (synchronization == NULL) {
                 // Cheat with event delivery
                 EmitEvent(event, EmitMode::Queued);
 
-                LogPedantic("Ping-Pong: Resent as queued event");
+                WrtLogD("Ping-Pong: Resent as queued event");
             } else {
                 // There is a problem
                 // Developer did something nasty, and we will not clean up his
                 // mess
                 synchronization->Signal();
 
-                LogPedantic("### Ping-Pong: Failed to deliver synchronized"
+                WrtLogD("### Ping-Pong: Failed to deliver synchronized"
                             "event in ping-pong scenario!");
             }
 
@@ -370,25 +354,20 @@ class EventSupport :
                    double dueTime = 0.0)
     {
         // Emit event, and retrieve later in current context to dispatch
-        std::unique_ptr<Mutex::ScopedLock> lock(
-            new Mutex::ScopedLock(&m_listenerDelegateMutex));
+        std::unique_lock<std::mutex> lock(m_listenerDelegateMutex);
 
         // Show some info
         switch (mode) {
         case EmitMode::Auto:
-            LogPedantic("Emitting AUTO event...");
             break;
 
         case EmitMode::Queued:
-            LogPedantic("Emitting QUEUED event...");
             break;
 
         case EmitMode::Blocking:
-            LogPedantic("Emitting BLOCKING event...");
             break;
 
         case EmitMode::Deffered:
-            LogPedantic("Emitting DEFFERED event...");
             break;
 
         default:
@@ -406,11 +385,9 @@ class EventSupport :
 
             if (iterator->second == NULL) {
                 // Send to main thread
-                LogPedantic("Sending event to main dispatcher");
                 dispatcher = &GetMainEventDispatcherInstance();
             } else {
                 // Setup thread dispatcher, and send to proper thread
-                LogPedantic("Sending event to thread dispatcher");
                 m_threadEventDispatcher.SetThread(iterator->second);
                 dispatcher = &m_threadEventDispatcher;
             }
@@ -475,8 +452,6 @@ class EventSupport :
             }
         }
 
-        LogPedantic("Added event to dispatchers");
-
         // Emit to all delegates
         FOREACH(iterator, m_delegateList)
         {
@@ -485,11 +460,9 @@ class EventSupport :
 
             if (iterator->second == NULL) {
                 // Send to main thread
-                LogPedantic("Sending event to main dispatcher");
                 dispatcher = &GetMainEventDispatcherInstance();
             } else {
                 // Setup thread dispatcher, and send to proper thread
-                LogPedantic("Sending event to thread dispatcher");
                 m_threadEventDispatcher.SetThread(iterator->second);
                 dispatcher = &m_threadEventDispatcher;
             }
@@ -562,16 +535,12 @@ class EventSupport :
             }
         }
 
-        LogPedantic("Added event to dispatchers");
-
         // Leave listeners lock in case of blocking call
         if (!synchronizationBarrier.empty()) {
-            LogPedantic("Leaving lock due to existing barrier");
-            lock.reset();
+            WrtLogD("Leaving lock due to existing barrier");
+            lock.unlock();
         }
 
-        LogPedantic("Size of barrier: " << synchronizationBarrier.size());
-
         // Synchronize with barrier
         // TODO: Implement generic WaitForAllMultipleHandles call
         while (!synchronizationBarrier.empty()) {
@@ -609,12 +578,7 @@ class EventSupport :
             }
 
             synchronizationBarrier.swap(clearedSynchronizationBarrier);
-
-            LogPedantic("Reduced size of barrier: "
-                        << synchronizationBarrier.size());
         }
-
-        LogPedantic("Event emitted");
     }
 
   public:
@@ -624,16 +588,14 @@ class EventSupport :
 
     virtual ~EventSupport()
     {
-        if (m_guardedCallInProgress != 0) {
-            LogError("The object will terminate, but guardCall is in progress, it could cause segmentation fault");
+        if( m_guardedCallInProgress != 0 ){
+            WrtLogD("The object will terminate, but guardCall is in progress, it could cause segmentation fault");
         }
 
         m_eventListenerList.clear();
         m_delegateList.clear();
 
-        Mutex::ScopedLock lock(&m_eventListMutex);
-
-        LogPedantic("Disabling events for EventSupport");
+        std::lock_guard<std::mutex> lock(m_eventListMutex);
 
         FOREACH(iterator, m_eventsList)
             (*iterator)->DisableEvent();
@@ -641,7 +603,7 @@ class EventSupport :
 
     void AddListener(EventListenerType *eventListener)
     {
-        Mutex::ScopedLock lock(&m_listenerDelegateMutex);
+        std::lock_guard<std::mutex> lock(m_listenerDelegateMutex);
 
         // Listener must not be NULL
         Assert(eventListener != NULL);
@@ -653,14 +615,11 @@ class EventSupport :
         // Add new listener, inherit dispatcher from current context
         m_eventListenerList.insert(
             std::make_pair(eventListener, Thread::GetCurrentThread()));
-
-        // Done
-        LogPedantic("Listener registered");
     }
 
     void AddListener(DelegateType delegate)
     {
-        Mutex::ScopedLock lock(&m_listenerDelegateMutex);
+        std::lock_guard<std::mutex> lock(m_listenerDelegateMutex);
 
         // Delegate must not be empty
         Assert(delegate);
@@ -671,14 +630,11 @@ class EventSupport :
         // Add new delegate, inherit dispatcher from current context
         m_delegateList.insert(
             std::make_pair(delegate, Thread::GetCurrentThread()));
-
-        // Done
-        LogPedantic("Delegate registered");
     }
 
     void RemoveListener(EventListenerType *eventListener)
     {
-        Mutex::ScopedLock lock(&m_listenerDelegateMutex);
+        std::lock_guard<std::mutex> lock(m_listenerDelegateMutex);
 
         // Listener must not be NULL
         Assert(eventListener != NULL);
@@ -691,12 +647,11 @@ class EventSupport :
 
         // Remove listener from list
         m_eventListenerList.erase(iterator);
-        LogPedantic("Listener unregistered");
     }
 
     void RemoveListener(DelegateType delegate)
     {
-        Mutex::ScopedLock lock(&m_listenerDelegateMutex);
+        std::lock_guard<std::mutex> lock(m_listenerDelegateMutex);
 
         // Delegate must not be empty
         Assert(delegate);
@@ -709,13 +664,12 @@ class EventSupport :
 
         // Remove delegate from list
         m_delegateList.erase(iterator);
-        LogPedantic("Delegate unregistered");
     }
 
     void SwitchListenerToThread(EventListenerType *eventListener,
                                 Thread *thread)
     {
-        Mutex::ScopedLock lock(&m_listenerDelegateMutex);
+        std::lock_guard<std::mutex> lock(m_listenerDelegateMutex);
 
         // Listener must not be NULL
         Assert(eventListener != NULL);
@@ -728,14 +682,12 @@ class EventSupport :
 
         // Set listener thread
         iterator->second = thread;
-
-        LogPedantic("Listener switched");
     }
 
     void SwitchListenerToThread(DelegateType delegate,
                                 Thread *thread)
     {
-        Mutex::ScopedLock lock(&m_listenerDelegateMutex);
+        std::lock_guard<std::mutex> lock(m_listenerDelegateMutex);
 
         // Delegate must not be empty
         Assert(!delegate.empty());
@@ -748,13 +700,11 @@ class EventSupport :
 
         // Set delegate thread
         iterator->second = thread;
-
-        LogPedantic("Delegate switched");
     }
 
     void SwitchAllListenersToThread(Thread *thread)
     {
-        Mutex::ScopedLock lock(&m_listenerDelegateMutex);
+        std::lock_guard<std::mutex> lock(m_listenerDelegateMutex);
 
         // Switch all listeners and delegates
         FOREACH(iterator, m_eventListenerList)
@@ -762,8 +712,6 @@ class EventSupport :
 
         FOREACH(iterator, m_delegateList)
         iterator->second = thread;
-
-        LogPedantic("All listeners and delegates switched");
     }
 };
 }