I have two threads. In each thread I use clock_gettime to measure how long a data transfer takes. (The data transfers take anything from 2 to 10 seconds) If only one thread is running then i get a sensible value. If both threads are running I start getting unrealistic values. What could cause this?
The function is documented as thread safe, so it should work. Either you are using it wrong (could you post some code) or there is a bug in the library.
I wrote a small test case to reproduce the problem (as you suggested) and it works fine, not a problem in sight. So… it’s not a problem with the PowerPC or with the library and it’s very probably something in our code which is causing the problem. Unfortunately I’ve no idea what at the moment…
When the problem occurs it looks like the clock is running too slowly => Is it possible for something in the code to affect the operation of the clock? I presume the clock always has the highest priority?
On x86 clock is managed through interrupt 0, it could be slow down if interrupt of higher priority takes too much CPU( the machine would appear to freeze though) or interrupt are disabled for long time.
On PowerPC I don’t know, but it must be hardware based. Again, build a small test case, or better run the test program you already wrote while your real application runs. If the test program still behave properly while your application is not then you know it’s not a problem with the system clock.
I noticed you used fprintf(stderr). Although fprintf is thread safe, the file descriptor isn’t, hence in theory you should protected usage of a file descriptor. Probably not related to your problem though.
After much testing, checking HW-Specs and a few emails with QNX i am now a little wiser
The CLOCK_REALTIME on the MGT5200 is generated from the Decrementer in the core. Unfortunately the decrementer interrupt has a lower priority than the external interrupt. The load on the external interrupt was so high that the interrupt from the decrementer wasn’t always serviced and hence the CLOCK_REALTIME ran slowly.
For anyone who has the same problem:
The problem was solved by writing a routine in assembler which reads directly from the time base register in the core. This routine was then used instead of clock_gettime(…)