0

通常我们必须这样做才能从函数指针调用函数:

int foo()
{
}

int main()
{
    int (*pFoo)() = foo; // pFoo points to function foo()
    foo();
    return 0;
}

在 Linux 内核代码中,sched_class有很多函数指针:

struct sched_class {
        const struct sched_class *next;

        void (*enqueue_task) (struct rq *rq, struct task_struct *p, int flags);
        void (*dequeue_task) (struct rq *rq, struct task_struct *p, int flags);
        void (*yield_task) (struct rq *rq);
        bool (*yield_to_task) (struct rq *rq, struct task_struct *p, bool preempt);
        .....
}

pick_next_tasksched_class函数中,它定义了一个named的本地实例class,并直接调用其中的函数,而不分配给具有相同签名的外部函数(从 开始for_each_class):

static inline struct task_struct *
pick_next_task(struct rq *rq)
{
        const struct sched_class *class;
        struct task_struct *p;
    /*
     * Optimization: we know that if all tasks are in
     * the fair class we can call that function directly:
     */
    if (likely(rq->nr_running == rq->cfs.h_nr_running)) {
            p = fair_sched_class.pick_next_task(rq);
            if (likely(p))
                    return p;
    }

    for_each_class(class) {
            p = class->pick_next_task(rq);
            if (p)
                    return p;
    }

    BUG(); /* the idle class will always have a runnable task */
}

是不是因为 中的每个函数指针sched_class都和实际实现的函数同名,所以每次通过 的函数指针调用时sched_class,都会自动在内核地址空间中找到匹配的符号?

4

3 回答 3

5

的定义for_each_class应该为你清楚

 #define for_each_class(class) \
       for (class = sched_class_highest; class; class = class->next)

如果你继续追踪,sched_class_highest最终会是这样的

#define sched_class_highest (&stop_sched_class)
extern const struct sched_class stop_sched_class;

/*
* Simple, special scheduling class for the per-CPU stop tasks:
*/
const struct sched_class stop_sched_class = {
      .next                   = &rt_sched_class,

      .enqueue_task           = enqueue_task_stop,
      .dequeue_task           = dequeue_task_stop,
      .yield_task             = yield_task_stop,

      .check_preempt_curr     = check_preempt_curr_stop,

      .pick_next_task         = pick_next_task_stop,
      .put_prev_task          = put_prev_task_stop,

#ifdef CONFIG_SMP
      .select_task_rq         = select_task_rq_stop,
#endif

     .set_curr_task          = set_curr_task_stop,
     .task_tick              = task_tick_stop,

     .get_rr_interval        = get_rr_interval_stop,

     .prio_changed           = prio_changed_stop,
     .switched_to            = switched_to_stop,
};

现在你快乐吗?:)

于 2012-04-04T07:50:56.747 回答
2

https://github.com/torvalds/linux/blob/v3.3/kernel/sched/sched.h#L850

看看for_each_class宏的扩展。class它在使用指针之前为其赋值。

于 2012-04-04T07:50:42.980 回答
0

每个sched_class结构及其包含的函数指针都被初始化(否则,它可能是一个错误)。例如,公平调度kernel/sched/fair.c在(参见此处)中初始化:

const struct sched_class fair_sched_class = {
        .next                   = &idle_sched_class,
        /* lots of assignments */
        .pick_next_task         = pick_next_task_fair,
        /* etc. */
};
于 2012-04-04T07:55:07.573 回答