summaryrefslogtreecommitdiffstats
path: root/cpukit/score/src/threadqtimeout.c
blob: 3f052fcf6f30ac36bf05159119d137054b879dae (plain) (blame)
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
/*
 * Copyright (c) 2016, 2017 embedded brains GmbH
 *
 * 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.
 */

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

#include <rtems/score/threadqimpl.h>
#include <rtems/score/threadimpl.h>
#include <rtems/score/watchdogimpl.h>

void _Thread_queue_Add_timeout_ticks(
  Thread_queue_Queue   *queue,
  Thread_Control       *the_thread,
  Per_CPU_Control      *cpu_self,
  Thread_queue_Context *queue_context
)
{
  Watchdog_Interval ticks;

  ticks = queue_context->Timeout.ticks;

  if ( ticks != WATCHDOG_NO_TIMEOUT ) {
    _Thread_Add_timeout_ticks(
      the_thread,
      cpu_self,
      queue_context->Timeout.ticks
    );
  }
}

static bool _Thread_queue_Lazy_insert_monotonic_timespec(
  Thread_Control        *the_thread,
  Per_CPU_Control       *cpu_self,
  const struct timespec *abstime
)
{
  uint64_t         expire;
  ISR_lock_Context lock_context;
  bool             insert;

  if ( abstime->tv_sec < 0 ) {
    expire = 0;
  } else if ( _Watchdog_Is_far_future_monotonic_timespec( abstime ) ) {
    expire = WATCHDOG_MAXIMUM_TICKS;
  } else {
    expire = _Watchdog_Monotonic_from_timespec( abstime );
  }

  _ISR_lock_ISR_disable_and_acquire(
    &the_thread->Timer.Lock,
    &lock_context
  );

  the_thread->Timer.header =
    &cpu_self->Watchdog.Header[ PER_CPU_WATCHDOG_MONOTONIC ];
  the_thread->Timer.Watchdog.routine = _Thread_Timeout;
  insert = _Watchdog_Per_CPU_lazy_insert_monotonic(
    &the_thread->Timer.Watchdog,
    cpu_self,
    expire
  );

  _ISR_lock_Release_and_ISR_enable(
    &the_thread->Timer.Lock,
    &lock_context
  );
  return insert;
}

void _Thread_queue_Add_timeout_monotonic_timespec(
  Thread_queue_Queue   *queue,
  Thread_Control       *the_thread,
  Per_CPU_Control      *cpu_self,
  Thread_queue_Context *queue_context
)
{
  const struct timespec *abstime;

  abstime = queue_context->Timeout.arg;

  if ( _Watchdog_Is_valid_timespec( abstime ) ) {
    if (
      !_Thread_queue_Lazy_insert_monotonic_timespec(
        the_thread,
        cpu_self,
        abstime
      )
    ) {
      _Thread_Continue( the_thread, STATUS_TIMEOUT );
    }
  } else {
    _Thread_Continue( the_thread, STATUS_INVALID_NUMBER );
  }
}

void _Thread_queue_Add_timeout_realtime_timespec(
  Thread_queue_Queue   *queue,
  Thread_Control       *the_thread,
  Per_CPU_Control      *cpu_self,
  Thread_queue_Context *queue_context
)
{
  const struct timespec *abstime;

  abstime = queue_context->Timeout.arg;

  if ( _Watchdog_Is_valid_timespec( abstime ) ) {
    uint64_t        expire;
    struct timespec now;

    if ( abstime->tv_sec < 0 ) {
      expire = 0;
    } else if ( _Watchdog_Is_far_future_realtime_timespec( abstime ) ) {
      expire = WATCHDOG_MAXIMUM_TICKS;
    } else {
      expire = _Watchdog_Realtime_from_timespec( abstime );
    }

    _Timecounter_Getnanotime( &now );

    if ( expire > _Watchdog_Realtime_from_timespec( &now ) ) {
      ISR_lock_Context lock_context;

      _ISR_lock_ISR_disable_and_acquire(
        &the_thread->Timer.Lock,
        &lock_context
      );

      the_thread->Timer.header =
        &cpu_self->Watchdog.Header[ PER_CPU_WATCHDOG_REALTIME ];
      the_thread->Timer.Watchdog.routine = _Thread_Timeout;
      _Watchdog_Per_CPU_insert_realtime(
        &the_thread->Timer.Watchdog,
        cpu_self,
        expire
      );

      _ISR_lock_Release_and_ISR_enable(
        &the_thread->Timer.Lock,
        &lock_context
      );
    } else {
      _Thread_Continue( the_thread, STATUS_TIMEOUT );
    }
  } else {
    _Thread_Continue( the_thread, STATUS_INVALID_NUMBER );
  }
}