[3.0] Added missing newline chars to logging commands
[platform/core/uifw/dali-core.git] / dali / devel-api / threading / conditional-wait.cpp
1 /*
2  * Copyright (c) 2015 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/devel-api/threading/conditional-wait.h>
20
21 // EXTERNAL INCLUDES
22 #include <pthread.h>
23
24 // INTERNAL INCLUDES
25 #include <dali/integration-api/debug.h>
26 #include <dali/internal/common/mutex-impl.h>
27
28 namespace Dali
29 {
30
31 struct ConditionalWait::ConditionalWaitImpl
32 {
33   pthread_mutex_t mutex;
34   pthread_cond_t condition;
35   volatile unsigned int count;
36 };
37
38
39 ConditionalWait::ScopedLock::ScopedLock( ConditionalWait& wait ) : mWait(wait)
40 {
41   Internal::Mutex::Lock( &wait.mImpl->mutex );
42 }
43
44 ConditionalWait::ScopedLock::~ScopedLock()
45 {
46   ConditionalWait& wait = mWait;
47   Internal::Mutex::Unlock( &wait.mImpl->mutex );
48 }
49
50 ConditionalWait::ConditionalWait()
51 : mImpl( new ConditionalWaitImpl )
52 {
53   if( pthread_mutex_init( &mImpl->mutex, NULL ) )
54   {
55     DALI_LOG_ERROR( "Unable to initialise mutex in ConditionalWait\n" );
56   }
57   if( pthread_cond_init( &mImpl->condition, NULL ) )
58   {
59     DALI_LOG_ERROR( "Unable to initialise conditional\n" );
60   }
61   mImpl->count = 0;
62 }
63
64 ConditionalWait::~ConditionalWait()
65 {
66   if( pthread_cond_destroy( &mImpl->condition ) )
67   {
68     DALI_LOG_ERROR( "Unable to destroy conditional\n" );
69   }
70   if( pthread_mutex_destroy( &mImpl->mutex ) )
71   {
72     DALI_LOG_ERROR( "Unable to destroy mutex in ConditionalWait\n" );
73   }
74   delete mImpl;
75 }
76
77 void ConditionalWait::Notify()
78 {
79   // pthread_cond_wait requires a lock to be held
80   Internal::Mutex::Lock( &mImpl->mutex );
81   volatile unsigned int previousCount = mImpl->count;
82   mImpl->count = 0; // change state before broadcast as that may wake clients immediately
83   // broadcast does nothing if the thread is not waiting but still has a system call overhead
84   // broadcast all threads to continue
85   if( 0 != previousCount )
86   {
87     if( pthread_cond_broadcast( &mImpl->condition ) )
88     {
89       DALI_LOG_ERROR( "Error calling pthread_cond_broadcast\n" );
90     }
91   }
92   Internal::Mutex::Unlock( &mImpl->mutex );
93 }
94
95 void ConditionalWait::Notify( const ScopedLock& scope )
96 {
97   // Scope must be locked:
98   DALI_ASSERT_DEBUG( &scope.GetLockedWait() == this );
99
100   volatile unsigned int previousCount = mImpl->count;
101   mImpl->count = 0; // change state before broadcast as that may wake clients immediately
102   // broadcast does nothing if the thread is not waiting but still has a system call overhead
103   // broadcast all threads to continue
104   if( 0 != previousCount )
105   {
106     if( pthread_cond_broadcast( &mImpl->condition ) )
107     {
108       DALI_LOG_ERROR( "Error calling pthread_cond_broadcast\n" );
109     }
110   }
111 }
112
113 void ConditionalWait::Wait()
114 {
115   // pthread_cond_wait requires a lock to be held
116   Internal::Mutex::Lock( &mImpl->mutex );
117   ++(mImpl->count);
118   // pthread_cond_wait may wake up without anyone calling Notify
119   do
120   {
121     // wait while condition changes
122     if( pthread_cond_wait( &mImpl->condition, &mImpl->mutex ) ) // releases the lock whilst waiting
123     {
124       DALI_LOG_ERROR( "Error calling pthread_cond_wait\n" );
125       break;
126     }
127   }
128   while( 0 != mImpl->count );
129   // when condition returns the mutex is locked so release the lock
130   Internal::Mutex::Unlock( &mImpl->mutex );
131 }
132
133 void ConditionalWait::Wait( const ScopedLock& scope )
134 {
135   // Scope must be locked:
136   DALI_ASSERT_DEBUG( &scope.GetLockedWait() == this );
137
138   ++(mImpl->count);
139
140   // pthread_cond_wait may wake up without anyone calling Notify so loop until
141   // count has been reset in a notify:
142   do
143   {
144     // wait while condition changes
145     if( pthread_cond_wait( &mImpl->condition, &mImpl->mutex ) ) // releases the lock whilst waiting
146     {
147       DALI_LOG_ERROR( "Error calling pthread_cond_wait\n" );
148       break;
149     }
150   }
151   while( 0 != mImpl->count );
152
153   // We return with our mutex locked safe in the knowledge that the ScopedLock
154   // passed in will unlock it in the caller.
155 }
156
157 unsigned int ConditionalWait::GetWaitCount() const
158 {
159   return mImpl->count;
160 }
161
162 } // namespace Dali