source: rtems/cpukit/posix/src/mutextimedlock.c @ b1dbfd7

4.104.115
Last change on this file since b1dbfd7 was b1dbfd7, checked in by Ralf Corsepius <ralf.corsepius@…>, on 02/03/09 at 10:10:57

Eliminate TRUE/FALSE.

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