2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "conditional-wait.h"
23 #include <dali/integration-api/debug.h>
34 struct ConditionalWait::ConditionalWaitImpl
36 pthread_mutex_t mutex;
37 pthread_cond_t condition;
38 volatile unsigned int count;
41 ConditionalWait::ConditionalWait()
42 : mImpl( new ConditionalWaitImpl )
44 pthread_mutex_init( &mImpl->mutex, NULL );
45 pthread_cond_init( &mImpl->condition, NULL );
49 ConditionalWait::~ConditionalWait()
51 pthread_cond_destroy( &mImpl->condition );
52 pthread_mutex_destroy( &mImpl->mutex );
56 void ConditionalWait::Notify()
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 )
66 pthread_cond_broadcast( &mImpl->condition );
68 pthread_mutex_unlock( &mImpl->mutex );
71 void ConditionalWait::Wait()
73 // pthread_cond_wait requires a lock to be held
74 pthread_mutex_lock( &mImpl->mutex );
76 // pthread_cond_wait may wake up without anyone calling Notify
79 // wait while condition changes
80 pthread_cond_wait( &mImpl->condition, &mImpl->mutex ); // releases the lock whilst waiting
82 while( 0 != mImpl->count );
83 // when condition returns the mutex is locked so release the lock
84 pthread_mutex_unlock( &mImpl->mutex );
87 unsigned int ConditionalWait::GetWaitCount() const
92 ConditionalWait::ScopedLock::ScopedLock( ConditionalWait& wait ) : mWait(wait)
94 pthread_mutex_lock( &wait.mImpl->mutex );
97 ConditionalWait::ScopedLock::~ScopedLock()
99 ConditionalWait& wait = mWait;
100 pthread_mutex_unlock( &wait.mImpl->mutex );
103 void ConditionalWait::Wait( const ScopedLock& scope )
105 // Scope must be locked:
106 DALI_ASSERT_DEBUG( &scope.GetLockedWait() == this );
110 // pthread_cond_wait may wake up without anyone calling Notify so loop until
111 // count has been reset in a notify:
114 // wait while condition changes
115 pthread_cond_wait( &mImpl->condition, &mImpl->mutex ); // releases the lock whilst waiting
117 while( 0 != mImpl->count );
119 // We return with our mutex locked safe in the knowledge that the ScopedLock
120 // passed in will unlock it in the caller.
124 } // namespace Adaptor
126 } // namespace Internal