Is there smth like pthread_barrier in SMP Linux kernel?
When kernel works simultaneously on 2 and more开发者_StackOverflow中文版 CPUs with the same structure, the barrier (like pthread_barrier) can be useful. It will stop all CPUs entering to it until last CPU will run the barrier. From this moment all CPUs again works.
You can probably get equivalent behavior using a completion:
struct fake_barrier_t {
atomic_t count;
struct completion comp;
}
/* run before each pass */
void initialize_fake_barrier(struct fake_barrier_t* b)
{
atomic_set(&b->count, 0);
init_completion(&b->comp);
}
/* make all tasks sleep until nth arrives, then wake all. */
void fake_barrier(struct fake_barrier_t* b, int n)
{
if (atomic_inc_return(&b->count) < n)
wait_for_completion(&b->comp);
else
complete_all(&b->comp);
}
I'm not familiar with the pthread_barrier() construct, but the kernel has a large number of options for memory barriers.
See lxr memory barriers for the documentation
If you're trying to force a set of threads to wait for each other, you can probably hack something together with mutexes and/or waitqueues - though I'm not sure when you'd want to do that. When do you ever want threads to wait on each other? I am very curious now...
精彩评论