Index: sys/kern/init_main.c =================================================================== --- sys/kern/init_main.c +++ sys/kern/init_main.c @@ -590,6 +590,7 @@ EVENTHANDLER_DIRECT_INVOKE(process_init, p); EVENTHANDLER_DIRECT_INVOKE(thread_init, td); EVENTHANDLER_DIRECT_INVOKE(process_ctor, p); + sched_thread_ctor(td); EVENTHANDLER_DIRECT_INVOKE(thread_ctor, td); /* Index: sys/kern/kern_proc.c =================================================================== --- sys/kern/kern_proc.c +++ sys/kern/kern_proc.c @@ -213,6 +213,7 @@ td = FIRST_THREAD_IN_PROC(p); if (td != NULL) { /* Make sure all thread constructors are executed */ + sched_thread_ctor(td); EVENTHANDLER_DIRECT_INVOKE(thread_ctor, td); } return (0); Index: sys/kern/kern_thread.c =================================================================== --- sys/kern/kern_thread.c +++ sys/kern/kern_thread.c @@ -208,6 +208,7 @@ */ td->td_critnest = 1; td->td_lend_user_pri = PRI_MAX; + sched_thread_ctor(td); EVENTHANDLER_DIRECT_INVOKE(thread_ctor, td); #ifdef AUDIT audit_thread_alloc(td); Index: sys/kern/sched_4bsd.c =================================================================== --- sys/kern/sched_4bsd.c +++ sys/kern/sched_4bsd.c @@ -1777,3 +1777,9 @@ } #endif } + +void +sched_thread_ctor(struct thread *td) +{ + /* NOP */ +} Index: sys/kern/sched_ule.c =================================================================== --- sys/kern/sched_ule.c +++ sys/kern/sched_ule.c @@ -1405,13 +1405,19 @@ /* Add thread0's load since it's running. */ TDQ_LOCK(tdq); - td_get_sched(&thread0)->ts_cpu = curcpu; /* Something valid to start */ thread0.td_lock = TDQ_LOCKPTR(TDQ_SELF()); tdq_load_add(tdq, &thread0); tdq->tdq_lowpri = thread0.td_priority; TDQ_UNLOCK(tdq); } +void +sched_thread_ctor(struct thread *td) +{ + /* set the initial ts_cpu value to a valid CPU number */ + td_get_sched(td)->ts_cpu = curcpu; +} + /* * This routine determines time constants after stathz and hz are setup. */ @@ -2453,7 +2459,6 @@ * Pick the destination cpu and if it isn't ours transfer to the * target cpu. */ - td_get_sched(td)->ts_cpu = curcpu; /* Pick something valid to start */ cpu = sched_pickcpu(td, flags); tdq = sched_setcpu(td, cpu, flags); tdq_add(tdq, td, flags); Index: sys/sys/sched.h =================================================================== --- sys/sys/sched.h +++ sys/sys/sched.h @@ -149,6 +149,12 @@ void sched_clear_tdname(struct thread *td); #endif +/* + * This routine initialize scheduler specific fields of the thread + * structure. + */ +void sched_thread_ctor(struct thread *td); + static __inline void sched_pin(void) {