Shan
Shan

Reputation: 5242

Why does the linux kernel contain pairs of functions that differ only by an __ prefix?

The Linux scheduler forking operation uses two function sched_fork() and __sched_fork(). __sched_fork() is called by sched_fork(). Similar code conventions with double underscore ( specifically used for system reserved name) are used in several other places in the kernel. How does defining two functions like this help, when one function could have been used? Why is this convention used?

Upvotes: 5

Views: 935

Answers (2)

bmargulies
bmargulies

Reputation: 100050

Historically, the pattern was to make the __function be the function that does the 'guts' of an operation, with an absolute minimum of argument checking. The no-__ function, on the other hand, would have some checks for parameter validity, or some other extra-cost operations. However, sometimes, it's just a modularity split into 'the very middle' and 'the rest'.

Upvotes: 4

Oleksii Shmalko
Oleksii Shmalko

Reputation: 3768

If you search sources, you will see that __sched_fork() is called from init_idle() as well as from sched_fork(). I think that it's a primary reason for defining two functions.

I also see pairs of functions (with and without __) with different implementations. For example, __hrtick_start() and hrtick_start(). However, they have slightly different usage: the first one should be called from hardirqs.

Other pairs may be separated when version with underscores does the core work of function and version without underscores does additional tests before invoking it. In that way, programmer can call underscored version if all conditions are already checked.

Upvotes: 2

Related Questions