简体   繁体   中英

NPTL: what is MULTIPLE_THREADS_OFFSET and how it is set

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM