[dali_1.1.8] Merge branch 'devel/master'
[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 // INTERNAL INCLUDES
22 #include <dali/internal/common/mutex-impl.h>
23
24 // EXTERNAL INCLUDES
25 #include <pthread.h>
26 #include <dali/integration-api/debug.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   pthread_mutex_init( &mImpl->mutex, NULL );
54   pthread_cond_init( &mImpl->condition, NULL );
55   mImpl->count = 0;
56 }
57
58 ConditionalWait::~ConditionalWait()
59 {
60   pthread_cond_destroy( &mImpl->condition );
61   pthread_mutex_destroy( &mImpl->mutex );
62   delete mImpl;
63 }
64
65 void ConditionalWait::Notify()
66 {
67   // pthread_cond_wait requires a lock to be held
68   Internal::Mutex::Lock( &mImpl->mutex );
69   volatile unsigned int previousCount = mImpl->count;
70   mImpl->count = 0; // change state before broadcast as that may wake clients immediately
71   // broadcast does nothing if the thread is not waiting but still has a system call overhead
72   // broadcast all threads to continue
73   if( 0 != previousCount )
74   {
75     pthread_cond_broadcast( &mImpl->condition );
76   }
77   Internal::Mutex::Unlock( &mImpl->mutex );
78 }
79
80 void ConditionalWait::Wait()
81 {
82   // pthread_cond_wait requires a lock to be held
83   Internal::Mutex::Lock( &mImpl->mutex );
84   ++(mImpl->count);
85   // pthread_cond_wait may wake up without anyone calling Notify
86   do
87   {
88     // wait while condition changes
89     pthread_cond_wait( &mImpl->condition, &mImpl->mutex ); // releases the lock whilst waiting
90   }
91   while( 0 != mImpl->count );
92   // when condition returns the mutex is locked so release the lock
93   Internal::Mutex::Unlock( &mImpl->mutex );
94 }
95
96 void ConditionalWait::Wait( const ScopedLock& scope )
97 {
98   // Scope must be locked:
99   DALI_ASSERT_DEBUG( &scope.GetLockedWait() == this );
100
101   ++(mImpl->count);
102
103   // pthread_cond_wait may wake up without anyone calling Notify so loop until
104   // count has been reset in a notify:
105   do
106   {
107     // wait while condition changes
108     pthread_cond_wait( &mImpl->condition, &mImpl->mutex ); // releases the lock whilst waiting
109   }
110   while( 0 != mImpl->count );
111
112   // We return with our mutex locked safe in the knowledge that the ScopedLock
113   // passed in will unlock it in the caller.
114 }
115
116 unsigned int ConditionalWait::GetWaitCount() const
117 {
118   return mImpl->count;
119 }
120
121 } // namespace Dali