/* Tries to find a free spu to run @ctx. If no free spu is available add the context to the runqueue so it gets woken up once an spu is available.  */
int spu_activate(struct spu_context *ctx, unsigned long flags)
/* Tries to find a free spu to run @ctx. If no free spu is available add the context to the runqueue so it gets woken up once an spu is available.  */
int spu_activate(struct spu_context *ctx, unsigned long flags)
{
	struct spu *spu;
	if (ctx->spu)
		return 0;
spu_activate_top:
	if (signal_pending(current))
		return -ERESTARTSYS;
	spu = spu_get_idle(ctx);
	if (!spu && rt_prio(ctx->prio))
		spu = find_victim(ctx);
	if (spu) {
		unsigned long runcntl;
		runcntl = ctx->ops->runcntl_read(ctx);
		__spu_schedule(spu, ctx);
		if (runcntl & SPU_RUNCNTL_RUNNABLE)
			spuctx_switch_state(ctx, SPU_UTIL_USER);
		return 0;
	}
	if (ctx->flags & SPU_CREATE_NOSCHED) {
		spu_prio_wait(ctx);
		goto spu_activate_top;
	}
	spu_add_to_rq(ctx);
	return 0;
}