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>
33 // Note, this is not thread safe - however, should not be using
34 // triggers from multiple threads - they should all be created on
36 std::vector<Dali::EventThreadCallback*> gEventThreadCallbacks;
43 struct EventThreadCallback::Impl
45 CallbackBase* callback;
49 EventThreadCallback::EventThreadCallback( CallbackBase* callback )
52 mImpl->callback = callback;
53 sem_init( &(mImpl->mySemaphore), 0, 0 );
55 gEventThreadCallbacks.push_back(this);
58 EventThreadCallback::~EventThreadCallback()
60 std::vector<EventThreadCallback*>::iterator iter =
61 std::find(gEventThreadCallbacks.begin(), gEventThreadCallbacks.end(), this);
62 if( iter != gEventThreadCallbacks.end() )
64 gEventThreadCallbacks.erase(iter);
69 void EventThreadCallback::Trigger()
71 sem_post( &(mImpl->mySemaphore) );
74 // returns true if timed out rather than triggered
75 bool EventThreadCallback::WaitingForTrigger()
78 clock_gettime( CLOCK_REALTIME, &now );
79 if( now.tv_nsec < 999900000 ) // 999, 900, 000
87 int error = sem_timedwait( &(mImpl->mySemaphore), &now );
88 return error != 0; // true if timeout
91 CallbackBase* EventThreadCallback::GetCallback()
93 return mImpl->callback;
102 bool WaitForEventThreadTrigger( int triggerCount, int timeoutInSeconds )
104 struct timespec startTime;
106 clock_gettime( CLOCK_REALTIME, &startTime );
107 now.tv_sec = startTime.tv_sec;
108 now.tv_nsec = startTime.tv_nsec;
110 // Round robin poll of each semaphore:
111 while ( triggerCount > 0 )
113 if( gEventThreadCallbacks.size() > 0 )
115 for( std::vector<Dali::EventThreadCallback*>::iterator iter = gEventThreadCallbacks.begin();
116 iter != gEventThreadCallbacks.end(); ++iter )
118 Dali::EventThreadCallback* eventTrigger = (*iter);
119 Dali::CallbackBase* callback = eventTrigger->GetCallback();
120 bool timedout = eventTrigger->WaitingForTrigger();
123 // Semaphore was unlocked - execute the trigger
124 Dali::CallbackBase::Execute( *callback );
127 if( triggerCount <= 0 )
133 clock_gettime( CLOCK_REALTIME, &now );
134 if( now.tv_sec - startTime.tv_sec > timeoutInSeconds )
136 // Ensure we break out of the loop if elapsed time has passed
141 clock_gettime( CLOCK_REALTIME, &now );
142 if( now.tv_sec > startTime.tv_sec + 1 )
144 fprintf(stderr, "WaitForEventThreadTrigger took %ld seconds\n", now.tv_sec - startTime.tv_sec );
146 return triggerCount == 0;