source: rtems/cpukit/posix/src/condwaitsupp.c @ c838e2f4

4.11
Last change on this file since c838e2f4 was c838e2f4, checked in by Joel Sherrill <joel.sherrill@…>, on Jul 28, 2010 at 8:39:48 PM

2010-07-28 Vinu Rajashekhar <vinutheraj@…>

  • posix/src/condinit.c, posix/src/condwaitsupp.c, posix/src/psignalunblockthread.c: Clean up some signal interruption code.
  • Property mode set to 100644
File size: 3.1 KB
Line 
1/*
2 *  COPYRIGHT (c) 1989-2007.
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 <pthread.h>
17#include <errno.h>
18
19#include <rtems/system.h>
20#include <rtems/score/object.h>
21#include <rtems/score/states.h>
22#include <rtems/score/watchdog.h>
23#include <rtems/posix/cond.h>
24#include <rtems/posix/time.h>
25#include <rtems/posix/mutex.h>
26
27/*PAGE
28 *
29 *  _POSIX_Condition_variables_Wait_support
30 *
31 *  A support routine which implements guts of the blocking, non-blocking, and
32 *  timed wait version of condition variable wait routines.
33 */
34
35int _POSIX_Condition_variables_Wait_support(
36  pthread_cond_t            *cond,
37  pthread_mutex_t           *mutex,
38  Watchdog_Interval          timeout,
39  bool                       already_timedout
40)
41{
42  register POSIX_Condition_variables_Control *the_cond;
43  Objects_Locations                           location;
44  int                                         status;
45  int                                         mutex_status;
46
47  if ( !_POSIX_Mutex_Get( mutex, &location ) ) {
48     return EINVAL;
49  }
50
51  _Thread_Unnest_dispatch();
52
53  the_cond = _POSIX_Condition_variables_Get( cond, &location );
54  switch ( location ) {
55
56    case OBJECTS_LOCAL:
57
58      if ( the_cond->Mutex && ( the_cond->Mutex != *mutex ) ) {
59        _Thread_Enable_dispatch();
60        return EINVAL;
61      }
62
63      (void) pthread_mutex_unlock( mutex );
64/* XXX ignore this for now  since behavior is undefined
65      if ( mutex_status ) {
66        _Thread_Enable_dispatch();
67        return EINVAL;
68      }
69*/
70
71      if ( !already_timedout ) {
72        the_cond->Mutex = *mutex;
73
74        _Thread_queue_Enter_critical_section( &the_cond->Wait_queue );
75        _Thread_Executing->Wait.return_code = 0;
76        _Thread_Executing->Wait.queue       = &the_cond->Wait_queue;
77        _Thread_Executing->Wait.id          = *cond;
78
79        _Thread_queue_Enqueue( &the_cond->Wait_queue, timeout );
80
81        _Thread_Enable_dispatch();
82
83        /*
84         *  Switch ourself out because we blocked as a result of the
85         *  _Thread_queue_Enqueue.
86         */
87
88        /*
89         *  If the thread is interrupted, while in the thread queue, by
90         *  a POSIX signal, then pthread_cond_wait returns spuriously,
91         *  according to the POSIX standard. It means that pthread_cond_wait
92         *  returns a success status, except for the fact that it was not
93         *  woken up a pthread_cond_signal or a pthread_cond_broadcast.
94         */
95        status = _Thread_Executing->Wait.return_code;
96        if ( status == EINTR )
97          status = 0;
98
99      } else {
100        _Thread_Enable_dispatch();
101        status = ETIMEDOUT;
102      }
103
104      /*
105       *  When we get here the dispatch disable level is 0.
106       */
107
108      mutex_status = pthread_mutex_lock( mutex );
109      if ( mutex_status )
110        return EINVAL;
111
112      return status;
113
114#if defined(RTEMS_MULTIPROCESSING)
115    case OBJECTS_REMOTE:
116#endif
117    case OBJECTS_ERROR:
118      break;
119  }
120
121  return EINVAL;
122}
Note: See TracBrowser for help on using the repository browser.