Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Update.
2003-06-30  Ulrich Drepper  <drepper@redhat.com>

	* sysdeps/unix/sysv/linux/i386/pthread_once.S (__pthread_once):
	Use correct cleanup handler registration.  Add unwind info.
	* tst-once3.c: Add cleanup handler and check it is called.
	* tst-once4.c: Likewise.
	* tst-oncex3.c: New file.
	* tst-oncex4.c: New file.
	* Makefile: Add rules to build and run tst-oncex3 and tst-oncex4.
  • Loading branch information
Ulrich Drepper committed Jul 1, 2003
1 parent 5a8e918 commit 3a4d1e1
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 60 deletions.
10 changes: 10 additions & 0 deletions nptl/ChangeLog
@@ -1,3 +1,13 @@
2003-06-30 Ulrich Drepper <drepper@redhat.com>

* sysdeps/unix/sysv/linux/i386/pthread_once.S (__pthread_once):
Use correct cleanup handler registration. Add unwind info.
* tst-once3.c: Add cleanup handler and check it is called.
* tst-once4.c: Likewise.
* tst-oncex3.c: New file.
* tst-oncex4.c: New file.
* Makefile: Add rules to build and run tst-oncex3 and tst-oncex4.

2003-06-29 Ulrich Drepper <drepper@redhat.com>

* sysdeps/pthread/configure.in: Check for C cleanup handling in gcc.
Expand Down
93 changes: 52 additions & 41 deletions nptl/sysdeps/unix/sysv/linux/i386/pthread_once.S
Expand Up @@ -17,6 +17,7 @@
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA. */

#include <unwindbuf.h>
#include <sysdep.h>

#ifndef UP
Expand All @@ -36,6 +37,7 @@
.globl __pthread_once
.type __pthread_once,@function
.align 16
cfi_startproc
__pthread_once:
movl 4(%esp), %ecx
testl $2, (%ecx)
Expand All @@ -44,7 +46,11 @@ __pthread_once:
ret

1: pushl %ebx
cfi_adjust_cfa_offset (4)
cfi_rel_offset (3, 0)
pushl %esi
cfi_adjust_cfa_offset (4)
cfi_rel_offset (6, 0)
movl %ecx, %ebx
xorl %esi, %esi

Expand Down Expand Up @@ -90,33 +96,35 @@ __pthread_once:
jmp 6b

3: /* Call the initializer function after setting up the
cancellation handler. */
subl $16, %esp

/* Push the cleanup handler. */
#ifdef PIC
leal clear_once_control@GOTOFF(%ecx), %eax
#else
leal clear_once_control, %eax
#endif
movl %esp, %edx
pushl %ebx
pushl %eax
pushl %edx
call __pthread_cleanup_push /* Note: no @PLT. */

call *44(%esp)

/* Pop the cleanup handler. This code depends on the once
handler and _pthread_cleanup_push not touch the content
of the stack. Otherwise the first parameter would have
to be reloaded. */
cancellation handler. Note that it is not possible here
to use the unwind-based cleanup handling. This would require
that the user-provided function and all the code it calls
is compiled with exceptions. Unfortunately this cannot be
guaranteed. */
subl $UNWINDBUFSIZE+8, %esp
cfi_adjust_cfa_offset (UNWINDBUFSIZE+8)
movl %ecx, %ebx /* PIC register value. */

leal 8+UWJMPBUF(%esp), %eax
movl $0, 4(%esp)
call __pthread_cleanup_pop /* Note: no @PLT. */
movl %eax, (%esp)
call __sigsetjmp@PLT
jne 7f

addl $28, %esp
leal 8(%esp), %eax
call __pthread_register_cancel

/* Call the user-provided initialization function. */
call *24+UNWINDBUFSIZE(%esp)

/* Pop the cleanup handler. */
leal 8(%esp), %eax
call __pthread_unregister_cancel
addl $UNWINDBUFSIZE+8, %esp
cfi_adjust_cfa_offset (-UNWINDBUFSIZE-8)

/* Sucessful run of the initializer. Signal that we are done. */
movl 12(%esp), %ebx
LOCK
addl $1, (%ebx)

Expand All @@ -127,35 +135,38 @@ __pthread_once:
ENTER_KERNEL

4: popl %esi
cfi_adjust_cfa_offset (-4)
cfi_restore (6)
popl %ebx
cfi_adjust_cfa_offset (-4)
cfi_restore (3)
xorl %eax, %eax
ret

.size __pthread_once,.-__pthread_once

.globl __pthread_once_internal
__pthread_once_internal = __pthread_once

.globl pthread_once
pthread_once = __pthread_once


.type clear_once_control,@function
.align 16
clear_once_control:
pushl %ebx

movl 8(%esp), %ebx
7: /* __sigsetjmp returned for the second time. */
movl 20+UNWINDBUFSIZE(%esp), %ebx
cfi_adjust_cfa_offset (UNWINDBUFSIZE+16)
cfi_offset (3, -8)
cfi_offset (6, -12)
movl $0, (%ebx)

movl $0x7fffffff, %edx
movl $FUTEX_WAKE, %ecx
movl $SYS_futex, %eax
ENTER_KERNEL

popl %ebx
ret
.size clear_once_control,.-clear_once_control
leal 8(%esp), %eax
call __pthread_unwind_next /* Note: no @PLT. */
/* NOTREACHED */
hlt
cfi_endproc
.size __pthread_once,.-__pthread_once

.globl __pthread_once_internal
__pthread_once_internal = __pthread_once

.globl pthread_once
pthread_once = __pthread_once


#ifdef PIC
Expand Down
37 changes: 29 additions & 8 deletions nptl/tst-once3.c
Expand Up @@ -33,6 +33,7 @@ static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
static pthread_barrier_t bar;

static int global;
static int cl_called;

static void
once_handler1 (void)
Expand All @@ -42,6 +43,7 @@ once_handler1 (void)
puts ("once_handler1: mutex_lock failed");
exit (1);
}
puts ("once_handler1: locked");

int r = pthread_barrier_wait (&bar);
if (r != 0 && r!= PTHREAD_BARRIER_SERIAL_THREAD)
Expand All @@ -53,6 +55,7 @@ once_handler1 (void)
pthread_cond_wait (&cond, &mut);

/* We should never get here. */
exit (42);
}

static void
Expand All @@ -62,11 +65,22 @@ once_handler2 (void)
}


static void
cl (void *arg)
{
cl_called = 1;
}


static void *
tf (void *arg)
{
pthread_cleanup_push (cl, NULL)

pthread_once (&once, once_handler1);

pthread_cleanup_pop (0);

/* We should never get here. */
puts ("pthread_once in tf returned");
exit (1);
Expand All @@ -81,56 +95,63 @@ do_test (void)
if (pthread_barrier_init (&bar, NULL, 2) != 0)
{
puts ("barrier_init failed");
exit (1);
return 1;
}

if (pthread_create (&th, NULL, tf, NULL) != 0)
{
puts ("first create failed");
exit (1);
return 1;
}

int r = pthread_barrier_wait (&bar);
if (r != 0 && r!= PTHREAD_BARRIER_SERIAL_THREAD)
{
puts ("barrier_wait failed");
exit (1);
return 1;
}

if (pthread_mutex_lock (&mut) != 0)
{
puts ("mutex_lock failed");
exit (1);
return 1;
}
/* We unlock the mutex so that we catch the case where the pthread_cond_wait
call incorrectly resumes and tries to get the mutex. */
if (pthread_mutex_unlock (&mut) != 0)
{
puts ("mutex_unlock failed");
exit (1);
return 1;
}

/* Cancel the thread. */
puts ("going to cancel");
if (pthread_cancel (th) != 0)
{
puts ("cancel failed");
exit (1);
return 1;
}

void *result;
pthread_join (th, &result);
if (result != PTHREAD_CANCELED)
{
puts ("join didn't return PTHREAD_CANCELED");
exit (1);
return 1;
}

if (cl_called != 1)
{
puts ("cleanup handler not called");
return 1;
}

pthread_once (&once, once_handler2);

if (global != 1)
{
puts ("global still 0");
exit (1);
return 1;
}

return 0;
Expand Down

0 comments on commit 3a4d1e1

Please sign in to comment.