Example #1
0
int main( int argc, char **argv )
{
    void *retval;
    pthread_attr_t attr;
    struct sched_param schedparam;

    CYG_TEST_INIT();

#ifdef TEST_NET
    sa.sin_family = AF_INET;
    sa.sin_len = sizeof(sa);
    inet_aton("127.0.0.1", &sa.sin_addr);
    sa.sin_port = htons(1234);
    init_all_network_interfaces();
#endif
    
    // Create test threads

    {
        pthread_attr_init( &attr );

        schedparam.sched_priority = 10;
        pthread_attr_setinheritsched( &attr, PTHREAD_EXPLICIT_SCHED );
        pthread_attr_setschedpolicy( &attr, SCHED_RR );
        pthread_attr_setschedparam( &attr, &schedparam );
        pthread_attr_setstackaddr( &attr, (void *)&thread1_stack[sizeof(thread1_stack)] );
        pthread_attr_setstacksize( &attr, sizeof(thread1_stack) );

        pthread_create( &thread1,
                        &attr,
                        pthread_entry1,
                        (void *)0x12345671);
    }

    {
        pthread_attr_init( &attr );

        schedparam.sched_priority = 5;
        pthread_attr_setinheritsched( &attr, PTHREAD_EXPLICIT_SCHED );
        pthread_attr_setschedpolicy( &attr, SCHED_RR );
        pthread_attr_setschedparam( &attr, &schedparam );
        pthread_attr_setstackaddr( &attr, (void *)&thread2_stack[sizeof(thread2_stack)] );
        pthread_attr_setstacksize( &attr, sizeof(thread2_stack) );

        pthread_create( &thread2,
                        &attr,
                        pthread_entry2,
                        (void *)0x12345672);
    }
    
    // Now join with thread1
    CYG_TEST_INFO( "Main: calling pthread_join(thread1)");
    pthread_join( thread1, &retval );

    // And thread 2
    CYG_TEST_INFO( "Main: calling pthread_join(thread2)");
    pthread_join( thread2, &retval );

    CYG_TEST_PASS_FINISH("select");
}
Example #2
0
int main(int argc, char **argv)
{
   int i;
   pthread_t cthread;
   pthread_attr_t stack_attr;

   if (argc != 2)
      exit(255);
   data[0] = atoi(argv[1]);

   pthread_attr_init(&stack_attr);

   for (i = 0; i < THREADS; i++) {
      pthread_mutex_init(mutex + i, NULL);
      pthread_mutex_lock(mutex + i);

#if defined(__MINGW32__) || defined(__MINGW64__)
      pthread_attr_setstackaddr(&stack_attr, &stacks[i]);
      pthread_attr_setstacksize(&stack_attr, sizeof(struct stack));
#else
      pthread_attr_setstack(&stack_attr, &stacks[i], sizeof(struct stack));
#endif

      pthread_create(&cthread, &stack_attr, thread, (void*)(uintptr_t)i);
   }

   pthread_mutex_unlock(mutex + 0);
   pthread_join(cthread, NULL);
}
Example #3
0
int main(int argc, char **argv)
{
    pthread_t thread;
    pthread_attr_t attr;
    void *retval;

    CYG_TEST_INIT();

    // Create test thread
    pthread_attr_init( &attr );

    pthread_attr_setstackaddr( &attr, (void *)&thread_stack[sizeof(thread_stack)] );
    pthread_attr_setstacksize( &attr, sizeof(thread_stack) );

    pthread_create( &thread,
                    &attr,
                    pthread_entry1,
                    (void *)0x12345678);

    // Now join with it
    pthread_join( thread, &retval );

    // check retval
    
    if( (long)retval == 0x12345679 )
        CYG_TEST_PASS_FINISH( "pthread1" );
    else
        CYG_TEST_FAIL_FINISH( "pthread1" );
}
Example #4
0
int main(int argc, char **argv)
{
    int i, j;
    int ret;
    void *retval[NTHREADS];

    CYG_TEST_INIT();

    // Create test threads
    for( i = 0; i < NTHREADS; i++ )
    {
        pthread_attr_t attr;
        pthread_attr_init( &attr );

        pthread_attr_setstackaddr( &attr, (void *)&thread_stack[i][sizeof(thread_stack[i])] );
        pthread_attr_setstacksize( &attr, sizeof(thread_stack[i]) );

        ret = pthread_create( &thread[i],
                              &attr,
                              pthread_entry[i],
                              (void *)(0x12340000+i));
        CYG_TEST_CHECK( ret == 0, "pthread_create() returned error");
    }

    // Let the threads get going    
    for ( i = 0; i < NTHREADS ; i++ ) {
        while ( thread_ready[i] == false )
            sched_yield();
    }

    // Now wait a bit to be sure that the other threads have reached
    // their cancellation points.
    for ( j = 0; j < 20 ; j++ )
        sched_yield();
    
    // Now cancel them
    for( i = 0; i < NTHREADS; i++ )    
        pthread_cancel( thread[i] );
        
    // Now join with threads
    for( i = 0; i < NTHREADS; i++ )
        pthread_join( thread[i], &retval[i] );


    // check retvals
    for( i = 0; i < NTHREADS; i++ )
        CYG_TEST_CHECK( retval[i] == PTHREAD_CANCELED,
                        "thread didn't exit with PTHREAD_CANCELED" );

    CYG_TEST_CHECK( cancel_handler1_called, "cancel_handler1 not called" );
    CYG_TEST_CHECK( cancel_handler2_called, "cancel_handler2 not called" );
    CYG_TEST_CHECK( cancel_handler3_called, "cancel_handler3 not called" );

    CYG_TEST_PASS_FINISH( "pthread3" );
        
}
Example #5
0
int
pthread_attr_setstack(pthread_attr_t *attrp, void *stackaddr, size_t stacksize)
{
	int n;

	if ((n = pthread_attr_setstackaddr(attrp, stackaddr)))
		return (n);
	(*attrp)->stack_size = stacksize;
	(*attrp)->stack_size -= (*attrp)->guard_size;

	return (0);
}
Example #6
0
int test( void )
{
  pthread_attr_t  attribute;
  void           *stackaddr;
  int             result;

  stackaddr = NULL;

  result = pthread_attr_setstackaddr( &attribute, stackaddr );

  return result;
}
Example #7
0
/*************************************************
  * Function:		Pthread_attr_setstackaddr()
  * Description:    设置线程栈的最低地址包裹函数(应用希望自己处理线程栈的分配问题)
  * Input:          *attr---线程属性结构 
  					stackaddr---线程栈内存单元最低地址
  * Output:         *attr---线程属性结构
  * Return:         0/errno 
*************************************************/
int Pthread_attr_setstackaddr(pthread_attr_t *attr,  void *stackaddr)
{
	int rval;
	
	if(attr==AII_NULL)
	{
		return -1;	
	}

	rval = pthread_attr_setstackaddr(attr, stackaddr);
	if (rval != 0)
	{
		debug_info(DEBUG_LEVEL_4,"Pthread_attr_setstackaddr() failed!\n");
	}
	
	return rval;
}
Example #8
0
int
pthread_attr_setstack(pthread_attr_t *attrp, void *stackaddr, size_t stacksize)
{
	int error;

	/*
	 * XXX Add an alignment test, on stackaddr for stack-grows-up
	 * archs or on stackaddr+stacksize for stack-grows-down archs
	 */
	if (stacksize < PTHREAD_STACK_MIN)
		return (EINVAL);
	if ((error = pthread_attr_setstackaddr(attrp, stackaddr)))
		return (error);
	(*attrp)->stack_size = stacksize;

	return (0);
}
Example #9
0
static pthread_t new_thread( void *(*entry)(void *))
{
    pthread_attr_t attr;

    pthread_attr_init( &attr );
    pthread_attr_setstackaddr( &attr, (void *)((char *)(&stack)+STACKSIZE) );
    pthread_attr_setstacksize( &attr, STACKSIZE );
    pthread_attr_setinheritsched( &attr, PTHREAD_EXPLICIT_SCHED );
    pthread_attr_setschedpolicy( &attr, SCHED_RR );

    pthread_create( &thread,
                    &attr,
                    entry,
                    0);

    return thread;
}
Example #10
0
void *POSIX_Init(
  void *argument
)
{
  int                 sc;
  pthread_t           id;
  pthread_attr_t      attr;
  struct timespec     delay_request;

  puts( "\n\n*** POSIX STACK ATTRIBUTE TEST 01 ***" );

  puts( "Init - Allocate stack from heap" );
  Stack_Low = malloc(PTHREAD_MINIMUM_STACK_SIZE);
  rtems_test_assert( Stack_Low );
  Stack_High = Stack_Low + PTHREAD_MINIMUM_STACK_SIZE;

  puts( "Init - Initialize thread attribute for user provided stack" );
  sc = pthread_attr_init( &attr );
  rtems_test_assert( !sc );

  sc = pthread_attr_setstackaddr( &attr, Stack_Low );
  rtems_test_assert( !sc );

  sc = pthread_attr_setstacksize( &attr, PTHREAD_MINIMUM_STACK_SIZE );
  rtems_test_assert( !sc );

  /* create threads */
  sc = pthread_create( &id, &attr, Test_Thread, NULL );
  rtems_test_assert( !sc );

  puts( "Init - let other thread run" );
  delay_request.tv_sec = 0;
  delay_request.tv_nsec = 5 * 100000000;
  sc = nanosleep( &delay_request, NULL );
  rtems_test_assert( !sc );

  puts( "*** END OF POSIX STACK ATTRIBUTE TEST 01 ***" );
  rtems_test_exit(0);

  return NULL; /* just so the compiler thinks we returned something */
}
Example #11
0
/*
 * pj_thread_create(...)
 */
PJ_DEF(pj_status_t) pj_thread_create( pj_pool_t *pool, 
				      const char *thread_name,
				      pj_thread_proc *proc, 
				      void *arg,
				      pj_size_t stack_size, 
				      unsigned flags,
				      pj_thread_t **ptr_thread)
{
#if PJ_HAS_THREADS
    pj_thread_t *rec;
    pthread_attr_t thread_attr;
    void *stack_addr;
    int rc;

    PJ_UNUSED_ARG(stack_addr);

    PJ_CHECK_STACK();
    PJ_ASSERT_RETURN(pool && proc && ptr_thread, PJ_EINVAL);

    /* Create thread record and assign name for the thread */
    rec = (struct pj_thread_t*) pj_pool_zalloc(pool, sizeof(pj_thread_t));
    PJ_ASSERT_RETURN(rec, PJ_ENOMEM);
    
    /* Set name. */
    if (!thread_name) 
	thread_name = "thr%p";
    
    if (strchr(thread_name, '%')) {
	pj_ansi_snprintf(rec->obj_name, PJ_MAX_OBJ_NAME, thread_name, rec);
    } else {
	strncpy(rec->obj_name, thread_name, PJ_MAX_OBJ_NAME);
	rec->obj_name[PJ_MAX_OBJ_NAME-1] = '\0';
    }

    /* Set default stack size */
    if (stack_size == 0)
	stack_size = PJ_THREAD_DEFAULT_STACK_SIZE;

#if defined(PJ_OS_HAS_CHECK_STACK) && PJ_OS_HAS_CHECK_STACK!=0
    rec->stk_size = stack_size;
    rec->stk_max_usage = 0;
#endif

    /* Emulate suspended thread with mutex. */
    if (flags & PJ_THREAD_SUSPENDED) {
	rc = pj_mutex_create_simple(pool, NULL, &rec->suspended_mutex);
	if (rc != PJ_SUCCESS) {
	    return rc;
	}

	pj_mutex_lock(rec->suspended_mutex);
    } else {
	pj_assert(rec->suspended_mutex == NULL);
    }
    

    /* Init thread attributes */
    pthread_attr_init(&thread_attr);

#if defined(PJ_THREAD_SET_STACK_SIZE) && PJ_THREAD_SET_STACK_SIZE!=0
    /* Set thread's stack size */
    rc = pthread_attr_setstacksize(&thread_attr, stack_size);
    if (rc != 0)
	return PJ_RETURN_OS_ERROR(rc);
#endif	/* PJ_THREAD_SET_STACK_SIZE */


#if defined(PJ_THREAD_ALLOCATE_STACK) && PJ_THREAD_ALLOCATE_STACK!=0
    /* Allocate memory for the stack */
    stack_addr = pj_pool_alloc(pool, stack_size);
    PJ_ASSERT_RETURN(stack_addr, PJ_ENOMEM);

    rc = pthread_attr_setstackaddr(&thread_attr, stack_addr);
    if (rc != 0)
	return PJ_RETURN_OS_ERROR(rc);
#endif	/* PJ_THREAD_ALLOCATE_STACK */


    /* Create the thread. */
    rec->proc = proc;
    rec->arg = arg;
    rc = pthread_create( &rec->thread, &thread_attr, &thread_main, rec);
    if (rc != 0) {
	return PJ_RETURN_OS_ERROR(rc);
    }

    *ptr_thread = rec;

    PJ_LOG(6, (rec->obj_name, "Thread created"));
    return PJ_SUCCESS;
#else
    pj_assert(!"Threading is disabled!");
    return PJ_EINVALIDOP;
#endif
}
VCOS_STATUS_T vcos_thread_create(VCOS_THREAD_T *thread,
                                 const char *name,
                                 VCOS_THREAD_ATTR_T *attrs,
                                 VCOS_THREAD_ENTRY_FN_T entry,
                                 void *arg)
{
   VCOS_STATUS_T st;
   pthread_attr_t pt_attrs;
   VCOS_THREAD_ATTR_T *local_attrs = attrs ? attrs : &default_attrs;

   int rc = pthread_attr_init(&pt_attrs);
   if (rc < 0)
      return VCOS_ENOMEM;

   st = vcos_semaphore_create(&thread->suspend, NULL, 0);
   if (st != VCOS_SUCCESS)
   {
      pthread_attr_destroy(&pt_attrs);
      return st;
   }

   thread->task_timer.pfn = NULL; /* Not initialised */

   pthread_attr_setstacksize(&pt_attrs, local_attrs->ta_stacksz);
#if VCOS_CAN_SET_STACK_ADDR
   if (local_attrs->ta_stackaddr)
   {
      pthread_attr_setstackaddr(&pt_attrs, local_attrs->ta_stackaddr);
   }
#else
   vcos_demand(local_attrs->ta_stackaddr == 0);
#endif

   /* pthread_attr_setpriority(&pt_attrs, local_attrs->ta_priority); */
   
   vcos_assert(local_attrs->ta_stackaddr == 0); /* Not possible */

   thread->entry = entry;
   thread->arg = arg;
   thread->legacy = local_attrs->legacy;

   if (name)
   {
	   strncpy(thread->name, name, sizeof(thread->name)-1);
	   thread->name[sizeof(thread->name)-1] = '\0';
   }
   else
   {
	   thread->name[0] = '\0';
   }

   rc = pthread_create(&thread->thread, &pt_attrs, vcos_thread_entry, thread);

   pthread_attr_destroy(&pt_attrs);

   memset(thread->at_exit, 0, sizeof(thread->at_exit));

   if (rc < 0)
   {
      vcos_semaphore_delete(&thread->suspend);
      return VCOS_ENOMEM;
   }
   else
   {
      return VCOS_SUCCESS;
   }
}
Example #13
0
int
GC_pthread_create(pthread_t *new_thread,
          const pthread_attr_t *attr_in,
          void * (*thread_execp)(void *), void *arg)
{
    int result;
    GC_thread t;
    pthread_t my_new_thread;
    pthread_attr_t  attr;
    word my_flags = 0;
    int  flag;
    void * stack = 0;
    size_t stack_size = 0;
    int    n;
    struct sched_param schedparam;
   
    (void)pthread_attr_init(&attr);
    if (attr_in != 0) {
	(void)pthread_attr_getstacksize(attr_in, &stack_size);
	(void)pthread_attr_getstackaddr(attr_in, &stack);
    }

    LOCK();
    if (!GC_is_initialized) {
	    GC_init_inner();
    }
    GC_multithreaded++;
	    
    if (stack == 0) {
     	if (stack_size == 0)
		stack_size = 1048576;
			  /* ^-- 1 MB (this was GC_min_stack_sz, but that
			   * violates the pthread_create documentation which
			   * says the default value if none is supplied is
			   * 1MB) */
	else
		stack_size += thr_min_stack();

     	stack = (void *)GC_stack_alloc(&stack_size);
     	if (stack == 0) {
	    GC_multithreaded--;
     	    UNLOCK();
	    errno = ENOMEM;
     	    return -1;
     	}
    } else {
    	my_flags |= CLIENT_OWNS_STACK;
    }
    (void)pthread_attr_setstacksize(&attr, stack_size);
    (void)pthread_attr_setstackaddr(&attr, stack);
    if (attr_in != 0) {
	(void)pthread_attr_getscope(attr_in, &n);
	(void)pthread_attr_setscope(&attr, n);
	(void)pthread_attr_getschedparam(attr_in, &schedparam);
	(void)pthread_attr_setschedparam(&attr, &schedparam);
	(void)pthread_attr_getschedpolicy(attr_in, &n);
	(void)pthread_attr_setschedpolicy(&attr, n);
	(void)pthread_attr_getinheritsched(attr_in, &n);
	(void)pthread_attr_setinheritsched(&attr, n);

	(void)pthread_attr_getdetachstate(attr_in, &flag);
	if (flag == PTHREAD_CREATE_DETACHED) {
		my_flags |= DETACHED;
	}
	(void)pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
    }
    /*
     * thr_create can call malloc(), which if redirected will
     * attempt to acquire the allocation lock.
     * Unlock here to prevent deadlock.
     */


#if 0
#ifdef I386
    UNLOCK();
#endif
#endif
    result = 
	    pthread_create(&my_new_thread, &attr, thread_execp, arg);
#if 0
#ifdef I386
    LOCK();
#endif
#endif
    if (result == 0) {
        t = GC_new_thread(my_new_thread);
        t -> flags = my_flags;
        if (!(my_flags & DETACHED)) cond_init(&(t->join_cv), USYNC_THREAD, 0);
        t -> stack = stack;
        t -> stack_size = stack_size;
        if (new_thread != 0) *new_thread = my_new_thread;
        pthread_cond_signal(&GC_create_cv);
    } else {
	    if (!(my_flags & CLIENT_OWNS_STACK)) {
		    GC_stack_free(stack, stack_size);
	    }        
	    GC_multithreaded--;
    }
    UNLOCK();
    pthread_attr_destroy(&attr);
    return(result);
}
Example #14
0
WRAPPER(int, pthread_create, pthread_t *thr, const pthread_attr_t *attr, 
	 void * (*start) (void *), void *arg)
{
  DECLARE(int, munmap, void *p, size_t l);
  DECLARE(void *, mmap, void *p, size_t l, int prot, int flags, int fd, off_t of);
  DECLARE(int, pthread_create, pthread_t *thr, const pthread_attr_t *attr, 
	  void * (*start) (void *), void *arg);
  int result;
  pthread_attr_t override_attr;
  void *override_stack;
  size_t override_stacksize;
  void *override_stack_alloc = (void *) 0;
  size_t override_stacksize_alloc = 0;
  unsigned i;

  TRACE ("pthread_create\n");

  /* Garbage-collect dead threads' stacks.  */
  LOCKTH ();
  for (i = 0; i < LIBMUDFLAPTH_THREADS_MAX; i++)
    {
      struct pthread_info *pi = & __mf_pthread_info [i];
      if (! pi->used_p)
	continue;
      if (! pi->dead_p)
	continue;

      /* VERBOSE_TRACE ("thread %u pi %p stack cleanup deferred (%u)\n",
	 (unsigned) pi->self, pi, pi->dead_p); */
	      
      /* Delay actual deallocation by a few cycles, try to discourage the
	 race mentioned at the end of __mf_pthread_spawner().  */
      if (pi->dead_p)
	pi->dead_p ++;
      if (pi->dead_p >= 10 /* XXX */)
	{
	  if (pi->stack)
	    CALL_REAL (munmap, pi->stack_alloc, pi->stack_size_alloc);

	  VERBOSE_TRACE ("slot %u freed, stack %p\n", i, pi->stack_alloc);
	  memset (pi, 0, sizeof (*pi));

	  /* One round of garbage collection is enough.  */
	  break;
	}
    }
  UNLOCKTH ();

  /* Let's allocate a stack for this thread, if one is not already
     supplied by the caller.  We don't want to let e.g. the
     linuxthreads manager thread do this allocation.  */
  if (attr != NULL)
    override_attr = *attr;
  else
    pthread_attr_init (& override_attr);

  /* Get supplied attributes, if any.  */
  /* XXX: consider using POSIX2K attr_getstack() */
  if (pthread_attr_getstackaddr (& override_attr, & override_stack) != 0 ||
      pthread_attr_getstacksize (& override_attr, & override_stacksize) != 0)
    {
      override_stack = NULL;
      override_stacksize = 0;
    }

  /* Do we need to allocate the new thread's stack?  */
  if (__mf_opts.thread_stack && override_stack == NULL)
    {
      uintptr_t alignment = 256; /* power of two */

      /* Perturb the initial stack addresses slightly, to encourage
	 threads to have nonconflicting entries in the lookup cache
	 for their tracked stack objects.  */
      static unsigned perturb = 0;
      const unsigned perturb_delta = 32;
      const unsigned perturb_count = 16;
      perturb += perturb_delta;
      if (perturb > perturb_delta*perturb_count) perturb = 0;

      /* Use glibc x86 defaults */
/* Should have been defined in <limits.h> */
#ifndef PTHREAD_STACK_MIN
#define PTHREAD_STACK_MIN 65536
#endif
      override_stacksize = max (PTHREAD_STACK_MIN, __mf_opts.thread_stack * 1024);


#if defined(MAP_ANONYMOUS)
#define MF_MAP_ANON MAP_ANONYMOUS
#elif defined(MAP_ANON)
#define MF_MAP_ANON MAP_ANON
#endif

#ifndef MAP_FAILED
#define MAP_FAILED ((void *) -1)
#endif

#ifdef MF_MAP_ANON
      override_stack = CALL_REAL (mmap, NULL, override_stacksize, 
				  PROT_READ|PROT_WRITE, 
				  MAP_PRIVATE|MF_MAP_ANON,
				  0, 0);
#else
      /* Try mapping /dev/zero instead.  */
      {
        static int zerofd = -1;
        if (zerofd == -1)
          zerofd = open ("/dev/zero", O_RDWR);
        if (zerofd == -1)
          override_stack = MAP_FAILED;
        else
          override_stack = CALL_REAL (mmap, NULL, override_stacksize, 
                                      PROT_READ|PROT_WRITE, 
                                      MAP_PRIVATE, zerofd, 0);
      }
#endif

      if (override_stack == 0 || override_stack == MAP_FAILED)
	{
	  errno = EAGAIN;
	  return -1;
	}

      VERBOSE_TRACE ("thread stack alloc %p size %lu\n", 
		     override_stack, (unsigned long) override_stacksize);

      /* Save the original allocated values for later deallocation.  */
      override_stack_alloc = override_stack;
      override_stacksize_alloc = override_stacksize;

      /* The stackaddr pthreads attribute is a candidate stack pointer.
	 It must point near the top or the bottom of this buffer, depending
	 on whether stack grows downward or upward, and suitably aligned.
	 On the x86, it grows down, so we set stackaddr near the top.  */
      /* XXX: port logic */
      override_stack = (void *)
	(((uintptr_t) override_stack + override_stacksize - alignment - perturb)
	 & (~(uintptr_t)(alignment-1)));
      
      /* XXX: consider using POSIX2K attr_setstack() */
      if (pthread_attr_setstackaddr (& override_attr, override_stack) != 0 ||
	  pthread_attr_setstacksize (& override_attr, 
				     override_stacksize - alignment - perturb) != 0)
	{
	  /* This should not happen.  */
	  CALL_REAL (munmap, override_stack, override_stacksize);
	  errno = EAGAIN;
	  return -1;
	}
  }

  /* Actually start the child thread.  */
  {
    struct pthread_start_info psi;
    struct pthread_info *pi = NULL;
    
    /* Fill in startup-control fields.  */
    psi.user_fn = start;
    psi.user_arg = arg;
    psi.thread_info = NULL;
    
    /* Actually create the thread.  */
    __mf_state = reentrant;
    result = CALL_REAL (pthread_create, thr, & override_attr,
			& __mf_pthread_spawner, (void *) & psi);
    __mf_state = active;
    /* We also hook pthread_join/pthread_exit to get into reentrant
       mode during thread shutdown/cleanup.  */

    /* Wait until child thread has progressed far enough into its
       __mf_pthread_spawner() call.  */
    while (1) /* XXX: timeout? */
      {
	volatile struct pthread_start_info *psip = & psi;
	pi = psip->thread_info;
	if (pi != NULL) 
	  break;
	sched_yield ();
      }

    /* Fill in remaining fields in pthread_info. */
    pi->stack = override_stack;
    pi->stack_size = override_stacksize;
    pi->stack_alloc = override_stack_alloc;
    pi->stack_size_alloc = override_stacksize_alloc;
    /* XXX: this might be too late for future heuristics that attempt
       to use thread stack bounds.  We may need to put the new thread
       to sleep. */
  }


  /* May need to clean up if we created a pthread_attr_t of our own.  */
  if (attr == NULL)
    pthread_attr_destroy (& override_attr); /* NB: this shouldn't deallocate stack */

  return result;
}
Example #15
0
void *POSIX_Init(
  void *argument
)
{
  int                 status;
  int                 scope;
  int                 inheritsched;
  int                 schedpolicy;
  size_t              stacksize;
#if HAVE_DECL_PTHREAD_ATTR_SETGUARDSIZE
  size_t              guardsize;
#endif
  void               *stackaddr;
  int                 detachstate;
  struct sched_param  schedparam;
  pthread_attr_t      attr;
  pthread_attr_t      destroyed_attr;

  TEST_BEGIN();

  /* set the time of day, and print our buffer in multiple ways */

  set_time( TM_FRIDAY, TM_MAY, 24, 96, 11, 5, 0 );

  /* get id of this thread */

  Init_id = pthread_self();
  printf( "Init's ID is 0x%08" PRIxpthread_t "\n", Init_id );

  /* exercise init and destroy */

  puts( "Init - pthread_attr_init - EINVAL (NULL attr)" );
  status = pthread_attr_init( NULL );
  fatal_directive_check_status_only( status, EINVAL, "null attribute" );

  puts( "Init - pthread_attr_init - SUCCESSFUL" );
  status = pthread_attr_init( &attr );
  posix_service_failed( status, "pthread_attr_init" );

  puts( "Init - initialize and destroy an attribute - SUCCESSFUL" );
  status = pthread_attr_init( &destroyed_attr );
  posix_service_failed( status, "pthread_attr_init");

  status = pthread_attr_destroy( &destroyed_attr );
  posix_service_failed( status, "pthread_attr_destroy");

  puts( "Init - pthread_attr_destroy - EINVAL (NULL attr)" );
  status = pthread_attr_destroy( NULL );
  fatal_directive_check_status_only( status, EINVAL, "NULL attribute" );

  puts( "Init - pthread_attr_destroy - EINVAL (not initialized)" );
  status = pthread_attr_destroy( &destroyed_attr );
  fatal_directive_check_status_only( status, EINVAL, "not initialized" );

  /* check some errors in pthread_create */

  puts( "Init - pthread_create - EINVAL (attr not initialized)" );
  status = pthread_create( &Task_id, &destroyed_attr, Task_1, NULL );
  fatal_directive_check_status_only( status, EINVAL, "attribute not initialized" );

  /* junk stack address */
  status = pthread_attr_setstackaddr( &attr, (void *)&schedparam );
  posix_service_failed( status, "setstackaddr");

  /* must go around pthread_attr_setstacksize to set a bad stack size */
  attr.stacksize = 0;

  puts( "Init - pthread_create - EINVAL (stacksize too small)" );
  status = pthread_create( &Task_id, &attr, Task_1, NULL );
  fatal_directive_check_status_only( status, EINVAL, "stacksize too small" );

  /* reset all the fields */
  status = pthread_attr_init( &attr );
  posix_service_failed( status, "pthread_attr_init");

#if HAVE_DECL_PTHREAD_ATTR_SETSTACKADDR
  attr.stacksize = rtems_configuration_get_work_space_size() * 10;
  puts( "Init - pthread_create - EAGAIN (stacksize too large)" );
  status = pthread_create( &Task_id, &attr, Task_1, NULL );
  fatal_directive_check_status_only( status, EAGAIN, "stacksize too large" );
#endif

  status = pthread_attr_init( &attr );
  posix_service_failed( status, "pthread_attr_init");

  /* must go around pthread_attr_set routines to set a bad value */
  attr.inheritsched = -1;

  puts( "Init - pthread_create - EINVAL (invalid inherit scheduler)" );
  status = pthread_create( &Task_id, &attr, Task_1, NULL );
  fatal_directive_check_status_only( status, EINVAL, "invalid inherit scheduler" );

  /* check out the error case for system scope not supported */

  status = pthread_attr_init( &attr );
  posix_service_failed( status, " pthread_attr_init");

  /* must go around pthread_attr_set routines to set a bad value */
  attr.contentionscope = PTHREAD_SCOPE_SYSTEM;

  puts( "Init - pthread_create - ENOTSUP (unsupported system contention scope)" );
  status = pthread_create( &Task_id, &attr, Task_1, NULL );
  fatal_directive_check_status_only( status, ENOTSUP,
    "unsupported system contention scope" );

  status = pthread_attr_init( &attr );
  posix_service_failed( status, "pthread_attr_init");

  /* now check out pthread_create for inherit scheduler */

  status = pthread_attr_setinheritsched( &attr, PTHREAD_INHERIT_SCHED );
  posix_service_failed( status, "pthread_attr_setinheritsched");

  puts( "Init - pthread_create - SUCCESSFUL (inherit scheduler)" );
  status = pthread_create( &Task_id, &attr, Task_1, NULL );
  posix_service_failed( status, "pthread_create");

  status = pthread_join( Task_id, NULL );
  posix_service_failed( status, " pthread_join");

    /* switch to Task_1 */

  /* exercise get and set scope */

  empty_line();

  status = pthread_attr_init( &attr );
  posix_service_failed( status, "pthread_attr_init");

  puts( "Init - pthread_attr_setscope - EINVAL (NULL attr)" );
  status = pthread_attr_setscope( NULL, PTHREAD_SCOPE_PROCESS );
  fatal_directive_check_status_only( status, EINVAL , "NULL attr" );

  puts( "Init - pthread_attr_setscope - ENOTSUP" );
  status = pthread_attr_setscope( &attr, PTHREAD_SCOPE_SYSTEM );
  fatal_directive_check_status_only( status, ENOTSUP, "PTHREAD_SCOPE_SYSTEM" );

  puts( "Init - pthread_attr_setscope - EINVAL (not initialized attr)" );
  status = pthread_attr_setscope( &destroyed_attr, PTHREAD_SCOPE_PROCESS );
  fatal_directive_check_status_only( status, EINVAL, "not initialized attr" );

  puts( "Init - pthread_attr_setscope - EINVAL (invalid scope)" );
  status = pthread_attr_setscope( &attr, -1 );
  fatal_directive_check_status_only( status, EINVAL, "invalid scope" );

  puts( "Init - pthread_attr_setscope - SUCCESSFUL" );
  status = pthread_attr_setscope( &attr, PTHREAD_SCOPE_PROCESS );
  posix_service_failed( status, "pthread_attr_setscope");

  puts( "Init - pthread_attr_getscope - EINVAL (NULL attr)" );
  status = pthread_attr_getscope( NULL, &scope );
  fatal_directive_check_status_only( status, EINVAL, "NULL attr" );

  puts( "Init - pthread_attr_getscope - EINVAL (NULL scope)" );
  status = pthread_attr_getscope( &attr, NULL );
  fatal_directive_check_status_only( status, EINVAL, "NULL scope" );

  puts( "Init - pthread_attr_getscope - EINVAL (not initialized attr)" );
  status = pthread_attr_getscope( &destroyed_attr, &scope );
  fatal_directive_check_status_only( status, EINVAL, "not initialized attr" );

  puts( "Init - pthread_attr_getscope - SUCCESSFUL" );
  status = pthread_attr_getscope( &attr, &scope );
  posix_service_failed( status, "pthread_attr_getscope");
  printf( "Init - current scope attribute = %d\n", scope );

  /* exercise get and set inherit scheduler */

  empty_line();

  puts( "Init - pthread_attr_setinheritsched - EINVAL (NULL attr)" );
  status = pthread_attr_setinheritsched( NULL, PTHREAD_INHERIT_SCHED );
  fatal_directive_check_status_only( status, EINVAL, "NULL attr" );

  puts( "Init - pthread_attr_setinheritsched - EINVAL (not initialized attr)" );
  status =
     pthread_attr_setinheritsched( &destroyed_attr, PTHREAD_INHERIT_SCHED );
  fatal_directive_check_status_only( status, EINVAL, "not initialized attr" );

  puts( "Init - pthread_attr_setinheritsched - ENOTSUP (invalid inheritsched)" );
  status = pthread_attr_setinheritsched( &attr, -1 );
  fatal_directive_check_status_only( status, ENOTSUP, "invalid inheritsched" );

  puts( "Init - pthread_attr_setinheritsched - SUCCESSFUL" );
  status = pthread_attr_setinheritsched( &attr, PTHREAD_INHERIT_SCHED );
  posix_service_failed( status, "pthread_attr_setinheritsched");

  puts( "Init - pthread_attr_getinheritsched - EINVAL (NULL attr)" );
  status = pthread_attr_getinheritsched( NULL, &inheritsched );
  fatal_directive_check_status_only( status, EINVAL, "NULL attr" );

  puts( "Init - pthread_attr_getinheritsched - EINVAL (NULL inheritsched)" );
  status = pthread_attr_getinheritsched( &attr, NULL );
  fatal_directive_check_status_only( status, EINVAL, "NULL inheritsched" );

  puts( "Init - pthread_attr_getinheritsched - EINVAL (not initialized attr)" );
  status = pthread_attr_getinheritsched( &destroyed_attr, &inheritsched );
  fatal_directive_check_status_only( status, EINVAL, "not initialized attr" );

  puts( "Init - pthread_attr_getinheritsched - SUCCESSFUL" );
  status = pthread_attr_getinheritsched( &attr, &inheritsched );
  posix_service_failed( status, "pthread_attr_getinheritsched");
  printf( "Init - current inherit scheduler attribute = %d\n", inheritsched );

  /* exercise get and set inherit scheduler */

  empty_line();

  puts( "Init - pthread_attr_setschedpolicy - EINVAL (NULL attr)" );
  status = pthread_attr_setschedpolicy( NULL, SCHED_FIFO );
  fatal_directive_check_status_only( status, EINVAL, "NULL attr" );

  puts( "Init - pthread_attr_setschedpolicy - EINVAL (not initialized attr)" );
  status =
     pthread_attr_setschedpolicy( &destroyed_attr, SCHED_OTHER );
  fatal_directive_check_status_only( status, EINVAL, "not initialized attr" );

  puts( "Init - pthread_attr_setschedpolicy - ENOTSUP (invalid schedpolicy)" );
  status = pthread_attr_setschedpolicy( &attr, -1 );
  fatal_directive_check_status_only( status, ENOTSUP, "invalid schedpolicy" );

  puts( "Init - pthread_attr_setschedpolicy - SUCCESSFUL" );
  status = pthread_attr_setschedpolicy( &attr, SCHED_RR );
  posix_service_failed( status, "pthread_attr_setschedpolicy");

  puts( "Init - pthread_attr_getschedpolicy - EINVAL (NULL attr)" );
  status = pthread_attr_getschedpolicy( NULL, &schedpolicy );
  fatal_directive_check_status_only( status, EINVAL, "NULL attr" );

  puts( "Init - pthread_attr_getschedpolicy - EINVAL (NULL schedpolicy)" );
  status = pthread_attr_getschedpolicy( &attr, NULL );
  fatal_directive_check_status_only( status, EINVAL, "NULL schedpolicy" );

  puts( "Init - pthread_attr_getschedpolicy - EINVAL (not initialized attr)" );
  status = pthread_attr_getschedpolicy( &destroyed_attr, &schedpolicy );
  fatal_directive_check_status_only( status, EINVAL, "not initialized attr" );

  puts( "Init - pthread_attr_getschedpolicy - SUCCESSFUL" );
  status = pthread_attr_getschedpolicy( &attr, &schedpolicy );
  posix_service_failed( status, "pthread_attr_getschedpolicy");
  printf( "Init - current scheduler policy attribute = %d\n", schedpolicy );

  /* exercise get and set stack size */

  empty_line();

  puts( "Init - pthread_attr_setstacksize - EINVAL (NULL attr)" );
  status = pthread_attr_setstacksize( NULL, 0 );
  fatal_directive_check_status_only( status, EINVAL, "NULL attr" );

  puts( "Init - pthread_attr_setstacksize - EINVAL (not initialized attr)" );
  status =
     pthread_attr_setstacksize( &destroyed_attr, 0 );
  fatal_directive_check_status_only( status, EINVAL, "not initialized attr" );

  puts( "Init - pthread_attr_setstacksize - SUCCESSFUL (low stacksize)" );
  status = pthread_attr_setstacksize( &attr, 0 );
  posix_service_failed( status, "pthread_attr_setstacksize");

  puts( "Init - pthread_attr_setstacksize - SUCCESSFUL (high stacksize)" );
  status = pthread_attr_setstacksize( &attr, STACK_MINIMUM_SIZE * 2 );
  posix_service_failed( status, "");

  puts( "Init - pthread_attr_getstacksize - EINVAL (NULL attr)" );
  status = pthread_attr_getstacksize( NULL, &stacksize );
  fatal_directive_check_status_only( status, EINVAL, "NULL attr" );

  puts( "Init - pthread_attr_getstacksize - EINVAL (NULL stacksize)" );
  status = pthread_attr_getstacksize( &attr, NULL );
  fatal_directive_check_status_only( status, EINVAL, "NULL stacksize" );

  puts( "Init - pthread_attr_getstacksize - EINVAL (not initialized attr)" );
  status = pthread_attr_getstacksize( &destroyed_attr, &stacksize );
  fatal_directive_check_status_only( status, EINVAL, "not initialized attr" );

  puts( "Init - pthread_attr_getstacksize - SUCCESSFUL" );
  status = pthread_attr_getstacksize( &attr, &stacksize );
  posix_service_failed( status, "pthread_attr_getstacksize");
  if ( stacksize == (STACK_MINIMUM_SIZE * 2) )
    printf( "Init - current stack size attribute is OK\n" );

  /* exercise get and set stack address */
  empty_line();

  puts( "Init - pthread_attr_setstackaddr - EINVAL (NULL attr)" );
  status = pthread_attr_setstackaddr( NULL, NULL );
  fatal_directive_check_status_only( status, EINVAL, "NULL attr" );

  puts( "Init - pthread_attr_setstackaddr - EINVAL (not initialized attr)" );
  status = pthread_attr_setstackaddr( &destroyed_attr, NULL );
  fatal_directive_check_status_only( status, EINVAL, "not initialized attr" );

  puts( "Init - pthread_attr_setstackaddr - SUCCESSFUL" );
  status = pthread_attr_setstackaddr( &attr, 0 );
  posix_service_failed( status, "");

  /* get stack addr */
  puts( "Init - pthread_attr_getstackaddr - EINVAL (NULL attr)" );
  status = pthread_attr_getstackaddr( NULL, &stackaddr );
  fatal_directive_check_status_only( status, EINVAL, "NULL attr" );

  puts( "Init - pthread_attr_getstackaddr - EINVAL (NULL stackaddr)" );
  status = pthread_attr_getstackaddr( &attr, NULL );
  fatal_directive_check_status_only( status, EINVAL, "NULL stackaddr" );

  puts( "Init - pthread_attr_getstackaddr - EINVAL (not initialized attr)" );
  status = pthread_attr_getstackaddr( &destroyed_attr, &stackaddr );
  fatal_directive_check_status_only( status, EINVAL, "not initialized attr" );

  puts( "Init - pthread_attr_getstackaddr - SUCCESSFUL" );
  status = pthread_attr_getstackaddr( &attr, &stackaddr );
  posix_service_failed( status, "pthread_attr_getstackaddr");
  printf( "Init - current stack address attribute = %p\n", stackaddr );

  /* exercise get and set stack (as pair) */
  empty_line();

#if HAVE_DECL_PTHREAD_ATTR_SETSTACK
  puts( "Init - pthread_attr_setstack- EINVAL (NULL attr)" );
  status = pthread_attr_setstack( NULL, &stackaddr, 1024 );
  fatal_directive_check_status_only( status, EINVAL, "NULL attr" );

  puts( "Init - pthread_attr_setstack- EINVAL (destroyed attr)" );
  status = pthread_attr_setstack( &destroyed_attr, &stackaddr, 1024 );
  fatal_directive_check_status_only( status, EINVAL, "NULL attr" );

  puts( "Init - pthread_attr_setstack- SUCCESSFUL (< min stack)" );
  status = pthread_attr_setstack( &attr, stackaddr, 0 );
  posix_service_failed( status, "OK");

  puts( "Init - pthread_attr_setstack- SUCCESSFUL (big stack)" );
  status = pthread_attr_setstack( &attr, stackaddr, STACK_MINIMUM_SIZE * 2 );
  posix_service_failed( status, "OK");
#endif

#if HAVE_DECL_PTHREAD_ATTR_GETSTACK
  puts( "Init - pthread_attr_getstack- EINVAL (NULL attr)" );
  status = pthread_attr_getstack( NULL, &stackaddr, &stacksize );
  fatal_directive_check_status_only( status, EINVAL, "NULL attr" );

  puts( "Init - pthread_attr_getstack- EINVAL (destroyed attr)" );
  status = pthread_attr_getstack( &destroyed_attr, &stackaddr, &stacksize );
  fatal_directive_check_status_only( status, EINVAL, "&destroyed attr" );

  puts( "Init - pthread_attr_getstack- EINVAL (NULL stack)" );
  status = pthread_attr_getstack( &attr, NULL, &stacksize );
  fatal_directive_check_status_only( status, EINVAL, "&NULL stack" );

  puts( "Init - pthread_attr_getstack- EINVAL (NULL stacksize)" );
  status = pthread_attr_getstack( &attr, &stackaddr, NULL );
  fatal_directive_check_status_only( status, EINVAL, "&NULL size" );

  puts( "Init - pthread_attr_getstack- SUCCESSFUL" );
  status = pthread_attr_getstack( &attr, &stackaddr, &stacksize );
  posix_service_failed( status, "pthread_attr_getstack");
#endif

  /* exercise get and set detach state */
  empty_line();

#if HAVE_DECL_PTHREAD_ATTR_SETGUARDSIZE
  puts( "Init - pthread_attr_setguardsize - EINVAL (NULL attr)" );
  status = pthread_attr_setguardsize( NULL, 0 );
  fatal_directive_check_status_only( status, EINVAL, "NULL attr" );

  puts( "Init - pthread_attr_setguardsize - EINVAL (not initialized attr)" );
  status = pthread_attr_setguardsize( &destroyed_attr, 0 );
  fatal_directive_check_status_only( status, EINVAL, "not initialized attr" );

  puts( "Init - pthread_attr_setguardsize - SUCCESSFUL (low guardsize)" );
  status = pthread_attr_setguardsize( &attr, 0 );
  posix_service_failed( status, "pthread_attr_setguardsize");

  puts( "Init - pthread_attr_setguardsize - SUCCESSFUL (high guardsize)" );
  status = pthread_attr_setguardsize( &attr, STACK_MINIMUM_SIZE * 2 );
  posix_service_failed( status, "");
#endif

#if HAVE_DECL_PTHREAD_ATTR_GETGUARDSIZE
  puts( "Init - pthread_attr_getguardsize - EINVAL (NULL attr)" );
  status = pthread_attr_getguardsize( NULL, &guardsize );
  fatal_directive_check_status_only( status, EINVAL, "NULL attr" );

  puts( "Init - pthread_attr_getguardsize - EINVAL (NULL guardsize)" );
  status = pthread_attr_getguardsize( &attr, NULL );
  fatal_directive_check_status_only( status, EINVAL, "NULL guardsize" );

  puts( "Init - pthread_attr_getguardsize - EINVAL (not initialized attr)" );
  status = pthread_attr_getguardsize( &destroyed_attr, &guardsize );
  fatal_directive_check_status_only( status, EINVAL, "not initialized attr" );

  puts( "Init - pthread_attr_getguardsize - SUCCESSFUL" );
  status = pthread_attr_getguardsize( &attr, &guardsize );
  posix_service_failed( status, "pthread_attr_getguardsize");
#endif

  /* exercise get and set detach state */
  empty_line();

  puts( "Init - pthread_attr_setdetachstate - EINVAL (NULL attr)" );
  status = pthread_attr_setdetachstate( NULL, PTHREAD_CREATE_DETACHED );
  fatal_directive_check_status_only( status, EINVAL, "NULL attr" );

  puts( "Init - pthread_attr_setdetachstate - EINVAL (not initialized attr)" );
  status =
     pthread_attr_setdetachstate( &destroyed_attr, PTHREAD_CREATE_JOINABLE );
  fatal_directive_check_status_only( status, EINVAL, "not initialized att" );

  puts( "Init - pthread_attr_setdetachstate - EINVAL (invalid detachstate)" );
  status = pthread_attr_setdetachstate( &attr, -1 );
  fatal_directive_check_status_only( status, EINVAL, "invalid detachstate" );

  puts( "Init - pthread_attr_setdetachstate - SUCCESSFUL" );
  status = pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_JOINABLE );
  posix_service_failed( status, "pthread_attr_setdetachstate");

  puts( "Init - pthread_attr_getdetachstate - EINVAL (NULL attr)" );
  status = pthread_attr_getdetachstate( NULL, &detachstate );
  fatal_directive_check_status_only( status, EINVAL, "NULL attr" );

  puts( "Init - pthread_attr_getdetachstate - EINVAL (NULL detatchstate)" );
  status = pthread_attr_getdetachstate( &attr, NULL );
  fatal_directive_check_status_only( status, EINVAL, "NULL detatchstate" );

  puts( "Init - pthread_attr_getdetachstate - EINVAL (not initialized attr)" );
  status = pthread_attr_getdetachstate( &destroyed_attr, &detachstate );
  fatal_directive_check_status_only( status, EINVAL, "not initialized attr" );

  puts( "Init - pthread_attr_getdetachstate - SUCCESSFUL" );
  status = pthread_attr_getdetachstate( &attr, &detachstate );
  posix_service_failed( status, "pthread_attr_getdetachstate");
  printf( "Init - current detach state attribute = %d\n", detachstate );

  /* exercise get and set scheduling parameters */

  empty_line();

  puts( "Init - pthread_attr_getschedparam - SUCCESSFUL" );
  status = pthread_attr_getschedparam( &attr, &schedparam );
  posix_service_failed( status, "pthread_attr_getschedparam");

  print_schedparam( "Init - ", &schedparam );

  puts( "Init - pthread_attr_setschedparam - EINVAL (NULL attr)" );
  status = pthread_attr_setschedparam( NULL, &schedparam );
  fatal_directive_check_status_only( status, EINVAL, "NULL attr" );

  puts( "Init - pthread_attr_setschedparam - EINVAL (not initialized attr)" );
  status = pthread_attr_setschedparam( &destroyed_attr, &schedparam );
  fatal_directive_check_status_only( status, EINVAL, "not initialized attr" );

  puts( "Init - pthread_attr_setschedparam - EINVAL (NULL schedparam)" );
  status = pthread_attr_setschedparam( &attr, NULL );
  fatal_directive_check_status_only( status, EINVAL, "NULL schedparam" );

  puts( "Init - pthread_attr_setschedparam - SUCCESSFUL" );
  status = pthread_attr_setschedparam( &attr, &schedparam );
  posix_service_failed( status, "pthread_attr_setschedparam");

  puts( "Init - pthread_attr_getschedparam - EINVAL (NULL attr)" );
  status = pthread_attr_getschedparam( NULL, &schedparam );
  fatal_directive_check_status_only( status, EINVAL, "pthread_attr_getschedparam" );

  puts( "Init - pthread_attr_getschedparam - EINVAL (not initialized attr)" );
  status = pthread_attr_getschedparam( &destroyed_attr, &schedparam );
  fatal_directive_check_status_only( status, EINVAL, "not initialized attr" );

  puts( "Init - pthread_attr_getschedparam - EINVAL (NULL schedparam)" );
  status = pthread_attr_getschedparam( &attr, NULL );
  fatal_directive_check_status_only( status, EINVAL, "NULL schedparam" );

  /* exercise pthread_getschedparam */

  empty_line();

  puts( "Init - pthread_getschedparam - EINVAL (NULL policy)" );
  status = pthread_getschedparam( pthread_self(), NULL, &schedparam );
  fatal_directive_check_status_only( status, EINVAL, "NULL policy" );

  puts( "Init - pthread_getschedparam - EINVAL (NULL schedparam)" );
  status = pthread_getschedparam( pthread_self(), &schedpolicy, NULL );
  fatal_directive_check_status_only( status, EINVAL, "NULL schedparam" );

  puts( "Init - pthread_getschedparam - ESRCH (bad thread)" );
  status = pthread_getschedparam( (pthread_t) -1, &schedpolicy, &schedparam );
  fatal_directive_check_status_only( status, ESRCH, "bad thread" );

  puts( "Init - pthread_getschedparam - SUCCESSFUL" );
  status = pthread_getschedparam( pthread_self(), &schedpolicy, &schedparam );
  posix_service_failed( status, "pthread_getschedparam");

  printf( "Init - policy = %d\n", schedpolicy );

  print_schedparam( "Init - ", &schedparam );

  /* exercise pthread_setschedparam */

  empty_line();

  puts( "Init - pthread_setschedparam - EINVAL (NULL schedparam)" );
  status = pthread_setschedparam( pthread_self(), SCHED_OTHER, NULL );
  fatal_directive_check_status_only( status, EINVAL, "NULL schedparam" );

  schedparam.sched_priority = -1;

  puts( "Init - pthread_setschedparam - EINVAL (invalid priority)" );
  status = pthread_setschedparam( pthread_self(), SCHED_OTHER, NULL );
  fatal_directive_check_status_only( status, EINVAL, "invalid priority" );

  /* reset sched_param */
  status = pthread_getschedparam( pthread_self(), &schedpolicy, &schedparam );
  posix_service_failed( status, "pthread_getschedparam");

  puts( "Init - pthread_setschedparam - EINVAL (invalid policy)" );
  status = pthread_setschedparam( pthread_self(), -1, &schedparam );
  fatal_directive_check_status_only( status, EINVAL, "invalid policy" );

  puts( "Init - pthread_setschedparam - ESRCH (invalid thread)" );
  status = pthread_setschedparam( (pthread_t) -1, SCHED_OTHER, &schedparam );
  fatal_directive_check_status_only( status, ESRCH, "invalid thread" );

  /* now get sporadic server errors */

  schedparam.sched_ss_repl_period.tv_sec = 0;
  schedparam.sched_ss_repl_period.tv_nsec = 0;
  schedparam.sched_ss_init_budget.tv_sec = 1;
  schedparam.sched_ss_init_budget.tv_nsec = 1;

  puts( "Init - pthread_setschedparam - EINVAL (replenish == 0)" );
  status = pthread_setschedparam( pthread_self(), SCHED_SPORADIC, &schedparam );
  fatal_directive_check_status_only( status, EINVAL, "replenish == 0" );

  schedparam.sched_ss_repl_period.tv_sec = 1;
  schedparam.sched_ss_repl_period.tv_nsec = 1;
  schedparam.sched_ss_init_budget.tv_sec = 0;
  schedparam.sched_ss_init_budget.tv_nsec = 0;

  puts( "Init - pthread_setschedparam - EINVAL (budget == 0)" );
  status = pthread_setschedparam( pthread_self(), SCHED_SPORADIC, &schedparam );
  fatal_directive_check_status_only( status, EINVAL, "budget == 0" );

  schedparam.sched_ss_repl_period.tv_sec = 1;
  schedparam.sched_ss_repl_period.tv_nsec = 0;
  schedparam.sched_ss_init_budget.tv_sec = 1;
  schedparam.sched_ss_init_budget.tv_nsec = 1;

  puts( "Init - pthread_setschedparam - EINVAL (replenish < budget)" );
  status = pthread_setschedparam( pthread_self(), SCHED_SPORADIC, &schedparam );
  fatal_directive_check_status_only( status, EINVAL, "replenish < budget" );

  schedparam.sched_ss_repl_period.tv_sec = 2;
  schedparam.sched_ss_repl_period.tv_nsec = 0;
  schedparam.sched_ss_init_budget.tv_sec = 1;
  schedparam.sched_ss_init_budget.tv_nsec = 0;
  schedparam.sched_ss_low_priority = -1;

  puts( "Init - pthread_setschedparam - EINVAL (invalid priority)" );
  status = pthread_setschedparam( pthread_self(), SCHED_SPORADIC, &schedparam );
  fatal_directive_check_status_only( status, EINVAL, "invalid priority" );

  /*
   *  Create a sporadic thread that doesn't need it's priority
   *  boosted
   */
  empty_line();

  puts( "Init - pthread_attr_init - SUCCESSFUL" );
  status = pthread_attr_init( &attr );
  posix_service_failed( status, "pthread_attr_init" );

  puts( "Init - pthread_attr_setinheritsched - EXPLICIT - SUCCESSFUL" );
  status = pthread_attr_setinheritsched( &attr, PTHREAD_EXPLICIT_SCHED );
  rtems_test_assert( !status );

  schedparam.sched_ss_repl_period.tv_sec = 3;
  schedparam.sched_ss_repl_period.tv_nsec = 3;
  schedparam.sched_ss_init_budget.tv_sec = 1;
  schedparam.sched_ss_init_budget.tv_nsec = 1;
  schedparam.sched_priority = sched_get_priority_max( SCHED_FIFO );
  schedparam.sched_ss_low_priority = sched_get_priority_max( SCHED_FIFO ) - 6;

  puts( "Init - pthread_attr_setschedpolicy - SUCCESSFUL" );
  status = pthread_attr_setschedpolicy( &attr, SCHED_SPORADIC );
  posix_service_failed( status, "pthread_attr_setschedparam");
  puts( "Init - pthread_attr_setschedparam - SUCCESSFUL" );
  status = pthread_attr_setschedparam( &attr, &schedparam );
  posix_service_failed( status, "pthread_attr_setschedparam");

  status = pthread_create( &Task2_id, &attr, Task_2, NULL );
  rtems_test_assert( !status );

  status = pthread_join( Task2_id, NULL );
  posix_service_failed( status, " pthread_join");

  TEST_END();
  rtems_test_exit( 0 );

  return NULL; /* just so the compiler thinks we returned something */
}
Example #16
0
int main(int argc, char **argv)
{
    int ret;
    sigset_t mask;
    pthread_attr_t attr;
    void *retval;
    union sigval value;
    
    CYG_TEST_INIT();

    // Make a full signal set
    sigfillset( &mask );

    
    // Install signal handlers
    {
        struct sigaction sa;

        sa.sa_handler = sigusr2;
        sa.sa_mask = mask;
        sa.sa_flags = 0;

        ret = sigaction( SIGUSR2, &sa, NULL );

        CYG_TEST_CHECK( ret == 0 , "sigaction returned error");
    }

    {
        struct sigaction sa;

        sa.sa_handler = sigalrm;
        sa.sa_mask = mask;
        sa.sa_flags = 0;

        ret = sigaction( SIGALRM, &sa, NULL );

        CYG_TEST_CHECK( ret == 0 , "sigaction returned error");
    }
    
    
    // Mask all signals
    pthread_sigmask( SIG_SETMASK, &mask, NULL );
    
    sem_init( &sem, 0, 0 );
    
    // Create test thread
    pthread_attr_init( &attr );

    pthread_attr_setstackaddr( &attr, (void *)&thread_stack[sizeof(thread_stack)] );
    pthread_attr_setstacksize( &attr, sizeof(thread_stack) );

    pthread_create( &thread1,
                    &attr,
                    pthread_entry1,
                    (void *)0x12345678);

    // Wait for other thread to get started
    CYG_TEST_INFO( "Main: calling sem_wait()");
    sem_wait( &sem );

    value.sival_int = 0;

    // send a signal to the other thread
    CYG_TEST_INFO( "Main: calling sigqueue(SIGUSR1)");
    sigqueue( 0, SIGUSR1, value );

    // Send the signal via kill
    CYG_TEST_INFO( "Main: calling kill(0, SIGUSR2)");
    kill( 0, SIGUSR2 );

    // Wait for thread1 to call pause()
    CYG_TEST_INFO( "Main: calling sleep(1)");
    sleep(1);

    // And again
    CYG_TEST_INFO( "Main: calling kill(0, SIGUSR2)");
    kill( 0, SIGUSR2 );

    // Set up an alarm for 1 second hence
    CYG_TEST_INFO( "Main: calling alarm(1)");
    alarm(1);
    
    // Wait for alarm signal to be delivered to thread1
    CYG_TEST_INFO( "Main: calling sleep(2)");
    sleep(2);
    
    // Now join with thread1
    CYG_TEST_INFO( "Main: calling pthread_join()");
    pthread_join( thread1, &retval );

    CYG_TEST_CHECK( sigusr2_called == 2, "SIGUSR2 signal handler not called twice" );

    CYG_TEST_CHECK( sigalrm_called == 1, "SIGALRM signal handler not called" );    
    
    // check retval
    
    if( (long)retval == 0x12345678+SIGUSR1 )
        CYG_TEST_PASS_FINISH( "signal1" );
    else
        CYG_TEST_FAIL_FINISH( "signal1" );
}
Example #17
0
RUBY_GLOBAL_SETUP
#endif

int main()
{
#ifdef DEMONSTRATE_STATIC
    ruby_coroutine_stack_size = sizeof(ruby_coroutine_stack);
#endif

#ifdef DEMONSTRATE_DYNAMIC
    /* allocate the coroutine stack */
    ruby_coroutine_stack = malloc(ruby_coroutine_stack_size);
    if (!ruby_coroutine_stack)
    {
        fprintf(stderr, "Could not allocate %lu bytes!\n", ruby_coroutine_stack_size);
        return 1;
    }
#endif

#ifdef DEMONSTRATE_PCL
    /* create coroutine to house Ruby */
    ruby_coroutine = co_create(ruby_coroutine_body, NULL,
            ruby_coroutine_stack, ruby_coroutine_stack_size);
#endif

#ifdef DEMONSTRATE_PTHREAD
    /* initialize the relay mechanism */
    pthread_mutex_init(&ruby_coroutine_lock, NULL);
    pthread_mutex_lock(&ruby_coroutine_lock);
    pthread_mutex_init(&main_coroutine_lock, NULL);
    pthread_mutex_lock(&main_coroutine_lock);

    /* create pthread to house Ruby */
    pthread_attr_t attr;
    pthread_attr_init(&attr);

#ifdef HAVE_PTHREAD_ATTR_SETSTACK
    pthread_attr_setstack(&attr, ruby_coroutine_stack, ruby_coroutine_stack_size);
#else
    pthread_attr_setstackaddr(&attr, ruby_coroutine_stack);
    pthread_attr_setstacksize(&attr, ruby_coroutine_stack_size);
#endif

    int error = pthread_create(&ruby_coroutine, &attr, &ruby_coroutine_body, NULL);
    if (error) {
        printf("ERROR: pthread_create() returned %d\n", error);
        return 1;
    }
#endif

#ifdef DEMONSTRATE_UCONTEXT
    /* create System V context to house Ruby */
    ruby_coroutine.uc_link          = &main_coroutine;
    ruby_coroutine.uc_stack.ss_sp   = ruby_coroutine_stack;
    ruby_coroutine.uc_stack.ss_size = ruby_coroutine_stack_size;
    getcontext(&ruby_coroutine);
    makecontext(&ruby_coroutine, (void (*)(void)) ruby_coroutine_body, 0);
#endif

    /* relay control to Ruby until it is finished */
    ruby_coroutine_finished = false;
    while (!ruby_coroutine_finished)
    {
        relay_from_main_to_ruby();
    }

    printf("Main: Goodbye!\n");
    return 0;
}
Example #18
0
int main(int argc, char **argv)
{
    int ret;
    sigset_t mask;
    pthread_attr_t attr;
    void *retval;
    
    CYG_TEST_INIT();

    // Make a full signal set
    sigfillset( &mask );

    
    // Install signal handlers
    {
        struct sigaction sa;

        sa.sa_sigaction = sigusr1;
        sa.sa_mask = mask;
        sa.sa_flags = SA_SIGINFO;

        ret = sigaction( SIGUSR1, &sa, NULL );

        CYG_TEST_CHECK( ret == 0 , "sigaction returned error");
    }
    {
        struct sigaction sa;

        sa.sa_sigaction = sigusr2;
        sa.sa_mask = mask;
        sa.sa_flags = SA_SIGINFO;

        ret = sigaction( SIGUSR2, &sa, NULL );

        CYG_TEST_CHECK( ret == 0 , "sigaction returned error");
    }


    // Create the timers

    {
        struct sigevent sev;
        struct itimerspec value;
        
        sev.sigev_notify                = SIGEV_SIGNAL;
        sev.sigev_signo                 = SIGUSR1;
        sev.sigev_value.sival_int       = 0xABCDEF01;

        value.it_value.tv_sec           = 1;
        value.it_value.tv_nsec          = 0;
        value.it_interval.tv_sec        = 0;
        value.it_interval.tv_nsec       = 0;
        
        ret = timer_create( CLOCK_REALTIME, &sev, &timer1 );

        CYG_TEST_CHECK( ret == 0 , "timer_create returned error");

        ret = timer_settime( timer1, 0, &value, NULL );

        CYG_TEST_CHECK( ret == 0 , "timer_settime returned error");
    }

    {
        struct sigevent sev;
        struct itimerspec value;
        
        sev.sigev_notify                = SIGEV_SIGNAL;
        sev.sigev_signo                 = SIGUSR2;
        sev.sigev_value.sival_int       = 0xABCDEF02;

        value.it_value.tv_sec           = 0;
        value.it_value.tv_nsec          = 500000000;
        value.it_interval.tv_sec        = 0;
        value.it_interval.tv_nsec       = 250000000;
        
        ret = timer_create( CLOCK_REALTIME, &sev, &timer2 );

        CYG_TEST_CHECK( ret == 0 , "timer_create returned error");

        ret = timer_settime( timer2, 0, &value, NULL );

        CYG_TEST_CHECK( ret == 0 , "timer_settime returned error");
    }

    
    // Mask all signals
    pthread_sigmask( SIG_SETMASK, &mask, NULL );
    
    sem_init( &sem, 0, 0 );
    
    // Create test threads

    {
        pthread_attr_init( &attr );

        pthread_attr_setstackaddr( &attr, (void *)&thread1_stack[sizeof(thread1_stack)] );
        pthread_attr_setstacksize( &attr, sizeof(thread1_stack) );

        pthread_create( &thread1,
                        &attr,
                        pthread_entry1,
                        (void *)0x12345671);
    }

    {
        pthread_attr_init( &attr );

        pthread_attr_setstackaddr( &attr, (void *)&thread2_stack[sizeof(thread2_stack)] );
        pthread_attr_setstacksize( &attr, sizeof(thread2_stack) );

        pthread_create( &thread2,
                        &attr,
                        pthread_entry2,
                        (void *)0x12345672);
    }
    
    // Wait for other thread to get started
    CYG_TEST_INFO( "Main: calling sem_wait()");
    sem_wait( &sem );
    CYG_TEST_INFO( "Main: calling sem_wait() again");    
    sem_wait( &sem );

    // Now join with thread1
    CYG_TEST_INFO( "Main: calling pthread_join(thread1)");
    pthread_join( thread1, &retval );

    CYG_TEST_CHECK( retval == (void *)0x12345671, "Thread 1 retval wrong");
    
    // And thread 2
    CYG_TEST_INFO( "Main: calling pthread_join(thread2)");
    pthread_join( thread2, &retval );

    // now delete the timers
    CYG_TEST_INFO( "Main: calling timer_delete(timer1)");    
    ret = timer_delete( timer1 );

    CYG_TEST_CHECK( ret == 0 , "timer_delete(timer1) returned error");

    CYG_TEST_INFO( "Main: calling timer_delete(timer2)");    
    ret = timer_delete( timer2 );

    CYG_TEST_CHECK( ret == 0 , "timer_delete(timer2) returned error");

    
    CYG_TEST_CHECK( retval == (void *)0x12345672, "Thread 2 retval wrong");
    
    CYG_TEST_CHECK( sigusr1_called == 1, "SIGUSR1 signal handler not called once" );
    CYG_TEST_CHECK( sigusr2_called == 6, "SIGUSR2 signal handler not called six times" );

    CYG_TEST_PASS_FINISH( "sigsetjmp" );
}
Example #19
0
int main( int argc, char **argv )
{
    void *retval;
    pthread_attr_t attr;
    struct sched_param schedparam;

    CYG_TEST_INIT();

    sa.sin_family = AF_INET;
    sa.sin_len = sizeof(sa);
    inet_aton("127.0.0.1", &sa.sin_addr);
    sa.sin_port = htons(1234);
    init_all_network_interfaces();
    
    // Create test threads

    {
        pthread_attr_init( &attr );

        schedparam.sched_priority = 5;
        pthread_attr_setinheritsched( &attr, PTHREAD_EXPLICIT_SCHED );
        pthread_attr_setschedpolicy( &attr, SCHED_RR );
        pthread_attr_setschedparam( &attr, &schedparam );
        pthread_attr_setstackaddr( &attr, (void *)&thread1_stack[sizeof(thread1_stack)] );
        pthread_attr_setstacksize( &attr, sizeof(thread1_stack) );

        pthread_create( &thread1,
                        &attr,
                        pthread_entry1,
                        (void *)0x12345671);
    }

    {
        pthread_attr_init( &attr );

        schedparam.sched_priority = 10;
        pthread_attr_setinheritsched( &attr, PTHREAD_EXPLICIT_SCHED );
        pthread_attr_setschedpolicy( &attr, SCHED_RR );
        pthread_attr_setschedparam( &attr, &schedparam );
        pthread_attr_setstackaddr( &attr, (void *)&thread2_stack[sizeof(thread2_stack)] );
        pthread_attr_setstacksize( &attr, sizeof(thread2_stack) );

        pthread_create( &thread2,
                        &attr,
                        pthread_entry2,
                        (void *)0x12345672);
    }
    
    // Now join with thread1
    CYG_TEST_INFO( "Main: calling pthread_join(thread1)");
    pthread_join( thread1, &retval );

    // And thread 2
    CYG_TEST_INFO( "Main: calling pthread_join(thread2)");
    pthread_join( thread2, &retval );

    diag_printf("INFO: pselect returns: %d\n", pselect_wakeups );
    diag_printf("INFO: pselect EINTR returns: %d\n", pselect_eintr );
    diag_printf("INFO: SIGUSR1 sent: %d\n", sigusr1_sent );
    diag_printf("INFO: SIGUSR1 delivered: %d\n", sigusr1_calls );
    
    CYG_TEST_CHECK( sigusr1_sent == sigusr1_calls, "SIGUSR1 calls != delivered");
    CYG_TEST_CHECK( sigusr1_sent == pselect_eintr, "SIGUSR1 calls != pselect EINTR wakeups");
    
    CYG_TEST_PASS_FINISH("pselect");
}
Example #20
0
int32_t system_thread_create(system_thread_t *thread, const system_thread_attr_t *attr,
        void*(*start_routine)(void *), void *param)
{
    int32_t status = ERR_SUCCESS;
    int32_t sched_policy;
    int32_t priority_max, priority_min;
    struct sched_param sched_param;
    pthread_attr_t pthread_attr;

    if (attr) {

        status = pthread_attr_init(&pthread_attr);

        /* Set priority */
        if (attr->priority && !status) {

            status = pthread_attr_getschedpolicy(&pthread_attr, &sched_policy);

            if (!status) {
                switch (attr->priority) {
                case MaxThreadPriority:
                    sched_param.sched_priority = sched_get_priority_max(
                            sched_policy);
                    break;
                case HighThreadPriority:
                    priority_max = sched_get_priority_max(sched_policy);
                    priority_min = sched_get_priority_min(sched_policy);
                    sched_param.sched_priority = (priority_min
                            + 3 * priority_max) / 4;
                    break;
                case NormalThreadPriority:
                    priority_max = sched_get_priority_max(sched_policy);
                    priority_min = sched_get_priority_min(sched_policy);
                    sched_param.sched_priority = (priority_min + priority_max)
                            / 2;
                    break;
                case LowThreadPriority:
                    priority_max = sched_get_priority_max(sched_policy);
                    priority_min = sched_get_priority_min(sched_policy);
                    sched_param.sched_priority = (priority_max
                            + 3 * priority_min) / 4;
                    break;
                case MinThreadPriority:
                    sched_param.sched_priority = sched_get_priority_min(
                            sched_policy);
                    break;
                default:
                    status = ERR_FAILURE;
                    break;
                }
            }

            if (!status)
                status = pthread_attr_setschedparam(&pthread_attr,
                        &sched_param);
        }

        /* Set stack address */
        if (attr->stack_loc && !status)
            status = pthread_attr_setstackaddr(&pthread_attr, attr->stack_loc);

        /* Set stack size */
        if (attr->stack_size && !status)
            status = pthread_attr_setstacksize(&pthread_attr, attr->stack_size);

        if (!status)
            status = pthread_create((pthread_t *) thread, &pthread_attr,
                    start_routine, param);

        pthread_attr_destroy(&pthread_attr);
    } else {
        status = pthread_create((pthread_t *) thread, NULL, start_routine,
                param);
    }

    if (status)
        return ERR_FAILURE;

    return ERR_SUCCESS;
}
Example #21
0
static int my_pthread_attr_setstackaddr(pthread_attr_t *__attr, void *stack_addr)
{
    pthread_attr_t *realattr = (pthread_attr_t *) *(unsigned int *) __attr;
    return pthread_attr_setstackaddr(realattr, stack_addr);
}