License conversion from Flora to Apache 2.0
[platform/core/uifw/dali-core.git] / dali / internal / event / common / notification-manager.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/event/common/notification-manager.h>
20
21 // EXTERNAL INCLUDES
22 #ifdef __clang__
23 #pragma clang diagnostic push
24 #pragma clang diagnostic ignored "-Wall"
25
26 #include <boost/thread/mutex.hpp>
27
28 #pragma clang diagnostic pop
29 #else
30
31 #include <boost/thread/mutex.hpp>
32
33 #endif // __clang__
34
35 // INTERNAL INCLUDES
36 #include <dali/public-api/common/dali-common.h>
37 #include <dali/internal/event/common/property-notification-impl.h>
38 #include <dali/internal/common/message-container.h>
39
40 using namespace std;
41
42 namespace Dali
43 {
44
45 namespace Internal
46 {
47
48 typedef boost::mutex MessageQueueMutex;
49
50 struct NotificationManager::Impl
51 {
52   Impl()
53   : notificationCount(0)
54   {
55   }
56
57   ~Impl()
58   {
59   }
60
61   // Used to skip duplicate operations during Notify()
62   unsigned int notificationCount;
63
64   // queueMutex must be locked whilst accessing queue
65   MessageQueueMutex queueMutex;
66   MessageContainer updateQueue;
67   MessageContainer eventQueue;
68 };
69
70 NotificationManager::NotificationManager()
71 {
72   mImpl = new Impl();
73 }
74
75 NotificationManager::~NotificationManager()
76 {
77   delete mImpl;
78 }
79
80 void NotificationManager::QueueMessage( MessageBase* message )
81 {
82   DALI_ASSERT_DEBUG( NULL != message );
83
84   // queueMutex must be locked whilst accessing queue
85   MessageQueueMutex::scoped_lock lock( mImpl->queueMutex );
86
87   mImpl->updateQueue.PushBack( message );
88 }
89
90 bool NotificationManager::MessagesToProcess()
91 {
92   // queueMutex must be locked whilst accessing queue
93   MessageQueueMutex::scoped_lock lock( mImpl->queueMutex );
94
95   return ( false == mImpl->updateQueue.IsEmpty() );
96 }
97
98 void NotificationManager::ProcessMessages()
99 {
100   // Done before messages are processed, for notification count comparisons
101   ++mImpl->notificationCount;
102
103   // queueMutex must be locked whilst accessing queue
104   {
105     MessageQueueMutex::scoped_lock lock( mImpl->queueMutex );
106
107     // Swap the queue, original queue ends up empty, then release the lock
108     mImpl->updateQueue.Swap( mImpl->eventQueue );
109   }
110   // end of scope, lock is released
111
112   MessageContainer::Iterator iter = mImpl->eventQueue.Begin();
113   MessageContainer::Iterator end = mImpl->eventQueue.End();
114   for( ; iter != end; ++iter )
115   {
116     (*iter)->Process( 0u/*ignored*/ );
117   }
118
119   // release the processed messages from event side queue
120   mImpl->eventQueue.Clear();
121 }
122
123 unsigned int NotificationManager::GetNotificationCount() const
124 {
125   return mImpl->notificationCount;
126 }
127
128 } // namespace Internal
129
130 } // namespace Dali