Back to index

glibc  2.9
Classes | Functions | Variables
spinlock.c File Reference
#include <errno.h>
#include <sched.h>
#include <time.h>
#include <stdlib.h>
#include <limits.h>
#include "pthread.h"
#include "internals.h"
#include "spinlock.h"
#include "restart.h"

Go to the source code of this file.

Classes

struct  wait_node

Functions

static void __pthread_acquire (int *spinlock)
static void __pthread_release (int *spinlock)
void internal_function __pthread_lock (struct _pthread_fastlock *lock, pthread_descr self)
int __pthread_unlock (struct _pthread_fastlock *lock)
static struct wait_nodewait_node_alloc (void)
static void wait_node_free (struct wait_node *wn)
void __pthread_alt_lock (struct _pthread_fastlock *lock, pthread_descr self)
int __pthread_alt_timedlock (struct _pthread_fastlock *lock, pthread_descr self, const struct timespec *abstime)
void __pthread_alt_unlock (struct _pthread_fastlock *lock)
int __pthread_compare_and_swap (long *ptr, long oldval, long newval, int *spinlock)

Variables

static long wait_node_free_list
static int wait_node_free_list_spinlock

Class Documentation

struct wait_node

Definition at line 269 of file spinlock.c.

Collaboration diagram for wait_node:
Class Members
int abandoned
struct wait_node * next
pthread_descr thr

Function Documentation

static void __pthread_acquire ( int spinlock) [static]

Definition at line 714 of file spinlock.c.

{
  int cnt = 0;
  struct timespec tm;

  READ_MEMORY_BARRIER();

  while (testandset(spinlock)) {
    if (cnt < MAX_SPIN_COUNT) {
      sched_yield();
      cnt++;
    } else {
      tm.tv_sec = 0;
      tm.tv_nsec = SPIN_SLEEP_DURATION;
      nanosleep(&tm, NULL);
      cnt = 0;
    }
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void __pthread_alt_lock ( struct _pthread_fastlock lock,
pthread_descr  self 
)

Definition at line 357 of file spinlock.c.

{
#if defined HAS_COMPARE_AND_SWAP
  long oldstatus, newstatus;
#endif
  struct wait_node wait_node;

#if defined TEST_FOR_COMPARE_AND_SWAP
  if (!__pthread_has_cas)
#endif
#if !defined HAS_COMPARE_AND_SWAP || defined TEST_FOR_COMPARE_AND_SWAP
  {
    int suspend_needed = 0;
    __pthread_acquire(&lock->__spinlock);

    if (lock->__status == 0)
      lock->__status = 1;
    else {
      if (self == NULL)
       self = thread_self();

      wait_node.abandoned = 0;
      wait_node.next = (struct wait_node *) lock->__status;
      wait_node.thr = self;
      lock->__status = (long) &wait_node;
      suspend_needed = 1;
    }

    __pthread_release(&lock->__spinlock);

    if (suspend_needed)
      suspend (self);
    return;
  }
#endif

#if defined HAS_COMPARE_AND_SWAP
  do {
    oldstatus = lock->__status;
    if (oldstatus == 0) {
      newstatus = 1;
    } else {
      if (self == NULL)
       self = thread_self();
      wait_node.thr = self;
      newstatus = (long) &wait_node;
    }
    wait_node.abandoned = 0;
    wait_node.next = (struct wait_node *) oldstatus;
    /* Make sure the store in wait_node.next completes before performing
       the compare-and-swap */
    MEMORY_BARRIER();
  } while(! __compare_and_swap(&lock->__status, oldstatus, newstatus));

  /* Suspend. Note that unlike in __pthread_lock, we don't worry
     here about spurious wakeup. That's because this lock is not
     used in situations where that can happen; the restart can
     only come from the previous lock owner. */

  if (oldstatus != 0)
    suspend(self);

  READ_MEMORY_BARRIER();
#endif
}

Here is the call graph for this function:

Here is the caller graph for this function:

int __pthread_alt_timedlock ( struct _pthread_fastlock lock,
pthread_descr  self,
const struct timespec abstime 
)

Definition at line 426 of file spinlock.c.

{
  long oldstatus = 0;
#if defined HAS_COMPARE_AND_SWAP
  long newstatus;
#endif
  struct wait_node *p_wait_node = wait_node_alloc();

  /* Out of memory, just give up and do ordinary lock. */
  if (p_wait_node == 0) {
    __pthread_alt_lock(lock, self);
    return 1;
  }

#if defined TEST_FOR_COMPARE_AND_SWAP
  if (!__pthread_has_cas)
#endif
#if !defined HAS_COMPARE_AND_SWAP || defined TEST_FOR_COMPARE_AND_SWAP
  {
    __pthread_acquire(&lock->__spinlock);

    if (lock->__status == 0)
      lock->__status = 1;
    else {
      if (self == NULL)
       self = thread_self();

      p_wait_node->abandoned = 0;
      p_wait_node->next = (struct wait_node *) lock->__status;
      p_wait_node->thr = self;
      lock->__status = (long) p_wait_node;
      oldstatus = 1; /* force suspend */
    }

    __pthread_release(&lock->__spinlock);
    goto suspend;
  }
#endif

#if defined HAS_COMPARE_AND_SWAP
  do {
    oldstatus = lock->__status;
    if (oldstatus == 0) {
      newstatus = 1;
    } else {
      if (self == NULL)
       self = thread_self();
      p_wait_node->thr = self;
      newstatus = (long) p_wait_node;
    }
    p_wait_node->abandoned = 0;
    p_wait_node->next = (struct wait_node *) oldstatus;
    /* Make sure the store in wait_node.next completes before performing
       the compare-and-swap */
    MEMORY_BARRIER();
  } while(! __compare_and_swap(&lock->__status, oldstatus, newstatus));
#endif

#if !defined HAS_COMPARE_AND_SWAP || defined TEST_FOR_COMPARE_AND_SWAP
  suspend:
#endif

  /* If we did not get the lock, do a timed suspend. If we wake up due
     to a timeout, then there is a race; the old lock owner may try
     to remove us from the queue. This race is resolved by us and the owner
     doing an atomic testandset() to change the state of the wait node from 0
     to 1. If we succeed, then it's a timeout and we abandon the node in the
     queue. If we fail, it means the owner gave us the lock. */

  if (oldstatus != 0) {
    if (timedsuspend(self, abstime) == 0) {
      if (!testandset(&p_wait_node->abandoned))
       return 0; /* Timeout! */

      /* Eat oustanding resume from owner, otherwise wait_node_free() below
        will race with owner's wait_node_dequeue(). */
      suspend(self);
    }
  }

  wait_node_free(p_wait_node);

  READ_MEMORY_BARRIER();

  return 1; /* Got the lock! */
}

Here is the call graph for this function:

Here is the caller graph for this function:

void __pthread_alt_unlock ( struct _pthread_fastlock lock)

Definition at line 514 of file spinlock.c.

{
  struct wait_node *p_node, **pp_node, *p_max_prio, **pp_max_prio;
  struct wait_node ** const pp_head = (struct wait_node **) &lock->__status;
  int maxprio;

  WRITE_MEMORY_BARRIER();

#if defined TEST_FOR_COMPARE_AND_SWAP
  if (!__pthread_has_cas)
#endif
#if !defined HAS_COMPARE_AND_SWAP || defined TEST_FOR_COMPARE_AND_SWAP
  {
    __pthread_acquire(&lock->__spinlock);
  }
#endif

  while (1) {

  /* If no threads are waiting for this lock, try to just
     atomically release it. */
#if defined TEST_FOR_COMPARE_AND_SWAP
    if (!__pthread_has_cas)
#endif
#if !defined HAS_COMPARE_AND_SWAP || defined TEST_FOR_COMPARE_AND_SWAP
    {
      if (lock->__status == 0 || lock->__status == 1) {
       lock->__status = 0;
       break;
      }
    }
#endif

#if defined TEST_FOR_COMPARE_AND_SWAP
    else
#endif

#if defined HAS_COMPARE_AND_SWAP
    {
      long oldstatus = lock->__status;
      if (oldstatus == 0 || oldstatus == 1) {
       if (__compare_and_swap_with_release_semantics (&lock->__status, oldstatus, 0))
         break;
       else
         continue;
      }
    }
#endif

    /* Process the entire queue of wait nodes. Remove all abandoned
       wait nodes and put them into the global free queue, and
       remember the one unabandoned node which refers to the thread
       having the highest priority. */

    pp_max_prio = pp_node = pp_head;
    p_max_prio = p_node = *pp_head;
    maxprio = INT_MIN;

    READ_MEMORY_BARRIER(); /* Prevent access to stale data through p_node */

    while (p_node != (struct wait_node *) 1) {
      int prio;

      if (p_node->abandoned) {
       /* Remove abandoned node. */
#if defined TEST_FOR_COMPARE_AND_SWAP
       if (!__pthread_has_cas)
#endif
#if !defined HAS_COMPARE_AND_SWAP || defined TEST_FOR_COMPARE_AND_SWAP
         *pp_node = p_node->next;
#endif
#if defined TEST_FOR_COMPARE_AND_SWAP
       else
#endif
#if defined HAS_COMPARE_AND_SWAP
         wait_node_dequeue(pp_head, pp_node, p_node);
#endif
       wait_node_free(p_node);
       /* Note that the next assignment may take us to the beginning
          of the queue, to newly inserted nodes, if pp_node == pp_head.
          In that case we need a memory barrier to stabilize the first of
          these new nodes. */
       p_node = *pp_node;
       if (pp_node == pp_head)
         READ_MEMORY_BARRIER(); /* No stale reads through p_node */
       continue;
      } else if ((prio = p_node->thr->p_priority) >= maxprio) {
       /* Otherwise remember it if its thread has a higher or equal priority
          compared to that of any node seen thus far. */
       maxprio = prio;
       pp_max_prio = pp_node;
       p_max_prio = p_node;
      }

      /* This canno6 jump backward in the list, so no further read
         barrier is needed. */
      pp_node = &p_node->next;
      p_node = *pp_node;
    }

    /* If all threads abandoned, go back to top */
    if (maxprio == INT_MIN)
      continue;

    ASSERT (p_max_prio != (struct wait_node *) 1);

    /* Now we want to to remove the max priority thread's wait node from
       the list. Before we can do this, we must atomically try to change the
       node's abandon state from zero to nonzero. If we succeed, that means we
       have the node that we will wake up. If we failed, then it means the
       thread timed out and abandoned the node in which case we repeat the
       whole unlock operation. */

    if (!testandset(&p_max_prio->abandoned)) {
#if defined TEST_FOR_COMPARE_AND_SWAP
      if (!__pthread_has_cas)
#endif
#if !defined HAS_COMPARE_AND_SWAP || defined TEST_FOR_COMPARE_AND_SWAP
       *pp_max_prio = p_max_prio->next;
#endif
#if defined TEST_FOR_COMPARE_AND_SWAP
      else
#endif
#if defined HAS_COMPARE_AND_SWAP
       wait_node_dequeue(pp_head, pp_max_prio, p_max_prio);
#endif

      /* Release the spinlock before restarting.  */
#if defined TEST_FOR_COMPARE_AND_SWAP
      if (!__pthread_has_cas)
#endif
#if !defined HAS_COMPARE_AND_SWAP || defined TEST_FOR_COMPARE_AND_SWAP
       {
         __pthread_release(&lock->__spinlock);
       }
#endif

      restart(p_max_prio->thr);

      return;
    }
  }

#if defined TEST_FOR_COMPARE_AND_SWAP
  if (!__pthread_has_cas)
#endif
#if !defined HAS_COMPARE_AND_SWAP || defined TEST_FOR_COMPARE_AND_SWAP
  {
    __pthread_release(&lock->__spinlock);
  }
#endif
}

Here is the call graph for this function:

Here is the caller graph for this function:

int __pthread_compare_and_swap ( long *  ptr,
long  oldval,
long  newval,
int spinlock 
)

Definition at line 676 of file spinlock.c.

{
  int res;

  __pthread_acquire(spinlock);

  if (*ptr == oldval) {
    *ptr = newval; res = 1;
  } else {
    res = 0;
  }

  __pthread_release(spinlock);

  return res;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 60 of file spinlock.c.

{
#if defined HAS_COMPARE_AND_SWAP
  long oldstatus, newstatus;
  int successful_seizure, spurious_wakeup_count;
  int spin_count;
#endif

#if defined TEST_FOR_COMPARE_AND_SWAP
  if (!__pthread_has_cas)
#endif
#if !defined HAS_COMPARE_AND_SWAP || defined TEST_FOR_COMPARE_AND_SWAP
  {
    __pthread_acquire(&lock->__spinlock);
    return;
  }
#endif

#if defined HAS_COMPARE_AND_SWAP
  /* First try it without preparation.  Maybe it's a completely
     uncontested lock.  */
  if (lock->__status == 0 && __compare_and_swap (&lock->__status, 0, 1))
    return;

  spurious_wakeup_count = 0;
  spin_count = 0;

  /* On SMP, try spinning to get the lock. */

  if (__pthread_smp_kernel) {
    int max_count = lock->__spinlock * 2 + 10;

    if (max_count > MAX_ADAPTIVE_SPIN_COUNT)
      max_count = MAX_ADAPTIVE_SPIN_COUNT;

    for (spin_count = 0; spin_count < max_count; spin_count++) {
      if (((oldstatus = lock->__status) & 1) == 0) {
       if(__compare_and_swap(&lock->__status, oldstatus, oldstatus | 1))
       {
         if (spin_count)
           lock->__spinlock += (spin_count - lock->__spinlock) / 8;
         READ_MEMORY_BARRIER();
         return;
       }
      }
#ifdef BUSY_WAIT_NOP
      BUSY_WAIT_NOP;
#endif
      __asm __volatile ("" : "=m" (lock->__status) : "m" (lock->__status));
    }

    lock->__spinlock += (spin_count - lock->__spinlock) / 8;
  }

again:

  /* No luck, try once more or suspend. */

  do {
    oldstatus = lock->__status;
    successful_seizure = 0;

    if ((oldstatus & 1) == 0) {
      newstatus = oldstatus | 1;
      successful_seizure = 1;
    } else {
      if (self == NULL)
       self = thread_self();
      newstatus = (long) self | 1;
    }

    if (self != NULL) {
      THREAD_SETMEM(self, p_nextlock, (pthread_descr) (oldstatus));
      /* Make sure the store in p_nextlock completes before performing
         the compare-and-swap */
      MEMORY_BARRIER();
    }
  } while(! __compare_and_swap(&lock->__status, oldstatus, newstatus));

  /* Suspend with guard against spurious wakeup.
     This can happen in pthread_cond_timedwait_relative, when the thread
     wakes up due to timeout and is still on the condvar queue, and then
     locks the queue to remove itself. At that point it may still be on the
     queue, and may be resumed by a condition signal. */

  if (!successful_seizure) {
    for (;;) {
      suspend(self);
      if (self->p_nextlock != NULL) {
       /* Count resumes that don't belong to us. */
       spurious_wakeup_count++;
       continue;
      }
      break;
    }
    goto again;
  }

  /* Put back any resumes we caught that don't belong to us. */
  while (spurious_wakeup_count--)
    restart(self);

  READ_MEMORY_BARRIER();
#endif
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void __pthread_release ( int spinlock) [inline, static]

Definition at line 29 of file spinlock.c.

{
  WRITE_MEMORY_BARRIER();
  *spinlock = __LT_SPINLOCK_INIT;
  __asm __volatile ("" : "=m" (*spinlock) : "m" (*spinlock));
}

Here is the caller graph for this function:

Definition at line 167 of file spinlock.c.

{
#if defined HAS_COMPARE_AND_SWAP
  long oldstatus;
  pthread_descr thr, * ptr, * maxptr;
  int maxprio;
#endif

#if defined TEST_FOR_COMPARE_AND_SWAP
  if (!__pthread_has_cas)
#endif
#if !defined HAS_COMPARE_AND_SWAP || defined TEST_FOR_COMPARE_AND_SWAP
  {
    __pthread_release(&lock->__spinlock);
    return 0;
  }
#endif

#if defined HAS_COMPARE_AND_SWAP
  WRITE_MEMORY_BARRIER();

again:
  while ((oldstatus = lock->__status) == 1) {
    if (__compare_and_swap_with_release_semantics(&lock->__status,
       oldstatus, 0))
      return 0;
  }

  /* Find thread in waiting queue with maximal priority */
  ptr = (pthread_descr *) &lock->__status;
  thr = (pthread_descr) (oldstatus & ~1L);
  maxprio = 0;
  maxptr = ptr;

  /* Before we iterate over the wait queue, we need to execute
     a read barrier, otherwise we may read stale contents of nodes that may
     just have been inserted by other processors. One read barrier is enough to
     ensure we have a stable list; we don't need one for each pointer chase
     through the list, because we are the owner of the lock; other threads
     can only add nodes at the front; if a front node is consistent,
     the ones behind it must also be. */

  READ_MEMORY_BARRIER();

  while (thr != 0) {
    if (thr->p_priority >= maxprio) {
      maxptr = ptr;
      maxprio = thr->p_priority;
    }
    ptr = &(thr->p_nextlock);
    thr = (pthread_descr)((long)(thr->p_nextlock) & ~1L);
  }

  /* Remove max prio thread from waiting list. */
  if (maxptr == (pthread_descr *) &lock->__status) {
    /* If max prio thread is at head, remove it with compare-and-swap
       to guard against concurrent lock operation. This removal
       also has the side effect of marking the lock as released
       because the new status comes from thr->p_nextlock whose
       least significant bit is clear. */
    thr = (pthread_descr) (oldstatus & ~1L);
    if (! __compare_and_swap_with_release_semantics
           (&lock->__status, oldstatus, (long)(thr->p_nextlock) & ~1L))
      goto again;
  } else {
    /* No risk of concurrent access, remove max prio thread normally.
       But in this case we must also flip the least significant bit
       of the status to mark the lock as released. */
    thr = (pthread_descr)((long)*maxptr & ~1L);
    *maxptr = thr->p_nextlock;

    /* Ensure deletion from linked list completes before we
       release the lock. */
    WRITE_MEMORY_BARRIER();

    do {
      oldstatus = lock->__status;
    } while (!__compare_and_swap_with_release_semantics(&lock->__status,
            oldstatus, oldstatus & ~1L));
  }

  /* Wake up the selected waiting thread. Woken thread can check
     its own p_nextlock field for NULL to detect that it has been removed. No
     barrier is needed here, since restart() and suspend() take
     care of memory synchronization. */

  thr->p_nextlock = NULL;
  restart(thr);

  return 0;
#endif
}

Here is the call graph for this function:

Here is the caller graph for this function:

static struct wait_node* wait_node_alloc ( void  ) [static, read]

Definition at line 284 of file spinlock.c.

{
    struct wait_node *new_node = 0;

    __pthread_acquire(&wait_node_free_list_spinlock);
    if (wait_node_free_list != 0) {
      new_node = (struct wait_node *) wait_node_free_list;
      wait_node_free_list = (long) new_node->next;
    }
    WRITE_MEMORY_BARRIER();
    __pthread_release(&wait_node_free_list_spinlock);

    if (new_node == 0)
      return malloc(sizeof *wait_node_alloc());

    return new_node;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void wait_node_free ( struct wait_node wn) [static]

Definition at line 305 of file spinlock.c.

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

long wait_node_free_list [static]

Definition at line 275 of file spinlock.c.

Definition at line 276 of file spinlock.c.