[pjsip] pj_thread_sleep(0) problem in linux platform issue

Benny Prijono bennylp at pjsip.org
Wed May 21 06:59:27 EDT 2008


On Wed, May 21, 2008 at 3:41 AM, Simon Chen <simonmychen at seed.net.tw> wrote:
> Hello!
>
>    pj_thread_sleep(0) is used for forced task switch sometimes. It can work
> find in windows platform. But in  linux platform, pj_thread_sleep(0) will
> sleep 1 tick. This will effect the performance of the thread.  I modify the
> pj_thread_sleep in os_core_uinx.c. When msec equals 0, call sched_yield().
> It can work find in my test. How about this modification for the pjlib? any
> suggestion? Thanks!
>

Can you explain more about which particular pj_thread_sleep(0) that
gives poor performance? I grep-ed the source for pj_thread_sleep(0)
and I found three places where it's used, and all I think are for
corner cases (like waiting for deadlock resolution) where I don't
think pj_thread_sleep(1) and sched_yield() will make big difference in
performance. But I could be wrong, hence it'll be great if you could
explain more.

The reservation that I have against sched_yield() is it seems that
this syscall is less well specified (see [1]), so considering that
application may create threads with different priorities and also the
process itself may be set to particular scheduling policy, it may not
achieve what we want (not that I know for sure what do we really want
in the first place with sleep(0), but it's something like "let all
other ready threads run then get back to me").

Cheers
 Benny

[1] http://lkml.org/lkml/2007/9/19/328



> PJ_DEF(pj_status_t) pj_thread_sleep(unsigned msec)
> {
> /* TODO: should change this to something like PJ_OS_HAS_NANOSLEEP */
> #if defined(PJ_RTEMS) && PJ_RTEMS!=0
>    enum { NANOSEC_PER_MSEC = 1000000 };
>    struct timespec req;
>
>    PJ_CHECK_STACK();
>
> // modification start -----------
> if( msec == 0 )
>  {
>  sched_yield();
>  return PJ_SUCCESS;
>  }
> // modification  end -----------
>    req.tv_sec = msec / 1000;
>    req.tv_nsec = (msec % 1000) * NANOSEC_PER_MSEC;
>
>    if (nanosleep(&req, NULL) == 0)
>  return PJ_SUCCESS;
>
>    return PJ_RETURN_OS_ERROR(pj_get_native_os_error());
> #else
>    PJ_CHECK_STACK();
>
> // modification  start -----------
> if( msec == 0 )
>  {
>  sched_yield();
>  return PJ_SUCCESS;
>  }
> // modification  end -----------
>    pj_set_os_error(0);
>
>    usleep(msec * 1000);
>
>    return pj_get_os_error();
> ;
> #endif /* PJ_RTEMS */
> }
>
>
> Simonmy
>
>




More information about the pjsip mailing list