Added network build option. Disabled by default.
[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 #include <dali/integration-api/debug.h>
24
25 namespace Dali
26 {
27
28 namespace Internal
29 {
30
31 namespace Adaptor
32 {
33
34 struct ConditionalWait::ConditionalWaitImpl
35 {
36   pthread_mutex_t mutex;
37   pthread_cond_t condition;
38   volatile unsigned int count;
39 };
40
41 ConditionalWait::ConditionalWait()
42 : mImpl( new ConditionalWaitImpl )
43 {
44   pthread_mutex_init( &mImpl->mutex, NULL );
45   pthread_cond_init( &mImpl->condition, NULL );
46   mImpl->count = 0;
47 }
48
49 ConditionalWait::~ConditionalWait()
50 {
51   pthread_cond_destroy( &mImpl->condition );
52   pthread_mutex_destroy( &mImpl->mutex );
53   delete mImpl;
54 }
55
56 void ConditionalWait::Notify()
57 {
58   // pthread_cond_wait requires a lock to be held
59   pthread_mutex_lock( &mImpl->mutex );
60   volatile unsigned int previousCount = mImpl->count;
61   mImpl->count = 0; // change state before broadcast as that may wake clients immediately
62   // broadcast does nothing if the thread is not waiting but still has a system call overhead
63   // broadcast all threads to continue
64   if( 0 != previousCount )
65   {
66     pthread_cond_broadcast( &mImpl->condition );
67   }
68   pthread_mutex_unlock( &mImpl->mutex );
69 }
70
71 void ConditionalWait::Wait()
72 {
73   // pthread_cond_wait requires a lock to be held
74   pthread_mutex_lock( &mImpl->mutex );
75   ++(mImpl->count);
76   // pthread_cond_wait may wake up without anyone calling Notify
77   do
78   {
79     // wait while condition changes
80     pthread_cond_wait( &mImpl->condition, &mImpl->mutex ); // releases the lock whilst waiting
81   }
82   while( 0 != mImpl->count );
83   // when condition returns the mutex is locked so release the lock
84   pthread_mutex_unlock( &mImpl->mutex );
85 }
86
87 unsigned int ConditionalWait::GetWaitCount() const
88 {
89   return mImpl->count;
90 }
91
92 ConditionalWait::ScopedLock::ScopedLock( ConditionalWait& wait ) : mWait(wait)
93 {
94   pthread_mutex_lock( &wait.mImpl->mutex );
95 }
96
97 ConditionalWait::ScopedLock::~ScopedLock()
98 {
99   ConditionalWait& wait = mWait;
100   pthread_mutex_unlock( &wait.mImpl->mutex );
101 }
102
103 void ConditionalWait::Wait( const ScopedLock& scope )
104 {
105   // Scope must be locked:
106   DALI_ASSERT_DEBUG( &scope.GetLockedWait() == this );
107
108   ++(mImpl->count);
109
110   // pthread_cond_wait may wake up without anyone calling Notify so loop until
111   // count has been reset in a notify:
112   do
113   {
114     // wait while condition changes
115     pthread_cond_wait( &mImpl->condition, &mImpl->mutex ); // releases the lock whilst waiting
116   }
117   while( 0 != mImpl->count );
118
119   // We return with our mutex locked safe in the knowledge that the ScopedLock
120   // passed in will unlock it in the caller.
121 }
122
123
124 } // namespace Adaptor
125
126 } // namespace Internal
127
128 } // namespace Dali