Back to index

glibc  2.9
tst-tsd6.c
Go to the documentation of this file.
00001 #include <errno.h>
00002 #include <pthread.h>
00003 #include <stdio.h>
00004 #include <stdlib.h>
00005 #include <unistd.h>
00006 #include <sys/wait.h>
00007 
00008 #define NKEYS 100
00009 static pthread_key_t keys[NKEYS];
00010 static pthread_barrier_t b;
00011 
00012 
00013 static void *
00014 tf (void *arg)
00015 {
00016   void *res = NULL;
00017   for (int i = 0; i < NKEYS; ++i)
00018     {
00019       void *p = pthread_getspecific (keys[i]);
00020       pthread_setspecific (keys[i], (void *) 7);
00021       if (p != NULL)
00022        res = p;
00023     }
00024   if (arg != NULL)
00025     {
00026       pthread_barrier_wait (arg);
00027       pthread_barrier_wait (arg);
00028     }
00029   return res;
00030 }
00031 
00032 
00033 static int
00034 do_test (void)
00035 {
00036   pthread_barrier_init (&b, NULL, 2);
00037 
00038   for (int i = 0; i < NKEYS; ++i)
00039     if (pthread_key_create (&keys[i], NULL) != 0)
00040       {
00041        puts ("cannot create keys");
00042        return 1;
00043       }
00044 
00045   pthread_t th;
00046   if (pthread_create (&th, NULL, tf, &b) != 0)
00047     {
00048       puts ("cannot create thread in parent");
00049       return 1;
00050     }
00051 
00052   pthread_barrier_wait (&b);
00053 
00054   pid_t pid = fork ();
00055   if (pid == 0)
00056     {
00057       if (pthread_create (&th, NULL, tf, NULL) != 0)
00058        {
00059          puts ("cannot create thread in child");
00060          exit (1);
00061        }
00062 
00063       void *res;
00064       pthread_join (th, &res);
00065 
00066       exit (res != NULL);
00067     }
00068   else if (pid == -1)
00069     {
00070       puts ("cannot create child process");
00071       return 1;
00072     }
00073 
00074   int s;
00075   if (TEMP_FAILURE_RETRY (waitpid (pid, &s, 0)) != pid)
00076     {
00077       puts ("failing to wait for child process");
00078       return 1;
00079     }
00080 
00081   pthread_barrier_wait (&b);
00082   pthread_join (th, NULL);
00083 
00084   return !WIFEXITED (s) ? 2 : WEXITSTATUS (s);
00085 }
00086 
00087 
00088 #define TEST_FUNCTION do_test ()
00089 #include "../test-skeleton.c"