pthread_cond_timedwait() : time internal resolution issue.

Thomas Kim thomas73.kim at gmail.com
Mon Aug 14 05:19:37 UTC 2017


Dear Sir,

I am testing pthread_cond_timedwait() on realview-pbx-a9 using QEMU.

My testing code is below;

#include <pthread.h>
#include <time.h>
static void thread_sleep(struct timespec *ti)
{
    pthread_mutex_t mtx;
    pthread_cond_t cnd;
    pthread_mutex_init(&mtx, 0);
    pthread_cond_init(&cnd, 0);
    pthread_mutex_lock(&mtx);
    pthread_cond_timedwait(&cnd, &mtx, ti);
    pthread_mutex_unlock(&mtx);
    pthread_cond_destroy(&cnd);
    pthread_mutex_destroy(&mtx);
}
void own_msleep(unsigned long msecs)
{
    struct timeval tv;
    gettimeofday(&tv, 0);
    struct timespec ti;
    ti.tv_nsec = (tv.tv_usec + (msecs % 1000) * 1000) * 1000;
    ti.tv_sec = tv.tv_sec + (msecs / 1000) + (ti.tv_nsec / 1000000000);
    ti.tv_nsec %= 1000000000;
    thread_sleep(&ti);
}
#define TEST_1 0
#define TEST_2 0
#define TEST_3 0
#define TEST_4 1
int main(int argc, char *argv[])
{
 int count = 0;

 while (1)
 {
#if TEST_1 // Normal operation. total time for count 100 = almost 1 second.
  own_msleep(10);
  count++;
  if (count%100 == 0) printf("count=%d\n", count);
#endif
#if TEST_2 // Abnormal operation. total time for count 200 = about 2
seconds.
  own_msleep(5);
  count++;
  if (count%200 == 0) printf("count=%d\n", count);
#endif
#if TEST_3 // Abnormal operation. total time for count 500 = about 5
seconds.
  own_msleep(2);
  count++;
  if (count%500 == 0) printf("count=%d\n", count);
#endif
#if TEST_4 // Abnormal operation. total time for count 1000 = about 10
seconds.
  own_msleep(1);
  count++;
  if (count%1000 == 0) printf("count=%d\n", count);
#endif
 }
 return 0;
}

As you see in my testing code, I called own_msleep() function using pthread
mutex, condition variable API.

As my result, when I use own_msleep(10), there is not any problem for this.
But, when I decrease time internal from 10ms to (5 or 2 or 1)ms, msleep()
have addtional delay.

As I know from BSP code for realview-pbx-a9, OS timer interval is 1ms.

Is it correct behaivor ?
Is there any limitation for using short time interval ?

Please advise me.

Best Regards,
Thomas Kim.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rtems.org/pipermail/users/attachments/20170814/7a687f33/attachment.html>


More information about the users mailing list