Back to index

glibc  2.9
Defines | Functions
lowlevellock.h File Reference
#include <time.h>
#include <sys/param.h>
#include <bits/pthreadtypes.h>
#include <atomic.h>
#include <kernel-features.h>
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Defines

#define __NR_futex   221
#define FUTEX_WAIT   0
#define FUTEX_WAKE   1
#define FUTEX_REQUEUE   3
#define FUTEX_CMP_REQUEUE   4
#define FUTEX_WAKE_OP   5
#define FUTEX_OP_CLEAR_WAKE_IF_GT_ONE   ((4 << 24) | 1)
#define FUTEX_LOCK_PI   6
#define FUTEX_UNLOCK_PI   7
#define FUTEX_TRYLOCK_PI   8
#define FUTEX_PRIVATE_FLAG   128
#define LLL_PRIVATE   0
#define LLL_SHARED   FUTEX_PRIVATE_FLAG
#define __lll_private_flag(fl, private)   ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex))
#define lll_futex_wait(futexp, val, private)   lll_futex_timed_wait (futexp, val, NULL, private)
#define lll_futex_timed_wait(futexp, val, timespec, private)
#define lll_futex_wake(futexp, nr, private)
#define lll_robust_dead(futexv, private)
#define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val, private)
#define lll_futex_wake_unlock(futexp, nr_wake, nr_wake2, futexp2, private)
#define __lll_acq_instr   "isync"
#define __lll_rel_instr   "sync"
#define __lll_robust_trylock(futex, id)
#define lll_robust_trylock(lock, id)   __lll_robust_trylock (&(lock), id)
#define __lll_trylock(futex)   __lll_robust_trylock (futex, 1)
#define lll_trylock(lock)   __lll_trylock (&(lock))
#define __lll_cond_trylock(futex)   __lll_robust_trylock (futex, 2)
#define lll_cond_trylock(lock)   __lll_cond_trylock (&(lock))
#define lll_lock(lock, private)
#define lll_robust_lock(lock, id, private)
#define lll_cond_lock(lock, private)
#define lll_robust_cond_lock(lock, id, private)
#define lll_timedlock(lock, abstime, private)
#define lll_robust_timedlock(lock, abstime, id, private)
#define lll_unlock(lock, private)
#define lll_robust_unlock(lock, private)
#define lll_islocked(futex)   (futex != 0)
#define LLL_LOCK_INITIALIZER   (0)
#define LLL_LOCK_INITIALIZER_LOCKED   (1)
#define lll_wait_tid(tid)
#define lll_timedwait_tid(tid, abstime)

Functions

void __lll_lock_wait_private (int *futex) attribute_hidden
void __lll_lock_wait (int *futex, int private) attribute_hidden
int __lll_robust_lock_wait (int *futex, int private) attribute_hidden
int __lll_timedlock_wait (int *futex, const struct timespec *, int private) attribute_hidden
int __lll_robust_timedlock_wait (int *futex, const struct timespec *, int private) attribute_hidden
int __lll_timedwait_tid (int *, const struct timespec *) attribute_hidden

Define Documentation

#define __lll_acq_instr   "isync"

Definition at line 140 of file lowlevellock.h.

#define __lll_cond_trylock (   futex)    __lll_robust_trylock (futex, 2)

Definition at line 181 of file lowlevellock.h.

#define __lll_private_flag (   fl,
  private 
)    ((fl) | THREAD_GETMEM (THREAD_SELF, header.private_futex))

Definition at line 55 of file lowlevellock.h.

#define __lll_rel_instr   "sync"

Definition at line 154 of file lowlevellock.h.

#define __lll_robust_trylock (   futex,
  id 
)
Value:
({ int __val;                                                        \
     __asm __volatile ("1:  lwarx  %0,0,%2" MUTEX_HINT_ACQ "\n"             \
                     "      cmpwi  0,%0,0\n"                         \
                     "      bne    2f\n"                             \
                     "      stwcx. %3,0,%2\n"                        \
                     "      bne-   1b\n"                             \
                     "2:    " __lll_acq_instr                        \
                     : "=&r" (__val), "=m" (*futex)                         \
                     : "r" (futex), "r" (id), "m" (*futex)                  \
                     : "cr0", "memory");                             \
     __val;                                                          \
  })

Definition at line 159 of file lowlevellock.h.

#define __lll_trylock (   futex)    __lll_robust_trylock (futex, 1)

Definition at line 176 of file lowlevellock.h.

#define __NR_futex   221

Definition at line 30 of file lowlevellock.h.

#define FUTEX_CMP_REQUEUE   4

Definition at line 35 of file lowlevellock.h.

#define FUTEX_LOCK_PI   6

Definition at line 38 of file lowlevellock.h.

#define FUTEX_OP_CLEAR_WAKE_IF_GT_ONE   ((4 << 24) | 1)

Definition at line 37 of file lowlevellock.h.

#define FUTEX_PRIVATE_FLAG   128

Definition at line 41 of file lowlevellock.h.

#define FUTEX_REQUEUE   3

Definition at line 34 of file lowlevellock.h.

#define FUTEX_TRYLOCK_PI   8

Definition at line 40 of file lowlevellock.h.

#define FUTEX_UNLOCK_PI   7

Definition at line 39 of file lowlevellock.h.

#define FUTEX_WAIT   0

Definition at line 32 of file lowlevellock.h.

#define FUTEX_WAKE   1

Definition at line 33 of file lowlevellock.h.

#define FUTEX_WAKE_OP   5

Definition at line 36 of file lowlevellock.h.

#define lll_cond_lock (   lock,
  private 
)
Value:
(void) ({                                                            \
    int *__futex = &(lock);                                          \
    if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 2, 0),\
                       0) != 0)                                      \
      __lll_lock_wait (__futex, private);                            \
  })

Definition at line 213 of file lowlevellock.h.

#define lll_cond_trylock (   lock)    __lll_cond_trylock (&(lock))

Definition at line 183 of file lowlevellock.h.

#define lll_futex_requeue (   futexp,
  nr_wake,
  nr_move,
  mutex,
  val,
  private 
)
Value:
({                                                                   \
    INTERNAL_SYSCALL_DECL (__err);                                   \
    long int __ret;                                                  \
                                                                     \
    __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp),                    \
                           __lll_private_flag (FUTEX_CMP_REQUEUE, private),\
                           (nr_wake), (nr_move), (mutex), (val));           \
    INTERNAL_SYSCALL_ERROR_P (__ret, __err);                                \
  })

Definition at line 111 of file lowlevellock.h.

#define lll_futex_timed_wait (   futexp,
  val,
  timespec,
  private 
)
Value:
({                                                                   \
    INTERNAL_SYSCALL_DECL (__err);                                   \
    long int __ret;                                                  \
                                                                     \
    __ret = INTERNAL_SYSCALL (futex, __err, 4, (futexp),                    \
                           __lll_private_flag (FUTEX_WAIT, private),        \
                           (val), (timespec));                       \
    INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret;               \
  })

Definition at line 76 of file lowlevellock.h.

#define lll_futex_wait (   futexp,
  val,
  private 
)    lll_futex_timed_wait (futexp, val, NULL, private)

Definition at line 73 of file lowlevellock.h.

#define lll_futex_wake (   futexp,
  nr,
  private 
)
Value:
({                                                                   \
    INTERNAL_SYSCALL_DECL (__err);                                   \
    long int __ret;                                                  \
                                                                     \
    __ret = INTERNAL_SYSCALL (futex, __err, 4, (futexp),                    \
                           __lll_private_flag (FUTEX_WAKE, private),        \
                           (nr), 0);                                        \
    INTERNAL_SYSCALL_ERROR_P (__ret, __err) ? -__ret : __ret;               \
  })

Definition at line 87 of file lowlevellock.h.

#define lll_futex_wake_unlock (   futexp,
  nr_wake,
  nr_wake2,
  futexp2,
  private 
)
Value:
({                                                                   \
    INTERNAL_SYSCALL_DECL (__err);                                   \
    long int __ret;                                                  \
                                                                     \
    __ret = INTERNAL_SYSCALL (futex, __err, 6, (futexp),                    \
                           __lll_private_flag (FUTEX_WAKE_OP, private),    \
                           (nr_wake), (nr_wake2), (futexp2),                \
                           FUTEX_OP_CLEAR_WAKE_IF_GT_ONE);                  \
    INTERNAL_SYSCALL_ERROR_P (__ret, __err);                                \
  })

Definition at line 123 of file lowlevellock.h.

#define lll_islocked (   futex)    (futex != 0)

Definition at line 274 of file lowlevellock.h.

#define lll_lock (   lock,
  private 
)
Value:
(void) ({                                                            \
    int *__futex = &(lock);                                          \
    if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 1, 0),\
                       0) != 0)                                      \
      {                                                                     \
       if (__builtin_constant_p (private) && (private) == LLL_PRIVATE)             \
         __lll_lock_wait_private (__futex);                                 \
	else								      \
	  __lll_lock_wait (__futex, private);                               \
      }                                                                     \
  })

Definition at line 190 of file lowlevellock.h.

#define LLL_LOCK_INITIALIZER   (0)

Definition at line 279 of file lowlevellock.h.

#define LLL_LOCK_INITIALIZER_LOCKED   (1)

Definition at line 280 of file lowlevellock.h.

#define LLL_PRIVATE   0

Definition at line 46 of file lowlevellock.h.

#define lll_robust_cond_lock (   lock,
  id,
  private 
)
Value:
({                                                                   \
    int *__futex = &(lock);                                          \
    int __val = 0;                                                   \
    int __id = id | FUTEX_WAITERS;                                   \
    if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, __id,\
                                                        0), 0))             \
      __val = __lll_robust_lock_wait (__futex, private);                    \
    __val;                                                           \
  })

Definition at line 221 of file lowlevellock.h.

#define lll_robust_dead (   futexv,
  private 
)
Value:
do                                                                   \
    {                                                                \
      INTERNAL_SYSCALL_DECL (__err);                                        \
      int *__futexp = &(futexv);                                     \
									      \
      atomic_or (__futexp, FUTEX_OWNER_DIED);                               \
      INTERNAL_SYSCALL (futex, __err, 4, __futexp,                          \
                     __lll_private_flag (FUTEX_WAKE, private), 1, 0);      \
    }                                                                \
  while (0)

Definition at line 98 of file lowlevellock.h.

#define lll_robust_lock (   lock,
  id,
  private 
)
Value:
({                                                                   \
    int *__futex = &(lock);                                          \
    int __val = 0;                                                   \
    if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, id,  \
                                                        0), 0))             \
      __val = __lll_robust_lock_wait (__futex, private);                    \
    __val;                                                           \
  })

Definition at line 203 of file lowlevellock.h.

#define lll_robust_timedlock (   lock,
  abstime,
  id,
  private 
)
Value:
({                                                                   \
    int *__futex = &(lock);                                          \
    int __val = 0;                                                   \
    if (__builtin_expect (atomic_compare_and_exchange_bool_acq (__futex, id,  \
                                                        0), 0))             \
      __val = __lll_robust_timedlock_wait (__futex, abstime, private);             \
    __val;                                                           \
  })

Definition at line 248 of file lowlevellock.h.

#define lll_robust_trylock (   lock,
  id 
)    __lll_robust_trylock (&(lock), id)

Definition at line 173 of file lowlevellock.h.

#define lll_robust_unlock (   lock,
  private 
)
Value:
((void) ({                                                           \
    int *__futex = &(lock);                                          \
    int __val = atomic_exchange_rel (__futex, 0);                           \
    if (__builtin_expect (__val & FUTEX_WAITERS, 0))                        \
      lll_futex_wake (__futex, 1, private);                                 \
  }))

Definition at line 266 of file lowlevellock.h.

Definition at line 47 of file lowlevellock.h.

#define lll_timedlock (   lock,
  abstime,
  private 
)
Value:
({                                                                   \
    int *__futex = &(lock);                                          \
    int __val = 0;                                                   \
    if (__builtin_expect (atomic_compare_and_exchange_val_acq (__futex, 1, 0),\
                       0) != 0)                                      \
      __val = __lll_timedlock_wait (__futex, abstime, private);                    \
    __val;                                                           \
  })

Definition at line 238 of file lowlevellock.h.

#define lll_timedwait_tid (   tid,
  abstime 
)
Value:
({                                                                   \
    int __res = 0;                                                   \
    if ((tid) != 0)                                                  \
      __res = __lll_timedwait_tid (&(tid), (abstime));                      \
    __res;                                                           \
  })

Definition at line 301 of file lowlevellock.h.

#define lll_trylock (   lock)    __lll_trylock (&(lock))

Definition at line 178 of file lowlevellock.h.

#define lll_unlock (   lock,
  private 
)
Value:
((void) ({                                                           \
    int *__futex = &(lock);                                          \
    int __val = atomic_exchange_rel (__futex, 0);                           \
    if (__builtin_expect (__val > 1, 0))                             \
      lll_futex_wake (__futex, 1, private);                                 \
  }))

Definition at line 258 of file lowlevellock.h.

#define lll_wait_tid (   tid)
Value:
do {                                                                 \
    __typeof (tid) __tid;                                            \
    while ((__tid = (tid)) != 0)                                     \
      lll_futex_wait (&(tid), __tid, LLL_SHARED);                           \
  } while (0)

Definition at line 291 of file lowlevellock.h.


Function Documentation

void __lll_lock_wait ( int futex,
int  private 
)

Definition at line 27 of file lowlevellock.c.

{
  do
    {
      int oldval = atomic_compare_and_exchange_val_acq (futex, 2, 1);
      if (oldval != 0)
       lll_futex_wait (futex, 2, private);
    }
  while (atomic_compare_and_exchange_val_acq (futex, 2, 0) != 0);
}
void __lll_lock_wait_private ( int futex)

Definition at line 28 of file lowlevellock.c.

{
  if (*futex == 2)
    lll_futex_wait (futex, 2, LLL_PRIVATE);

  while (atomic_exchange_acq (futex, 2) != 0)
    lll_futex_wait (futex, 2, LLL_PRIVATE);
}
int __lll_robust_lock_wait ( int futex,
int  private 
)

Definition at line 28 of file lowlevelrobustlock.c.

{
  int oldval = *futex;
  int tid = THREAD_GETMEM (THREAD_SELF, tid);

  /* If the futex changed meanwhile try locking again.  */
  if (oldval == 0)
    goto try;

  do
    {
      if (__builtin_expect (oldval & FUTEX_OWNER_DIED, 0))
       return oldval;

      int newval = oldval | FUTEX_WAITERS;
      if (oldval != newval
         && atomic_compare_and_exchange_bool_acq (futex, newval, oldval))
       continue;

      lll_futex_wait (futex, newval, private);

    try:
      ;
    }
  while ((oldval = atomic_compare_and_exchange_val_acq (futex,
                                                 tid | FUTEX_WAITERS,
                                                 0)) != 0);
  return 0;
}
int __lll_robust_timedlock_wait ( int futex,
const struct timespec ,
int  private 
)

Definition at line 60 of file lowlevelrobustlock.c.

{
  /* Reject invalid timeouts.  */
  if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
    return EINVAL;

  int tid = THREAD_GETMEM (THREAD_SELF, tid);
  int oldval = *futex;

  /* If the futex changed meanwhile try locking again.  */
  if (oldval == 0)
    goto try;

  do
    {
      struct timeval tv;
      struct timespec rt;

      /* Get the current time.  */
      (void) __gettimeofday (&tv, NULL);

      /* Compute relative timeout.  */
      rt.tv_sec = abstime->tv_sec - tv.tv_sec;
      rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
      if (rt.tv_nsec < 0)
       {
         rt.tv_nsec += 1000000000;
         --rt.tv_sec;
       }

      /* Already timed out?  */
      if (rt.tv_sec < 0)
       return ETIMEDOUT;

      /* Wait.  */
      if (__builtin_expect (oldval & FUTEX_OWNER_DIED, 0))
       return oldval;

      int newval = oldval | FUTEX_WAITERS;
      if (oldval != newval
         && atomic_compare_and_exchange_bool_acq (futex, newval, oldval))
       continue;

      lll_futex_timed_wait (futex, newval, &rt, private);

    try:
      ;
    }
  while ((oldval = atomic_compare_and_exchange_val_acq (futex,
                                                 tid | FUTEX_WAITERS,
                                                 0)) != 0);

  return 0;
}
int __lll_timedlock_wait ( int futex,
const struct timespec ,
int  private 
)

Definition at line 51 of file lowlevellock.c.

{
  /* Reject invalid timeouts.  */
  if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000)
    return EINVAL;

  do
    {
      struct timeval tv;
      struct timespec rt;

      /* Get the current time.  */
      (void) __gettimeofday (&tv, NULL);

      /* Compute relative timeout.  */
      rt.tv_sec = abstime->tv_sec - tv.tv_sec;
      rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
      if (rt.tv_nsec < 0)
       {
         rt.tv_nsec += 1000000000;
         --rt.tv_sec;
       }

      /* Already timed out?  */
      if (rt.tv_sec < 0)
       return ETIMEDOUT;

      /* Wait.  */
      int oldval = atomic_compare_and_exchange_val_acq (futex, 2, 1);
      if (oldval != 0)
       lll_futex_timed_wait (futex, 2, &rt, private);
    }
  while (atomic_compare_and_exchange_bool_acq (futex, 2, 0) != 0);
  return 0;
}
int __lll_timedwait_tid ( int ,
const struct timespec  
)