source: rtems/cpukit/posix/src/semtimedwait.c @ 1de949a8

4.104.11
Last change on this file since 1de949a8 was 1de949a8, checked in by Ralf Corsepius <ralf.corsepius@…>, on Nov 30, 2009 at 3:49:52 PM

Whitespace removal.

  • 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  bool                                         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   *  If the status is POSIX_ABSOLUTE_TIMEOUT_INVALID,
57   *  POSIX_ABSOLUTE_TIMEOUT_IS_IN_PAST, or POSIX_ABSOLUTE_TIMEOUT_IS_NOW,
58   *  then we should not wait.
59   */
60  status = _POSIX_Absolute_timeout_to_ticks( abstime, &ticks );
61  if ( status != POSIX_ABSOLUTE_TIMEOUT_IS_IN_FUTURE )
62    do_wait = false;
63
64  lock_status = _POSIX_Semaphore_Wait_support( sem, do_wait, ticks );
65
66  /*
67   *  This service only gives us the option to block.  We used a polling
68   *  attempt to obtain if the abstime was not in the future.  If we did
69   *  not obtain the semaphore, then not look at the status immediately,
70   *  make sure the right reason is returned.
71   */
72  if ( !do_wait && (lock_status == EBUSY) ) {
73    switch (lock_status) {
74      case POSIX_ABSOLUTE_TIMEOUT_INVALID:
75        return EINVAL;
76      case POSIX_ABSOLUTE_TIMEOUT_IS_IN_PAST:
77      case POSIX_ABSOLUTE_TIMEOUT_IS_NOW:
78        return ETIMEDOUT;
79      case POSIX_ABSOLUTE_TIMEOUT_IS_IN_FUTURE:
80        break;
81    }
82  }
83
84  return lock_status;
85}
Note: See TracBrowser for help on using the repository browser.