ThreadCtl(), ThreadCtl_r()

Control a thread

Synopsis:

#include <sys/neutrino.h>

int ThreadCtl( int cmd, 
               void * data );

int ThreadCtl_r( int cmd, 
                 void * data );

Arguments:

cmd
The command you want to execute; one of the following:

For more information, see below.

data
A pointer to data associated with the specific command; see below.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

These kernel calls allow you to make OS-specific changes to a thread.

The ThreadCtl() and ThreadCtl_r() functions are identical except in the way they indicate errors. See the Returns section for details.

The sections that follow describe the possible commands.

_NTO_TCTL_ALIGN_FAULT

ThreadCtl( _NTO_TCTL_ALIGN_FAULT, data );

This command controls the response to a misaligned access. The data argument must be a pointer to an int whose value indicates how you want to respond:

The function sets data to a positive or negative number, indicating the previous state of the alignment-fault handling.

Note:
  • Threads created by the calling thread inherit the _NTO_TCTL_ALIGN_FAULT status.
  • In QNX Neutrino 6.6 and later, on ARMv7 targets, the _NTO_TCTL_ALIGN_FAULT command is ignored; you can't control the alignment-fault behavior on a per-thread basis on these targets. You can set the global behavior by specifying the -ad or -ae option to procnto*:
    -ad
    Causes alignment faults for all threads.
    -ae
    Performs hardware misaligned accesses and causes alignment faults only for certain misaligned accesses that can't be performed by hardware (the default).

_NTO_TCTL_IO, _NTO_TCTL_IO_PRIV

ThreadCtl( _NTO_TCTL_IO, 0 );
ThreadCtl( _NTO_TCTL_IO_PRIV, 0 );

_NTO_TCTL_IO superlocks the process's memory (see "Locking memory" in the "Process Manager" chapter of the System Architecture guide) and requests I/O privileges.

I/O privileges let you: See:
Execute the in, ins, out, and outs I/O opcodes on x86 in16(), inbe16(), inle16(), in16s(), in32(), inbe32(), inle32(), in32s(), in8(), in8s(), out16(), outbe16(), outle16(), out16s(), out32(), outbe32(), outle32(), out32s(), out8(), out8s()
Attach or detach IRQs and interrupt events InterruptAttach(), InterruptAttachEvent(), InterruptDetach()
Disable and enable interrupts InterruptDisable(), InterruptEnable(), InterruptLock(), InterruptUnlock()
Mask or unmask interrupts InterruptMask(), InterruptUnmask()
Set the acceptable latency for an interrupt InterruptCharacteristic()
Add or delete event handlers TraceEvent()

_NTO_TCTL_IO_PRIV (QNX Neutrino 6.6 or later) lets you do everything that _NTO_TCTL_IO does, plus the following on ARMv7:

Note:
  • In order to use these commands, your process must have the PROCMGR_AID_IO ability enabled. For more information, see procmgr_ability().
  • Threads created by the calling thread inherit the _NTO_TCTL_IO or _NTO_TCTL_IO_PRIV status.

_NTO_TCTL_NAME

ThreadCtl( _NTO_TCTL_NAME, data );

(QNX Neutrino Core OS 6.3.2 or later) Set or retrieve the name of the current thread. The data argument must be a pointer to a _thread_name structure, which is defined as follows:

struct _thread_name {
    int     new_name_len;
    int     name_buf_len;
    char    name_buf[1];
};

The name_buf member is a contiguous buffer that extends the structure; name_buf_len is the size of this buffer.

If new_name_len is: The command:
Less than 0 Copies the thread's current name into name_buf, up to the number of bytes specified by name_buf_len
0 Deletes the thread's name
Greater than 0 Sets the thread's name to the value in name_buf, up to the number of bytes specified by name_buf_len

If you're setting or deleting the thread's name, the old name is copied as a NULL-terminated string into name_buf, up to the number of bytes specified by name_buf_len.

Note:

Here's an example:

#include <stdio.h>
#include <sys/neutrino.h>
#include <stdlib.h>

int main () {
    struct _thread_name *tname;
    int size;
    
    size = sizeof(*tname) * 2 + _NTO_THREAD_NAME_MAX * sizeof(char);
    tname = malloc (size);
    
    if (tname == NULL) {
        perror ("malloc");
        return EXIT_FAILURE;
    } else {
        memset (tname, 0x00, size);
        tname->name_buf_len = _NTO_THREAD_NAME_MAX;

        /* To change the name, put the name into name_buf and set
           new_name_len to the length of the new name. */

        strcpy (tname->name_buf, "Hello!");
        tname->new_name_len = strlen (tname->name_buf);

        if (ThreadCtl (_NTO_TCTL_NAME, tname) == -1) {
            perror ("ThreadCtl()");
            return EXIT_FAILURE;
        } else {
            printf ("The old name was: '%s'.\n", tname->name_buf);
        }

        /* To get the current name, set new_name_len to -1. */

        tname->new_name_len = -1;

        if (ThreadCtl (_NTO_TCTL_NAME, tname) == -1) {
            perror ("ThreadCtl()");
            return EXIT_FAILURE;
        } else {
            printf ("The current name is: '%s'.\n",
                    tname->name_buf);
        }

        /* To delete the name, set new_name_len to 0. */

        tname->new_name_len = 0;
        
        if (ThreadCtl (_NTO_TCTL_NAME, tname) == -1) {
            perror ("ThreadCtl()");
            return EXIT_FAILURE;
        } else {
            printf ("The old name was: '%s'.\n", tname->name_buf);
        }
        free (tname);
    }
    return EXIT_SUCCESS;
}

_NTO_TCTL_ONE_THREAD_CONT

ThreadCtl( _NTO_TCTL_ONE_THREAD_CONT, data );

(QNX Neutrino Core OS 6.3.2 or later) Unfreeze the thread with the given thread ID, which was frozen by an earlier _NTO_TCTL_ONE_THREAD_HOLD command. The data is the thread ID, cast to be a pointer (i.e. (void *) tid). This command returns an error of ESRCH if there's no thread with an ID of tid.

_NTO_TCTL_ONE_THREAD_HOLD

ThreadCtl( _NTO_TCTL_ONE_THREAD_HOLD, data );

(QNX Neutrino Core OS 6.3.2 or later) Hold the thread with the given thread ID in the calling process. The data is the thread ID, cast to be a pointer (i.e., (void *) tid). This command returns an error of ESRCH if there's no thread with an ID of tid.

_NTO_TCTL_RCM_GET_AND_SET

ThreadCtl( _NTO_TCTL_RCM_GET_AND_SET, data );

(QNX Neutrino 6.6 or later) Enter or leave resource-constrained mode. The data is a pointer to an integer; set the integer to 1 to entry constrained mode, or to 0 to leave it. The call stores the previous value in the memory pointed to by data:

int value = 1;
ThreadCtl(_NTO_TCTL_RCM_GET_AND_SET, &value); /* swaps current state with value */

/* Handle the request... */

ThreadCtl(_NTO_TCTL_RCM_GET_AND_SET, &value); /* restores original state */

For more information, see Resource constraint thresholds in the Processes chapter of the QNX Neutrino Programmer's Guide.

_NTO_TCTL_RUNMASK

ThreadCtl( _NTO_TCTL_RUNMASK, data );

Set the processor affinity for the calling thread in a multiprocessor system. The data is the runmask, cast to be a pointer (i.e., (void *) runmask). Each set bit in runmask represents a processor that the thread can run on.

By default, a thread's runmask is set to all ones, which allows it to run on any available processor. A value of 0x01 would, for example, force the thread to run only on the first processor. The new runmask takes effect immediately.

You can use _NTO_TCTL_RUNMASK to optimize the runtime performance of your system by, for example, relegating nonrealtime threads to a specific processor. In general, this shouldn't be necessary, since the QNX Neutrino realtime scheduler always preempts a lower-priority thread immediately when a higher priority thread becomes ready.

The main effect of processor locking is the effectiveness of the CPU cache, since threads can be prevented from migrating.

Note: Threads created by the calling thread don't inherit the specified runmask.

_NTO_TCTL_RUNMASK_GET_AND_SET

ThreadCtl( _NTO_TCTL_RUNMASK_GET_AND_SET, data );

(QNX Neutrino 6.3.0 SP2 or later) Get and set the runmask (the processor affinity) to a proper value for the calling thread in a multiprocessor system. The data parameter is a pointer to the runmask. On input, the pointer to value is used to set the new runmask for the thread (see _NTO_TCTL_SET_RUNMASK for details). The new runmask takes effect immediately. After the function has completed, the contents of *data will be replaced with the previous runmask for the thread. Calling ThreadCtl again with the same pointer will restore the runmask to the state before the call.

_NTO_TCTL_RUNMASK_GET_AND_SET_INHERIT

ThreadCtl( _NTO_TCTL_RUNMASK_GET_AND_SET_INHERIT,
data );

(QNX Neutrino Core OS 6.3.2 or later) Manipulate the calling thread's runmask (the processor affinity) and inherit mask (the runmask to use for the process's children). The data argument must be a pointer to a struct _thread_runmask:

struct _thread_runmask {
	int			size;
	/*	unsigned	runmask[size];		*/
	/*	unsigned	inherit_mask[size];	*/
};

The size of the masks (and hence the size of the structure) depends on the number of processors on your system. We've defined the following macros to make it easier for you to work with this structure:

RMSK_SIZE(num_cpu)
Determine the number of integers required to hold each mask, given the number of CPUs on your system (which you can find in _syspage_ptr->num_cpu).
RMSK_SET(cpu, p)
Set the bit for cpu (where cpu is zero-based) in the mask p.
RMSK_CLR(cpu, p)
Clear the bit for cpu (where cpu is zero-based) in the mask p.
RMSK_ISSET(cpu, p)
Determine the value of the bit for cpu in the mask p.

The p argument must be a pointer of type unsigned * to the appropriate mask. For more information about using these macros, see "Thread affinity" in the Developing Multicore Systems chapter of the Multicore Processing User's Guide.

The _NTO_TCTL_RUNMASK_GET_AND_SET_INHERIT command saves the values for both masks at the time of the call in their respective members of this structure. If you pass 0 for the masks, the masks are left unaltered; otherwise they're set to the specified value(s). The new runmasks take effect immediately.

Here's an example:

#include <sys/neutrino.h>
#include <sys/syspage.h>
#include <malloc.h>
#include <stdio.h>

int
main(void)
{
   int *rsizep, rsize, size_tot;
   unsigned *rmaskp, *inheritp;
   unsigned buf[8];
   void *freep;

   /*
    * struct _thread_runmask is not
    * uniquely sized, so we construct
    * our own.
    */
   
   rsize = RMSK_SIZE(_syspage_ptr->num_cpu);

   size_tot = sizeof(*rsizep);
   size_tot += sizeof(*rmaskp) * rsize;
   size_tot += sizeof(*inheritp) * rsize;

   if (size_tot <= sizeof(buf)) {
      rsizep = buf;
      freep = NULL;
   }
   else if ((rsizep = freep = malloc(size_tot)) == NULL) {
      perror("malloc");
      return 1;
   }

   memset(rsizep, 0x00, size_tot);

   *rsizep = rsize;
   rmaskp = (unsigned *)(rsizep + 1);
   inheritp = rmaskp + rsize;

   /*
    * Both masks set to 0 means get the current
    * values without alteration.
    */

   if (ThreadCtl(_NTO_TCTL_RUNMASK_GET_AND_SET_INHERIT,
                 rsizep) == -1) {
      perror("_NTO_TCTL_RUNMASK_GET_AND_SET_INHERIT");
      free(freep);
      return 1;
   }

   /*
    * Restrict our inherit mask to the last cpu; leave the
    * runmask unaltered.
    */
   
   memset(rsizep, 0x00, size_tot);
   *rsizep = rsize;
   RMSK_SET(_syspage_ptr->num_cpu - 1, inheritp);

   if (ThreadCtl(_NTO_TCTL_RUNMASK_GET_AND_SET_INHERIT,
                 rsizep) == -1) {
      perror("_NTO_TCTL_RUNMASK_GET_AND_SET_INHERIT");
      free(freep);
      return 1;
   }

   free(freep);
   return 0;
}

_NTO_TCTL_THREADS_CONT

ThreadCtl( _NTO_TCTL_THREADS_CONT, 0 );

Unfreeze all threads in the current process that were frozen using the _NTO_TCTL_THREADS_HOLD command.

_NTO_TCTL_THREADS_HOLD

ThreadCtl( _NTO_TCTL_THREADS_HOLD, 0 );

Hold all threads in the current process except the calling thread.

Note: Threads created by the calling thread aren't frozen.

Blocking states

STATE_WAITPAGE
The _NTO_TCTL_IO and _NTO_TCTL_IO_PRIV commands superlock memory, which generally puts the first thread to use these commands into STATE_WAITPAGE while physical memory is allocated.

These calls don't block for other _NTO_TCTL_* commands.

Returns:

The only difference between these functions is the way they indicate errors:

ThreadCtl()
If an error occurs, this function returns -1 and sets errno. Any other value returned indicates success.
ThreadCtl_r()
This function returns EOK on success. It doesn't set errno. If an error occurs, the function may return any value listed in the Errors section.

Errors:

E2BIG
The name is larger than the accepted size.
EINVAL
The name buffer length is invalid or smaller than the new name length, or the specified runmask is invalid.
EPERM
The calling process doesn't have the required permission; if you're using _NTO_TCTL_IO, see procmgr_ability().
ESRCH
There's no thread with an ID of tid.

Classification:

QNX Neutrino

Safety:  
Cancellation point No
Interrupt handler No
Signal handler Yes
Thread Yes