示例#1
0
mlt_slices mlt_slices_init( int threads, int policy, int priority )
{
	pthread_attr_t tattr;
	struct sched_param param;
	mlt_slices ctx = (mlt_slices)calloc( 1, sizeof( struct mlt_slices_s ) );
	char *env = getenv( ENV_SLICES );
#ifdef _WIN32
	int cpus = GetCurrentProcessorNumber( );
#else
	int cpus = sysconf( _SC_NPROCESSORS_ONLN );
#endif
	int i, env_val = env ? atoi(env) : 0;

	/* check given threads count */
	if ( !env || !env_val )
	{
		if ( threads < 0 )
			threads = -threads * cpus;
		else if ( !threads )
			threads = cpus;
	}
	else if ( env_val < 0 )
	{
		if ( threads < 0 )
			threads = env_val * threads * cpus;
		else if ( !threads )
			threads = -env_val * cpus;
		else
			threads = -env_val * threads;
	}
	else // env_val > 0
	{
		if ( threads < 0 )
			threads = env_val * threads;
		else if ( !threads )
			threads = env_val;
		else
			threads = threads;
	}
	if ( threads > MAX_SLICES )
		threads = MAX_SLICES;

	ctx->count = threads;

	/* init attributes */
	pthread_mutex_init ( &ctx->cond_mutex, NULL );
	pthread_cond_init ( &ctx->cond_var_job, NULL );
	pthread_cond_init ( &ctx->cond_var_ready, NULL );
	pthread_attr_init( &tattr );
	pthread_attr_setschedpolicy( &tattr, policy );
	param.sched_priority = priority;
	pthread_attr_setschedparam( &tattr, &param );

	/* run worker threads */
	for ( i = 0; i < ctx->count; i++ )
	{
		pthread_create( &ctx->threads[i], &tattr, mlt_slices_worker, ctx );
		pthread_setschedparam( ctx->threads[i], policy, &param);
	}

	pthread_attr_destroy( &tattr );

	/* ready wait workers */
	pthread_mutex_lock( &ctx->cond_mutex );
	while ( ctx->readys != ctx->count )
		pthread_cond_wait( &ctx->cond_var_ready, &ctx->cond_mutex );
	pthread_mutex_unlock( &ctx->cond_mutex );

	/* return context */
	return ctx;
}
示例#2
0
/*
 * Here's the entry point
 */
int main (int argc, char *argv[])
{
    int sock;
    char *filenm = NULL;
    struct in_addr rcvr_addr;
    program_type prog_type = unknown;
    sec_serv_t sec_servs = sec_serv_conf | sec_serv_auth;
    int num_threads = 1;
    int c;
    char *input_key = NULL;
    char *address = NULL;
    char key[MAX_KEY_LEN];
    unsigned short start_port = 0;
    srtp_policy_t *policy;
    err_status_t status;
    thread_parms_t *tplist[MAX_THREADS_SUPPORTED];
    thread_parms_t *tp;
    int i;
    pthread_attr_t t_attr;
    struct sched_param sp1 = { 11 };
    int len;
    int rc;

    /* initialize srtp library */
    status = srtp_init();
    if (status) {
        printf("error: srtp initialization failed with error code %d\n", status);
        exit(1);
    }

    /* check args */
    while (1) {
        c = getopt(argc, argv, "k:n:o:rs");
        if (c == -1) {
            break;
        }
        switch (c) {
        case 'n':
            num_threads = atoi(optarg);
            if (num_threads > MAX_THREADS_SUPPORTED) {
                printf("error: maximum number of threads supported is %d\n", MAX_THREADS_SUPPORTED);
                exit(1);
            }
            printf("Running %d threads\n", num_threads);
            break;
        case 'k':
            input_key = optarg;
            printf("Using key\n");
            break;
        case 'o':
            filenm = optarg;
            printf("Using output file: %s\n", filenm);
            break;
        case 'r':
            prog_type = receiver;
            break;
        case 's':
            prog_type = sender;
            break;
        default:
            usage(argv[0]);
        }
    }

    if (prog_type == unknown) {
        printf("error: neither sender [-s] nor receiver [-r] specified\n");
        usage(argv[0]);
    }

    if (!input_key) {
        /*
         * a key must be provided if and only if security services have
         * been requested
         */
        usage(argv[0]);
    }

    if (argc != optind + 2) {
        /* wrong number of arguments */
        usage(argv[0]);
    }

    /* get IP address from arg */
    address = argv[optind++];

    /*
     * read key from hexadecimal on command line into an octet string
     */
    len = hex_string_to_octet_string(key, input_key, MASTER_KEY_LEN * 2);

    /* check that hex string is the right length */
    if (len < MASTER_KEY_LEN * 2) {
        fprintf(stderr,
                "error: too few digits in key/salt "
                "(should be %d hexadecimal digits, found %d)\n",
                MASTER_KEY_LEN * 2, len);
        exit(1);
    }
    if (strlen(input_key) > MASTER_KEY_LEN * 2) {
        fprintf(stderr,
                "error: too many digits in key/salt "
                "(should be %d hexadecimal digits, found %u)\n",
                MASTER_KEY_LEN * 2, (unsigned)strlen(input_key));
        exit(1);
    }

    printf("set master key/salt to %s/", octet_string_hex_string(key, 16));
    printf("%s\n", octet_string_hex_string(key + 16, 14));


    /* get starting port from arg */
    start_port = atoi(argv[optind++]);

    /* set address */
    rcvr_addr.s_addr = inet_addr(address);
    if (0xffffffff == rcvr_addr.s_addr) {
        fprintf(stderr, "%s: cannot parse IP v4 address %s\n", argv[0], address);
        exit(1);
    }

    if (ADDR_IS_MULTICAST(rcvr_addr.s_addr)) {
        printf("\nMulticast addresses not supported\n");
        exit(1);
    }

    if (prog_type == receiver && filenm != NULL) {
        output = fopen(filenm, "wa");
        if (output == NULL) {
            printf("\nUnable to open output file.\n");
            exit(1);
        }
    } else {
        output = stdout;
    }

    /*
     * Setup and kick-off each thread.  Each thread will be either
     * a receiver or a sender, depending on the arguments passed to us
     */
    for (i = 0; i < num_threads; i++) {
        /* open socket */
        sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
        if (sock < 0) {
            int err;
            err = errno;
            fprintf(stderr, "%s: couldn't open socket: %d\n", argv[0], err);
            exit(1);
        }

        /*
         * Create an SRTP policy
         */
        policy = malloc(sizeof(srtp_policy_t));
        if (policy == NULL) {
            fprintf(stderr, "Unable to malloc\n");
            exit(1);
        }

        crypto_policy_set_rtp_default(&policy->rtp);
        crypto_policy_set_rtcp_default(&policy->rtcp);
        policy->ssrc.type  = ssrc_specific;
        policy->ssrc.value = SSRC_BASE;
        policy->key  = (uint8_t*)key;
        policy->next = NULL;
        policy->rtp.sec_serv = sec_servs;
        policy->rtcp.sec_serv = sec_serv_none;  /* we don't do RTCP anyway */

        /*
         * Create a thread_parms_t instance to manage this thread
         */
        tplist[i] = malloc(sizeof(thread_parms_t));
        if (tplist[i] == NULL) {
            fprintf(stderr, "Unable to malloc\n");
            exit(1);
        }
        tp = tplist[i];

        tp->thread_num = i;
        tp->sock = sock;
        tp->port = start_port + i;
        tp->policy = policy;
        tp->name.sin_addr   = rcvr_addr;
        tp->name.sin_family = PF_INET;
        tp->name.sin_port   = htons(start_port + i);
        tp->ssrc = SSRC_BASE;
        tp->stream_cnt = NUM_SSRC_PER_THREAD; //Number of streams to create for the session


        /*
         * Setup the pthread attributes
         */
        pthread_attr_init(&t_attr);
        pthread_attr_setschedparam(&t_attr, &sp1);
        pthread_attr_setschedpolicy(&t_attr, SCHED_RR);

        if (prog_type == sender) {
            /*
             * Start a sender thread
             */
            rc = pthread_create(&tp->thread_id, &t_attr, sender_thread, tp);
            if (rc) {
                fprintf(stderr, "Unable to create thread\n");
                exit(1);
            }
        } else { /* prog_type == receiver */
            /*
             * Start a receiver thread
             */
            rc = pthread_create(&tp->thread_id, &t_attr, receiver_thread, tp);
            if (rc) {
                fprintf(stderr, "Unable to create thread\n");
                exit(1);
            }
        }
        /*
         * Clean-up the attributes
         */
        pthread_attr_destroy(&t_attr);
    }


    /*
     * Wait for all threads to finish
     */
    for (i = 0; i < num_threads; i++) {
        void *res;
        tp = tplist[i];
        pthread_join(tp->thread_id, &res);
        close(tplist[i]->sock);
        free(tplist[i]->policy);
        free(tplist[i]);
    }

    /*
     * If we don't call this, we get a memory leak in the
     * libsrtp libary
     */
    status = srtp_shutdown();
    if (status) {
	printf("error: srtp shutdown failed with error code %d\n", status);
	exit(1);
    }

    return (0);
}
int nsh_parse(FAR struct nsh_vtbl_s *vtbl, char *cmdline)
{
  FAR char *argv[MAX_ARGV_ENTRIES];
  FAR char *saveptr;
  FAR char *cmd;
#if CONFIG_NFILE_STREAMS > 0
  FAR char *redirfile = NULL;
  int       oflags = 0;
  int       fd = -1;
#endif
  int       argc;
  int       ret;

  /* Initialize parser state */

  memset(argv, 0, MAX_ARGV_ENTRIES*sizeof(FAR char *));
#ifndef CONFIG_NSH_DISABLEBG
  vtbl->np.np_bg       = false;
#endif
#if CONFIG_NFILE_STREAMS > 0
  vtbl->np.np_redirect = false;
#endif

  /* Parse out the command at the beginning of the line */

  saveptr = cmdline;
  cmd = nsh_argument(vtbl, &saveptr);

  /* Handler if-then-else-fi */

#ifndef CONFIG_NSH_DISABLESCRIPT
  if (nsh_ifthenelse(vtbl, &cmd, &saveptr) != 0)
    {
      goto errout;
    }
#endif

  /* Handle nice */

#ifndef CONFIG_NSH_DISABLEBG
  if (nsh_nice(vtbl, &cmd, &saveptr) != 0)
    {
      goto errout;
    }
#endif

  /* Check if any command was provided -OR- if command processing is
   * currently disabled.
   */

#ifndef CONFIG_NSH_DISABLESCRIPT
  if (!cmd || !nsh_cmdenabled(vtbl))
#else
  if (!cmd)
#endif
    {
      /* An empty line is not an error and an unprocessed command cannot
       * generate an error, but neither should they change the last
       * command status.
       */

      return OK;
    }

  /* Parse all of the arguments following the command name.  The form
   * of argv is:
   *
   *   argv[0]:      The command name.
   *   argv[1]:      The beginning of argument (up to CONFIG_NSH_MAXARGUMENTS)
   *   argv[argc-3]: Possibly '>' or '>>'
   *   argv[argc-2]: Possibly <file>
   *   argv[argc-1]: Possibly '&'
   *   argv[argc]:   NULL terminating pointer
   *
   * Maximum size is CONFIG_NSH_MAXARGUMENTS+5
   */

  argv[0] = cmd;
  for (argc = 1; argc < MAX_ARGV_ENTRIES-1; argc++)
    {
      argv[argc] = nsh_argument(vtbl, &saveptr);
      if (!argv[argc])
        {
          break;
        }
    }

  argv[argc] = NULL;

  /* Check if the command should run in background */

#ifndef CONFIG_NSH_DISABLEBG
  if (argc > 1 && strcmp(argv[argc-1], "&") == 0)
    {
      vtbl->np.np_bg = true;
      argv[argc-1] = NULL;
      argc--;
    }
#endif

#if CONFIG_NFILE_STREAMS > 0
  /* Check if the output was re-directed using > or >> */

  if (argc > 2)
    {
      /* Check for redirection to a new file */

      if (strcmp(argv[argc-2], g_redirect1) == 0)
        {
          vtbl->np.np_redirect = true;
          oflags               = O_WRONLY|O_CREAT|O_TRUNC;
          redirfile            = nsh_getfullpath(vtbl, argv[argc-1]);
          argc                -= 2;
        }

      /* Check for redirection by appending to an existing file */

      else if (strcmp(argv[argc-2], g_redirect2) == 0)
        {
          vtbl->np.np_redirect = true;
          oflags               = O_WRONLY|O_CREAT|O_APPEND;
          redirfile            = nsh_getfullpath(vtbl, argv[argc-1]);
          argc                -= 2;
        }
    }
#endif

  /* Check if the maximum number of arguments was exceeded */

  if (argc > CONFIG_NSH_MAXARGUMENTS)
    {
      nsh_output(vtbl, g_fmttoomanyargs, cmd);
    }

  /* Does this command correspond to an application filename?
   * nsh_fileapp() returns:
   *
   *   -1 (ERROR)  if the application task corresponding to 'argv[0]' could not
   *               be started (possibly because it doesn not exist).
   *    0 (OK)     if the application task corresponding to 'argv[0]' was
   *               and successfully started.  If CONFIG_SCHED_WAITPID is
   *               defined, this return value also indicates that the
   *               application returned successful status (EXIT_SUCCESS)
   *    1          If CONFIG_SCHED_WAITPID is defined, then this return value
   *               indicates that the application task was spawned successfully
   *               but returned failure exit status.
   *
   * Note the priority if not effected by nice-ness.
   */

#ifdef CONFIG_NSH_FILE_APPS
  ret = nsh_fileapp(vtbl, argv[0], argv, redirfile, oflags);
  if (ret >= 0)
    {
      /* nsh_fileapp() returned 0 or 1.  This means that the builtin
       * command was successfully started (although it may not have ran
       * successfully).  So certainly it is not an NSH command.
       */

      /* Free the redirected output file path */

      if (redirfile)
        {
          nsh_freefullpath(redirfile);
        }

      /* Save the result:  success if 0; failure if 1 */

      return nsh_saveresult(vtbl, ret != OK);
    }

  /* No, not a built in command (or, at least, we were unable to start a
   * builtin command of that name).  Treat it like an NSH command.
   */

#endif

  /* Does this command correspond to a builtin command?
   * nsh_builtin() returns:
   *
   *   -1 (ERROR)  if the application task corresponding to 'argv[0]' could not
   *               be started (possibly because it doesn not exist).
   *    0 (OK)     if the application task corresponding to 'argv[0]' was
   *               and successfully started.  If CONFIG_SCHED_WAITPID is
   *               defined, this return value also indicates that the
   *               application returned successful status (EXIT_SUCCESS)
   *    1          If CONFIG_SCHED_WAITPID is defined, then this return value
   *               indicates that the application task was spawned successfully
   *               but returned failure exit status.
   *
   * Note the priority if not effected by nice-ness.
   */

#if defined(CONFIG_NSH_BUILTIN_APPS) && (!defined(CONFIG_NSH_FILE_APPS) || !defined(CONFIG_FS_BINFS))
#if CONFIG_NFILE_STREAMS > 0
  ret = nsh_builtin(vtbl, argv[0], argv, redirfile, oflags);
#else
  ret = nsh_builtin(vtbl, argv[0], argv, NULL, 0);
#endif
  if (ret >= 0)
    {
      /* nsh_builtin() returned 0 or 1.  This means that the builtin
       * command was successfully started (although it may not have ran
       * successfully).  So certainly it is not an NSH command.
       */

#if CONFIG_NFILE_STREAMS > 0
      /* Free the redirected output file path */

      if (redirfile)
        {
          nsh_freefullpath(redirfile);
        }
#endif

      /* Save the result:  success if 0; failure if 1 */

      return nsh_saveresult(vtbl, ret != OK);
    }

  /* No, not a built in command (or, at least, we were unable to start a
   * builtin command of that name).  Treat it like an NSH command.
   */

#endif

#if CONFIG_NFILE_STREAMS > 0
  /* Redirected output? */

  if (vtbl->np.np_redirect)
    {
      /* Open the redirection file.  This file will eventually
       * be closed by a call to either nsh_release (if the command
       * is executed in the background) or by nsh_undirect if the
       * command is executed in the foreground.
       */

      fd = open(redirfile, oflags, 0666);
      nsh_freefullpath(redirfile);
      redirfile = NULL;

      if (fd < 0)
        {
          nsh_output(vtbl, g_fmtcmdfailed, cmd, "open", NSH_ERRNO);
          goto errout;
        }
    }
#endif

  /* Handle the case where the command is executed in background.
   * However is app is to be started as builtin new process will
   * be created anyway, so skip this step.
   */

#ifndef CONFIG_NSH_DISABLEBG
  if (vtbl->np.np_bg)
    {
      struct sched_param param;
      struct nsh_vtbl_s *bkgvtbl;
      struct cmdarg_s *args;
      pthread_attr_t attr;
      pthread_t thread;

      /* Get a cloned copy of the vtbl with reference count=1.
       * after the command has been processed, the nsh_release() call
       * at the end of nsh_child() will destroy the clone.
       */

      bkgvtbl = nsh_clone(vtbl);
      if (!bkgvtbl)
        {
          goto errout_with_redirect;
        }

      /* Create a container for the command arguments */

      args = nsh_cloneargs(bkgvtbl, fd, argc, argv);
      if (!args)
        {
          nsh_release(bkgvtbl);
          goto errout_with_redirect;
        }

#if CONFIG_NFILE_STREAMS > 0
      /* Handle redirection of output via a file descriptor */

      if (vtbl->np.np_redirect)
        {
          (void)nsh_redirect(bkgvtbl, fd, NULL);
        }
#endif

      /* Get the execution priority of this task */

      ret = sched_getparam(0, &param);
      if (ret != 0)
        {
          nsh_output(vtbl, g_fmtcmdfailed, cmd, "sched_getparm", NSH_ERRNO);
          nsh_releaseargs(args);
          nsh_release(bkgvtbl);
          goto errout;
        }

      /* Determine the priority to execute the command */

      if (vtbl->np.np_nice != 0)
        {
          int priority = param.sched_priority - vtbl->np.np_nice;
          if (vtbl->np.np_nice < 0)
            {
              int max_priority = sched_get_priority_max(SCHED_NSH);
              if (priority > max_priority)
                {
                  priority = max_priority;
                }
            }
          else
            {
              int min_priority = sched_get_priority_min(SCHED_NSH);
              if (priority < min_priority)
                {
                  priority = min_priority;
                }
            }

          param.sched_priority = priority;
        }

      /* Set up the thread attributes */

      (void)pthread_attr_init(&attr);
      (void)pthread_attr_setschedpolicy(&attr, SCHED_NSH);
      (void)pthread_attr_setschedparam(&attr, &param);

      /* Execute the command as a separate thread at the appropriate priority */

      ret = pthread_create(&thread, &attr, nsh_child, (pthread_addr_t)args);
      if (ret != 0)
        {
          nsh_output(vtbl, g_fmtcmdfailed, cmd, "pthread_create", NSH_ERRNO_OF(ret));
          nsh_releaseargs(args);
          nsh_release(bkgvtbl);
          goto errout;
        }

      /* Detach from the pthread since we are not going to join with it.
       * Otherwise, we would have a memory leak.
       */

      (void)pthread_detach(thread);

      nsh_output(vtbl, "%s [%d:%d]\n", cmd, thread, param.sched_priority);
    }
  else
#endif
    {
#if CONFIG_NFILE_STREAMS > 0
      uint8_t save[SAVE_SIZE];

      /* Handle redirection of output via a file descriptor */

      if (vtbl->np.np_redirect)
        {
          nsh_redirect(vtbl, fd, save);
        }
#endif

      /* Then execute the command in "foreground" -- i.e., while the user waits
       * for the next prompt.  nsh_execute will return:
       *
       * -1 (ERRROR) if the command was unsuccessful
       *  0 (OK)     if the command was successful
       */

      ret = nsh_execute(vtbl, argc, argv);

#if CONFIG_NFILE_STREAMS > 0
      /* Restore the original output.  Undirect will close the redirection
       * file descriptor.
       */

      if (vtbl->np.np_redirect)
        {
          nsh_undirect(vtbl, save);
        }
#endif

      /* Mark errors so that it is possible to test for non-zero return values
       * in nsh scripts.
       */

      if (ret < 0)
        {
          goto errout;
        }
    }

  /* Return success if the command succeeded (or at least, starting of the
   * command task succeeded).
   */

  return nsh_saveresult(vtbl, false);

#ifndef CONFIG_NSH_DISABLEBG
errout_with_redirect:
#if CONFIG_NFILE_STREAMS > 0
  if (vtbl->np.np_redirect)
    {
      close(fd);
    }
#endif
#endif

errout:
  return nsh_saveresult(vtbl, true);
}
示例#4
0
/* 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 (ret != 0)  {  output("WARNING: The TPS option is claimed to be supported but setscope fails\n");  }
				
			#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)  {  
                                    fprintf(stderr, "test %d\n", i);
                                    UNRESOLVED(errno,"Unable to alloc enough memory for alternative stack"); 
                                }
				
				ret = pthread_attr_setstack(&scenarii[i].ta, scenarii[i].bottom, minstacksize);

				if (ret != 0)  {  
                                    fprintf(stderr, "test %d pthread_attr_setstack did not return 0, returned %d\n", i, ret);
                                    UNRESOLVED(ret, "Failed to specify alternate stack, pthread_attr_setstack");  
                                }
			
				#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)  {  
                            fprintf(stderr, "test %d, ret %d\n", i, ret);
                            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)  {  
                                    fprintf(stderr, "test %d, ret %d\n", i, ret);
                                    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
}