Back to index

glibc  2.9
lowlevellock.h
Go to the documentation of this file.
00001 /* Copyright (C) 2002, 2003, 2004, 2006, 2007 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #ifndef _LOWLEVELLOCK_H
00021 #define _LOWLEVELLOCK_H     1
00022 
00023 #ifndef __ASSEMBLER__
00024 # include <time.h>
00025 # include <sys/param.h>
00026 # include <bits/pthreadtypes.h>
00027 # include <kernel-features.h>
00028 # include <tcb-offsets.h>
00029 
00030 # ifndef LOCK_INSTR
00031 #  ifdef UP
00032 #   define LOCK_INSTR       /* nothing */
00033 #  else
00034 #   define LOCK_INSTR "lock;"
00035 #  endif
00036 # endif
00037 #else
00038 # ifndef LOCK
00039 #  ifdef UP
00040 #   define LOCK
00041 #  else
00042 #   define LOCK lock
00043 #  endif
00044 # endif
00045 #endif
00046 
00047 #define SYS_futex           202
00048 #define FUTEX_WAIT          0
00049 #define FUTEX_WAKE          1
00050 #define FUTEX_CMP_REQUEUE   4
00051 #define FUTEX_WAKE_OP          5
00052 #define FUTEX_LOCK_PI              6
00053 #define FUTEX_UNLOCK_PI            7
00054 #define FUTEX_TRYLOCK_PI    8
00055 #define FUTEX_PRIVATE_FLAG  128
00056 
00057 #define FUTEX_OP_CLEAR_WAKE_IF_GT_ONE     ((4 << 24) | 1)
00058 
00059 /* Values for 'private' parameter of locking macros.  Yes, the
00060    definition seems to be backwards.  But it is not.  The bit will be
00061    reversed before passing to the system call.  */
00062 #define LLL_PRIVATE  0
00063 #define LLL_SHARED   FUTEX_PRIVATE_FLAG
00064 
00065 #ifndef __ASSEMBLER__
00066 
00067 #if !defined NOT_IN_libc || defined IS_IN_rtld
00068 /* In libc.so or ld.so all futexes are private.  */
00069 # ifdef __ASSUME_PRIVATE_FUTEX
00070 #  define __lll_private_flag(fl, private) \
00071   ((fl) | FUTEX_PRIVATE_FLAG)
00072 # else
00073 #  define __lll_private_flag(fl, private) \
00074   ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex))
00075 # endif
00076 #else
00077 # ifdef __ASSUME_PRIVATE_FUTEX
00078 #  define __lll_private_flag(fl, private) \
00079   (((fl) | FUTEX_PRIVATE_FLAG) ^ (private))
00080 # else
00081 #  define __lll_private_flag(fl, private) \
00082   (__builtin_constant_p (private)                                    \
00083    ? ((private) == 0                                                 \
00084       ? ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex))          \
00085       : (fl))                                                        \
00086    : ({ unsigned int __fl = ((private) ^ FUTEX_PRIVATE_FLAG);               \
00087        asm ("andl %%fs:%P1, %0" : "+r" (__fl)                               \
00088             : "i" (offsetof (struct pthread, header.private_futex)));       \
00089        __fl | (fl); }))
00090 # endif             
00091 #endif
00092 
00093 /* Initializer for lock.  */
00094 #define LLL_LOCK_INITIALIZER              (0)
00095 #define LLL_LOCK_INITIALIZER_LOCKED       (1)
00096 #define LLL_LOCK_INITIALIZER_WAITERS      (2)
00097 
00098 /* Delay in spinlock loop.  */
00099 #define BUSY_WAIT_NOP         asm ("rep; nop")
00100 
00101 
00102 #define LLL_STUB_UNWIND_INFO_START \
00103        ".section     .eh_frame,\"a\",@progbits\n"              \
00104 "7:\t" ".long 9f-8f  # Length of Common Information Entry\n" \
00105 "8:\t" ".long 0x0    # CIE Identifier Tag\n\t"          \
00106        ".byte 0x1    # CIE Version\n\t"                 \
00107        ".ascii \"zR\\0\"    # CIE Augmentation\n\t"     \
00108        ".uleb128 0x1 # CIE Code Alignment Factor\n\t"   \
00109        ".sleb128 -8  # CIE Data Alignment Factor\n\t"   \
00110        ".byte 0x10   # CIE RA Column\n\t"                      \
00111        ".uleb128 0x1 # Augmentation size\n\t"           \
00112        ".byte 0x1b   # FDE Encoding (pcrel sdata4)\n\t"        \
00113        ".byte 0x12   # DW_CFA_def_cfa_sf\n\t"           \
00114        ".uleb128 0x7\n\t"                               \
00115        ".sleb128 16\n\t"                                \
00116        ".align 8\n"                                     \
00117 "9:\t" ".long 23f-10f       # FDE Length\n"                    \
00118 "10:\t"       ".long 10b-7b # FDE CIE offset\n\t"                     \
00119        ".long 1b-.   # FDE initial location\n\t"               \
00120        ".long 6b-1b  # FDE address range\n\t"           \
00121        ".uleb128 0x0 # Augmentation size\n\t"           \
00122        ".byte 0x16   # DW_CFA_val_expression\n\t"              \
00123        ".uleb128 0x10\n\t"                              \
00124        ".uleb128 12f-11f\n"                                    \
00125 "11:\t"       ".byte 0x80   # DW_OP_breg16\n\t"                \
00126        ".sleb128 4b-1b\n"
00127 #define LLL_STUB_UNWIND_INFO_END \
00128        ".byte 0x16   # DW_CFA_val_expression\n\t"              \
00129        ".uleb128 0x10\n\t"                              \
00130        ".uleb128 14f-13f\n"                                    \
00131 "13:\t"       ".byte 0x80   # DW_OP_breg16\n\t"                \
00132        ".sleb128 4b-2b\n"                               \
00133 "14:\t"       ".byte 0x40 + (3b-2b) # DW_CFA_advance_loc\n\t"  \
00134        ".byte 0x0e   # DW_CFA_def_cfa_offset\n\t"              \
00135        ".uleb128 0\n\t"                                 \
00136        ".byte 0x16   # DW_CFA_val_expression\n\t"              \
00137        ".uleb128 0x10\n\t"                              \
00138        ".uleb128 16f-15f\n"                                    \
00139 "15:\t"       ".byte 0x80   # DW_OP_breg16\n\t"                \
00140        ".sleb128 4b-3b\n"                               \
00141 "16:\t"       ".byte 0x40 + (4b-3b-1) # DW_CFA_advance_loc\n\t"       \
00142        ".byte 0x0e   # DW_CFA_def_cfa_offset\n\t"              \
00143        ".uleb128 128\n\t"                               \
00144        ".byte 0x16   # DW_CFA_val_expression\n\t"              \
00145        ".uleb128 0x10\n\t"                              \
00146        ".uleb128 20f-17f\n"                                    \
00147 "17:\t"       ".byte 0x80   # DW_OP_breg16\n\t"                \
00148        ".sleb128 19f-18f\n\t"                                  \
00149        ".byte 0x0d   # DW_OP_const4s\n"                 \
00150 "18:\t"       ".4byte       4b-.\n\t"                                 \
00151        ".byte 0x1c   # DW_OP_minus\n\t"                 \
00152        ".byte 0x0d   # DW_OP_const4s\n"                 \
00153 "19:\t"       ".4byte       24f-.\n\t"                                \
00154        ".byte 0x22   # DW_OP_plus\n"                    \
00155 "20:\t"       ".byte 0x40 + (5b-4b+1) # DW_CFA_advance_loc\n\t"       \
00156        ".byte 0x13   # DW_CFA_def_cfa_offset_sf\n\t"    \
00157        ".sleb128 16\n\t"                                \
00158        ".byte 0x16   # DW_CFA_val_expression\n\t"              \
00159        ".uleb128 0x10\n\t"                              \
00160        ".uleb128 22f-21f\n"                                    \
00161 "21:\t"       ".byte 0x80   # DW_OP_breg16\n\t"                \
00162        ".sleb128 4b-5b\n"                               \
00163 "22:\t"       ".align 8\n"                                     \
00164 "23:\t"       ".previous\n"
00165 
00166 /* Unwind info for
00167    1: leaq ..., %rdi
00168    2: subq $128, %rsp
00169    3: callq ...
00170    4: addq $128, %rsp
00171    5: jmp 24f
00172    6:
00173    snippet.  */
00174 #define LLL_STUB_UNWIND_INFO_5 \
00175 LLL_STUB_UNWIND_INFO_START                              \
00176 "12:\t"       ".byte 0x40 + (2b-1b) # DW_CFA_advance_loc\n\t"  \
00177 LLL_STUB_UNWIND_INFO_END
00178 
00179 /* Unwind info for
00180    1: leaq ..., %rdi
00181    0: movq ..., %rdx
00182    2: subq $128, %rsp
00183    3: callq ...
00184    4: addq $128, %rsp
00185    5: jmp 24f
00186    6:
00187    snippet.  */
00188 #define LLL_STUB_UNWIND_INFO_6 \
00189 LLL_STUB_UNWIND_INFO_START                              \
00190 "12:\t"       ".byte 0x40 + (0b-1b) # DW_CFA_advance_loc\n\t"  \
00191        ".byte 0x16   # DW_CFA_val_expression\n\t"              \
00192        ".uleb128 0x10\n\t"                              \
00193        ".uleb128 26f-25f\n"                                    \
00194 "25:\t"       ".byte 0x80   # DW_OP_breg16\n\t"                \
00195        ".sleb128 4b-0b\n"                               \
00196 "26:\t"       ".byte 0x40 + (2b-0b) # DW_CFA_advance_loc\n\t"  \
00197 LLL_STUB_UNWIND_INFO_END
00198 
00199 
00200 #define lll_futex_wait(futex, val, private) \
00201   lll_futex_timed_wait(futex, val, NULL, private)
00202 
00203 
00204 #define lll_futex_timed_wait(futex, val, timeout, private) \
00205   ({                                                                 \
00206     register const struct timespec *__to __asm ("r10") = timeout;           \
00207     int __status;                                                    \
00208     register __typeof (val) _val __asm ("edx") = (val);                     \
00209     __asm __volatile ("syscall"                                             \
00210                     : "=a" (__status)                                       \
00211                     : "0" (SYS_futex), "D" (futex),                         \
00212                      "S" (__lll_private_flag (FUTEX_WAIT, private)),        \
00213                      "d" (_val), "r" (__to)                                 \
00214                     : "memory", "cc", "r11", "cx");                         \
00215     __status;                                                        \
00216   })
00217 
00218 
00219 #define lll_futex_wake(futex, nr, private) \
00220   do {                                                               \
00221     int __ignore;                                                    \
00222     register __typeof (nr) _nr __asm ("edx") = (nr);                        \
00223     __asm __volatile ("syscall"                                             \
00224                     : "=a" (__ignore)                                       \
00225                     : "0" (SYS_futex), "D" (futex),                         \
00226                      "S" (__lll_private_flag (FUTEX_WAKE, private)),        \
00227                      "d" (_nr)                                       \
00228                     : "memory", "cc", "r10", "r11", "cx");                  \
00229   } while (0)
00230 
00231 
00232 /* NB: in the lll_trylock macro we simply return the value in %eax
00233    after the cmpxchg instruction.  In case the operation succeded this
00234    value is zero.  In case the operation failed, the cmpxchg instruction
00235    has loaded the current value of the memory work which is guaranteed
00236    to be nonzero.  */
00237 #if defined NOT_IN_libc || defined UP
00238 # define __lll_trylock_asm LOCK_INSTR "cmpxchgl %2, %1"
00239 #else
00240 # define __lll_trylock_asm "cmpl $0, __libc_multiple_threads(%%rip)\n\t"      \
00241                         "je 0f\n\t"                                         \
00242                         "lock; cmpxchgl %2, %1\n\t"                         \
00243                         "jmp 1f\n\t"                                        \
00244                         "0:\tcmpxchgl %2, %1\n\t"                           \
00245                         "1:"
00246 #endif
00247 
00248 #define lll_trylock(futex) \
00249   ({ int ret;                                                        \
00250      __asm __volatile (__lll_trylock_asm                             \
00251                      : "=a" (ret), "=m" (futex)                      \
00252                      : "r" (LLL_LOCK_INITIALIZER_LOCKED), "m" (futex),      \
00253                       "0" (LLL_LOCK_INITIALIZER)                     \
00254                      : "memory");                                    \
00255      ret; })
00256 
00257 #define lll_robust_trylock(futex, id) \
00258   ({ int ret;                                                        \
00259      __asm __volatile (LOCK_INSTR "cmpxchgl %2, %1"                         \
00260                      : "=a" (ret), "=m" (futex)                      \
00261                      : "r" (id), "m" (futex),    "0" (LLL_LOCK_INITIALIZER)    \
00262                      : "memory");                                    \
00263      ret; })
00264 
00265 #define lll_cond_trylock(futex) \
00266   ({ int ret;                                                        \
00267      __asm __volatile (LOCK_INSTR "cmpxchgl %2, %1"                         \
00268                      : "=a" (ret), "=m" (futex)                      \
00269                      : "r" (LLL_LOCK_INITIALIZER_WAITERS),                  \
00270                       "m" (futex), "0" (LLL_LOCK_INITIALIZER)        \
00271                      : "memory");                                    \
00272      ret; })
00273 
00274 #if defined NOT_IN_libc || defined UP
00275 # define __lll_lock_asm_start LOCK_INSTR "cmpxchgl %4, %2\n\t"              \
00276                            "jnz 1f\n\t"
00277 #else
00278 # define __lll_lock_asm_start "cmpl $0, __libc_multiple_threads(%%rip)\n\t"   \
00279                            "je 0f\n\t"                               \
00280                            "lock; cmpxchgl %4, %2\n\t"               \
00281                            "jnz 1f\n\t"                              \
00282                            "jmp 24f\n"                               \
00283                            "0:\tcmpxchgl %4, %2\n\t"                        \
00284                            "jnz 1f\n\t"
00285 #endif
00286 
00287 #define lll_lock(futex, private) \
00288   (void)                                                             \
00289     ({ int ignore1, ignore2, ignore3;                                       \
00290        if (__builtin_constant_p (private) && (private) == LLL_PRIVATE)             \
00291         __asm __volatile (__lll_lock_asm_start                              \
00292                         ".subsection 1\n\t"                                 \
00293                         ".type _L_lock_%=, @function\n"              \
00294                         "_L_lock_%=:\n"                              \
00295                         "1:\tleaq %2, %%rdi\n"                       \
00296                         "2:\tsubq $128, %%rsp\n"                     \
00297                         "3:\tcallq __lll_lock_wait_private\n"        \
00298                         "4:\taddq $128, %%rsp\n"                     \
00299                         "5:\tjmp 24f\n"                              \
00300                         "6:\t.size _L_lock_%=, 6b-1b\n\t"                   \
00301                         ".previous\n"                                \
00302                         LLL_STUB_UNWIND_INFO_5                       \
00303                         "24:"                                        \
00304                         : "=S" (ignore1), "=&D" (ignore2), "=m" (futex),   \
00305                           "=a" (ignore3)                             \
00306                         : "0" (1), "m" (futex), "3" (0)              \
00307                         : "cx", "r11", "cc", "memory");              \
00308        else                                                          \
00309         __asm __volatile (__lll_lock_asm_start                              \
00310                         ".subsection 1\n\t"                                 \
00311                         ".type _L_lock_%=, @function\n"              \
00312                         "_L_lock_%=:\n"                              \
00313                         "1:\tleaq %2, %%rdi\n"                       \
00314                         "2:\tsubq $128, %%rsp\n"                     \
00315                         "3:\tcallq __lll_lock_wait\n"                \
00316                         "4:\taddq $128, %%rsp\n"                     \
00317                         "5:\tjmp 24f\n"                              \
00318                         "6:\t.size _L_lock_%=, 6b-1b\n\t"                   \
00319                         ".previous\n"                                \
00320                         LLL_STUB_UNWIND_INFO_5                       \
00321                         "24:"                                        \
00322                         : "=S" (ignore1), "=D" (ignore2), "=m" (futex),    \
00323                           "=a" (ignore3)                             \
00324                         : "1" (1), "m" (futex), "3" (0), "0" (private)     \
00325                         : "cx", "r11", "cc", "memory");              \
00326     })                                                               \
00327 
00328 #define lll_robust_lock(futex, id, private) \
00329   ({ int result, ignore1, ignore2;                                   \
00330     __asm __volatile (LOCK_INSTR "cmpxchgl %4, %2\n\t"                      \
00331                     "jnz 1f\n\t"                                     \
00332                     ".subsection 1\n\t"                              \
00333                     ".type _L_robust_lock_%=, @function\n"                  \
00334                     "_L_robust_lock_%=:\n"                                  \
00335                     "1:\tleaq %2, %%rdi\n"                                  \
00336                     "2:\tsubq $128, %%rsp\n"                                \
00337                     "3:\tcallq __lll_robust_lock_wait\n"                    \
00338                     "4:\taddq $128, %%rsp\n"                                \
00339                     "5:\tjmp 24f\n"                                         \
00340                     "6:\t.size _L_robust_lock_%=, 6b-1b\n\t"                \
00341                     ".previous\n"                                    \
00342                     LLL_STUB_UNWIND_INFO_5                                  \
00343                     "24:"                                            \
00344                     : "=S" (ignore1), "=D" (ignore2), "=m" (futex),         \
00345                      "=a" (result)                                   \
00346                     : "1" (id), "m" (futex), "3" (0), "0" (private)         \
00347                     : "cx", "r11", "cc", "memory");                         \
00348     result; })
00349 
00350 #define lll_cond_lock(futex, private) \
00351   (void)                                                             \
00352     ({ int ignore1, ignore2, ignore3;                                       \
00353        __asm __volatile (LOCK_INSTR "cmpxchgl %4, %2\n\t"                   \
00354                       "jnz 1f\n\t"                                   \
00355                       ".subsection 1\n\t"                            \
00356                       ".type _L_cond_lock_%=, @function\n"                  \
00357                       "_L_cond_lock_%=:\n"                                  \
00358                       "1:\tleaq %2, %%rdi\n"                                \
00359                       "2:\tsubq $128, %%rsp\n"                       \
00360                       "3:\tcallq __lll_lock_wait\n"                         \
00361                       "4:\taddq $128, %%rsp\n"                       \
00362                       "5:\tjmp 24f\n"                                \
00363                       "6:\t.size _L_cond_lock_%=, 6b-1b\n\t"                \
00364                       ".previous\n"                                         \
00365                       LLL_STUB_UNWIND_INFO_5                                \
00366                       "24:"                                          \
00367                       : "=S" (ignore1), "=D" (ignore2), "=m" (futex),      \
00368                         "=a" (ignore3)                               \
00369                       : "1" (2), "m" (futex), "3" (0), "0" (private)        \
00370                       : "cx", "r11", "cc", "memory");                \
00371     })
00372 
00373 #define lll_robust_cond_lock(futex, id, private) \
00374   ({ int result, ignore1, ignore2;                                   \
00375     __asm __volatile (LOCK_INSTR "cmpxchgl %4, %2\n\t"                      \
00376                     "jnz 1f\n\t"                                     \
00377                     ".subsection 1\n\t"                              \
00378                     ".type _L_robust_cond_lock_%=, @function\n"             \
00379                     "_L_robust_cond_lock_%=:\n"                      \
00380                     "1:\tleaq %2, %%rdi\n"                                  \
00381                     "2:\tsubq $128, %%rsp\n"                                \
00382                     "3:\tcallq __lll_robust_lock_wait\n"                    \
00383                     "4:\taddq $128, %%rsp\n"                                \
00384                     "5:\tjmp 24f\n"                                         \
00385                     "6:\t.size _L_robust_cond_lock_%=, 6b-1b\n\t"           \
00386                     ".previous\n"                                    \
00387                     LLL_STUB_UNWIND_INFO_5                                  \
00388                     "24:"                                            \
00389                     : "=S" (ignore1), "=D" (ignore2), "=m" (futex),         \
00390                      "=a" (result)                                   \
00391                     : "1" (id | FUTEX_WAITERS), "m" (futex), "3" (0),       \
00392                      "0" (private)                                   \
00393                     : "cx", "r11", "cc", "memory");                         \
00394     result; })
00395 
00396 #define lll_timedlock(futex, timeout, private) \
00397   ({ int result, ignore1, ignore2, ignore3;                                 \
00398      __asm __volatile (LOCK_INSTR "cmpxchgl %1, %4\n\t"                     \
00399                      "jnz 1f\n\t"                                    \
00400                      ".subsection 1\n\t"                             \
00401                      ".type _L_timedlock_%=, @function\n"                   \
00402                      "_L_timedlock_%=:\n"                            \
00403                      "1:\tleaq %4, %%rdi\n"                                 \
00404                      "0:\tmovq %8, %%rdx\n"                                 \
00405                      "2:\tsubq $128, %%rsp\n"                               \
00406                      "3:\tcallq __lll_timedlock_wait\n"              \
00407                      "4:\taddq $128, %%rsp\n"                               \
00408                      "5:\tjmp 24f\n"                                        \
00409                      "6:\t.size _L_timedlock_%=, 6b-1b\n\t"                 \
00410                      ".previous\n"                                   \
00411                      LLL_STUB_UNWIND_INFO_6                                 \
00412                      "24:"                                           \
00413                      : "=a" (result), "=D" (ignore1), "=S" (ignore2),       \
00414                       "=&d" (ignore3), "=m" (futex)                         \
00415                      : "0" (0), "1" (1), "m" (futex), "m" (timeout),        \
00416                       "2" (private)                                         \
00417                      : "memory", "cx", "cc", "r10", "r11");                 \
00418      result; })
00419 
00420 #define lll_robust_timedlock(futex, timeout, id, private) \
00421   ({ int result, ignore1, ignore2, ignore3;                                 \
00422      __asm __volatile (LOCK_INSTR "cmpxchgl %1, %4\n\t"                     \
00423                      "jnz 1f\n\t"                                    \
00424                      ".subsection 1\n\t"                             \
00425                      ".type _L_robust_timedlock_%=, @function\n"            \
00426                      "_L_robust_timedlock_%=:\n"                     \
00427                      "1:\tleaq %4, %%rdi\n"                                 \
00428                      "0:\tmovq %8, %%rdx\n"                                 \
00429                      "2:\tsubq $128, %%rsp\n"                               \
00430                      "3:\tcallq __lll_robust_timedlock_wait\n"       \
00431                      "4:\taddq $128, %%rsp\n"                               \
00432                      "5:\tjmp 24f\n"                                        \
00433                      "6:\t.size _L_robust_timedlock_%=, 6b-1b\n\t"          \
00434                      ".previous\n"                                   \
00435                      LLL_STUB_UNWIND_INFO_6                                 \
00436                      "24:"                                           \
00437                      : "=a" (result), "=D" (ignore1), "=S" (ignore2),       \
00438                       "=&d" (ignore3), "=m" (futex)                         \
00439                      : "0" (0), "1" (id), "m" (futex), "m" (timeout),       \
00440                       "2" (private)                                         \
00441                      : "memory", "cx", "cc", "r10", "r11");                 \
00442      result; })
00443 
00444 #if defined NOT_IN_libc || defined UP
00445 # define __lll_unlock_asm_start LOCK_INSTR "decl %0\n\t"                    \
00446                             "jne 1f\n\t"
00447 #else
00448 # define __lll_unlock_asm_start "cmpl $0, __libc_multiple_threads(%%rip)\n\t" \
00449                             "je 0f\n\t"                              \
00450                             "lock; decl %0\n\t"                      \
00451                             "jne 1f\n\t"                             \
00452                             "jmp 24f\n\t"                            \
00453                             "0:\tdecl %0\n\t"                        \
00454                             "jne 1f\n\t"
00455 #endif
00456 
00457 #define lll_unlock(futex, private) \
00458   (void)                                                             \
00459     ({ int ignore;                                                   \
00460        if (__builtin_constant_p (private) && (private) == LLL_PRIVATE)             \
00461         __asm __volatile (__lll_unlock_asm_start                     \
00462                         ".subsection 1\n\t"                                 \
00463                         ".type _L_unlock_%=, @function\n"                   \
00464                         "_L_unlock_%=:\n"                            \
00465                         "1:\tleaq %0, %%rdi\n"                       \
00466                         "2:\tsubq $128, %%rsp\n"                     \
00467                         "3:\tcallq __lll_unlock_wake_private\n"             \
00468                         "4:\taddq $128, %%rsp\n"                     \
00469                         "5:\tjmp 24f\n"                              \
00470                         "6:\t.size _L_unlock_%=, 6b-1b\n\t"                 \
00471                         ".previous\n"                                \
00472                         LLL_STUB_UNWIND_INFO_5                       \
00473                         "24:"                                        \
00474                         : "=m" (futex), "=&D" (ignore)               \
00475                         : "m" (futex)                                \
00476                         : "ax", "cx", "r11", "cc", "memory");        \
00477        else                                                          \
00478         __asm __volatile (__lll_unlock_asm_start                     \
00479                         ".subsection 1\n\t"                                 \
00480                         ".type _L_unlock_%=, @function\n"                   \
00481                         "_L_unlock_%=:\n"                            \
00482                         "1:\tleaq %0, %%rdi\n"                       \
00483                         "2:\tsubq $128, %%rsp\n"                     \
00484                         "3:\tcallq __lll_unlock_wake\n"              \
00485                         "4:\taddq $128, %%rsp\n"                     \
00486                         "5:\tjmp 24f\n"                              \
00487                         "6:\t.size _L_unlock_%=, 6b-1b\n\t"                 \
00488                         ".previous\n"                                \
00489                         LLL_STUB_UNWIND_INFO_5                       \
00490                         "24:"                                        \
00491                         : "=m" (futex), "=&D" (ignore)               \
00492                         : "m" (futex), "S" (private)                        \
00493                         : "ax", "cx", "r11", "cc", "memory");        \
00494     })
00495 
00496 #define lll_robust_unlock(futex, private) \
00497   do                                                                 \
00498     {                                                                \
00499       int ignore;                                                    \
00500       __asm __volatile (LOCK_INSTR "andl %2, %0\n\t"                        \
00501                      "jne 1f\n\t"                                    \
00502                      ".subsection 1\n\t"                             \
00503                      ".type _L_robust_unlock_%=, @function\n"        \
00504                      "_L_robust_unlock_%=:\n"                        \
00505                      "1:\tleaq %0, %%rdi\n"                                 \
00506                      "2:\tsubq $128, %%rsp\n"                        \
00507                      "3:\tcallq __lll_unlock_wake\n"                        \
00508                      "4:\taddq $128, %%rsp\n"                        \
00509                      "5:\tjmp 24f\n"                                        \
00510                      "6:\t.size _L_robust_unlock_%=, 6b-1b\n\t"             \
00511                      ".previous\n"                                   \
00512                      LLL_STUB_UNWIND_INFO_5                                 \
00513                      "24:"                                           \
00514                      : "=m" (futex), "=&D" (ignore)                         \
00515                      : "i" (FUTEX_WAITERS), "m" (futex),                    \
00516                        "S" (private)                                        \
00517                      : "ax", "cx", "r11", "cc", "memory");                  \
00518     }                                                                \
00519   while (0)
00520 
00521 #define lll_robust_dead(futex, private) \
00522   do                                                                 \
00523     {                                                                \
00524       int ignore;                                                    \
00525       __asm __volatile (LOCK_INSTR "orl %3, (%2)\n\t"                       \
00526                      "syscall"                                       \
00527                      : "=m" (futex), "=a" (ignore)                          \
00528                      : "D" (&(futex)), "i" (FUTEX_OWNER_DIED),       \
00529                        "S" (__lll_private_flag (FUTEX_WAKE, private)),     \
00530                        "1" (__NR_futex), "d" (1)                     \
00531                      : "cx", "r11", "cc", "memory");                        \
00532     }                                                                \
00533   while (0)
00534 
00535 /* Returns non-zero if error happened, zero if success.  */
00536 #define lll_futex_requeue(ftx, nr_wake, nr_move, mutex, val, private) \
00537   ({ int __res;                                                             \
00538      register int __nr_move __asm ("r10") = nr_move;                        \
00539      register void *__mutex __asm ("r8") = mutex;                           \
00540      register int __val __asm ("r9") = val;                                 \
00541      __asm __volatile ("syscall"                                     \
00542                      : "=a" (__res)                                         \
00543                      : "0" (__NR_futex), "D" ((void *) ftx),                \
00544                       "S" (__lll_private_flag (FUTEX_CMP_REQUEUE,           \
00545                                             private)), "d" (nr_wake),   \
00546                       "r" (__nr_move), "r" (__mutex), "r" (__val)           \
00547                      : "cx", "r11", "cc", "memory");                        \
00548      __res < 0; })
00549 
00550 #define lll_islocked(futex) \
00551   (futex != LLL_LOCK_INITIALIZER)
00552 
00553 
00554 /* The kernel notifies a process with uses CLONE_CLEARTID via futex
00555    wakeup when the clone terminates.  The memory location contains the
00556    thread ID while the clone is running and is reset to zero
00557    afterwards.
00558 
00559    The macro parameter must not have any side effect.  */
00560 #define lll_wait_tid(tid) \
00561   do {                                                               \
00562     int __ignore;                                                    \
00563     register __typeof (tid) _tid asm ("edx") = (tid);                       \
00564     if (_tid != 0)                                                   \
00565       __asm __volatile ("xorq %%r10, %%r10\n\t"                             \
00566                      "1:\tmovq %2, %%rax\n\t"                        \
00567                      "syscall\n\t"                                   \
00568                      "cmpl $0, (%%rdi)\n\t"                                 \
00569                      "jne 1b"                                        \
00570                      : "=&a" (__ignore)                              \
00571                      : "S" (FUTEX_WAIT), "i" (SYS_futex), "D" (&tid),      \
00572                        "d" (_tid)                                    \
00573                      : "memory", "cc", "r10", "r11", "cx");                 \
00574   } while (0)
00575 
00576 extern int __lll_timedwait_tid (int *tid, const struct timespec *abstime)
00577      attribute_hidden;
00578 #define lll_timedwait_tid(tid, abstime) \
00579   ({                                                                 \
00580     int __result = 0;                                                       \
00581     if (tid != 0)                                                    \
00582       {                                                                     \
00583        if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)          \
00584          __result = EINVAL;                                          \
00585        else                                                          \
00586          __result = __lll_timedwait_tid (&tid, abstime);                    \
00587       }                                                                     \
00588     __result; })
00589 
00590 #endif  /* !__ASSEMBLER__ */
00591 
00592 #endif /* lowlevellock.h */