/* * Starboard API 15 scheduled-event queue for Vita's native main thread. * Scheduling/cancellation is thread-safe. Only ytv_starboard_pump_scheduled() * dispatches callbacks; the main Vita event loop must call it periodically. * * This is NOT a substitute for Starboard's complete event/application lifecycle. */ #include "starboard/event.h" #include "starboard/time.h" #include #include #include "starboard/mutex.h" #include "starboard/condition_variable.h" #include #include #include namespace { constexpr int kCapacity=192; struct Scheduled { SbEventId id; int64_t deadline; SbEventCallback callback; void* context; }; Scheduled queue[kCapacity]={}; SbMutex lock={}; // native LwMutex (port_sync.cpp); zero = static init // Signalled whenever an event is scheduled, so the main loop wakes at once // instead of sleeping a fixed frame. SbConditionVariable wake={}; pthread_t owner={}; bool owner_set=false; uint32_t sequence=0; int64_t now_us(){ return static_cast(sceKernelGetSystemTimeWide()); } } extern "C" { SbEventId SbEventSchedule(SbEventCallback callback,void* context,int64_t delay){ if(!callback)return 0; if(delay<0)delay=0; SbMutexAcquire(&lock); int index=-1; for(int i=0;iINT64_MAX-now)delay=INT64_MAX-now; queue[index]={next,now+delay,callback,context}; SbConditionVariableSignal(&wake); SbMutexRelease(&lock); return next; } void SbEventCancel(SbEventId event){ if(event==0)return; SbMutexAcquire(&lock); for(int i=0;ikCapacity)limit=kCapacity; pthread_t caller=pthread_self(); SbMutexAcquire(&lock); if(!owner_set){owner=caller;owner_set=true;} if(!pthread_equal(owner,caller)){ SbMutexRelease(&lock); return -1; // fail closed; scheduled events cannot fire on worker thread } SbMutexRelease(&lock); int called=0; while(called=0){ func=queue[winner].callback; data=queue[winner].context; queue[winner]={}; } SbMutexRelease(&lock); if(!func)break; func(data); // runs on owner main thread, OUTSIDE lock called++; } return called; } // Main loop helper: blocks until the earliest scheduled event is due, a new // event is scheduled, or max_wait_us elapses (input polling cadence). void ytv_starboard_wait_scheduled(int64_t max_wait_us){ SbMutexAcquire(&lock); int64_t now=now_us(); int64_t until=now+max_wait_us; for(int i=0;inow){ SbConditionVariableWaitTimed(&wake,&lock,until-now); } SbMutexRelease(&lock); } }