0

Linux kernel has the following definition inside sched_class struct definition.

#ifdef CONFIG_SMP
  int  (*select_task_rq)(struct task_struct *p, int task_cpu, int sd_flag, int flags);
  .......
  .......

Now what does this line mean, I have fairly good knowledge in C & C++. But I was having hard time understanding this declaration construct:

 int  (*select_task_rq)(struct task_struct *p, int task_cpu, int sd_flag, int flags);

Can someone please explain what does this mean and what it does.

Thanks

Ace
  • 1,501
  • 4
  • 30
  • 49
  • 4
    It's just a [function pointer](http://www.cprogramming.com/tutorial/function-pointers.html). – Paul R Jan 30 '14 at 22:20

1 Answers1

2

It's a function pointer.

int (*select_task_rq)(struct task_struct *p, int task_cpu, int sd_flag, int flags);

means that select_task_rq is a pointer to a function returning an int and taking all those parameter types.

It can be set to any compatible function and called as if it were a fixed function name. It's sometimes used to provide a primitive form of polymorphism in C, or to allow easy switching between functions based on other information.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Awesome, did some wonderful research on it myself as well. But what if I want a generic pointer that points to any function like ANYRETURN function(ANYTYPE, ANYTYPE); ? I mean it should be more like a void pointer assignable to any type? – Ace Jan 30 '14 at 22:35
  • @Anup, you hit the nail on the head. A void pointer can be converted to/from any other pointer, including a function pointer. – paxdiablo Jan 30 '14 at 23:08
  • @AnupSaumithri: In C++, you'd use `std::function<>` for that. It can point to any function, but still is typesafe and therefore can be called directly without cast (provided that you pass the right arguments, i.e. two `ANYTYPE` objects in this case) – MSalters Jan 31 '14 at 08:08