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  * The cancelability state of a newly created thread is PTHREAD_CANCEL_ENABLE.
     10  *
     11  * STEPS:
     12  * 1. Create a thread.
     13  * 2. In the thread function, set cancel_flag to 1.
     14  * 3. Send out a thread cancel request to the new thread
     15  * 4. If the cancel request was honored, the cancel_flag will remain 1.
     16  * 5. If not, the thread will continue until the end of execution, cancel_flag will be set to -1
     17  *    and,therefore failing the test.
     18  */
     19 
     20 #include <pthread.h>
     21 #include <stdio.h>
     22 #include <errno.h>
     23 #include <unistd.h>
     24 #include "posixtest.h"
     25 
     26 #define INTHREAD 0		/* Control going to or is already for Thread */
     27 #define INMAIN 1		/* Control going to or is already for Main */
     28 
     29 int sem1;			/* Manual semaphore */
     30 int cancel_flag;
     31 
     32 /* Function that the thread executes upon its creation */
     33 void *a_thread_func()
     34 {
     35 	/* Set default cancel state should be ENABLE, meaning it should honor all
     36 	 * cancel requests. */
     37 	/* Set cancel type to ASYNCHRONOUS so that it honors cancel requests immediately. */
     38 	pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
     39 
     40 	cancel_flag = 1;
     41 
     42 	/* Indicate to main() that the thread has been created. */
     43 	sem1 = INMAIN;
     44 
     45 	/* Wait until main() has sent out a cancel request, meaning until it
     46 	 * sets sem1==INTHREAD. */
     47 	while (sem1 == INMAIN)
     48 		sleep(1);
     49 
     50 	/* If the thread correctly honors the cancel request, then the cancel_flag will
     51 	 * remain 1.  If it contiues on with the thread execution, then the cancel_flag
     52 	 * will be -1, and therefore failing this test. */
     53 	pthread_testcancel();
     54 
     55 	/* Should not reach here if the thread correctly honors the cancel 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 ignored rather than honored, and
     96 	 * the test fails. */
     97 	if (cancel_flag < 0) {
     98 		printf
     99 		    ("Test FAILED: Thread default cancel type is not PTHREAD_CANCEL_ENABLE, it did not honor cancel request\n");
    100 		return PTS_FAIL;
    101 	}
    102 
    103 	printf("Test PASSED\n");
    104 	return PTS_PASS;
    105 }
    106