Beispiel #1
0
/*************************************************
  * Function:		Pthread_attr_getdetachstate()
  * Description:    获取线程分离属性包裹函数 
  * Input:          *attr---线程属性结构 
  * Output:         *detachstate---线程分离属性 
  * Return:         0/errno 
*************************************************/
int Pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate)
{
	int rval;

	if(attr==AII_NULL || detachstate==AII_NULL)
	{
		return -1;	
	}

	if((rval = pthread_attr_getdetachstate(attr, detachstate)) != 0)
	{
		debug_info(DEBUG_LEVEL_4,"Pthread_attr_getdetacgstate() failed!\n");
	}

	return rval;
}
Beispiel #2
0
bool Thread::isDetached(void)
{
        if(!priv)
                return false;

#ifdef  WIN32
	// win32 doesn't support detached threads directly
	return priv->_detached;
#else
        int state;

        pthread_attr_getdetachstate(&priv->_attr, &state);
        if(state == PTHREAD_CREATE_DETACHED)
                return true;
        return false;
#endif
}
static __always_inline
int pthread_create_intercept(pthread_t* thread, const pthread_attr_t* attr,
                             void* (*start)(void*), void* arg)
{
   int    ret;
   OrigFn fn;
   DrdSema wrapper_started;
   DrdPosixThreadArgs thread_args;

   VALGRIND_GET_ORIG_FN(fn);

   DRD_(sema_init)(&wrapper_started);
   thread_args.start           = start;
   thread_args.arg             = arg;
   thread_args.wrapper_started = &wrapper_started;
   /*
    * Find out whether the thread will be started as a joinable thread
    * or as a detached thread. If no thread attributes have been specified,
    * this means that the new thread will be started as a joinable thread.
    */
   thread_args.detachstate = PTHREAD_CREATE_JOINABLE;
   if (attr)
   {
      if (pthread_attr_getdetachstate(attr, &thread_args.detachstate) != 0)
         assert(0);
   }
   assert(thread_args.detachstate == PTHREAD_CREATE_JOINABLE
          || thread_args.detachstate == PTHREAD_CREATE_DETACHED);

   DRD_(entering_pthread_create)();
   CALL_FN_W_WWWW(ret, fn, thread, attr, DRD_(thread_wrapper), &thread_args);
   DRD_(left_pthread_create)();

   if (ret == 0) {
      /* Wait until the thread wrapper started. */
      DRD_(sema_down)(&wrapper_started);
   }

   DRD_(sema_destroy)(&wrapper_started);

   VALGRIND_DO_CLIENT_REQUEST_STMT(VG_USERREQ__DRD_START_NEW_SEGMENT,
                                   pthread_self(), 0, 0, 0, 0);

   return ret;
}
TEST(pthread_attr_init, defaults) {
  pthread_attr_t attr;
  ASSERT_EQ(0, pthread_attr_init(&attr));

  {
    int detachstate;
    ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &detachstate));
    ASSERT_EQ(PTHREAD_CREATE_JOINABLE, detachstate);
  }

  {
    size_t stacksize;
    ASSERT_EQ(0, pthread_attr_getstacksize(&attr, &stacksize));
    ASSERT_LE(PTHREAD_STACK_MIN, stacksize);
  }

  ASSERT_EQ(0, pthread_attr_destroy(&attr));
}
Beispiel #5
0
/* Cygwin-pthreads calls CreateThread internally, but it's not
 * easily interceptible by us..
 *   so intercept pthread_create instead
 */
int
GC_pthread_create(pthread_t *new_thread,
		  const pthread_attr_t *attr,
                  void *(*start_routine)(void *), void *arg) {
    int result;
    struct start_info * si;

    if (!parallel_initialized) GC_init_parallel();
    		/* make sure GC is initialized (i.e. main thread is attached) */
    if (GC_win32_dll_threads) {
      return pthread_create(new_thread, attr, start_routine, arg);
    }
    
    /* This is otherwise saved only in an area mmapped by the thread */
    /* library, which isn't visible to the collector.		 */
    si = GC_malloc_uncollectable(sizeof(struct start_info)); 
    if (0 == si) return(EAGAIN);

    si -> start_routine = start_routine;
    si -> arg = arg;
    if (attr != 0 &&
        pthread_attr_getdetachstate(attr, &si->detached)
	== PTHREAD_CREATE_DETACHED) {
      si->detached = TRUE;
    }

#   if DEBUG_CYGWIN_THREADS
      GC_printf("About to create a thread from 0x%x(0x%x)\n",
		(int)pthread_self(), GetCurrentThreadId);
#   endif
#   if DEBUG_WIN32_PTHREADS
      GC_printf("About to create a thread from 0x%x(0x%x)\n",
		(int)(pthread_self()).p, GetCurrentThreadId());
#   endif
    GC_need_to_lock = TRUE;
    result = pthread_create(new_thread, attr, GC_pthread_start, si); 

    if (result) { /* failure */
      	GC_free(si);
    } 

    return(result);
}
void* startRoutine(void *data) {
      int state=0;

      pthread_t tid = pthread_self();
      displayThreadId(tid);

      printf("\n Running startRoutine()...: %d ************", start++); 
      //sleep(SLEEPTIME_SMALL);

      pthread_attr_t myattr;
      pthread_getattr_np(pthread_self(), &myattr);
      pthread_attr_getdetachstate(&myattr, &state);
      
      if(state == PTHREAD_CREATE_JOINABLE) {
        printf("\n Joinable thread ! state=%d", state);
      }

      printf("\n Leaving startRoutine()... : %d************\n",end++);
     return 0; 
}
Beispiel #7
0
extern "C" int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
    void *(*start_routine)(void*), void *arg)
{
  /* Reap Existing Threads */
  dmtcp::ThreadInfo::reapThreads();
  int retval;

  /* Here I've split apart WRAPPER_HEADER_RAW because we need to create the
   * reaper thread even if the current mode is SYNC_IS_NOOP. */
  void *return_addr = GET_RETURN_ADDRESS();
  if (!dmtcp_is_running_state() ||
      !validAddress(return_addr) || isProcessGDB()) {
    retval =  _real_pthread_create(thread, attr, start_routine, arg);
  } else if (SYNC_IS_NOOP) {
    struct create_arg *createArg =
      (struct create_arg*) JALLOC_HELPER_MALLOC(sizeof(*createArg));
    createArg->fn = start_routine;
    createArg->thread_arg = arg;
    createArg->userStack = (void*) -1; // Make sure it is non-NULL
    if (attr != NULL) {
      createArg->attr = *attr;
      pthread_attr_getdetachstate(attr, &createArg->userDetachState);
    } else {
      createArg->userDetachState = PTHREAD_CREATE_JOINABLE;
    }
    dmtcp::ThreadInfo::prePthreadCreate();
    retval = _real_pthread_create(thread, attr, start_wrapper, createArg);
    if (retval != 0) {
      dmtcp::ThreadInfo::postPthreadCreate();
    }
  } else {
    retval = internal_pthread_create(thread, attr, start_routine, arg);
  }
  if (!pthread_cancel_initialized) {
    initialize_pthread_cancel();
  }
  return retval;
}
Beispiel #8
0
static int print_thread_attr(pthread_attr_t *attr)
{
    if (attr == NULL)
    {
        return THREAD_ATTR_FAILED;
    }

    int detach_state = 0;
    int ret = 0;
    char *stack_addr = NULL;
    size_t stack_size = 0;
    size_t guard_size = 0;

    ret = pthread_attr_getdetachstate(attr, &detach_state);
    if (detach_state == PTHREAD_CREATE_JOINABLE)
    {
        printf("Thread detach state: JOINABLE\n");
    }
    else if (detach_state == PTHREAD_CREATE_DETACHED)
    {
        printf("Thread detach state: DETACHED\n");
    }

    ret = pthread_attr_getstack(attr, (void**)&stack_addr, &stack_size);
    printf("stack_addr:%p, stack_size:%zd\n", stack_addr, stack_size);

    ret = pthread_attr_getstacksize(attr, &stack_size);
    printf("stack size:%#x\n", stack_size);

    ret = pthread_attr_getguardsize(attr, &guard_size);
    printf("guard size:%#x\n", guard_size);


    ret = pthread_getconcurrency();
    printf("concurrency is %d\n", ret);
    
    return THREAD_ATTR_OK;
}
Beispiel #9
0
int main()
{
    pthread_attr_t new_attr;
    int detach_state;

    /* Initialize attribute */
    if (pthread_attr_init(&new_attr) != 0)
    {
        perror("Cannot initialize attribute object\n");
        return PTS_UNRESOLVED;
    }

    /* Set the attribute object to PTHREAD_CREATE_JOINABLE. */
    if (pthread_attr_setdetachstate(&new_attr, PTHREAD_CREATE_JOINABLE) != 0)
    {
        perror("Error in pthread_attr_setdetachstate()\n");
        return PTS_UNRESOLVED;
    }

    /* Check to see if the detachstate is truly PTHREAD_CREATE_JOINABLE. */
    if (pthread_attr_getdetachstate(&new_attr, &detach_state) != 0)
    {
        perror("Error in pthread_attr_getdetachstate.\n");
        return PTS_UNRESOLVED;
    }

    if (detach_state == PTHREAD_CREATE_JOINABLE)
    {
        printf("Test PASSED\n");
        return PTS_PASS;
    }
    else
    {
        printf("Test FAILED\n");
        return PTS_FAIL;
    }
}
Beispiel #10
0
int main(int argc, const char * argv[])
{
    
    pthread_t tid1, tid2;
    param p1, p2;
    pthread_attr_t attr;
    int detachstate, scope, sched, algoritmo;
    size_t stacksize;
    
    
    p1.tid = 1;
    strcpy(p1.nombre, "Hilo 1");
    
    p2.tid = 2;
    strcpy(p2.nombre, "Hilo 2");
    
    pthread_key_create(&tsd, NULL);
    
    pthread_attr_init(&attr);
    
    pthread_attr_getdetachstate(&attr, &detachstate);
    pthread_attr_getscope(&attr, &scope);
    pthread_attr_getstacksize(&attr, &stacksize);
    pthread_attr_getschedpolicy(&attr, &algoritmo);
    pthread_attr_getinheritsched(&attr, &sched);
    
    printf("Hilo 1 creado con valores siguientes: \n");
    
    printf("Valores iniciales de atributos: \n");
    printf("DETACHSTATE = %d \n", detachstate);
    printf("SCOPE = %d \n", scope);
    printf("STACKSIZE = %zu bytes\n", stacksize);
    printf("SCHEDPOLICY = %d \n", algoritmo);
    printf("INHERSCHED = %d \n", sched);
    
    pthread_create(&tid1, &attr, hilo, (void *) &p1);
    
    
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
    pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
    pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED);
    
    printf("Hilo 2 creado con valores siguientes: \n");
    
    pthread_attr_getdetachstate(&attr, &detachstate);
    pthread_attr_getscope(&attr, &scope);
    pthread_attr_getstacksize(&attr, &stacksize);
    pthread_attr_getschedpolicy(&attr, &algoritmo);
    pthread_attr_getinheritsched(&attr, &sched);
    
    printf("Valores iniciales de atributos: \n");
    printf("DETACHSTATE = %d \n", detachstate);
    printf("SCOPE = %d \n", scope);
    printf("STACKSIZE = %zu bytes\n", stacksize);
    printf("SCHEDPOLICY = %d \n", algoritmo);
    printf("INHERSCHED = %d \n", sched);
    
    pthread_create(&tid2, &attr, hilo, (void *) &p2);
    
    if (pthread_equal(tid1, tid2)) {
        printf(" Son iguales \n");
    }
    else {
        printf("Son diferentes \n");
    }
    
    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);
    
    pthread_key_delete(tsd);
    
    pthread_attr_destroy(&attr);
    
    pthread_exit(NULL);
    
    return 0;
}
Beispiel #11
0
/* The following function will return
  0 if a thread created with the attribute ta is able to fill the stack up to {minstacksize}.
  1 if the operation failed.
  2 if an error prevented the test to complete

  If newsize is not 0, the stack size in ta will be set to this value once the thread is created.

*/
int test_stack(pthread_attr_t * ta, size_t newsize)
{
	pid_t child, ctrl;
	int status;
	int ret;

	child = fork();		/* We'll test the feature in another process as this test may segfault */

	if (child == -1) {
		output("Failed to fork (%s)\n", strerror(errno));
		return 2;
	}

	if (child != 0) {	/* father */
		/* Just wait for the child and check its return value */
		ctrl = waitpid(child, &status, 0);
		if (ctrl != child) {
			output("Failed to wait for process termination (%s)\n",
			       strerror(errno));
			return 2;
		}

		if (WIFEXITED(status)) {	/* The process exited */
			if (WEXITSTATUS(status) == 0) {
				return 0;
			}	/* We were able to fill the stack */
			if (WEXITSTATUS(status) == PTS_UNRESOLVED) {
				output
				    ("The child process returned unresolved status\n");
				return 2;
			} else {
				output("The child process returned: %i\n",
				       WEXITSTATUS(status));
				return 2;
			}
		} else {
#if VERBOSE > 4
			output("The child process did not return\n");
			if (WIFSIGNALED(status))
				output("It was killed with signal %i\n",
				       WTERMSIG(status));
			else
				output("neither was it killed. (status = %i)\n",
				       status);
#endif
		}

		return 1;
	}

	/* else */
	/* this is the new process */
	{
		pthread_t th;
		void *rc;
		int detach;

		/* Semaphore to force the child to wait */
		ret = sem_init(&semsync[0], 0, 0);
		if (ret == -1) {
			UNRESOLVED(errno, "Unable to init a semaphore");
		}
		/* Semaphore to detect thread ending */
		ret = sem_init(&semsync[1], 0, 0);
		if (ret == -1) {
			UNRESOLVED(errno, "Unable to init a semaphore");
		}

		ret = pthread_create(&th, ta, overflow, NULL);	/* Create a new thread with the same attributes */
		if (ret != 0) {
			UNRESOLVED(ret,
				   "Unable to create a thread in the new process");
		}

		/* If we were asked to perform a change on ta, do it now. */
		if (newsize) {
			ret = pthread_attr_setstacksize(ta, newsize);
			if (ret != 0) {
				UNRESOLVED(ret,
					   "Failed to set the new stack size");
			}
		}

		/* Ok the child can run now */
		do {
			ret = sem_post(&semsync[0]);
		}
		while ((ret == -1) && (errno == EINTR));
		if (ret == -1) {
			UNRESOLVED(errno, "Failed to post the semaphore");
		}

		/* Wait for its termination */
		do {
			ret = sem_wait(&semsync[1]);
		}
		while ((ret == -1) && (errno == EINTR));
		if (ret == -1) {
			UNRESOLVED(errno, "Failed to wait for the semaphore");
		}

		if (ta != NULL) {
			ret = pthread_attr_getdetachstate(ta, &detach);
			if (ret != 0) {
				UNRESOLVED(ret,
					   "Failed to get detach state from the thread attribute");
			}
		} else {
			detach = PTHREAD_CREATE_JOINABLE;	/* default */
		}

		if (detach == PTHREAD_CREATE_JOINABLE) {
			ret = pthread_join(th, &rc);
			if (ret != 0) {
				UNRESOLVED(ret, "Unable to join a thread");
			}
			if (rc != (void *)0) {
				UNRESOLVED((int)(long)rc,
					   "The overflow function returned an unexpected value");
			}
		}

		/* Terminate the child process here */
		exit(0);
	}
}
Beispiel #12
0
static void *
tf (void *arg)
{
  pthread_attr_t a, *ap, a2;
  int err;
  void *result = NULL;

  if (arg == NULL)
    {
      ap = &a2;
      err = pthread_attr_init (ap);
      if (err)
	{
	  error (0, err, "pthread_attr_init failed");
	  return tf;
	}
    }
  else
    ap = (pthread_attr_t *) arg;

  err = pthread_getattr_np (pthread_self (), &a);
  if (err)
    {
      error (0, err, "pthread_getattr_np failed");
      result = tf;
    }

  int detachstate1, detachstate2;
  err = pthread_attr_getdetachstate (&a, &detachstate1);
  if (err)
    {
      error (0, err, "pthread_attr_getdetachstate failed");
      result = tf;
    }
  else
    {
      err = pthread_attr_getdetachstate (ap, &detachstate2);
      if (err)
	{
	  error (0, err, "pthread_attr_getdetachstate failed");
	  result = tf;
	}
      else if (detachstate1 != detachstate2)
	{
	  error (0, 0, "detachstate differs %d != %d",
		 detachstate1, detachstate2);
	  result = tf;
	}
    }

  void *stackaddr;
  size_t stacksize;
  err = pthread_attr_getstack (&a, &stackaddr, &stacksize);
  if (err)
    {
      error (0, err, "pthread_attr_getstack failed");
      result = tf;
    }
  else if ((void *) &a < stackaddr
	   || (void *) &a >= stackaddr + stacksize)
    {
      error (0, 0, "pthread_attr_getstack returned range does not cover thread's stack");
      result = tf;
    }
  else
    printf ("thread stack %p-%p (0x%zx)\n", stackaddr, stackaddr + stacksize,
	    stacksize);

  size_t guardsize1, guardsize2;
  err = pthread_attr_getguardsize (&a, &guardsize1);
  if (err)
    {
      error (0, err, "pthread_attr_getguardsize failed");
      result = tf;
    }
  else
    {
      err = pthread_attr_getguardsize (ap, &guardsize2);
      if (err)
	{
	  error (0, err, "pthread_attr_getguardsize failed");
	  result = tf;
	}
      else if (guardsize1 != guardsize2)
	{
	  error (0, 0, "guardsize differs %zd != %zd",
		 guardsize1, guardsize2);
	  result = tf;
	}
      else
	printf ("thread guardsize %zd\n", guardsize1);
    }

  int scope1, scope2;
  err = pthread_attr_getscope (&a, &scope1);
  if (err)
    {
      error (0, err, "pthread_attr_getscope failed");
      result = tf;
    }
  else
    {
      err = pthread_attr_getscope (ap, &scope2);
      if (err)
	{
	  error (0, err, "pthread_attr_getscope failed");
	  result = tf;
	}
      else if (scope1 != scope2)
	{
	  error (0, 0, "scope differs %d != %d",
		 scope1, scope2);
	  result = tf;
	}
    }

  int inheritsched1, inheritsched2;
  err = pthread_attr_getinheritsched (&a, &inheritsched1);
  if (err)
    {
      error (0, err, "pthread_attr_getinheritsched failed");
      result = tf;
    }
  else
    {
      err = pthread_attr_getinheritsched (ap, &inheritsched2);
      if (err)
	{
	  error (0, err, "pthread_attr_getinheritsched failed");
	  result = tf;
	}
      else if (inheritsched1 != inheritsched2)
	{
	  error (0, 0, "inheritsched differs %d != %d",
		 inheritsched1, inheritsched2);
	  result = tf;
	}
    }

  cpu_set_t c1, c2;
  err = pthread_getaffinity_np (pthread_self (), sizeof (c1), &c1);
  if (err == 0)
    {
      err = pthread_attr_getaffinity_np (&a, sizeof (c2), &c2);
      if (err)
	{
	  error (0, err, "pthread_attr_getaffinity_np failed");
	  result = tf;
	}
      else if (memcmp (&c1, &c2, sizeof (c1)))
	{
	  error (0, 0, "pthread_attr_getaffinity_np returned different CPU mask than pthread_getattr_np");
	  result = tf;
	}
    }

  err = pthread_attr_destroy (&a);
  if (err)
    {
      error (0, err, "pthread_attr_destroy failed");
      result = tf;
    }

  if (ap == &a2)
    {
      err = pthread_attr_destroy (ap);
      if (err)
	{
	  error (0, err, "pthread_attr_destroy failed");
	  result = tf;
	}
    }

  return result;
}
Beispiel #13
0
bool ThreadAttributes::IsDetached() const {
    int state = 0;
    TOFT_CHECK_PTHREAD_ERROR(pthread_attr_getdetachstate(&m_attr, &state));
    return state == PTHREAD_CREATE_DETACHED;
}
Beispiel #14
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);
}
Beispiel #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 */
}
Beispiel #16
0
int X(ithreads_init)(void)
{
#ifdef USING_POSIX_THREADS
     /* Set the thread creation attributes as necessary.  If we don't
	change anything, just use the default attributes (NULL). */
     int err, attr, attr_changed = 0;

     err = pthread_attr_init(&fftw_pthread_attributes); /* set to defaults */
     if (err) return err;

     /* Make sure that threads are joinable!  (they aren't on AIX) */
     err = pthread_attr_getdetachstate(&fftw_pthread_attributes, &attr);
     if (err) return err;
     if (attr != PTHREAD_CREATE_JOINABLE) {
	  err = pthread_attr_setdetachstate(&fftw_pthread_attributes,
					    PTHREAD_CREATE_JOINABLE);
	  if (err) return err;
	  attr_changed = 1;
     }

     /* Make sure threads parallelize (they don't by default on Solaris).
	
        Note, however that the POSIX standard does not *require*
	implementations to support PTHREAD_SCOPE_SYSTEM.  They may
        only support PTHREAD_SCOPE_PROCESS (e.g. IRIX, Cygwin).  In
        this case, how the threads interact with other resources on
        the system is undefined by the standard, and we have to
        hope for the best. */
     err = pthread_attr_getscope(&fftw_pthread_attributes, &attr);
     if (err) return err;
     if (attr != PTHREAD_SCOPE_SYSTEM) {
	  err = pthread_attr_setscope(&fftw_pthread_attributes,
				      PTHREAD_SCOPE_SYSTEM);
	  if (err == 0) attr_changed = 1;
          /* ignore errors */
     }

     if (attr_changed)  /* we aren't using the defaults */
	  fftw_pthread_attributes_p = &fftw_pthread_attributes;
     else {
	  fftw_pthread_attributes_p = NULL;  /* use default attributes */
	  err = pthread_attr_destroy(&fftw_pthread_attributes);
	  if (err) return err;
     }
#endif /* USING_POSIX_THREADS */

#ifdef USING_MACOS_THREADS
     /* FIXME: don't have malloc hooks (yet) in fftw3 */
     /* Must use MPAllocate and MPFree instead of malloc and free: */
     if (MPLibraryIsLoaded()) {
	  MALLOC_hook = MPAllocate;
	  fftw_free_hook = MPFree;
     }
#endif /* USING_MACOS_THREADS */

#if defined(USING_OPENMP_THREADS) && ! defined(_OPENMP)
#error OpenMP enabled but not using an OpenMP compiler
#endif

#ifdef HAVE_THREADS
     X(mksolver_ct_hook) = X(mksolver_ct_threads);
     X(mksolver_hc2hc_hook) = X(mksolver_hc2hc_threads);
     return 0; /* no error */
#else
     return 0; /* no threads, no error */
#endif
}
Beispiel #17
0
int
PGPThreadAttrGetDetachState( PGPThreadAttr_t *attr, int *detachstate )
{
	return pthread_attr_getdetachstate(attr, detachstate);
}
void *my_thread(void *arg){
    int retval = 0;
    pthread_attr_t attr;
    struct sched_param param;
    size_t stacksize;
    int detachstate;
    int scope;
    int inherit;
    int policy;

    if(pthread_attr_init (&attr) == 0){

        if(pthread_attr_getstacksize(&attr, &stacksize) == 0){
            printf("StackSize :%d\n", stacksize);
        }

        if(pthread_attr_getdetachstate (&attr, &detachstate) == 0){

            if(detachstate == PTHREAD_CREATE_JOINABLE){
                printf("DetachState : PTHREAD_CREATE_JOINABLE\n");
            }else if(detachstate == PTHREAD_CREATE_DETACHED){
                printf("DetachState:PTHREAD_CREATE_DETACHED\n");
            }
        }

        if(pthread_attr_getscope (&attr, &scope) == 0){
            if(scope == PTHREAD_SCOPE_PROCESS){
                printf("Scope : PTHREAD_SCOPE_PROCESS\n");
            }else if(scope == PTHREAD_SCOPE_SYSTEM){
                printf("Scope : PTHREAD_SCOPE_SYSTEM\n");
            }
        }


        if(pthread_attr_getinheritsched(&attr, &inherit) == 0){
            if(inherit == PTHREAD_INHERIT_SCHED){
                printf("InheritSched : PTHREAD_INHERIT_SCHED\n");
            }else if(inherit == PTHREAD_EXPLICIT_SCHED){
                printf("InheritSched : PTHREAD_EXPLICIT_SCHED\n");
            }
        }

        if(pthread_attr_getschedpolicy (&attr, &policy) == 0){
            if(policy == SCHED_FIFO){
                printf("SchedPolicy : SCHED_FIFO\n");
            }else if(policy == SCHED_RR){
                printf("SchedPolicy:SCHED_RR\n");
            }else{
                printf("SchedPolicy :SCHED_OTHER\n");
            }
        }

        if(pthread_attr_getschedparam (&attr, &param) == 0){
            printf("schedPriority:%d\n", param.sched_priority);
        }

        pthread_attr_destroy (&attr);
    }

    pthread_exit(&retval);
}
Beispiel #19
0
int
do_test (void)
{
  int i;
  pthread_attr_t a;

  if (pthread_attr_init (&a) != 0)
    {
      puts ("attr_init failed");
      exit (1);
    }

  pthread_mutexattr_t ma;

  if (pthread_mutexattr_init (&ma) != 0)
    {
      puts ("mutexattr_init failed");
      exit (1);
    }

  pthread_rwlockattr_t rwa;

  if (pthread_rwlockattr_init (&rwa) != 0)
    {
      puts ("rwlockattr_init failed");
      exit (1);
    }

  /* XXX Remove if default value is clear.  */
  pthread_attr_setinheritsched (&a, PTHREAD_INHERIT_SCHED);
  pthread_attr_setschedpolicy (&a, SCHED_OTHER);
  pthread_attr_setscope (&a, PTHREAD_SCOPE_SYSTEM);

  for (i = 0; i < 10000; ++i)
    {
      long int r = random ();

      if (r != PTHREAD_CREATE_DETACHED && r != PTHREAD_CREATE_JOINABLE)
	{
	  int e = pthread_attr_setdetachstate (&a, r);

	  if (e == 0)
	    {
	      printf ("attr_setdetachstate with value %ld succeeded\n", r);
	      exit (1);
	    }
	  if (e != EINVAL)
	    {
	      puts ("attr_setdetachstate didn't return EINVAL");
	      exit (1);
	    }

	  int s;
	  if (pthread_attr_getdetachstate (&a, &s) != 0)
	    {
	      puts ("attr_getdetachstate failed");
	      exit (1);
	    }

	  if (s != PTHREAD_CREATE_JOINABLE)
	    {
	      printf ("\
detach state changed to %d by invalid setdetachstate call\n", s);
	      exit (1);
	    }
	}
Beispiel #20
0
static int
do_test (void)
{
  int result = 0;
  pthread_attr_t a;
  cpu_set_t c1, c2;

  int err = pthread_attr_init (&a);
  if (err)
    {
      error (0, err, "pthread_attr_init failed");
      result = 1;
    }

  err = pthread_attr_getaffinity_np (&a, sizeof (c1), &c1);
  if (err && err != ENOSYS)
    {
      error (0, err, "pthread_attr_getaffinity_np failed");
      result = 1;
    }

  err = pthread_attr_destroy (&a);
  if (err)
    {
      error (0, err, "pthread_attr_destroy failed");
      result = 1;
    }

  err = pthread_getattr_np (pthread_self (), &a);
  if (err)
    {
      error (0, err, "pthread_getattr_np failed");
      result = 1;
    }

  int detachstate;
  err = pthread_attr_getdetachstate (&a, &detachstate);
  if (err)
    {
      error (0, err, "pthread_attr_getdetachstate failed");
      result = 1;
    }
  else if (detachstate != PTHREAD_CREATE_JOINABLE)
    {
      error (0, 0, "initial thread not joinable");
      result = 1;
    }

  void *stackaddr;
  size_t stacksize;
  err = pthread_attr_getstack (&a, &stackaddr, &stacksize);
  if (err)
    {
      error (0, err, "pthread_attr_getstack failed");
      result = 1;
    }
  else if ((void *) &a < stackaddr
	   || (void *) &a >= stackaddr + stacksize)
    {
      error (0, 0, "pthread_attr_getstack returned range does not cover main's stack");
      result = 1;
    }
  else
    printf ("initial thread stack %p-%p (0x%zx)\n", stackaddr,
	    stackaddr + stacksize, stacksize);

  size_t guardsize;
  err = pthread_attr_getguardsize (&a, &guardsize);
  if (err)
    {
      error (0, err, "pthread_attr_getguardsize failed");
      result = 1;
    }
  else if (guardsize != 0)
    {
      error (0, 0, "pthread_attr_getguardsize returned %zd != 0",
	     guardsize);
      result = 1;
    }

  int scope;
  err = pthread_attr_getscope (&a, &scope);
  if (err)
    {
      error (0, err, "pthread_attr_getscope failed");
      result = 1;
    }
  else if (scope != PTHREAD_SCOPE_SYSTEM)
    {
      error (0, 0, "pthread_attr_getscope returned %d != PTHREAD_SCOPE_SYSTEM",
	     scope);
      result = 1;
    }

  int inheritsched;
  err = pthread_attr_getinheritsched (&a, &inheritsched);
  if (err)
    {
      error (0, err, "pthread_attr_getinheritsched failed");
      result = 1;
    }
  else if (inheritsched != PTHREAD_INHERIT_SCHED)
    {
      error (0, 0, "pthread_attr_getinheritsched returned %d != PTHREAD_INHERIT_SCHED",
	     inheritsched);
      result = 1;
    }

  err = pthread_getaffinity_np (pthread_self (), sizeof (c1), &c1);
  if (err == 0)
    {
      err = pthread_attr_getaffinity_np (&a, sizeof (c2), &c2);
      if (err)
	{
	  error (0, err, "pthread_attr_getaffinity_np failed");
	  result = 1;
	}
      else if (memcmp (&c1, &c2, sizeof (c1)))
	{
	  error (0, 0, "pthread_attr_getaffinity_np returned different CPU mask than pthread_getattr_np");
	  result = 1;
	}
    }

  err = pthread_attr_destroy (&a);
  if (err)
    {
      error (0, err, "pthread_attr_destroy failed");
      result = 1;
    }

  pthread_t th;
  err = pthread_create (&th, NULL, tf, NULL);
  if (err)
    {
      error (0, err, "pthread_create #1 failed");
      result = 1;
    }
  else
    {
      void *ret;
      err = pthread_join (th, &ret);
      if (err)
	{
	  error (0, err, "pthread_join #1 failed");
	  result = 1;
	}
      else if (ret != NULL)
	result = 1;
    }

  err = pthread_attr_init (&a);
  if (err)
    {
      error (0, err, "pthread_attr_init failed");
      result = 1;
    }

  DIAG_PUSH_NEEDS_COMMENT;
#if __GNUC_PREREQ (7, 0)
  /* GCC 8 warns about aliasing of the restrict-qualified arguments
     passed &a.  Since pthread_create does not dereference its fourth
     argument, this aliasing, which is deliberate in this test, cannot
     in fact cause problems.  */
  DIAG_IGNORE_NEEDS_COMMENT (8, "-Wrestrict");
#endif
  err = pthread_create (&th, &a, tf, &a);
  DIAG_POP_NEEDS_COMMENT;
  if (err)
    {
      error (0, err, "pthread_create #2 failed");
      result = 1;
    }
  else
    {
      void *ret;
      err = pthread_join (th, &ret);
      if (err)
	{
	  error (0, err, "pthread_join #2 failed");
	  result = 1;
	}
      else if (ret != NULL)
	result = 1;
    }

  err = pthread_attr_setguardsize (&a, 16 * sysconf (_SC_PAGESIZE));
  if (err)
    {
      error (0, err, "pthread_attr_setguardsize failed");
      result = 1;
    }

  DIAG_PUSH_NEEDS_COMMENT;
#if __GNUC_PREREQ (7, 0)
  /* GCC 8 warns about aliasing of the restrict-qualified arguments
     passed &a.  Since pthread_create does not dereference its fourth
     argument, this aliasing, which is deliberate in this test, cannot
     in fact cause problems.  */
  DIAG_IGNORE_NEEDS_COMMENT (8, "-Wrestrict");
#endif
  err = pthread_create (&th, &a, tf, &a);
  DIAG_POP_NEEDS_COMMENT;
  if (err)
    {
      error (0, err, "pthread_create #3 failed");
      result = 1;
    }
  else
    {
      void *ret;
      err = pthread_join (th, &ret);
      if (err)
	{
	  error (0, err, "pthread_join #3 failed");
	  result = 1;
	}
      else if (ret != NULL)
	result = 1;
    }

  err = pthread_attr_destroy (&a);
  if (err)
    {
      error (0, err, "pthread_attr_destroy failed");
      result = 1;
    }

  return result;
}
/* This function will initialize every pthread_attr_t object in the scenarii array */
void scenar_init()
{
	int ret=0;
	int i;
	int old;
	long pagesize, minstacksize;
	long tsa, tss, tps;
	
	pagesize	=sysconf(_SC_PAGESIZE);
	minstacksize 	=sysconf(_SC_THREAD_STACK_MIN);
	tsa		=sysconf(_SC_THREAD_ATTR_STACKADDR);
	tss		=sysconf(_SC_THREAD_ATTR_STACKSIZE);
	tps		=sysconf(_SC_THREAD_PRIORITY_SCHEDULING);
	
	#if VERBOSE > 0
	output("System abilities:\n");
	output(" TSA: %li\n", tsa);
	output(" TSS: %li\n", tss);
	output(" TPS: %li\n", tps);
	output(" pagesize: %li\n", pagesize);
	output(" min stack size: %li\n", minstacksize);
	#endif
	
	
	if (minstacksize % pagesize)
	{
		UNTESTED("The min stack size is not a multiple of the page size");
	}
	
	for (i=0; i<NSCENAR; i++)
	{
		#if VERBOSE > 2
		output("Initializing attribute for scenario %i: %s\n", i, scenarii[i].descr);
		#endif
		
		ret = pthread_attr_init(&scenarii[i].ta);
		if (ret != 0)  {  UNRESOLVED(ret, "Failed to initialize a thread attribute object");  }
		
		/* Set the attributes according to the scenario */
		if (scenarii[i].detached == 1)
		{
			ret = pthread_attr_setdetachstate(&scenarii[i].ta, PTHREAD_CREATE_DETACHED);
			if (ret != 0)  {  UNRESOLVED(ret, "Unable to set detachstate");  }
		}
		else
		{
			ret = pthread_attr_getdetachstate(&scenarii[i].ta, &old);
			if (ret != 0)  {  UNRESOLVED(ret, "Unable to get detachstate from initialized attribute");  }
			if (old != PTHREAD_CREATE_JOINABLE)  {  FAILED("The default attribute is not PTHREAD_CREATE_JOINABLE");  }
		}
		#if VERBOSE > 4
		output("Detach state was set sucessfully\n");
		#endif
		
		/* Sched related attributes */
		if (tps>0) /* This routine is dependent on the Thread Execution Scheduling option */
		{
			if (scenarii[i].explicitsched == 1)
				ret = pthread_attr_setinheritsched(&scenarii[i].ta, PTHREAD_EXPLICIT_SCHED);
			else
				ret = pthread_attr_setinheritsched(&scenarii[i].ta, PTHREAD_INHERIT_SCHED);
			if (ret != 0)  {  UNRESOLVED(ret, "Unable to set inheritsched attribute");  }
			#if VERBOSE > 4
			output("inheritsched state was set sucessfully\n");
			#endif
		}
		#if VERBOSE > 4
		else
			output("TPS unsupported => inheritsched parameter untouched\n");
		#endif
		
		if (tps>0) /* This routine is dependent on the Thread Execution Scheduling option */
		{
			if (scenarii[i].schedpolicy == 1)
			{
				ret = pthread_attr_setschedpolicy(&scenarii[i].ta, SCHED_FIFO);
			}
			if (scenarii[i].schedpolicy == 2)
			{
				ret = pthread_attr_setschedpolicy(&scenarii[i].ta, SCHED_RR);
			}
			if (ret != 0)  {  UNRESOLVED(ret, "Unable to set the sched policy");  }
			#if VERBOSE > 4
			if (scenarii[i].schedpolicy)
				output("Sched policy was set sucessfully\n");
			else
				output("Sched policy untouched\n");
			#endif
		}
		#if VERBOSE > 4
		else
			output("TPS unsupported => sched policy parameter untouched\n");
		#endif
		
		if (scenarii[i].schedparam != 0)
		{
			struct sched_param sp;
			
			ret = pthread_attr_getschedpolicy(&scenarii[i].ta, &old);
			if (ret != 0)  {  UNRESOLVED(ret, "Unable to get sched policy from attribute"); }
			
			if (scenarii[i].schedparam == 1)
				sp.sched_priority = sched_get_priority_max(old);
			if (scenarii[i].schedparam == -1)
				sp.sched_priority = sched_get_priority_min(old);
			
			ret = pthread_attr_setschedparam(&scenarii[i].ta, &sp);
			if (ret != 0)  {  UNRESOLVED(ret, "Failed to set the sched param");  }
			
		#if VERBOSE > 4
			output("Sched param was set sucessfully to %i\n", sp.sched_priority);
		}
		else
		{
			output("Sched param untouched\n");
		#endif
		}
		
		if (tps>0) /* This routine is dependent on the Thread Execution Scheduling option */
		{
			ret = pthread_attr_getscope(&scenarii[i].ta, &old);
			if (ret != 0)  {  UNRESOLVED(ret, "Failed to get contension scope from thread attribute");  }
			
			if (scenarii[i].altscope != 0)
			{
				if (old == PTHREAD_SCOPE_PROCESS)
					old = PTHREAD_SCOPE_SYSTEM;
				else
					old = PTHREAD_SCOPE_PROCESS;
				
				ret = pthread_attr_setscope(&scenarii[i].ta, old);
				//if (ret != 0)  {  UNRESOLVED(ret, "Failed to set contension scope");  }
				#if VERBOSE > 0
				if (ret != 0)  {  output("WARNING: The TPS option is claimed to be supported but setscope fails\n");  }
				#endif
				
			#if VERBOSE > 4
				output("Contension scope set to %s\n", old==PTHREAD_SCOPE_PROCESS?"PTHREAD_SCOPE_PROCESS":"PTHREAD_SCOPE_SYSTEM");
			}
			else
			{
				output("Contension scope untouched (%s)\n", old==PTHREAD_SCOPE_PROCESS?"PTHREAD_SCOPE_PROCESS":"PTHREAD_SCOPE_SYSTEM");
			#endif
			}
		}
		#if VERBOSE > 4
		else
			output("TPS unsupported => sched contension scope parameter untouched\n");
		#endif
		
		/* Stack related attributes */
		if ((tss>0) && (tsa>0)) /* This routine is dependent on the Thread Stack Address Attribute 
			                   and Thread Stack Size Attribute options */
		{
			if (scenarii[i].altstack != 0)
			{
				/* This is slightly more complicated. We need to alloc a new stack
				and free it upon test termination */
				/* We will alloc with a simulated guardsize of 1 pagesize */
				scenarii[i].bottom = malloc(minstacksize + pagesize);
				if (scenarii[i].bottom == NULL)  {  UNRESOLVED(errno,"Unable to alloc enough memory for alternative stack"); }
				
				ret = pthread_attr_setstack(&scenarii[i].ta, scenarii[i].bottom, minstacksize);
				if (ret != 0)  {  UNRESOLVED(ret, "Failed to specify alternate stack");  }
			
				#if VERBOSE > 1
				output("Alternate stack created successfully. Bottom=%p, Size=%i\n", scenarii[i].bottom, minstacksize);
				#endif
			}
		}
		#if VERBOSE > 4
		else
			output("TSA or TSS unsupported => No alternative stack\n");
		#endif
		
		#ifndef WITHOUT_XOPEN
		if (scenarii[i].guard != 0)
		{
			if (scenarii[i].guard == 1)
				ret = pthread_attr_setguardsize(&scenarii[i].ta, 0);
			if (scenarii[i].guard == 2)
				ret = pthread_attr_setguardsize(&scenarii[i].ta, pagesize);
			if (ret != 0)  {  UNRESOLVED(ret, "Unable to set guard area size in thread stack");  }
			#if VERBOSE > 4
			output("Guard size set to %i\n", (scenarii[i].guard==1)?1:pagesize);
			#endif
		}
		#endif
		
		if (tss>0) /* This routine is dependent on the Thread Stack Size Attribute option */
		{
			if (scenarii[i].altsize != 0)
			{
				ret = pthread_attr_setstacksize(&scenarii[i].ta, minstacksize);
				if (ret != 0)  {  UNRESOLVED(ret, "Unable to change stack size");  }
				#if VERBOSE > 4
				output("Stack size set to %i (this is the min)\n", minstacksize);
				#endif
			}
		}
		#if VERBOSE > 4
		else
			output("TSS unsupported => stack size unchanged\n");
		#endif

		ret = sem_init(&scenarii[i].sem, 0,0);
		if (ret == -1) {  UNRESOLVED(errno, "Unable to init a semaphore");  }
		
	}
	#if VERBOSE > 0
	output("All %i thread attribute objects were initialized\n\n", NSCENAR);
	#endif
}
Beispiel #22
0
static uintptr_t
isAttrOk(const create_attr_t *expected, const omrthread_attr_t actual)
{
	uintptr_t status = 0;

	/* expected should never be null */
	if (expected == NULL) {
		return BAD_TEST;
	}

	if (actual == NULL) {
		status |= NULL_ATTR;
		/* no further checks are possible */
		omrTestEnv->log(LEVEL_ERROR, "attr is NULL\n");
		return status;
	}

	/* both should refer to the same memory */
	if (expected->name != actual->name) {
		omrTestEnv->log(LEVEL_ERROR, "wrong name\n");
		status |= WRONG_NAME;
	}

	if (expected->stacksize != actual->stacksize) {
		printMismatchU("stacksize", expected->stacksize, actual->stacksize);
		status |= WRONG_STACKSIZE;
	}

	if (expected->policy != actual->schedpolicy) {
		printMismatchI("schedpolicy", expected->policy, actual->schedpolicy);
		status |= WRONG_SCHEDPOLICY;
	}

	if (expected->priority != actual->priority) {
		printMismatchU("priority", expected->priority, actual->priority);
		status |= WRONG_PRIORITY;
	}

#if defined(SPEC_PTHREAD_API)
	{
		pthread_attr_t *attr = &((unixthread_attr_t)actual)->pattr;
		size_t osStacksize;
#ifndef J9ZOS390
		struct sched_param osSchedparam;
		int osPolicy;
		int osInheritsched;
#endif
#if defined(AIXPPC) || defined(LINUX) || defined(OSX)
		int osScope;
#endif /* defined(AIXPPC) || defined(LINUX) || defined(OSX) */
#ifdef J9ZOS390
		int osThreadweight;
		int osDetachstate;
#endif

		PTHREAD_VERBOSE(pthread_attr_getstacksize(attr, &osStacksize));
		if (osStacksize != expected->osStacksize) {
			printMismatchI("os stacksize", expected->osStacksize, osStacksize);
			status |= WRONG_OS_STACKSIZE;
		}

#ifndef J9ZOS390

		PTHREAD_VERBOSE(pthread_attr_getschedpolicy(attr, &osPolicy));
#if (defined(AIXPPC) || defined(LINUX) || defined(OSX))
		/* aix and linux bug - need to set the schedpolicy to OTHER if inheritsched used */
		if (J9THREAD_SCHEDPOLICY_INHERIT == expected->policy) {
			if (osPolicy != OS_SCHED_OTHER) {
				printMismatchS("os schedpolicy", mapOSPolicy(OS_SCHED_OTHER), mapOSPolicy(osPolicy));
				status |= WRONG_OS_SCHEDPOLICY;
			}
		}
		else
#endif /* (defined(AIXPPC) || defined(LINUX) || defined(OSX)) */
		{
			if (osPolicy != expected->osPolicy) {
				printMismatchS("os schedpolicy", mapOSPolicy(expected->osPolicy), mapOSPolicy(osPolicy));
				status |= WRONG_OS_SCHEDPOLICY;
			}
		}

		PTHREAD_VERBOSE(pthread_attr_getinheritsched(attr, &osInheritsched));
		if (osInheritsched != expected->osInheritsched) {
			printMismatchS("os inheritsched", mapOSInherit(expected->osInheritsched), mapOSInherit(osInheritsched));
			status |= WRONG_OS_INHERITSCHED;
		}

		PTHREAD_VERBOSE(pthread_attr_getschedparam(attr, &osSchedparam));
		if (osSchedparam.sched_priority != expected->osPriority) {
			printMismatchI("os priority", expected->osPriority, osSchedparam.sched_priority);
			status |= WRONG_OS_PRIORITY;
		}
#endif /* not J9ZOS390 */

#if defined(AIXPPC) || defined(LINUX) || defined(OSX)
		PTHREAD_VERBOSE(pthread_attr_getscope(attr, &osScope));
		if (osScope != expected->osScope) {
			printMismatchI("os scope", expected->osScope, osScope);
			status |= WRONG_OS_SCOPE;
		}
#endif /* defined(AIXPPC) || defined(LINUX) || defined(OSX) */

#ifdef J9ZOS390
		osThreadweight = pthread_attr_getweight_np(attr);
		if (osThreadweight != expected->osThreadweight) {
			printMismatchI("os threadweight", expected->osThreadweight, osThreadweight);
			status |= WRONG_OS_THREADWEIGHT;
		}

		osDetachstate = pthread_attr_getdetachstate(attr);
		if (osDetachstate != expected->osDetachstate) {
			printMismatchI("os detachstate", expected->osDetachstate, osDetachstate);
			status |= WRONG_OS_DETACHSTATE;
		}
#endif
	}
#endif /* defined(SPEC_PTHREAD_API) */

	if (status == 0) {
		status |= canCreateThread(expected, actual);
	}
	return status;
}
Beispiel #23
0
static int my_pthread_attr_getdetachstate(pthread_attr_t const *__attr, int *state)
{
    pthread_attr_t *realattr = (pthread_attr_t *) *(unsigned int *) __attr;
    return pthread_attr_getdetachstate(realattr, state);
}
Beispiel #24
0
static int
do_test (void)
{
  int result = 0;
  pthread_attr_t a;
  cpu_set_t c1, c2;

  int err = pthread_attr_init (&a);
  if (err)
    {
      error (0, err, "pthread_attr_init failed");
      result = 1;
    }

  err = pthread_attr_getaffinity_np (&a, sizeof (c1), &c1);
  if (err && err != ENOSYS)
    {
      error (0, err, "pthread_attr_getaffinity_np failed");
      result = 1;
    }

  err = pthread_attr_destroy (&a);
  if (err)
    {
      error (0, err, "pthread_attr_destroy failed");
      result = 1;
    }

  err = pthread_getattr_np (pthread_self (), &a);
  if (err)
    {
      error (0, err, "pthread_getattr_np failed");
      result = 1;
    }

  int detachstate;
  err = pthread_attr_getdetachstate (&a, &detachstate);
  if (err)
    {
      error (0, err, "pthread_attr_getdetachstate failed");
      result = 1;
    }
  else if (detachstate != PTHREAD_CREATE_JOINABLE)
    {
      error (0, 0, "initial thread not joinable");
      result = 1;
    }

  void *stackaddr;
  size_t stacksize;
  err = pthread_attr_getstack (&a, &stackaddr, &stacksize);
  if (err)
    {
      error (0, err, "pthread_attr_getstack failed");
      result = 1;
    }
  else if ((void *) &a < stackaddr
	   || (void *) &a >= stackaddr + stacksize)
    {
      error (0, 0, "pthread_attr_getstack returned range does not cover main's stack");
      result = 1;
    }
  else
    printf ("initial thread stack %p-%p (0x%zx)\n", stackaddr,
	    stackaddr + stacksize, stacksize);

  size_t guardsize;
  err = pthread_attr_getguardsize (&a, &guardsize);
  if (err)
    {
      error (0, err, "pthread_attr_getguardsize failed");
      result = 1;
    }
  else if (guardsize != 0)
    {
      error (0, 0, "pthread_attr_getguardsize returned %zd != 0",
	     guardsize);
      result = 1;
    }

  int scope;
  err = pthread_attr_getscope (&a, &scope);
  if (err)
    {
      error (0, err, "pthread_attr_getscope failed");
      result = 1;
    }
  else if (scope != PTHREAD_SCOPE_SYSTEM)
    {
      error (0, 0, "pthread_attr_getscope returned %d != PTHREAD_SCOPE_SYSTEM",
	     scope);
      result = 1;
    }

  int inheritsched;
  err = pthread_attr_getinheritsched (&a, &inheritsched);
  if (err)
    {
      error (0, err, "pthread_attr_getinheritsched failed");
      result = 1;
    }
  else if (inheritsched != PTHREAD_INHERIT_SCHED)
    {
      error (0, 0, "pthread_attr_getinheritsched returned %d != PTHREAD_INHERIT_SCHED",
	     inheritsched);
      result = 1;
    }

  err = pthread_getaffinity_np (pthread_self (), sizeof (c1), &c1);
  if (err == 0)
    {
      err = pthread_attr_getaffinity_np (&a, sizeof (c2), &c2);
      if (err)
	{
	  error (0, err, "pthread_attr_getaffinity_np failed");
	  result = 1;
	}
      else if (memcmp (&c1, &c2, sizeof (c1)))
	{
	  error (0, 0, "pthread_attr_getaffinity_np returned different CPU mask than pthread_getattr_np");
	  result = 1;
	}
    }

  err = pthread_attr_destroy (&a);
  if (err)
    {
      error (0, err, "pthread_attr_destroy failed");
      result = 1;
    }

  pthread_t th;
  err = pthread_create (&th, NULL, tf, NULL);
  if (err)
    {
      error (0, err, "pthread_create #1 failed");
      result = 1;
    }
  else
    {
      void *ret;
      err = pthread_join (th, &ret);
      if (err)
	{
	  error (0, err, "pthread_join #1 failed");
	  result = 1;
	}
      else if (ret != NULL)
	result = 1;
    }

  err = pthread_attr_init (&a);
  if (err)
    {
      error (0, err, "pthread_attr_init failed");
      result = 1;
    }

  err = pthread_create (&th, &a, tf, &a);
  if (err)
    {
      error (0, err, "pthread_create #2 failed");
      result = 1;
    }
  else
    {
      void *ret;
      err = pthread_join (th, &ret);
      if (err)
	{
	  error (0, err, "pthread_join #2 failed");
	  result = 1;
	}
      else if (ret != NULL)
	result = 1;
    }

  err = pthread_attr_setguardsize (&a, 16 * sysconf (_SC_PAGESIZE));
  if (err)
    {
      error (0, err, "pthread_attr_setguardsize failed");
      result = 1;
    }

  err = pthread_create (&th, &a, tf, &a);
  if (err)
    {
      error (0, err, "pthread_create #3 failed");
      result = 1;
    }
  else
    {
      void *ret;
      err = pthread_join (th, &ret);
      if (err)
	{
	  error (0, err, "pthread_join #3 failed");
	  result = 1;
	}
      else if (ret != NULL)
	result = 1;
    }

  err = pthread_attr_destroy (&a);
  if (err)
    {
      error (0, err, "pthread_attr_destroy failed");
      result = 1;
    }

  return result;
}
Beispiel #25
0
int pthread_attr_getdetachstate_ex(const pthread_attr_ex_t *attr_ex,
				   int *detachstate)
{
	return pthread_attr_getdetachstate(&attr_ex->std, detachstate);
}
// pthread_create
PTH_FUNC(int, pthreadZucreateZa, // pthread_create*
         pthread_t *thread, const pthread_attr_t *attr,
         void *(*start) (void *), void *arg)
{
   int    res;
   int    ret;
   OrigFn fn;
#if defined(ALLOCATE_THREAD_ARGS_ON_THE_STACK)
   DrdPosixThreadArgs thread_args;
#endif
   DrdPosixThreadArgs* thread_args_p;

   VALGRIND_GET_ORIG_FN(fn);

#if defined(ALLOCATE_THREAD_ARGS_ON_THE_STACK)
   thread_args_p = &thread_args;
#else
   thread_args_p = malloc(sizeof(*thread_args_p));
#endif
   assert(thread_args_p);

   thread_args_p->start           = start;
   thread_args_p->arg             = arg;
#if defined(WAIT_UNTIL_CREATED_THREAD_STARTED)
   DRD_IGNORE_VAR(thread_args_p->wrapper_started);
   thread_args_p->wrapper_started = 0;
#endif
   /*
    * Find out whether the thread will be started as a joinable thread
    * or as a detached thread. If no thread attributes have been specified,
    * this means that the new thread will be started as a joinable thread.
    */
   thread_args_p->detachstate = PTHREAD_CREATE_JOINABLE;
   if (attr)
   {
      if (pthread_attr_getdetachstate(attr, &thread_args_p->detachstate) != 0)
      {
         assert(0);
      }
   }
   assert(thread_args_p->detachstate == PTHREAD_CREATE_JOINABLE
          || thread_args_p->detachstate == PTHREAD_CREATE_DETACHED);


   DRD_(entering_pthread_create)();
   CALL_FN_W_WWWW(ret, fn, thread, attr, DRD_(thread_wrapper), thread_args_p);
   DRD_(left_pthread_create)();

#if defined(WAIT_UNTIL_CREATED_THREAD_STARTED)
   if (ret == 0)
   {
      /*
       * Wait until the thread wrapper started.
       * @todo Find out why some regression tests fail if thread arguments are
       *   passed via dynamically allocated memory and if the loop below is
       *   removed.
       */
      while (! thread_args_p->wrapper_started)
      {
         sched_yield();
      }
   }

#if defined(ALLOCATE_THREAD_ARGS_DYNAMICALLY)
   free(thread_args_p);
#endif

#endif

   VALGRIND_DO_CLIENT_REQUEST(res, -1, VG_USERREQ__DRD_START_NEW_SEGMENT,
                              pthread_self(), 0, 0, 0, 0);

   return ret;
}
Beispiel #27
0
/* Performs the _real version with log and replay. Does NOT check
   shouldSynchronize() and shouldn't be called directly unless you know what
   you're doing. */
static int internal_pthread_create(pthread_t *thread,
    const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg)
{
  int retval = 0, was_detached = 0;
  pthread_attr_t the_attr;
  size_t stack_size;
  void *stack_addr;
  struct create_arg *createArg =
    (struct create_arg*) JALLOC_HELPER_MALLOC(sizeof(*createArg));
  createArg->fn = start_routine;
  createArg->thread_arg = arg;
  if (attr != NULL) {
    pthread_attr_getstack(attr, &createArg->userStack, &stack_size);
    pthread_attr_getdetachstate(attr, &createArg->userDetachState);
  } else {
    createArg->userStack = NULL;
    createArg->userDetachState = PTHREAD_CREATE_JOINABLE;
  }

  log_entry_t my_entry = create_pthread_create_entry(my_clone_id,
                                                     pthread_create_event,
                                                     thread, attr,
                                                     start_routine, arg);
  if (SYNC_IS_REPLAY) {
    WRAPPER_REPLAY_START(pthread_create);
    stack_addr = (void *)GET_FIELD(my_entry, pthread_create, stack_addr);
    stack_size = GET_FIELD(my_entry, pthread_create, stack_size);
    dmtcp::ThreadInfo::prePthreadCreate();
    WRAPPER_REPLAY_END(pthread_create);

    // Register a new thread with ThreadInfo.
    pthread_t newPthreadId = GET_FIELD(my_entry, pthread_create, ret_thread);
    // Set up thread stacks to how they were at record time.
    pthread_attr_init(&the_attr);

    setupThreadStack(&the_attr, attr, stack_size);
    // Never let the user create a detached thread:
    disableDetachState(&the_attr);
    createArg->attr = the_attr;
    retval = _real_pthread_create(thread, &the_attr,
                                  start_wrapper, (void *)createArg);
    pthread_attr_destroy(&the_attr);

  } else if (SYNC_IS_RECORD) {
    WRAPPER_LOG_RESERVE_SLOT(pthread_create);
    dmtcp::ThreadInfo::prePthreadCreate();

    pthread_attr_init(&the_attr);
    // Possibly create a thread stack if the user has not provided one:
    setupThreadStack(&the_attr, attr, 0);
    // Never let the user create a detached thread:
    disableDetachState(&the_attr);

    createArg->attr = the_attr;
    // MTCP may call jalib::malloc to allocate space for thread struct.
    dmtcp::ThreadInfo::setOptionalEvent();
    retval = _real_pthread_create(thread, &the_attr,
                                  start_wrapper, (void *)createArg);
    dmtcp::ThreadInfo::unsetOptionalEvent();
    SET_RETVAL_ERRNO(my_entry, pthread_create, retval, errno);

    // Log whatever stack we ended up using:
    pthread_attr_getstack(&the_attr, &stack_addr, &stack_size);
    pthread_attr_destroy(&the_attr);
    SET_FIELD(my_entry, pthread_create, stack_addr);
    SET_FIELD(my_entry, pthread_create, stack_size);
    SET_FIELD2(my_entry, pthread_create, ret_thread, *thread);

    // Log annotation on the fly.
    WRAPPER_LOG_UPDATE_ENTRY(pthread_create);
  }

  if (retval != 0) {
    dmtcp::ThreadInfo::postPthreadCreate();
  }
  return retval;
}