Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Update.
	* sysdeps/unix/sysv/linux/smp.h: New file.
	* sysdeps/unix/sysv/linux/sh/smp.h: New file.
	* init.c: Define __is_smp.
	(__pthread_initialize_minimal_internal): Call is_smp_system to
	initialize __is_smp.
	* pthreadP.h: Declare __is_smp.
	Define MAX_ADAPTIVE_COUNT is necessary.
	* pthread_mutex_init.c: Add comment regarding __spins field.
	* pthread_mutex_lock.c: Implement adaptive mutex type.
	* pthread_mutex_timedlock.c: Likewise.
	* sysdeps/unix/sysv/linux/pthread_mutex_cond_lock.c: Likewise.
	* sysdeps/unix/sysv/linux/alpha/bits/pthreadtypes.h (pthread_mutex_t):
	Add __spins field.
	* sysdeps/unix/sysv/linux/i386/bits/pthreadtypes.h: Likewise.
	* sysdeps/unix/sysv/linux/ia64/bits/pthreadtypes.h: Likewise.
	* sysdeps/unix/sysv/linux/powerpc/bits/pthreadtypes.h: Likewise.
	* sysdeps/unix/sysv/linux/sh/bits/pthreadtypes.h: Likewise.
	* sysdeps/unix/sysv/linux/s390/bits/pthreadtypes.h: Likewise.
	* sysdeps/unix/sysv/linux/sparc/bits/pthreadtypes.h: Likewise.
	* sysdeps/unix/sysv/linux/x86_64/bits/pthreadtypes.h: Likewise.
	* sysdeps/unix/sysv/linux/alpha/bits/pthreadtypes.h: Define
	lll_mutex_cond_trylock.
	* sysdeps/unix/sysv/linux/powerpc/bits/pthreadtypes.h: Likewise.
	* sysdeps/unix/sysv/linux/sh/bits/pthreadtypes.h: Likewise.
	* sysdeps/unix/sysv/linux/s390/bits/pthreadtypes.h: Likewise.
	* sysdeps/unix/sysv/linux/sparc/bits/pthreadtypes.h: Likewise.
	* sysdeps/unix/sysv/linux/i386/bits/pthreadtypes.h: Likewise.
	Define BUSY_WAIT_NOP.
	* sysdeps/unix/sysv/linux/ia64/bits/pthreadtypes.h: Likewise.
	* sysdeps/unix/sysv/linux/x86_64/bits/pthreadtypes.h: Likewise.

	* tst-mutex5.c: Add support for testing adaptive mutexes.
	* tst-mutex7.c: Likewise.
	* tst-mutex5a.c: New file.
	* tst-mutex7a.c: New file.
	* Makefile (tests): Add tst-mutex5a and tst-mutex7a.
  • Loading branch information
Ulrich Drepper committed Mar 24, 2004
1 parent 565699e commit 2c0b891
Show file tree
Hide file tree
Showing 31 changed files with 364 additions and 28 deletions.
2 changes: 1 addition & 1 deletion nptl/Banner
@@ -1 +1 @@
NPTL 0.60 by Ulrich Drepper
NPTL 0.61 by Ulrich Drepper
37 changes: 37 additions & 0 deletions nptl/ChangeLog
@@ -1,5 +1,42 @@
2004-03-23 Ulrich Drepper <drepper@redhat.com>

* sysdeps/unix/sysv/linux/smp.h: New file.
* sysdeps/unix/sysv/linux/sh/smp.h: New file.
* init.c: Define __is_smp.
(__pthread_initialize_minimal_internal): Call is_smp_system to
initialize __is_smp.
* pthreadP.h: Declare __is_smp.
Define MAX_ADAPTIVE_COUNT is necessary.
* pthread_mutex_init.c: Add comment regarding __spins field.
* pthread_mutex_lock.c: Implement adaptive mutex type.
* pthread_mutex_timedlock.c: Likewise.
* sysdeps/unix/sysv/linux/pthread_mutex_cond_lock.c: Likewise.
* sysdeps/unix/sysv/linux/alpha/bits/pthreadtypes.h (pthread_mutex_t):
Add __spins field.
* sysdeps/unix/sysv/linux/i386/bits/pthreadtypes.h: Likewise.
* sysdeps/unix/sysv/linux/ia64/bits/pthreadtypes.h: Likewise.
* sysdeps/unix/sysv/linux/powerpc/bits/pthreadtypes.h: Likewise.
* sysdeps/unix/sysv/linux/sh/bits/pthreadtypes.h: Likewise.
* sysdeps/unix/sysv/linux/s390/bits/pthreadtypes.h: Likewise.
* sysdeps/unix/sysv/linux/sparc/bits/pthreadtypes.h: Likewise.
* sysdeps/unix/sysv/linux/x86_64/bits/pthreadtypes.h: Likewise.
* sysdeps/unix/sysv/linux/alpha/bits/pthreadtypes.h: Define
lll_mutex_cond_trylock.
* sysdeps/unix/sysv/linux/powerpc/bits/pthreadtypes.h: Likewise.
* sysdeps/unix/sysv/linux/sh/bits/pthreadtypes.h: Likewise.
* sysdeps/unix/sysv/linux/s390/bits/pthreadtypes.h: Likewise.
* sysdeps/unix/sysv/linux/sparc/bits/pthreadtypes.h: Likewise.
* sysdeps/unix/sysv/linux/i386/bits/pthreadtypes.h: Likewise.
Define BUSY_WAIT_NOP.
* sysdeps/unix/sysv/linux/ia64/bits/pthreadtypes.h: Likewise.
* sysdeps/unix/sysv/linux/x86_64/bits/pthreadtypes.h: Likewise.

* tst-mutex5.c: Add support for testing adaptive mutexes.
* tst-mutex7.c: Likewise.
* tst-mutex5a.c: New file.
* tst-mutex7a.c: New file.
* Makefile (tests): Add tst-mutex5a and tst-mutex7a.

* sysdeps/unix/sysv/linux/x86_64/lowlevellock.S
(__lll_mutex_timedlock_wait): Preserve r8 and r9 since the
vgettimeofday call miht destroy the content.
Expand Down
2 changes: 1 addition & 1 deletion nptl/Makefile
Expand Up @@ -190,7 +190,7 @@ omit-deps = $(unix-syscalls:%=ptw-%)

tests = tst-attr1 tst-attr2 tst-attr3 \
tst-mutex1 tst-mutex2 tst-mutex3 tst-mutex4 tst-mutex5 tst-mutex6 \
tst-mutex7 tst-mutex8 tst-mutex9 \
tst-mutex7 tst-mutex8 tst-mutex9 tst-mutex5a tst-mutex7a \
tst-spin1 tst-spin2 tst-spin3 \
tst-cond1 tst-cond2 tst-cond3 tst-cond4 tst-cond5 tst-cond6 tst-cond7 \
tst-cond8 tst-cond9 tst-cond10 tst-cond11 tst-cond12 tst-cond13 \
Expand Down
7 changes: 7 additions & 0 deletions nptl/init.c
Expand Up @@ -31,6 +31,7 @@
#include <fork.h>
#include <version.h>
#include <shlib-compat.h>
#include <smp.h>


#ifndef __NR_set_tid_address
Expand Down Expand Up @@ -61,6 +62,9 @@ size_t __default_stacksize attribute_hidden;
size_t __static_tls_size;
size_t __static_tls_align_m1;

/* Flag whether the machine is SMP or not. */
int __is_smp attribute_hidden;

/* Version of the library, used in libthread_db to detect mismatches. */
static const char nptl_version[] __attribute_used__ = VERSION;

Expand Down Expand Up @@ -301,6 +305,9 @@ __pthread_initialize_minimal_internal (void)
#endif
__libc_pthread_init (&__fork_generation, __reclaim_stacks,
ptr_pthread_functions);

/* Determine whether the machine is SMP or not. */
__is_smp = is_smp_system ();
}
strong_alias (__pthread_initialize_minimal_internal,
__pthread_initialize_minimal)
9 changes: 9 additions & 0 deletions nptl/pthreadP.h
Expand Up @@ -45,6 +45,12 @@
#endif


/* Adaptive mutex definitions. */
#ifndef MAX_ADAPTIVE_COUNT
# define MAX_ADAPTIVE_COUNT 100
#endif


/* Internal variables. */


Expand All @@ -55,6 +61,9 @@ extern size_t __default_stacksize attribute_hidden;
extern size_t __static_tls_size attribute_hidden;
extern size_t __static_tls_align_m1 attribute_hidden;

/* Flag whether the machine is SMP or not. */
extern int __is_smp attribute_hidden;

/* Thread descriptor handling. */
extern list_t __stack_user;
hidden_proto (__stack_user)
Expand Down
3 changes: 2 additions & 1 deletion nptl/pthread_mutex_init.c
@@ -1,4 +1,4 @@
/* Copyright (C) 2002, 2003 Free Software Foundation, Inc.
/* Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
Expand Down Expand Up @@ -50,6 +50,7 @@ __pthread_mutex_init (mutex, mutexattr)
// mutex->__count = 0; already done by memset
// mutex->__owner = 0; already done by memset
// mutex->__nusers = 0; already done by memset
// mutex->__spins = 0; already done by memset

return 0;
}
Expand Down
34 changes: 32 additions & 2 deletions nptl/pthread_mutex_lock.c
@@ -1,4 +1,4 @@
/* Copyright (C) 2002, 2003 Free Software Foundation, Inc.
/* Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
Expand All @@ -25,13 +25,16 @@

#ifndef LLL_MUTEX_LOCK
# define LLL_MUTEX_LOCK(mutex) lll_mutex_lock (mutex)
# define LLL_MUTEX_TRYLOCK(mutex) lll_mutex_trylock (mutex)
#endif


int
__pthread_mutex_lock (mutex)
pthread_mutex_t *mutex;
{
assert (sizeof (mutex->__size) >= sizeof (mutex->__data));

pid_t id = THREAD_GETMEM (THREAD_SELF, tid);

switch (__builtin_expect (mutex->__data.__kind, PTHREAD_MUTEX_TIMED_NP))
Expand Down Expand Up @@ -68,10 +71,37 @@ __pthread_mutex_lock (mutex)
default:
/* Correct code cannot set any other type. */
case PTHREAD_MUTEX_TIMED_NP:
case PTHREAD_MUTEX_ADAPTIVE_NP:
simple:
/* Normal mutex. */
LLL_MUTEX_LOCK (mutex->__data.__lock);
break;

case PTHREAD_MUTEX_ADAPTIVE_NP:
if (! __is_smp)
goto simple;

if (LLL_MUTEX_TRYLOCK (mutex->__data.__lock) != 0)
{
int cnt = 0;
int max_cnt = MIN (MAX_ADAPTIVE_COUNT,
mutex->__data.__spins * 2 + 10);
do
{
if (cnt++ >= max_cnt)
{
LLL_MUTEX_LOCK (mutex->__data.__lock);
break;
}

#ifdef BUSY_WAIT_NOP
BUSY_WAIT_NOP;
#endif
}
while (LLL_MUTEX_TRYLOCK (mutex->__data.__lock) != 0);

mutex->__data.__spins += (cnt - mutex->__data.__spins) / 8;
}
break;
}

/* Record the ownership. */
Expand Down
31 changes: 29 additions & 2 deletions nptl/pthread_mutex_timedlock.c
@@ -1,4 +1,4 @@
/* Copyright (C) 2002, 2003 Free Software Foundation, Inc.
/* Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
Expand Down Expand Up @@ -73,10 +73,37 @@ pthread_mutex_timedlock (mutex, abstime)
default:
/* Correct code cannot set any other type. */
case PTHREAD_MUTEX_TIMED_NP:
case PTHREAD_MUTEX_ADAPTIVE_NP:
simple:
/* Normal mutex. */
result = lll_mutex_timedlock (mutex->__data.__lock, abstime);
break;

case PTHREAD_MUTEX_ADAPTIVE_NP:
if (! __is_smp)
goto simple;

if (lll_mutex_trylock (mutex->__data.__lock) != 0)
{
int cnt = 0;
int max_cnt = MIN (MAX_ADAPTIVE_COUNT,
mutex->__data.__spins * 2 + 10);
do
{
if (cnt++ >= max_cnt)
{
result = lll_mutex_timedlock (mutex->__data.__lock, abstime);
break;
}

#ifdef BUSY_WAIT_NOP
BUSY_WAIT_NOP;
#endif
}
while (lll_mutex_trylock (mutex->__data.__lock) != 0);

mutex->__data.__spins += (cnt - mutex->__data.__spins) / 8;
}
break;
}

if (result == 0)
Expand Down
3 changes: 2 additions & 1 deletion nptl/sysdeps/unix/sysv/linux/alpha/bits/pthreadtypes.h
@@ -1,5 +1,5 @@
/* Machine-specific pthread type layouts. Alpha version.
Copyright (C) 2003 Free Software Foundation, Inc.
Copyright (C) 2003, 2004 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
Expand Down Expand Up @@ -56,6 +56,7 @@ typedef union
/* KIND must stay at this position in the structure to maintain
binary compatibility. */
int __kind;
int __spins;
} __data;
char __size[__SIZEOF_PTHREAD_MUTEX_T];
long int __align;
Expand Down
10 changes: 9 additions & 1 deletion nptl/sysdeps/unix/sysv/linux/alpha/lowlevellock.h
@@ -1,4 +1,4 @@
/* Copyright (C) 2003 Free Software Foundation, Inc.
/* Copyright (C) 2003, 2004 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
Expand Down Expand Up @@ -80,6 +80,14 @@ __lll_mutex_trylock(int *futex)
#define lll_mutex_trylock(lock) __lll_mutex_trylock (&(lock))


static inline int __attribute__((always_inline))
__lll_mutex_cond_trylock(int *futex)
{
return atomic_compare_and_exchange_val_acq (futex, 2, 0) != 0;
}
#define lll_mutex_cond_trylock(lock) __lll_mutex_cond_trylock (&(lock))


extern void __lll_lock_wait (int *futex) attribute_hidden;

static inline void __attribute__((always_inline))
Expand Down
3 changes: 2 additions & 1 deletion nptl/sysdeps/unix/sysv/linux/i386/bits/pthreadtypes.h
@@ -1,4 +1,4 @@
/* Copyright (C) 2002, 2003 Free Software Foundation, Inc.
/* Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
Expand Down Expand Up @@ -56,6 +56,7 @@ typedef union
binary compatibility. */
int __kind;
unsigned int __nusers;
int __spins;
} __data;
char __size[__SIZEOF_PTHREAD_MUTEX_T];
long int __align;
Expand Down
14 changes: 14 additions & 0 deletions nptl/sysdeps/unix/sysv/linux/i386/lowlevellock.h
Expand Up @@ -40,6 +40,7 @@
/* Initializer for compatibility lock. */
#define LLL_MUTEX_LOCK_INITIALIZER (0)
#define LLL_MUTEX_LOCK_INITIALIZER_LOCKED (1)
#define LLL_MUTEX_LOCK_INITIALIZER_WAITERS (2)


#ifdef PIC
Expand All @@ -60,6 +61,9 @@
# define LLL_ENTER_KERNEL "int $0x80\n\t"
#endif

/* Delay in spinlock loop. */
#define BUSY_WAIT_NOP asm ("rep; nop")


#define lll_futex_wait(futex, val) \
do { \
Expand Down Expand Up @@ -117,6 +121,16 @@ extern int __lll_mutex_unlock_wake (int *__futex)
ret; })


#define lll_mutex_cond_trylock(futex) \
({ int ret; \
__asm __volatile (LOCK_INSTR "cmpxchgl %2, %1" \
: "=a" (ret), "=m" (futex) \
: "r" (LLL_MUTEX_LOCK_INITIALIZER_WAITERS), \
"m" (futex), "0" (LLL_MUTEX_LOCK_INITIALIZER) \
: "memory"); \
ret; })


#define lll_mutex_lock(futex) \
(void) ({ int ignore1, ignore2; \
__asm __volatile (LOCK_INSTR "cmpxchgl %1, %2\n\t" \
Expand Down
3 changes: 2 additions & 1 deletion nptl/sysdeps/unix/sysv/linux/ia64/bits/pthreadtypes.h
@@ -1,4 +1,4 @@
/* Copyright (C) 2003 Free Software Foundation, Inc.
/* Copyright (C) 2003, 2004 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Jakub Jelinek <jakub@redhat.com>, 2003.
Expand Down Expand Up @@ -56,6 +56,7 @@ typedef union
/* KIND must stay at this position in the structure to maintain
binary compatibility. */
int __kind;
int __spins;
} __data;
char __size[__SIZEOF_PTHREAD_MUTEX_T];
long int __align;
Expand Down
10 changes: 9 additions & 1 deletion nptl/sysdeps/unix/sysv/linux/ia64/lowlevellock.h
@@ -1,4 +1,4 @@
/* Copyright (C) 2003 Free Software Foundation, Inc.
/* Copyright (C) 2003, 2004 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Jakub Jelinek <jakub@redhat.com>, 2003.
Expand Down Expand Up @@ -31,6 +31,9 @@
#define FUTEX_WAKE 1
#define FUTEX_REQUEUE 3

/* Delay in spinlock loop. */
#define BUSY_WAIT_NOP asm ("hint @pause")

/* Initializer for compatibility lock. */
#define LLL_MUTEX_LOCK_INITIALIZER (0)

Expand Down Expand Up @@ -62,6 +65,11 @@
#define lll_mutex_trylock(futex) __lll_mutex_trylock (&(futex))


#define __lll_mutex_cond_trylock(futex) \
(atomic_compare_and_exchange_val_acq (futex, 2, 0) != 0)
#define lll_mutex_cond_trylock(futex) __lll_mutex_cond_trylock (&(futex))


extern void __lll_lock_wait (int *futex) attribute_hidden;


Expand Down
3 changes: 2 additions & 1 deletion nptl/sysdeps/unix/sysv/linux/powerpc/bits/pthreadtypes.h
@@ -1,5 +1,5 @@
/* Machine-specific pthread type layouts. PowerPC version.
Copyright (C) 2003 Free Software Foundation, Inc.
Copyright (C) 2003, 2004 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Paul Mackerras <paulus@au.ibm.com>, 2003.
Expand Down Expand Up @@ -76,6 +76,7 @@ typedef union
#if __WORDSIZE != 64
unsigned int __nusers;
#endif
int __spins;
} __data;
char __size[__SIZEOF_PTHREAD_MUTEX_T];
long int __align;
Expand Down
17 changes: 16 additions & 1 deletion nptl/sysdeps/unix/sysv/linux/powerpc/lowlevellock.h
@@ -1,4 +1,4 @@
/* Copyright (C) 2003 Free Software Foundation, Inc.
/* Copyright (C) 2003, 2004 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Paul Mackerras <paulus@au.ibm.com>, 2003.
Expand Down Expand Up @@ -100,6 +100,21 @@

#define lll_mutex_trylock(lock) __lll_trylock (&(lock))

/* Set *futex to 2 if it is 0, atomically. Returns the old value */
#define lll_mutex_cond_trylock(futex) \
({ int __val; \
__asm __volatile ("1: lwarx %0,0,%2\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" (2), "1" (*futex) \
: "cr0", "memory"); \
__val; \
})


extern void __lll_lock_wait (int *futex) attribute_hidden;

Expand Down

0 comments on commit 2c0b891

Please sign in to comment.