Way to run a routine on different cpu

Wei Hu weh at microsoft.com
Thu Aug 28 13:01:31 UTC 2014


Many thanks, John!

Wei

-----Original Message-----
From: John Baldwin [mailto:jhb at freebsd.org] 
Sent: Thursday, August 28, 2014 2:28 AM
To: freebsd-drivers at freebsd.org
Cc: Wei Hu
Subject: Re: Way to run a routine on different cpu

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