NPTL: what is MULTIPLE_THREADS_OFFSET and how it is set

219 views Asked by At

There was rather huge commit-git into nptl/glibc:

http://sourceware.org/git/?p=glibc.git;a=commit;h=e51deae7f6ba2e490d5faeb8fbf4eeb32ae8f1ee

by Ulrich Drepper and Jakub Jelinek @ 2007

I interested in the change to lll_lock/lll_unlock

In SMP code, lll_unlock was modified to

+# define __lll_unlock_asm "cmpl $0, %%gs:%P3\n\t"                            \
+                         "je 0f\n\t"                                         \
+                         "lock\n"                                            \
+                         "0:\tsubl $1,%0\n\t"

where $0 is the futex address Zero and %P3 is MULTIPLE_THREADS_OFFSET constant.

So, What is stored at $gs:MULTIPLE_THREADS_OFFSET (aka $gs:(offsetof (tcbhead_t, multiple_threads))? How this value is changed in the lifetime of program?

1

There are 1 answers

3
Dan Aloni On BEST ANSWER

This jump is an optimization for the case where multi-threaded code is used in a single-threaded process. If you are using this code in a single-threaded process, then the 'lock' prefix to the subl instruction is not needed because atomically is not needed, and therefore can be eliminated in run-time. Instruction atomically incurs a run-time overhead at the CPU level.

So, the short answer is that multiple_threads field is a boolean that tells whether we are actually in a multi-threaded run-time environment.