1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * |
---|
6 | * @ingroup RTEMSScoreThreadQueue |
---|
7 | * |
---|
8 | * @brief This source file contains the implementation of |
---|
9 | * _Thread_queue_Add_timeout_ticks(), _Thread_queue_Add_timeout_timespec(), |
---|
10 | * _Thread_queue_Add_timeout_monotonic_timespec(), and |
---|
11 | * _Thread_queue_Add_timeout_realtime_timespec(). |
---|
12 | */ |
---|
13 | |
---|
14 | /* |
---|
15 | * Copyright (C) 2016, 2021 embedded brains GmbH & Co. KG |
---|
16 | * |
---|
17 | * Redistribution and use in source and binary forms, with or without |
---|
18 | * modification, are permitted provided that the following conditions |
---|
19 | * are met: |
---|
20 | * 1. Redistributions of source code must retain the above copyright |
---|
21 | * notice, this list of conditions and the following disclaimer. |
---|
22 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
23 | * notice, this list of conditions and the following disclaimer in the |
---|
24 | * documentation and/or other materials provided with the distribution. |
---|
25 | * |
---|
26 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
27 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
28 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
29 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
30 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
31 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
32 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
33 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
34 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
35 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
36 | * POSSIBILITY OF SUCH DAMAGE. |
---|
37 | */ |
---|
38 | |
---|
39 | #ifdef HAVE_CONFIG_H |
---|
40 | #include "config.h" |
---|
41 | #endif |
---|
42 | |
---|
43 | #include <rtems/score/threadqimpl.h> |
---|
44 | #include <rtems/score/threadimpl.h> |
---|
45 | #include <rtems/score/watchdogimpl.h> |
---|
46 | |
---|
47 | void _Thread_queue_Add_timeout_ticks( |
---|
48 | Thread_queue_Queue *queue, |
---|
49 | Thread_Control *the_thread, |
---|
50 | Per_CPU_Control *cpu_self, |
---|
51 | Thread_queue_Context *queue_context |
---|
52 | ) |
---|
53 | { |
---|
54 | Watchdog_Interval ticks; |
---|
55 | |
---|
56 | (void) queue; |
---|
57 | ticks = queue_context->Timeout.ticks; |
---|
58 | |
---|
59 | if ( ticks != WATCHDOG_NO_TIMEOUT ) { |
---|
60 | _Thread_Add_timeout_ticks( |
---|
61 | the_thread, |
---|
62 | cpu_self, |
---|
63 | queue_context->Timeout.ticks |
---|
64 | ); |
---|
65 | } |
---|
66 | } |
---|
67 | |
---|
68 | static void _Thread_queue_Add_timeout_timespec( |
---|
69 | Thread_queue_Queue *queue, |
---|
70 | Thread_Control *the_thread, |
---|
71 | Per_CPU_Control *cpu_self, |
---|
72 | Thread_queue_Context *queue_context, |
---|
73 | Watchdog_Header *header, |
---|
74 | const struct timespec *now |
---|
75 | ) |
---|
76 | { |
---|
77 | const struct timespec *abstime; |
---|
78 | struct timespec base; |
---|
79 | |
---|
80 | if ( queue_context->timeout_absolute ) { |
---|
81 | abstime = queue_context->Timeout.arg; |
---|
82 | } else { |
---|
83 | base = *now; |
---|
84 | abstime = _Watchdog_Future_timespec( &base, queue_context->Timeout.arg ); |
---|
85 | } |
---|
86 | |
---|
87 | if ( _Watchdog_Is_valid_timespec( abstime ) ) { |
---|
88 | uint64_t expire; |
---|
89 | |
---|
90 | if ( abstime->tv_sec < 0 ) { |
---|
91 | expire = 0; |
---|
92 | } else if ( _Watchdog_Is_far_future_timespec( abstime ) ) { |
---|
93 | expire = WATCHDOG_MAXIMUM_TICKS; |
---|
94 | } else { |
---|
95 | expire = _Watchdog_Ticks_from_timespec( abstime ); |
---|
96 | } |
---|
97 | |
---|
98 | if ( expire > _Watchdog_Ticks_from_timespec( now ) ) { |
---|
99 | ISR_lock_Context lock_context; |
---|
100 | |
---|
101 | _ISR_lock_ISR_disable_and_acquire( |
---|
102 | &the_thread->Timer.Lock, |
---|
103 | &lock_context |
---|
104 | ); |
---|
105 | |
---|
106 | the_thread->Timer.header = header; |
---|
107 | the_thread->Timer.Watchdog.routine = _Thread_Timeout; |
---|
108 | _Watchdog_Per_CPU_insert( |
---|
109 | &the_thread->Timer.Watchdog, |
---|
110 | cpu_self, |
---|
111 | header, |
---|
112 | expire |
---|
113 | ); |
---|
114 | |
---|
115 | _ISR_lock_Release_and_ISR_enable( |
---|
116 | &the_thread->Timer.Lock, |
---|
117 | &lock_context |
---|
118 | ); |
---|
119 | } else { |
---|
120 | _Thread_Continue( the_thread, STATUS_TIMEOUT ); |
---|
121 | } |
---|
122 | } else { |
---|
123 | _Thread_Continue( the_thread, STATUS_INVALID_NUMBER ); |
---|
124 | } |
---|
125 | } |
---|
126 | |
---|
127 | void _Thread_queue_Add_timeout_monotonic_timespec( |
---|
128 | Thread_queue_Queue *queue, |
---|
129 | Thread_Control *the_thread, |
---|
130 | Per_CPU_Control *cpu_self, |
---|
131 | Thread_queue_Context *queue_context |
---|
132 | ) |
---|
133 | { |
---|
134 | struct timespec now; |
---|
135 | |
---|
136 | _Timecounter_Nanouptime( &now ); |
---|
137 | _Thread_queue_Add_timeout_timespec( |
---|
138 | queue, |
---|
139 | the_thread, |
---|
140 | cpu_self, |
---|
141 | queue_context, |
---|
142 | &cpu_self->Watchdog.Header[ PER_CPU_WATCHDOG_MONOTONIC ], |
---|
143 | &now |
---|
144 | ); |
---|
145 | } |
---|
146 | |
---|
147 | void _Thread_queue_Add_timeout_realtime_timespec( |
---|
148 | Thread_queue_Queue *queue, |
---|
149 | Thread_Control *the_thread, |
---|
150 | Per_CPU_Control *cpu_self, |
---|
151 | Thread_queue_Context *queue_context |
---|
152 | ) |
---|
153 | { |
---|
154 | struct timespec now; |
---|
155 | |
---|
156 | _Timecounter_Nanotime( &now ); |
---|
157 | _Thread_queue_Add_timeout_timespec( |
---|
158 | queue, |
---|
159 | the_thread, |
---|
160 | cpu_self, |
---|
161 | queue_context, |
---|
162 | &cpu_self->Watchdog.Header[ PER_CPU_WATCHDOG_REALTIME ], |
---|
163 | &now |
---|
164 | ); |
---|
165 | } |
---|