Home | History | Annotate | Download | only in pthread_cancel
      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_cancel
      9  * Shall request that 'thread' shall be canceled.  The target thread's
     10  * cancelability state and type determines when the cancelation takes
     11  * effect.
     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
     19  * 3. Setup a cleanup handler for the thread.
     20  * 4. Send out a thread cancel request to the new thread
     21  * 5. If the cancel request was honored, the cleanup handler would have
     22  *    been executed and the test will fail.
     23  * 6. If not, the thread will continue until the end of execution and exit
     24  *    correctly, therefore passing the test.
     25  */
     26 
     27 #include <pthread.h>
     28 #include <stdio.h>
     29 #include <errno.h>
     30 #include <unistd.h>
     31 #include "posixtest.h"
     32 
     33 #define INTHREAD 0		/* Control going to or is already for Thread */
     34 #define INMAIN 1		/* Control going to or is already for Main */
     35 
     36 int sem1;			/* Manual semaphore */
     37 int cleanup_flag;
     38 
     39 /* Cleanup function that the thread executes when it is canceled.  So if
     40  * cleanup_flag is -1, it means that the thread was canceled, meaning
     41  * the test will fail. */
     42 void a_cleanup_func()
     43 {
     44 	cleanup_flag = -1;
     45 	return;
     46 }
     47 
     48 /* Function that the thread executes upon its creation */
     49 void *a_thread_func()
     50 {
     51 	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
     52 
     53 	pthread_cleanup_push(a_cleanup_func, NULL);
     54 
     55 	/* Indicate to main() that the thread has been created. */
     56 	sem1 = INMAIN;
     57 
     58 	/* Wait until main() has sent out a cancel request, meaning until it
     59 	 * sets sem1==INMAIN.  Sleeping for 3 secs. to give time for the
     60 	 * cancel request to be sent and processed. */
     61 	while (sem1 == INMAIN)
     62 		sleep(1);
     63 
     64 	/* Should reach here if the thread correctly ignores the cancel
     65 	 * request. */
     66 	pthread_cleanup_pop(0);
     67 	cleanup_flag = 1;
     68 	pthread_exit(0);
     69 	return NULL;
     70 }
     71 
     72 int main(void)
     73 {
     74 	pthread_t new_th;
     75 
     76 	/* Initializing values */
     77 	sem1 = INTHREAD;
     78 	cleanup_flag = 0;
     79 
     80 	/* Create a new thread. */
     81 	if (pthread_create(&new_th, NULL, a_thread_func, NULL) != 0) {
     82 		perror("Error creating thread\n");
     83 		return PTS_UNRESOLVED;
     84 	}
     85 
     86 	/* Make sure thread is created before we cancel it. (wait for
     87 	 * a_thread_func() to set sem1=1.) */
     88 	while (sem1 == INTHREAD)
     89 		sleep(1);
     90 
     91 	if (pthread_cancel(new_th) != 0) {
     92 		perror("Error sending cancel request\n");
     93 		return PTS_UNRESOLVED;
     94 	}
     95 
     96 	/* Indicate to the thread function that the thread cancel request
     97 	 * has been sent to it. */
     98 	sem1 = INTHREAD;
     99 
    100 	/* Wait for thread to return. (i.e. either canceled incorrectly and
    101 	 * called the cleanup function, or exited normally at the end of
    102 	 * thread execution like it should do. */
    103 	while (cleanup_flag == 0)
    104 		sleep(1);
    105 
    106 	/* This means that the cleanup function was called, meaning the
    107 	 * thread was canceled rather than ignored the cancel request. */
    108 	if (cleanup_flag <= 0) {
    109 		printf("Test FAILED\n");
    110 		return PTS_FAIL;
    111 	}
    112 
    113 	printf("Test PASSED\n");
    114 	return PTS_PASS;
    115 }
    116