0

In studying the spin lock synchronization mechanism, i noticed that for each function there is also a corresponding macro, for example for spin_lock there is a function

static __always_inline void spin_lock(spinlock_t *lock) {
    raw_spin_lock(&lock->rlock); }

and the macro

#define spin_lock(x) pthread_mutex_lock(x)

Why there are two different definitions and in which circumstances it is used each one of them? Thanks a lot!

  • Read the CPP conditions surrounding the two definitions and you'll have your answer. – Ginnungagap Aug 17 '21 at 12:18
  • @Ginnungagap that’s rather unlikely; there are no conditions surrounding the definitions, other than the usual header guards. – Stephen Kitt Aug 17 '21 at 12:22
  • It sounded like that was a question that noticed two different definitions based on compiler support. Since most of the context was omitted that's what I assumed. I read your answer afterwards, my bad! – Ginnungagap Aug 17 '21 at 12:24
  • I wonder if this programming-level question would be more appropriate on SO? – Jeff Schaller Aug 17 '21 at 13:09
  • 1
    @Jeff it’s a programming question, but it’s Linux-specific (*i.e.* someone knowledgeable in C but unfamiliar with the Linux kernel wouldn’t necessarily be able to answer it), so at home here as well as on SO. I don’t mind either way. – Stephen Kitt Aug 17 '21 at 13:25

1 Answers1

3

The macro is defined in tools/include/linux/spinlock.h: it is therefore part of the tools tree, and only used for kernel-related tools shipped in the kernel tree. It exists so that the same primitives can be used in the kernel tree for kernel code and user-space code; the macro translates spin_lock into the equivalent pthreads primitive.

The inline function, defined in include/linux/spinlock.h, is used in the kernel itself.

Stephen Kitt
  • 411,918
  • 54
  • 1,065
  • 1,164