source: rtems/cpukit/posix/src/semtimedwait.c @ 6a0898b

4.104.114.95
Last change on this file since 6a0898b was 6a0898b, checked in by Joel Sherrill <joel.sherrill@…>, on 07/18/08 at 18:45:56

2008-07-18 Joel Sherrill <joel.sherrill@…>

PR 1291/cpukit

  • itron/inline/rtems/itron/semaphore.inl, itron/src/twai_sem.c, posix/include/mqueue.h, posix/include/rtems/posix/mqueue.h, posix/include/rtems/posix/semaphore.h, posix/include/rtems/posix/time.h, posix/src/condtimedwait.c, posix/src/mqueuereceive.c, posix/src/mqueuerecvsupp.c, posix/src/mqueuesend.c, posix/src/mqueuesendsupp.c, posix/src/mqueuetimedreceive.c, posix/src/mqueuetimedsend.c, posix/src/mutextimedlock.c, posix/src/mutextranslatereturncode.c, posix/src/posixtimespecabsolutetimeout.c, posix/src/prwlocktimedrdlock.c, posix/src/prwlocktimedwrlock.c, posix/src/semaphoretranslatereturncode.c, posix/src/semaphorewaitsupp.c, posix/src/semtimedwait.c, posix/src/semtrywait.c, posix/src/semwait.c, posix/src/sigtimedwait.c, posix/src/timersettime.c, posix/src/ualarm.c, rtems/src/semobtain.c, rtems/src/semtranslatereturncode.c, score/include/rtems/score/coremutex.h, score/include/rtems/score/coresem.h, score/src/coresemseize.c: This patch addresses issues on implementation of the timeout on the following POSIX services. Some of these services incorrectly took a timeout as a relative time. Others would compute a 0 delta to timeout if the absolute time and the current time were equal and thus incorrectly block the caller forever. The root of the confusion is that POSIX specifies that if the timeout is incorrect (e.g. in the past, is now, or is numerically invalid), that it does not matter if the call would succeed without blocking. This is in contrast to RTEMS programming style where all errors are checked before any critical sections are entered. This fix implemented a more uniform way of handling POSIX absolute time timeouts.

+ pthread_cond_timedwait - could block forever
+ mq_timedreceive - used relative not absolute time
+ mq_timedsend - used relative not absolute time
+ pthread_mutex_timedlock - used relative not absolute time
+ pthread_rwlock_timedrdlock- used relative not absolute time
+ pthread_rwlock_timedwrlock- used relative not absolute time
+ sem_timedwait - could block forever

  • Property mode set to 100644
File size: 2.4 KB
Line 
1/*
2 *  COPYRIGHT (c) 1989-2008.
3 *  On-Line Applications Research Corporation (OAR).
4 *
5 *  The license and distribution terms for this file may be
6 *  found in the file LICENSE in this distribution or at
7 *  http://www.rtems.com/license/LICENSE.
8 *
9 *  $Id$
10 */
11
12#if HAVE_CONFIG_H
13#include "config.h"
14#endif
15
16#include <stdarg.h>
17
18#include <errno.h>
19#include <fcntl.h>
20#include <pthread.h>
21#include <semaphore.h>
22#include <limits.h>
23
24#include <rtems/system.h>
25#include <rtems/score/object.h>
26#include <rtems/posix/semaphore.h>
27#include <rtems/posix/time.h>
28#include <rtems/seterr.h>
29
30/*PAGE
31 *
32 *  11.2.6 Lock a Semaphore, P1003.1b-1993, p.226
33 *
34 *  NOTE: P1003.4b/D8 adds sem_timedwait(), p. 27
35 */
36
37int sem_timedwait(
38  sem_t                 *sem,
39  const struct timespec *abstime
40)
41{
42  Watchdog_Interval                            ticks;
43  boolean                                      do_wait = TRUE;
44  POSIX_Absolute_timeout_conversion_results_t  status;
45  int                                          lock_status;
46
47  /*
48   *  POSIX requires that blocking calls with timeouts that take
49   *  an absolute timeout must ignore issues with the absolute
50   *  time provided if the operation would otherwise succeed.
51   *  So we check the abstime provided, and hold on to whether it
52   *  is valid or not.  If it isn't correct and in the future,
53   *  then we do a polling operation and convert the UNSATISFIED
54   *  status into the appropriate error.
55   */
56  status = _POSIX_Absolute_timeout_to_ticks( abstime, &ticks );
57  switch ( status ) {
58    case POSIX_ABSOLUTE_TIMEOUT_INVALID:
59    case POSIX_ABSOLUTE_TIMEOUT_IS_IN_PAST:
60    case POSIX_ABSOLUTE_TIMEOUT_IS_NOW:
61      do_wait = FALSE;
62      break;
63    case POSIX_ABSOLUTE_TIMEOUT_IS_IN_FUTURE:
64      do_wait = TRUE;
65      break;
66  }
67
68  lock_status = _POSIX_Semaphore_Wait_support( sem, do_wait, ticks );
69
70  /*
71   *  This service only gives us the option to block.  We used a polling
72   *  attempt to obtain if the abstime was not in the future.  If we did
73   *  not obtain the semaphore, then not look at the status immediately,
74   *  make sure the right reason is returned.
75   */
76  if ( !do_wait && (lock_status == EBUSY) ) {
77    switch (lock_status) {
78      case POSIX_ABSOLUTE_TIMEOUT_INVALID:
79        return EINVAL;
80      case POSIX_ABSOLUTE_TIMEOUT_IS_IN_PAST:
81      case POSIX_ABSOLUTE_TIMEOUT_IS_NOW:
82        return ETIMEDOUT;
83      case POSIX_ABSOLUTE_TIMEOUT_IS_IN_FUTURE:
84        break;
85    }
86  }
87
88  return lock_status;
89}
Note: See TracBrowser for help on using the repository browser.