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.
18 #include "toolkit-event-thread-callback.h"
22 #include <semaphore.h>
31 EventThreadCallback* gEventThreadCallback = NULL;
34 struct EventThreadCallback::Impl
36 CallbackBase* callback;
37 unsigned int triggeredCount;
38 unsigned int expectedCount;
42 EventThreadCallback::EventThreadCallback( CallbackBase* callback )
45 mImpl->callback = callback;
46 mImpl->triggeredCount = 0u;
47 mImpl->expectedCount = UINT_MAX;
48 sem_init( &(mImpl->mySemaphore), 0, 0 );
49 gEventThreadCallback = this;
52 EventThreadCallback::~EventThreadCallback()
57 void EventThreadCallback::Trigger()
59 mImpl->triggeredCount++;
60 if( mImpl->triggeredCount >= mImpl->expectedCount )
62 sem_post( &(mImpl->mySemaphore) );
66 void EventThreadCallback::WaitingForTrigger(unsigned int count)
68 if( mImpl->triggeredCount >= count )
72 mImpl->expectedCount = count;
73 sem_wait( &(mImpl->mySemaphore) );
76 CallbackBase* EventThreadCallback::GetCallback()
78 return mImpl->callback;
81 EventThreadCallback* EventThreadCallback::Get()
83 return gEventThreadCallback;