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>
34 EventThreadCallback* gEventThreadCallback = NULL;
37 struct EventThreadCallback::Impl
39 CallbackBase* callback;
40 unsigned int triggeredCount;
41 unsigned int expectedCount;
45 EventThreadCallback::EventThreadCallback( CallbackBase* callback )
48 mImpl->callback = callback;
49 mImpl->triggeredCount = 0u;
50 mImpl->expectedCount = UINT_MAX;
51 sem_init( &(mImpl->mySemaphore), 0, 0 );
52 gEventThreadCallback = this;
55 EventThreadCallback::~EventThreadCallback()
60 void EventThreadCallback::Trigger()
62 mImpl->triggeredCount++;
63 if( mImpl->triggeredCount >= mImpl->expectedCount )
65 sem_post( &(mImpl->mySemaphore) );
69 bool EventThreadCallback::WaitingForTrigger(unsigned int count, unsigned int seconds)
71 if( mImpl->triggeredCount >= count )
76 clock_gettime( CLOCK_REALTIME, &now );
77 now.tv_sec += seconds;
78 mImpl->expectedCount = count;
79 int error = sem_timedwait( &(mImpl->mySemaphore), &now );
83 CallbackBase* EventThreadCallback::GetCallback()
85 return mImpl->callback;
88 EventThreadCallback* EventThreadCallback::Get()
90 return gEventThreadCallback;
98 bool WaitForEventThreadTrigger( int triggerCount )
101 const int TEST_TIMEOUT(30);
103 struct timespec startTime;
105 clock_gettime( CLOCK_REALTIME, &startTime );
106 now.tv_sec = startTime.tv_sec;
107 now.tv_nsec = startTime.tv_nsec;
109 Dali::EventThreadCallback* eventTrigger = NULL;
110 while( eventTrigger == NULL )
112 eventTrigger = Dali::EventThreadCallback::Get();
113 clock_gettime( CLOCK_REALTIME, &now );
114 if( now.tv_sec - startTime.tv_sec > TEST_TIMEOUT )
121 if( eventTrigger != NULL )
123 Dali::CallbackBase* callback = eventTrigger->GetCallback();
124 eventTrigger->WaitingForTrigger( triggerCount, TEST_TIMEOUT - (now.tv_sec - startTime.tv_sec) );
125 Dali::CallbackBase::Execute( *callback );
128 clock_gettime( CLOCK_REALTIME, &now );
129 if( now.tv_sec > startTime.tv_sec + 1 )
131 fprintf(stderr, "WaitForEventThreadTrigger took %ld seconds\n", now.tv_sec - startTime.tv_sec );