/* Linuxthreads - a simple clone()-based implementation of Posix */
/* threads for Linux. */
/* Copyright (C) 1998 Xavier Leroy (Xavier.Leroy@inria.fr) */
/* */
/* This program is free software; you can redistribute it and/or */
/* modify it under the terms of the GNU Library General Public License */
/* as published by the Free Software Foundation; either version 2 */
/* of the License, or (at your option) any later version. */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU Library General Public License for more details. */
/* Internal locks */
#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"
static void __pthread_acquire(int * spinlock);
static inline void __pthread_release(int * spinlock)
{
WRITE_MEMORY_BARRIER();
*spinlock = __LT_SPINLOCK_INIT;
__asm __volatile ("" : "=m" (*spinlock) : "m" (*spinlock));
}
/* The status field of a spinlock is a pointer whose least significant
bit is a locked flag.
Thus the field values have the following meanings:
status == 0: spinlock is free
status == 1: spinlock is taken; no thread is waiting on it
(status & 1) == 1: spinlock is taken and (status & ~1L) is a
pointer to the first waiting thread; other
waiting threads are linked via the p_nextlock
field.
(status & 1) == 0: same as above, but spinlock is not taken.
The waiting list is not sorted by priority order.
Actually, we always insert at top of list (sole insertion mode
that can be performed without locking).
For __pthread_unlock, we perform a linear search in the list
to find the highest-priority, oldest waiting thread.
This is safe because there are no concurrent __pthread_unlock
operations -- only the thread that locked the mutex can unlock it. */
void internal_function __pthread_lock(struct _pthread_fastlock * lock,
pthread_descr self)
{
#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;
}