Automatic image atlasing
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / toolkit-event-thread-callback.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 // CLASS HEADER
18 #include "toolkit-event-thread-callback.h"
19
20 // EXTERNAL INCLUDES
21 #include <cstddef>
22 #include <semaphore.h>
23 #include <math.h>
24
25 namespace Dali
26 {
27
28 namespace
29 {
30 EventThreadCallback* gEventThreadCallback = NULL;
31 }
32
33 struct EventThreadCallback::Impl
34 {
35   CallbackBase* callback;
36   unsigned int triggeredCount;
37   unsigned int expectedCount;
38   sem_t mySemaphore;
39 };
40
41 EventThreadCallback::EventThreadCallback( CallbackBase* callback )
42 : mImpl( new Impl() )
43 {
44   mImpl->callback = callback;
45   mImpl->triggeredCount = 0u;
46   mImpl->expectedCount = INFINITY;
47   sem_init( &(mImpl->mySemaphore), 0, 0 );
48   gEventThreadCallback = this;
49 }
50
51 EventThreadCallback::~EventThreadCallback()
52 {
53   delete mImpl;
54 }
55
56 void EventThreadCallback::Trigger()
57 {
58   mImpl->triggeredCount++;
59   if(  mImpl->triggeredCount >= mImpl->expectedCount )
60   {
61     sem_post( &(mImpl->mySemaphore) );
62   }
63 }
64
65 void EventThreadCallback::WaitingForTrigger(unsigned int count)
66 {
67   if(  mImpl->triggeredCount >= count )
68   {
69     return;
70   }
71   mImpl->expectedCount = count;
72   sem_wait( &(mImpl->mySemaphore) );
73 }
74
75 CallbackBase* EventThreadCallback::GetCallback()
76 {
77   return mImpl->callback;
78 }
79
80 EventThreadCallback* EventThreadCallback::Get()
81 {
82   return gEventThreadCallback;
83 }
84
85 }