pthread_cond_timedwait

Waits on the condition variable for a limited time.

Library:LibC
Classification:POSIX
Service:Synchronization

Syntax

  #include <pthread.h>
   
  int   pthread_cond_timedwait (
     pthread_cond_t          *cond,
     pthread_mutex_t         *mutex,
     const struct timespec   *abstime);
  

Parameters

cond

(IN) Points to the condition variable to wait on.

mutex

(IN) Points to a mutually exclusive lock held by the calling thread.

abstime

(IN) Specifies the maximum amount of time you are willing to wait. See timespec_t, which is also called timespec and timestrc_t.

Return Values

If successful, returns zero; otherwise, returns a nonzero error code:

Decimal

Constant

Description

9

EINVAL

The cond parameter is invalid.

60

ETIMEDOUT

The specified time interval expired.

Remarks

The pthread_cond_timedwait function atomically releases the entry lock pointed to by mutex and blocks the calling thread. When the condition is signaled, pthread_cond_timedwait returns.

It is the responsibility of the calling thread to re-evaluate the entry condition when pthread_cond_timedwait returns. The condition might have been consumed by a different thread between the time when a blocked thread within pthread_cond_timedwait awakens.

Two cases are viewed as errors:

  • The wait can be interrupted, in which case pthread_cond_timedwait returns EINTR.

  • If the condition is not signaled (with respect to the calling thread) within the specified interval (measured from the point of blocking), the interval expires and pthread_cond_timedwait returns ETIMEDOUT.

The abstime parameter, prior to NetWare 6.5 and Consolidated Support Pack 10 (Support Pack 7 for NetWare 5.1 and Support Pack 4 for NetWare 6.0), is a duration parameter. In other words, you specify that you want to wait 5 seconds. In the newer versions (NetWare 6.5 and CSP 10), you obtain the current UTC time, add your wait interval, and pass this combined time as the absolute time. This change from duration to absolute time is required to make pthread_cond_timedwait comply with the POSIX specification.

NLM applications written using the duration semantic will still run on LibC with the absolute time semantic. LibC checks the value of the abstime parameter. If the value is less than 20 years (before January 1, 1991), it is assumed to be a duration value. Otherwise, it is considered to be an absolute time.

See Also

Example

The following sample illustrates how to code pthread_cond_timedwait so that it functions correctly on both old LibC versions that accept a duration value and newer versions that accept an absolute time value.

  #include <time.h>
  #include <library.h>
  #include <ndkvers.h>
  #include <netware.h>
  #include <pthread.h>
  
  
  int   xvers_pthread_cond_timedwait( pthread_cond_t *, 
                       pthread_mutex_t *, const struct timespec * );
  
  int xvers_pthread_cond_timedwait
  (
     pthread_cond_t          *cvp,
     pthread_mutex_t         *mp,
     const struct timespec   *abstime //do NOT call this with a duration!
  )
  {
     struct timespec          duration;
     const struct timespec   *timer;
     static int               once_thru = FALSE, 
                     always_use_duration = FALSE;
  
     if (!once_thru)
     {
        int    (*libcthreshold)( int, int * );
        void   *handle = getnlmhandle();
  
        /*
        ** If we can’t get this symbol, LibC is too old to have the 
        ** corrected, absolute time semantic for
        ** pthread_cond_timedwait().
        */
        libcthreshold = ImportPublicObject(handle, "libcthreshold");
  
        if (!libcthreshold)
        {
           always_use_duration = TRUE;
        }
        else
        {
           int   threshold;
  
           (void) (*libcthreshold)(0, &threshold);
  
           always_use_duration = (threshold < NETWARE_65_FCS);
        }
  
        UnImportPublicObject(handle, "libcthreshold");
  
        once_thru = TRUE;
     }
  
     if (always_use_duration)
     {
  // convert this absolute time into duration for use on older LibCs...
        timer            = &duration;
        duration.tv_sec  = time(NULL) + abstime->tv_sec;
        duration.tv_nsec = abstime->tv_nsec;
     }
     else
     {
        timer = abstime;
     }
  
     return pthread_cond_timedwait(cvp, mp, timer);
  }