summaryrefslogtreecommitdiff
path: root/cpukit/posix/src/condwaitsupp.c
blob: b759ee54b8c456904bf9bb91e73dfc77a5bf2dd8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
 * @file
 *
 * @brief POSIX Condition Variables Wait Support
 * @ingroup POSIX_COND_VARS
 */

/*
 *  COPYRIGHT (c) 1989-2014.
 *  On-Line Applications Research Corporation (OAR).
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.org/license/LICENSE.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <rtems/posix/condimpl.h>
#include <rtems/posix/posixapi.h>
#include <rtems/score/assert.h>
#include <rtems/score/statesimpl.h>
#include <rtems/score/status.h>
#include <rtems/score/threaddispatch.h>

static void _POSIX_Condition_variables_Mutex_unlock(
  Thread_queue_Queue   *queue,
  Thread_Control       *the_thread,
  Thread_queue_Context *queue_context
)
{
  POSIX_Condition_variables_Control *the_cond;
  int                                mutex_error;

  the_cond = POSIX_CONDITION_VARIABLE_OF_THREAD_QUEUE_QUEUE( queue );

  mutex_error = pthread_mutex_unlock( the_cond->mutex );
  if ( mutex_error != 0 ) {
    /*
     *  Historically, we ignored the unlock status since the behavior
     *  is undefined by POSIX. But GNU/Linux returns EPERM in this
     *  case, so we follow their lead.
     */
    _Assert( mutex_error == EINVAL || mutex_error == EPERM );
    _Thread_Continue( the_thread, STATUS_NOT_OWNER );
  }
}

static void _POSIX_Condition_variables_Enqueue_no_timeout(
  Thread_queue_Queue   *queue,
  Thread_Control       *the_thread,
  Per_CPU_Control      *cpu_self,
  Thread_queue_Context *queue_context
)
{
  _POSIX_Condition_variables_Mutex_unlock( queue, the_thread, queue_context );
}

static void _POSIX_Condition_variables_Enqueue_with_timeout_monotonic(
  Thread_queue_Queue   *queue,
  Thread_Control       *the_thread,
  Per_CPU_Control      *cpu_self,
  Thread_queue_Context *queue_context
)
{
  _Thread_queue_Add_timeout_monotonic_timespec(
    queue,
    the_thread,
    cpu_self,
    queue_context
  );
  _POSIX_Condition_variables_Mutex_unlock( queue, the_thread, queue_context );
}

static void _POSIX_Condition_variables_Enqueue_with_timeout_realtime(
  Thread_queue_Queue   *queue,
  Thread_Control       *the_thread,
  Per_CPU_Control      *cpu_self,
  Thread_queue_Context *queue_context
)
{
  _Thread_queue_Add_timeout_realtime_timespec(
    queue,
    the_thread,
    cpu_self,
    queue_context
  );
  _POSIX_Condition_variables_Mutex_unlock( queue, the_thread, queue_context );
}

int _POSIX_Condition_variables_Wait_support(
  pthread_cond_t            *cond,
  pthread_mutex_t           *mutex,
  const struct timespec     *abstime
)
{
  POSIX_Condition_variables_Control *the_cond;
  unsigned long                      flags;
  Thread_queue_Context               queue_context;
  int                                error;
  Thread_Control                    *executing;

  the_cond = _POSIX_Condition_variables_Get( cond );
  POSIX_CONDITION_VARIABLES_VALIDATE_OBJECT( the_cond, flags );

  _Thread_queue_Context_initialize( &queue_context );

  if ( abstime != NULL ) {
    _Thread_queue_Context_set_timeout_argument( &queue_context, abstime );

    if ( _POSIX_Condition_variables_Get_clock( flags ) == CLOCK_MONOTONIC ) {
      _Thread_queue_Context_set_enqueue_callout(
        &queue_context,
        _POSIX_Condition_variables_Enqueue_with_timeout_monotonic
      );
    } else {
      _Thread_queue_Context_set_enqueue_callout(
        &queue_context,
        _POSIX_Condition_variables_Enqueue_with_timeout_realtime
      );
    }
  } else {
    _Thread_queue_Context_set_enqueue_callout(
      &queue_context,
      _POSIX_Condition_variables_Enqueue_no_timeout
    );
  }

  executing = _POSIX_Condition_variables_Acquire( the_cond, &queue_context );

  if (
    the_cond->mutex != POSIX_CONDITION_VARIABLES_NO_MUTEX
      && the_cond->mutex != mutex
  ) {
    _POSIX_Condition_variables_Release( the_cond, &queue_context );
    return EINVAL;
  }

  the_cond->mutex = mutex;

  _Thread_queue_Context_set_thread_state(
    &queue_context,
    STATES_WAITING_FOR_CONDITION_VARIABLE
  );
  _Thread_queue_Enqueue(
    &the_cond->Queue.Queue,
    POSIX_CONDITION_VARIABLES_TQ_OPERATIONS,
    executing,
    &queue_context
  );
  error = _POSIX_Get_error_after_wait( executing );

  /*
   *  If the thread is interrupted, while in the thread queue, by
   *  a POSIX signal, then pthread_cond_wait returns spuriously,
   *  according to the POSIX standard. It means that pthread_cond_wait
   *  returns a success status, except for the fact that it was not
   *  woken up a pthread_cond_signal() or a pthread_cond_broadcast().
   */

  if ( error == EINTR ) {
    error = 0;
  }

  /*
   *  When we get here the dispatch disable level is 0.
   */

  if ( error != EPERM ) {
    int mutex_error;

    mutex_error = pthread_mutex_lock( mutex );
    if ( mutex_error != 0 ) {
      _Assert( mutex_error == EINVAL );
      error = EINVAL;
    }
  }

  return error;
}