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