Back to index

glibc  2.9
tst-once3.c
Go to the documentation of this file.
00001 /* Copyright (C) 2002, 2003, 2005 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <pthread.h>
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include <time.h>
00024 
00025 
00026 #define N 100
00027 
00028 static pthread_once_t once = PTHREAD_ONCE_INIT;
00029 
00030 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
00031 static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
00032 
00033 static pthread_barrier_t bar;
00034 
00035 static int global;
00036 static int cl_called;
00037 
00038 static void
00039 once_handler1 (void)
00040 {
00041   if (pthread_mutex_lock (&mut) != 0)
00042     {
00043       puts ("once_handler1: mutex_lock failed");
00044       exit (1);
00045     }
00046   puts ("once_handler1: locked");
00047 
00048   int r = pthread_barrier_wait (&bar);
00049   if (r != 0 && r!= PTHREAD_BARRIER_SERIAL_THREAD)
00050     {
00051       puts ("once_handler1: barrier_wait failed");
00052       exit (1);
00053     }
00054 
00055   puts ("once_handler1: going to wait on cond");
00056 
00057   pthread_cond_wait (&cond, &mut);
00058 
00059   /* We should never get here.  */
00060   exit (42);
00061 }
00062 
00063 static void
00064 once_handler2 (void)
00065 {
00066   global = 1;
00067 }
00068 
00069 
00070 static void
00071 cl (void *arg)
00072 {
00073   cl_called = 1;
00074 }
00075 
00076 
00077 static void *
00078 tf (void *arg)
00079 {
00080   pthread_cleanup_push (cl, NULL)
00081 
00082   pthread_once (&once, once_handler1);
00083 
00084   pthread_cleanup_pop (0);
00085 
00086   /* We should never get here.  */
00087   puts ("pthread_once in tf returned");
00088   exit (1);
00089 }
00090 
00091 
00092 static int
00093 do_test (void)
00094 {
00095   pthread_t th;
00096 
00097   if (pthread_barrier_init (&bar, NULL, 2) != 0)
00098     {
00099       puts ("barrier_init failed");
00100       return 1;
00101     }
00102 
00103   if (pthread_create (&th, NULL, tf, NULL) != 0)
00104     {
00105       puts ("first create failed");
00106       return 1;
00107     }
00108 
00109   int r = pthread_barrier_wait (&bar);
00110   if (r != 0 && r!= PTHREAD_BARRIER_SERIAL_THREAD)
00111     {
00112       puts ("barrier_wait failed");
00113       return 1;
00114     }
00115 
00116   if (pthread_mutex_lock (&mut) != 0)
00117     {
00118       puts ("mutex_lock failed");
00119       return 1;
00120     }
00121   /* We unlock the mutex so that we catch the case where the pthread_cond_wait
00122      call incorrectly resumes and tries to get the mutex.  */
00123   if (pthread_mutex_unlock (&mut) != 0)
00124     {
00125       puts ("mutex_unlock failed");
00126       return 1;
00127     }
00128 
00129   /* Cancel the thread.  */
00130   puts ("going to cancel");
00131   if (pthread_cancel (th) != 0)
00132     {
00133       puts ("cancel failed");
00134       return 1;
00135     }
00136 
00137   void *result;
00138   pthread_join (th, &result);
00139   if (result != PTHREAD_CANCELED)
00140     {
00141       puts ("join didn't return PTHREAD_CANCELED");
00142       return 1;
00143     }
00144   puts ("joined successfully");
00145 
00146   printf ("once = %d\n", *(int *) &once);
00147 
00148   if (cl_called != 1)
00149     {
00150       puts ("cleanup handler not called");
00151       return 1;
00152     }
00153 
00154   pthread_once (&once, once_handler2);
00155 
00156   if (global != 1)
00157     {
00158       puts ("global still 0");
00159       return 1;
00160     }
00161 
00162   return 0;
00163 }
00164 
00165 #define TEST_FUNCTION do_test ()
00166 #define TIMEOUT 4
00167 #include "../test-skeleton.c"