Project Home
Project Home
Documents
Documents
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
Forum Topic - QNX CPU affinity: (2 Items)
   
QNX CPU affinity  
Hi folks,

I got a question regarding how to implement CPY affinity in QNX. I want to port some Linux code to QNX, and in my Linux 
code I use pthread_setaffinity_np(), which sets some given cpu_set_t to a specific thread, not the calling thread. 
Essentially, we create a thread from the calling thread and then we assign that newly created thread some specific CPU 
affinity.

I can see that in QNX we can use threadCtl() for the calling thread. Is there any way to do it for a specific thread, 
given its ID/name, and not just from the calling thread?

Thanks!
Victor.
Re: QNX CPU affinity  
Please see the documentation for DCMD_PROC_THREADCTL and _NTO_TCTL_RUNMASK:

Something like:
...
unsigned long runmask = 0x1ul; // run on first cpu only.
procfs_threadctl tctl;
tctl.tid=<tid>;
tctl.cmd=_NTO_TCTL_RUNMASK;
tctl.data=(void*)runmask;
int rc = devctl(fd, DCMD_PROC_THREADCTL, &tctl, sizeof(tctl), NULL);
...


But since you are creating the thread you could do an easier trick with
ThreadCtl by providing a setup thread routine. E.g.

...
struct th_setup_data
{
 void* (*start_routine)(void* );
 unsigned long runmask;
 void* realarg;  
};

static void *
th_setup_routine(void *tsdp)
{
  struct th_setup_data *tsp = tsdp;
  if (ThreadCtl(_NTO_TCL_RUNMASK, (void*)tsp->runmask) == -1) {
    /* Failed. Do something about it. */
    abort();
  }
  return (tsp->start_routine)(tsp->realarg);
}

...
struct th_setup_data tsd = { your_thread_start_routine, 0x1ul, your_arg };
pthread_t th;

if (pthread_create(&th,  &attr, th_setup_routine, &tsd) != EOK) {
  abort();
}
/* If tsd is a stack variable, sync. ht_setup_routine with this thread */
...

(Note: snippet above is just for illustration, I did not compile it).


HTH,
Aleksandar