ConditionalWait to replace boost conditional variable usage
[platform/core/uifw/dali-adaptor.git] / adaptors / base / 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 "conditional-wait.h"
20
21 // EXTERNAL INCLUDES
22 #include <pthread.h>
23
24 namespace Dali
25 {
26
27 namespace Internal
28 {
29
30 namespace Adaptor
31 {
32
33 namespace
34 {
35 } // unnamed namespace
36
37 struct ConditionalWait::ConditionalWaitImpl
38 {
39   pthread_mutex_t mutex;
40   pthread_cond_t condition;
41   volatile bool wait;
42 };
43
44 ConditionalWait::ConditionalWait()
45 : mImpl( new ConditionalWaitImpl )
46 {
47   pthread_mutex_init( &mImpl->mutex, NULL );
48   pthread_cond_init( &mImpl->condition, NULL );
49   mImpl->wait = false;
50 }
51
52 ConditionalWait::~ConditionalWait()
53 {
54   pthread_cond_destroy( &mImpl->condition );
55   pthread_mutex_destroy( &mImpl->mutex );
56   delete mImpl;
57 }
58
59 void ConditionalWait::Notify()
60 {
61   // pthread_cond_wait requires a lock to be held
62   pthread_mutex_lock( &mImpl->mutex );
63   bool wasWaiting = mImpl->wait;
64   mImpl->wait = false;
65   pthread_mutex_unlock( &mImpl->mutex );
66   // broadcast does nothing if the thread is not waiting but still has a system call overhead
67   // broadcast all threads to continue
68   if( wasWaiting )
69   {
70     pthread_cond_broadcast( &mImpl->condition );
71   }
72 }
73
74 void ConditionalWait::Wait()
75 {
76   // pthread_cond_wait requires a lock to be held
77   pthread_mutex_lock( &mImpl->mutex );
78   mImpl->wait = true;
79   // pthread_cond_wait may wake up without anyone calling Notify
80   while( mImpl->wait )
81   {
82     // wait while condition changes
83     pthread_cond_wait( &mImpl->condition, &mImpl->mutex ); // releases the lock whilst waiting
84   }
85   // when condition returns the mutex is locked so release the lock
86   pthread_mutex_unlock( &mImpl->mutex );
87 }
88
89 bool ConditionalWait::IsWaiting() const
90 {
91   bool isWaiting( false );
92   pthread_mutex_lock( &mImpl->mutex );
93   isWaiting = mImpl->wait;
94   pthread_mutex_unlock( &mImpl->mutex );
95   return isWaiting;
96 }
97
98
99
100 } // namespace Adaptor
101
102 } // namespace Internal
103
104 } // namespace Dali