Home | History | Annotate | Download | only in pthread_testcancel
      1 /*
      2  * Copyright (c) 2002, Intel Corporation. All rights reserved.
      3  * Created by:  rolla.n.selbak REMOVE-THIS AT intel DOT com
      4  * This file is licensed under the GPL license.  For the full content
      5  * of this license, see the COPYING file at the top level of this
      6  * source tree.
      7 
      8  * Test pthread_cancelstate
      9  * It will have no affect on the calling thread if cancelability is disabled.
     10  *
     11  * STEPS:
     12  * 1. Create a thread.
     13  * 2. In the thread function, set the state to
     14  *    PTHREAD_CANCEL_DISABLE and the cancel_flag to -1.
     15  * 3. Send out a thread cancel request to the new thread
     16  * 4. If the cancel request was honored, the cancel_flag will remain -1.
     17  * 5. If not, the thread will continue until the end of execution, cancel_flag will be set to 1
     18  *    and,therefore passing the test.
     19  */
     20 
     21 #include <pthread.h>
     22 #include <stdio.h>
     23 #include <errno.h>
     24 #include <unistd.h>
     25 #include "posixtest.h"
     26 
     27 #define INTHREAD 0		/* Control going to or is already for Thread */
     28 #define INMAIN 1		/* Control going to or is already for Main */
     29 
     30 int sem1;			/* Manual semaphore */
     31 int cancel_flag;
     32 
     33 /* Function that the thread executes upon its creation */
     34 void *a_thread_func()
     35 {
     36 	/* Set cancel state to DISABLE, meaning it shouldn't honor any cancel requests. */
     37 	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
     38 
     39 	cancel_flag = -1;
     40 
     41 	/* Indicate to main() that the thread has been created. */
     42 	sem1 = INMAIN;
     43 
     44 	/* Wait until main() has sent out a cancel request, meaning until it
     45 	 * sets sem1==INTHREAD. */
     46 	while (sem1 == INMAIN)
     47 		sleep(1);
     48 
     49 	/* If the thread incorrectly honors the cancel request, then the cancel_flag will
     50 	 * remain -1.  If it contiues on with the thread execution, then the cancel_flag
     51 	 * will be 1, and therefore passing this test. */
     52 	pthread_testcancel();
     53 
     54 	/* Should reach here if the thread correctly ignores the cancel
     55 	 * request. */
     56 	cancel_flag = 1;
     57 	pthread_exit(0);
     58 	return NULL;
     59 }
     60 
     61 int main(void)
     62 {
     63 	pthread_t new_th;
     64 
     65 	/* Initializing values */
     66 	sem1 = INTHREAD;
     67 	cancel_flag = 0;
     68 
     69 	/* Create a new thread. */
     70 	if (pthread_create(&new_th, NULL, a_thread_func, NULL) != 0) {
     71 		perror("Error creating thread\n");
     72 		return PTS_UNRESOLVED;
     73 	}
     74 
     75 	/* Make sure thread is created before we cancel it. (wait for
     76 	 * a_thread_func() to set sem1=INMAIN.) */
     77 	while (sem1 == INTHREAD)
     78 		sleep(1);
     79 
     80 	if (pthread_cancel(new_th) != 0) {
     81 		perror("Error sending cancel request\n");
     82 		return PTS_UNRESOLVED;
     83 	}
     84 
     85 	/* Indicate to the thread function that the thread cancel request
     86 	 * has been sent to it. */
     87 	sem1 = INTHREAD;
     88 
     89 	/* Wait for thread to end execution. */
     90 	if (pthread_join(new_th, NULL) != 0) {
     91 		perror("Error in pthread_join()\n");
     92 		return PTS_UNRESOLVED;
     93 	}
     94 
     95 	/* This means that the cancel request was honored rather than ignored, and
     96 	 * the test fails. */
     97 	if (cancel_flag <= 0) {
     98 		printf
     99 		    ("Test FAILED: pthread_testcancel() was honored even though cancelability was disabled.\n");
    100 		return PTS_FAIL;
    101 	}
    102 
    103 	printf("Test PASSED\n");
    104 	return PTS_PASS;
    105 }
    106