dbebbeca79de1111d99b8358266c8dab794cc46e
[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 updateCompletedQueue;
67   MessageContainer updateWorkingQueue;
68   MessageContainer eventQueue;
69 };
70
71 NotificationManager::NotificationManager()
72 {
73   mImpl = new Impl();
74 }
75
76 NotificationManager::~NotificationManager()
77 {
78   delete mImpl;
79 }
80
81 void NotificationManager::QueueMessage( MessageBase* message )
82 {
83   DALI_ASSERT_DEBUG( NULL != message );
84
85   // queueMutex must be locked whilst accessing queue
86   MessageQueueMutex::scoped_lock lock( mImpl->queueMutex );
87
88   mImpl->updateWorkingQueue.PushBack( message );
89 }
90
91 void NotificationManager::UpdateCompleted()
92 {
93   // queueMutex must be locked whilst accessing queue
94   MessageQueueMutex::scoped_lock lock( mImpl->queueMutex );
95   // Swap the queue, original queue ends up empty, then release the lock
96   mImpl->updateCompletedQueue.Swap( mImpl->updateWorkingQueue );
97 }
98
99 bool NotificationManager::MessagesToProcess()
100 {
101   // queueMutex must be locked whilst accessing queue
102   MessageQueueMutex::scoped_lock lock( mImpl->queueMutex );
103
104   return ( false == mImpl->updateCompletedQueue.IsEmpty() );
105 }
106
107 void NotificationManager::ProcessMessages()
108 {
109   // Done before messages are processed, for notification count comparisons
110   ++mImpl->notificationCount;
111
112   // queueMutex must be locked whilst accessing queue
113   {
114     MessageQueueMutex::scoped_lock lock( mImpl->queueMutex );
115
116     // Swap the queue, original queue ends up empty, then release the lock
117     mImpl->updateCompletedQueue.Swap( mImpl->eventQueue );
118   }
119   // end of scope, lock is released
120
121   MessageContainer::Iterator iter = mImpl->eventQueue.Begin();
122   MessageContainer::Iterator end = mImpl->eventQueue.End();
123   for( ; iter != end; ++iter )
124   {
125     (*iter)->Process( 0u/*ignored*/ );
126   }
127
128   // release the processed messages from event side queue
129   mImpl->eventQueue.Clear();
130 }
131
132 unsigned int NotificationManager::GetNotificationCount() const
133 {
134   return mImpl->notificationCount;
135 }
136
137 } // namespace Internal
138
139 } // namespace Dali