+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.
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA. */
+#include <unwindbuf.h>
#include <sysdep.h>
#ifndef UP
.globl __pthread_once
.type __pthread_once,@function
.align 16
+ cfi_startproc
__pthread_once:
movl 4(%esp), %ecx
testl $2, (%ecx)
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
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)
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 $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
static pthread_barrier_t bar;
static int global;
+static int cl_called;
static void
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)
pthread_cond_wait (&cond, &mut);
/* We should never get here. */
+ exit (42);
}
static 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);
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;
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;
static pthread_barrier_t bar;
static int global;
+static int cl_called;
static void
once_handler1 (void)
}
+static void
+cl (void *arg)
+{
+ ++cl_called;
+}
+
+
static void *
tf1 (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);
static void *
tf2 (void *arg)
{
+ pthread_cleanup_push (cl, NULL);
+
int r = pthread_barrier_wait (&bar);
if (r != 0 && r!= PTHREAD_BARRIER_SERIAL_THREAD)
{
exit (1);
}
+ pthread_cleanup_pop (0);
+
pthread_once (&once, once_handler2);
return NULL;
if (pthread_barrier_init (&bar, NULL, 2) != 0)
{
puts ("barrier_init failed");
- exit (1);
+ return 1;
}
if (pthread_create (&th[0], NULL, tf1, 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 ("first 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;
}
if (pthread_create (&th[1], NULL, tf2, NULL) != 0)
{
puts ("second create failed");
- exit (1);
+ return 1;
}
r = pthread_barrier_wait (&bar);
if (r != 0 && r!= PTHREAD_BARRIER_SERIAL_THREAD)
{
puts ("second barrier_wait failed");
- exit (1);
+ return 1;
}
/* Give the second thread a chance to reach the pthread_once call. */
if (pthread_cancel (th[0]) != 0)
{
puts ("cancel failed");
- exit (1);
+ return 1;
}
void *result;
if (result != PTHREAD_CANCELED)
{
puts ("first join didn't return PTHREAD_CANCELED");
- exit (1);
+ return 1;
}
puts ("joined first thread");
if (result != NULL)
{
puts ("second join didn't return PTHREAD_CANCELED");
- exit (1);
+ return 1;
}
if (global != 1)
{
puts ("global still 0");
- exit (1);
+ return 1;
+ }
+
+ if (cl_called != 1)
+ {
+ printf ("cl_called = %d\n", cl_called);
+ return 1;
}
return 0;
--- /dev/null
+#include "tst-once3.c"
--- /dev/null
+#include "tst-once4.c"