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>
30 EventThreadCallback* gEventThreadCallback = NULL;
33 struct EventThreadCallback::Impl
35 CallbackBase* callback;
36 unsigned int triggeredCount;
37 unsigned int expectedCount;
41 EventThreadCallback::EventThreadCallback( CallbackBase* callback )
44 mImpl->callback = callback;
45 mImpl->triggeredCount = 0u;
46 mImpl->expectedCount = INFINITY;
47 sem_init( &(mImpl->mySemaphore), 0, 0 );
48 gEventThreadCallback = this;
51 EventThreadCallback::~EventThreadCallback()
56 void EventThreadCallback::Trigger()
58 mImpl->triggeredCount++;
59 if( mImpl->triggeredCount >= mImpl->expectedCount )
61 sem_post( &(mImpl->mySemaphore) );
65 void EventThreadCallback::WaitingForTrigger(unsigned int count)
67 if( mImpl->triggeredCount >= count )
71 mImpl->expectedCount = count;
72 sem_wait( &(mImpl->mySemaphore) );
75 CallbackBase* EventThreadCallback::GetCallback()
77 return mImpl->callback;
80 EventThreadCallback* EventThreadCallback::Get()
82 return gEventThreadCallback;