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 <sysdep.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 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_trylock(lock)   atomic_compare_and_exchange_val_acq(&(lock), 1, 0)
#define lll_cond_trylock(lock)   atomic_compare_and_exchange_val_acq(&(lock), 2, 0)
#define __lll_robust_trylock(futex, id)   (atomic_compare_and_exchange_val_acq (futex, id, 0) != 0)
#define lll_robust_trylock(lock, id)   __lll_robust_trylock (&(lock), id)
#define __lll_lock(futex, private)
#define lll_lock(futex, private)   __lll_lock (&(futex), private)
#define __lll_robust_lock(futex, id, private)
#define lll_robust_lock(futex, id, private)   __lll_robust_lock (&(futex), id, private)
#define __lll_cond_lock(futex, private)
#define lll_cond_lock(futex, private)   __lll_cond_lock (&(futex), private)
#define lll_robust_cond_lock(futex, id, private)   __lll_robust_lock (&(futex), (id) | FUTEX_WAITERS, private)
#define __lll_timedlock(futex, abstime, private)
#define lll_timedlock(futex, abstime, private)   __lll_timedlock (&(futex), abstime, private)
#define __lll_robust_timedlock(futex, abstime, id, private)
#define lll_robust_timedlock(futex, abstime, id, private)   __lll_robust_timedlock (&(futex), abstime, id, private)
#define __lll_unlock(futex, private)
#define lll_unlock(futex, private)   __lll_unlock(&(futex), private)
#define __lll_robust_unlock(futex, private)
#define lll_robust_unlock(futex, private)   __lll_robust_unlock(&(futex), 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_cond_lock (   futex,
  private 
)
Value:
((void) ({                                                           \
    int *__futex = (futex);                                          \
    if (__builtin_expect (atomic_exchange_acq (__futex, 2), 0))                    \
      __lll_lock_wait (__futex, private);                            \
  }))

Definition at line 173 of file lowlevellock.h.

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

Definition at line 144 of file lowlevellock.h.

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

Definition at line 53 of file lowlevellock.h.

#define __lll_robust_lock (   futex,
  id,
  private 
)
Value:
({                                                                   \
    int *__futex = (futex);                                          \
    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 159 of file lowlevellock.h.

#define __lll_robust_timedlock (   futex,
  abstime,
  id,
  private 
)
Value:
({                                                                   \
    int *__futex = (futex);                                          \
    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 204 of file lowlevellock.h.

#define __lll_robust_trylock (   futex,
  id 
)    (atomic_compare_and_exchange_val_acq (futex, id, 0) != 0)

Definition at line 135 of file lowlevellock.h.

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

Definition at line 228 of file lowlevellock.h.

#define __lll_timedlock (   futex,
  abstime,
  private 
)
Value:
({                                                                   \
     int *__futex = (futex);                                                \
     int __val = 0;                                                  \
                                                                     \
     if (__builtin_expect (atomic_exchange_acq (__futex, 1), 0))            \
       __val = __lll_timedlock_wait (__futex, abstime, private);            \
     __val;                                                          \
  })

Definition at line 191 of file lowlevellock.h.

#define __lll_unlock (   futex,
  private 
)
Value:
(void)                                           \
    ({ int *__futex = (futex);                                 \
       int __oldval = atomic_exchange_rel (__futex, 0);        \
       if (__builtin_expect (__oldval > 1, 0))                 \
        lll_futex_wake (__futex, 1, private);                  \
    })

Definition at line 218 of file lowlevellock.h.

#define FUTEX_CMP_REQUEUE   4

Definition at line 32 of file lowlevellock.h.

#define FUTEX_LOCK_PI   6

Definition at line 35 of file lowlevellock.h.

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

Definition at line 34 of file lowlevellock.h.

#define FUTEX_PRIVATE_FLAG   128

Definition at line 38 of file lowlevellock.h.

#define FUTEX_REQUEUE   3

Definition at line 31 of file lowlevellock.h.

#define FUTEX_TRYLOCK_PI   8

Definition at line 37 of file lowlevellock.h.

#define FUTEX_UNLOCK_PI   7

Definition at line 36 of file lowlevellock.h.

#define FUTEX_WAIT   0

Definition at line 29 of file lowlevellock.h.

#define FUTEX_WAKE   1

Definition at line 30 of file lowlevellock.h.

#define FUTEX_WAKE_OP   5

Definition at line 33 of file lowlevellock.h.

#define lll_cond_lock (   futex,
  private 
)    __lll_cond_lock (&(futex), private)

Definition at line 179 of file lowlevellock.h.

Definition at line 132 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));           \
    __ret;                                                           \
  })

Definition at line 105 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));                       \
    __ret;                                                           \
  })

Definition at line 75 of file lowlevellock.h.

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

Definition at line 72 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);                                        \
    __ret;                                                           \
  })

Definition at line 85 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);                  \
    __ret;                                                           \
  })

Definition at line 117 of file lowlevellock.h.

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

Definition at line 239 of file lowlevellock.h.

#define lll_lock (   futex,
  private 
)    __lll_lock (&(futex), private)

Definition at line 156 of file lowlevellock.h.

#define LLL_LOCK_INITIALIZER   (0)

Definition at line 247 of file lowlevellock.h.

#define LLL_LOCK_INITIALIZER_LOCKED   (1)

Definition at line 248 of file lowlevellock.h.

#define LLL_PRIVATE   0

Definition at line 43 of file lowlevellock.h.

#define lll_robust_cond_lock (   futex,
  id,
  private 
)    __lll_robust_lock (&(futex), (id) | FUTEX_WAITERS, private)

Definition at line 182 of file lowlevellock.h.

#define lll_robust_dead (   futexv,
  private 
)
Value:
do                                                                   \
    {                                                                \
      int *__futexp = &(futexv);                                     \
      atomic_or (__futexp, FUTEX_OWNER_DIED);                               \
      lll_futex_wake (__futexp, 1, private);                                \
    }                                                                \
  while (0)

Definition at line 95 of file lowlevellock.h.

#define lll_robust_lock (   futex,
  id,
  private 
)    __lll_robust_lock (&(futex), id, private)

Definition at line 169 of file lowlevellock.h.

#define lll_robust_timedlock (   futex,
  abstime,
  id,
  private 
)    __lll_robust_timedlock (&(futex), abstime, id, private)

Definition at line 214 of file lowlevellock.h.

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

Definition at line 137 of file lowlevellock.h.

#define lll_robust_unlock (   futex,
  private 
)    __lll_robust_unlock(&(futex), private)

Definition at line 235 of file lowlevellock.h.

Definition at line 44 of file lowlevellock.h.

#define lll_timedlock (   futex,
  abstime,
  private 
)    __lll_timedlock (&(futex), abstime, private)

Definition at line 200 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 269 of file lowlevellock.h.

Definition at line 129 of file lowlevellock.h.

#define lll_unlock (   futex,
  private 
)    __lll_unlock(&(futex), private)

Definition at line 225 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 259 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  
)