The kernel thread library ensures that all calls to the scheduler object are atomic. This is achieved by locking a valiable9.2 & disabling context switch (by setting do_Context_Switch to 0) before such a call, and then unlocking & enabling context switch afterwards. So a typical implementation of a thread library function looks like the following.
int thread_xxx(...) do_Context_Switch=0; spin_lock(&scheduler->sched_lock); .................. Do Necessary Stuff .................. spin_unlock(&scheduler->sched_lock); do_Context_Switch=1;