source: rtems/cpukit/posix/src/mutextimedlock.c @ 6805ac37

4.104.115
Last change on this file since 6805ac37 was 6805ac37, checked in by Joel Sherrill <joel.sherrill@…>, on 10/10/09 at 15:21:41

2009-10-10 Joel Sherrill <joel.sherrill@…>

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