/* * Starboard 15 native PS Vita thread runtime built on VitaSDK PTE pthreads. * pthread_t is a pointer-sized opaque handle on Vita, matching SbThread. */ #include "starboard/thread.h" #include "starboard/log.h" #include #include extern "C" int ytv_vita_cpu_mask(); #include #include #include #include #include #include #include #include static_assert(sizeof(pthread_t)==sizeof(SbThread), "Starboard thread handle must have Vita pthread pointer ABI"); struct SbThreadLocalKeyPrivate { pthread_key_t key; }; namespace { struct ThreadStart { SbThreadEntryPoint function; void *context; char name[32]; SbThreadPriority priority; }; // Starboard priority -> Vita kernel priority (lower value runs first; user // range 64..191, default 160). Cobalt marks its rasterizer/audio/input-path // threads High/RealTime and its background work Low/Lowest. int vita_priority(SbThreadPriority p){ switch(p){ case kSbThreadPriorityLowest: return 180; case kSbThreadPriorityLow: return 170; case kSbThreadPriorityHigh: return 140; case kSbThreadPriorityHighest: return 128; case kSbThreadPriorityRealTime: return 112; case kSbThreadPriorityNormal: default: return 160; } } pthread_once_t name_once=PTHREAD_ONCE_INIT; pthread_key_t name_key={}; bool name_ready=false; void free_name(void* data){free(data);} void init_name_key(){ name_ready=pthread_key_create(&name_key,free_name)==0; } // DIAGNOSTIC thread registry for the per-thread CPU report. struct ThreadSlot { SceUID uid; char name[24]; uint64_t last; }; ThreadSlot g_slots[96]; volatile int g_slot_count=0; void register_thread(const char* name){ int i=__sync_fetch_and_add(&g_slot_count,1); if(i>=96)return; g_slots[i].uid=sceKernelGetThreadId(); snprintf(g_slots[i].name,sizeof(g_slots[i].name),"%s",name&&*name?name:"?"); g_slots[i].last=0; } void* run_thread(void *arg) { ThreadStart state=*(ThreadStart*)arg; free(arg); register_thread(state.name); char line[190]; snprintf(line,sizeof(line),"YTV_THREAD_ENTRY tid=%d name=%s entry=%p\n", sceKernelGetThreadId(),state.name,(void*)state.function); SbLogRaw(line); if(state.name[0])SbThreadSetName(state.name); // pthread_create ignores Starboard priority; apply it, and allow all three // user cores (the browser runs JS, layout and raster threads in parallel). sceKernelChangeThreadPriority(0,vita_priority(state.priority)); sceKernelChangeThreadCpuAffinityMask(0,ytv_vita_cpu_mask()); void* result=state.function(state.context); snprintf(line,sizeof(line),"YTV_THREAD_EXIT tid=%d name=%s result=%p\n", sceKernelGetThreadId(),state.name,result); SbLogRaw(line); return result; } } // CPU cores for every thread. With the CapUnlocker kernel plugin // (*KERNEL in taiHEN config) the kernel accepts the system-reserved 4th core // (SCE_KERNEL_CPU_MASK_SYSTEM); without it that request fails and the three // user cores are used. Probed once, on the main thread, at startup. namespace { int g_cpu_mask=SCE_KERNEL_CPU_MASK_USER_ALL; int g_cpu_count=3; pthread_once_t g_cpu_once=PTHREAD_ONCE_INIT; void probe_cpu_mask(){ const int four=SCE_KERNEL_CPU_MASK_USER_ALL|SCE_KERNEL_CPU_MASK_SYSTEM; if(sceKernelChangeThreadCpuAffinityMask(0,four)>=0){ g_cpu_mask=four; g_cpu_count=4; }else{ sceKernelChangeThreadCpuAffinityMask(0,SCE_KERNEL_CPU_MASK_USER_ALL); } char line[96]; snprintf(line,sizeof(line),"YTV_LIFECYCLE_CPU_CORES %d (mask=0x%x)\n",g_cpu_count,g_cpu_mask); SbLogRaw(line); } } // Per-thread CPU time since the previous call (kernel runClocks), busiest // first; written into out as one log line. Diagnostic only. extern "C" void ytv_thread_cpu_report(char* out,int n,const char* main_name){ static bool main_done=false; if(!main_done&&main_name){main_done=true;register_thread(main_name);} struct Row{int i;uint64_t d;} rows[96]; int count=0; int total=g_slot_count<96?g_slot_count:96; for(int i=0;irows[a].d){Row t=rows[a];rows[a]=rows[b];rows[b]=t;} int len=snprintf(out,n,"YTV_THREAD_CPU"); for(int k=0;k16*1024*1024LL)return kSbThreadInvalid; ThreadStart* state=(ThreadStart*)calloc(1,sizeof(ThreadStart)); if(!state)return kSbThreadInvalid; state->function=entry; state->context=context; state->priority=priority; if(name) { strncpy(state->name,name,sizeof(state->name)-1); state->name[sizeof(state->name)-1]=0; } pthread_attr_t attrs={}; if(pthread_attr_init(&attrs)!=0){free(state);return kSbThreadInvalid;} // stack_size==0 means "platform default". VitaSDK's pthread default stack // is far below what Chromium-style code (V8 compiler, Skia, ICU) expects; // use 1 MiB like other Starboard ports. if(stack_size==0)stack_size=1024*1024; if(pthread_attr_setstacksize(&attrs,(size_t)stack_size)!=0){ pthread_attr_destroy(&attrs);free(state);return kSbThreadInvalid; } if(!joinable && pthread_attr_setdetachstate(&attrs,PTHREAD_CREATE_DETACHED)!=0){ pthread_attr_destroy(&attrs);free(state);return kSbThreadInvalid; } pthread_t handle={}; int result=pthread_create(&handle,&attrs,run_thread,state); pthread_attr_destroy(&attrs); if(num<=70){ char line[165]; snprintf(line,sizeof(line),"YTV_THREAD_CREATE_RESULT n=%d result=%d handle=%p\n", num,result,(void*)handle); SbLogRaw(line); } if(result!=0){free(state);return kSbThreadInvalid;} return (SbThread)handle; } bool SbThreadJoin(SbThread thread,void** out_return){ if(!thread)return false; return pthread_join((pthread_t)thread,out_return)==0; } void SbThreadDetach(SbThread thread){ if(thread)pthread_detach((pthread_t)thread); } void SbThreadYield() { sched_yield(); } void SbThreadSleep(int64_t duration) { while(duration>0){ unsigned step=(unsigned)(duration>1000000LL?1000000LL:duration); sceKernelDelayThread(step); duration-=step; } } SbThread SbThreadGetCurrent() {return (SbThread)pthread_self();} bool SbThreadIsEqual(SbThread a,SbThread b) { if(!a || !b)return false; return pthread_equal((pthread_t)a,(pthread_t)b)!=0; } void SbThreadGetName(char* buffer,int len){ if(!buffer || len<=0)return; buffer[0]=0; pthread_once(&name_once,init_name_key); if(!name_ready)return; const char* text=(const char*)pthread_getspecific(name_key); if(!text)return; strncpy(buffer,text,len-1); buffer[len-1]=0; } void SbThreadSetName(const char *name) { pthread_once(&name_once,init_name_key); if(!name_ready)return; char* current=(char*)pthread_getspecific(name_key); if(!name) { pthread_setspecific(name_key,nullptr); free(current); return; } { SceUID me=sceKernelGetThreadId(); int total=g_slot_count<96?g_slot_count:96; for(int i=0;ikey,destructor)!=0) { free(k);return kSbThreadLocalKeyInvalid; } return k; } void SbThreadDestroyLocalKey(SbThreadLocalKey key){ if(!key)return; pthread_key_delete(key->key); free(key); } void* SbThreadGetLocalValue(SbThreadLocalKey key){ return key?pthread_getspecific(key->key):nullptr; } bool SbThreadSetLocalValue(SbThreadLocalKey key,void* value){ return key && pthread_setspecific(key->key,value)==0; } } // extern "C"