Back to index

glibc  2.9
tst-sem11.c
Go to the documentation of this file.
00001 #include <semaphore.h>
00002 #include <stdio.h>
00003 #include <unistd.h>
00004 #include <pthread.h>
00005 #include <internaltypes.h>
00006 
00007 #ifndef SEM_WAIT
00008 # define SEM_WAIT(s) sem_wait (s)
00009 #endif
00010 
00011 static void *
00012 tf (void *arg)
00013 {
00014 #ifdef PREPARE
00015   PREPARE
00016 #endif
00017   SEM_WAIT (arg);
00018   return NULL;
00019 }
00020 
00021 int
00022 main (void)
00023 {
00024   int tries = 5;
00025   pthread_t th;
00026   sem_t s;
00027  again:
00028   if (sem_init (&s, 0, 0) != 0)
00029     {
00030       puts ("sem_init failed");
00031       return 1;
00032     }
00033 
00034   struct new_sem *is = (struct new_sem *) &s;
00035 
00036   if (is->nwaiters != 0)
00037     {
00038       puts ("nwaiters not initialized");
00039       return 1;
00040     }
00041 
00042   if (pthread_create (&th, NULL, tf, &s) != 0)
00043     {
00044       puts ("pthread_create failed");
00045       return 1;
00046     }
00047 
00048   sleep (1);
00049 
00050   if (pthread_cancel (th) != 0)
00051     {
00052       puts ("pthread_cancel failed");
00053       return 1;
00054     }
00055 
00056   void *r;
00057   if (pthread_join (th, &r) != 0)
00058     {
00059       puts ("pthread_join failed");
00060       return 1;
00061     }
00062   if (r != PTHREAD_CANCELED && --tries > 0)
00063     {
00064       /* Maybe we get the scheduling right the next time.  */
00065       sem_destroy (&s);
00066       goto again;
00067     }
00068 
00069   if (is->nwaiters != 0)
00070     {
00071       puts ("nwaiters not reset");
00072       return 1;
00073     }
00074 
00075   return 0;
00076 }