Way to run a routine on different cpu

John Baldwin jhb at freebsd.org
Wed Aug 27 20:43:08 UTC 2014


On Tuesday, August 26, 2014 9:04:11 am Wei Hu wrote:
> Hi,
> 
> I am wondering what is the right way to run a routine on a differnet cpu and 
wait for it to complete in FreeBSD kernel. For example, on cpu-0 I want to 
send a IPI to cpu-1 to let it run a routine called foo(). On cpu-0 I will wait 
till foo() completes. Is smp_rendezvous() the right way to do it? What if I 
only want it to run on one cpu, not all cpus?

You can use 'sched_bind()' to move yourself to CPU x:

	struct thread *td;

	td = curthread;
	thread_lock(td);
	sched_bind(td, X);
	thread_unlock(td);

	/* Perform work on CPU X. */

	thread_lock(td);
	sched_unbind(td);
	thread_lock(td);

	/* Thread can now run anywhere its cpuset permits. */

That might be simpler than a rendezvous as a rendezvous handler runs in a more 
restricted environment (you can't take any locks, not even spin locks, etc.)

-- 
John Baldwin


More information about the freebsd-drivers mailing list