summaryrefslogblamecommitdiffstats
path: root/testsuites/psxtests/psxcancel/init.c
blob: 2007529d42738fabbddf6efa3bd1b2f63b9a2d48 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
  

                                                           
                                         

   



                    



                     

                   

                  
                      


                      
      
 

                                           





                                             






                                            
                                      
 

                 







                                                               



                                                                    
   

 
                                   
 

                 







                                                                  
                                                       


                                                                 
   





                                                  

 

                                 
     
                                 
      

                 


                              
 
               
 
















                                                                   



                                                            

                                

                                                                        
                                          
                                    


                                             



                                                           



                                    

                                                                     
                                          
                                    


                                             



                                                        


   
             
 





                                  
 






                                                  

                                                                 







                                         
/*
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.org/license/LICENSE.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/time.h>
#include <unistd.h>
#include <errno.h>
#include <sched.h>

#if defined(__rtems__)
  #include <rtems.h>
  #include <bsp.h>
  #include <pmacros.h>
#endif

const char rtems_test_name[] = "PSXCANCEL";

/* forward declarations to avoid warnings */
void *POSIX_Init(void *argument);
void countTask_cancel_handler(void *ignored);
void *countTaskDeferred(void *ignored);
void *countTaskAsync(void *ignored);

volatile bool countTask_handler;

void countTask_cancel_handler(void *ignored)
{
  countTask_handler = true;
}

void *countTaskDeferred(void *ignored)
{
  int i=0;
  int type,state;
  int sc;

  sc = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &type);
  fatal_posix_service_status( sc, 0, "cancel state deferred" );
  rtems_test_assert( type == PTHREAD_CANCEL_ENABLE );
  sc = pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &state);
  fatal_posix_service_status( sc, 0, "cancel type deferred" );
  rtems_test_assert( state == PTHREAD_CANCEL_DEFERRED );
  while (1) {
    printf("countTaskDeferred: elapsed time (second): %2d\n", i++ );
    sleep(1);
    pthread_testcancel();
  }
}

void *countTaskAsync(void *ignored)
{
  int i=0;
  int type,state;
  int sc;

  sc = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &type);
  fatal_posix_service_status( sc, 0, "cancel state async" );
  rtems_test_assert( type == PTHREAD_CANCEL_ENABLE );
  sc = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &state);
  fatal_posix_service_status( sc, 0, "cancel type async" );
  rtems_test_assert( state == PTHREAD_CANCEL_DEFERRED );
  pthread_cleanup_push(countTask_cancel_handler, NULL);
  while (1) {
    printf("countTaskAsync: elapsed time (second): %2d\n", i++ );
    sleep(1);
  }
  countTask_handler = false;
  pthread_cleanup_pop(1);
  if ( countTask_handler == false ){
    puts("countTask_cancel_handler not executed");
    rtems_test_exit(0);
  }
}

#if defined(__rtems__)
  void *POSIX_Init(void *ignored)
#else
  int main(int argc, char **argv)
#endif
{
  pthread_t task;
  int       taskparameter = 0;
  int       sc;
  int       old;

  TEST_BEGIN();

  /* generate some error conditions */
  puts( "Init - pthread_setcancelstate - NULL oldstate - EINVAL" );
  sc = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
  fatal_posix_service_status( sc, EINVAL, "cancel state EINVAL" );

  puts( "Init - pthread_setcancelstate - bad state - EINVAL" );
  sc = pthread_setcancelstate(12, &old);
  fatal_posix_service_status( sc, EINVAL, "cancel state EINVAL" );

  puts( "Init - pthread_setcanceltype - NULL oldtype - EINVAL" );
  sc = pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
  fatal_posix_service_status( sc, EINVAL, "cancel type EINVAL" );

  puts( "Init - pthread_setcanceltype - bad type - EINVAL" );
  sc = pthread_setcanceltype(12, &old);
  fatal_posix_service_status( sc, EINVAL, "cancel type EINVAL" );

  puts( "Init - pthread_cancel - bad ID - EINVAL" );
  sc = pthread_cancel(0x100);
  fatal_posix_service_status( sc, EINVAL, "cancel bad Id" );

  /* Start countTask deferred */
  {
    sc = pthread_create(&task, NULL, countTaskDeferred, &taskparameter);
    if (sc) {
      perror("pthread_create: countTask");
      rtems_test_exit(EXIT_FAILURE);
    }
    /* sleep for 5 seconds, then cancel it */
    sleep(5);
    sc = pthread_cancel(task);
    fatal_posix_service_status( sc, 0, "cancel deferred" );
    sc = pthread_join(task, NULL);
    fatal_posix_service_status( sc, 0, "join deferred" );
  }

  /* Start countTask asynchronous */
  {
    sc = pthread_create(&task, NULL, countTaskAsync, &taskparameter);
    if (sc) {
      perror("pthread_create: countTask");
      rtems_test_exit(EXIT_FAILURE);
    }
    /* sleep for 5 seconds, then cancel it */
    sleep(5);
    sc = pthread_cancel(task);
    fatal_posix_service_status( sc, 0, "cancel async" );
    sc = pthread_join(task, NULL);
    fatal_posix_service_status( sc, 0, "join async" );
  }


  TEST_END();

  #if defined(__rtems__)
    rtems_test_exit(EXIT_SUCCESS);
    return NULL;
  #else
    return 0;
  #endif
}

/* configuration information */
#if defined(__rtems__)

#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER

#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION

#define CONFIGURE_MAXIMUM_POSIX_THREADS 2
#define CONFIGURE_POSIX_INIT_THREAD_TABLE

#define CONFIGURE_INIT
#include <rtems/confdefs.h>

#endif /* __rtems__ */