1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * |
---|
6 | * @ingroup RTEMSScoreThread |
---|
7 | * |
---|
8 | * @brief This source file contains the definition of |
---|
9 | * ::_Thread_CPU_budget_reset_timeslice and the implementation of |
---|
10 | * _Thread_CPU_budget_consume_and_yield() and |
---|
11 | * _Thread_CPU_budget_set_to_ticks_per_timeslice(). |
---|
12 | */ |
---|
13 | |
---|
14 | /* |
---|
15 | * Copyright (C) 2021 embedded brains GmbH & Co. KG |
---|
16 | * Copyright (C) 2010 Gedare Bloom <gedare@rtems.org> |
---|
17 | * |
---|
18 | * Redistribution and use in source and binary forms, with or without |
---|
19 | * modification, are permitted provided that the following conditions |
---|
20 | * are met: |
---|
21 | * 1. Redistributions of source code must retain the above copyright |
---|
22 | * notice, this list of conditions and the following disclaimer. |
---|
23 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
24 | * notice, this list of conditions and the following disclaimer in the |
---|
25 | * documentation and/or other materials provided with the distribution. |
---|
26 | * |
---|
27 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
28 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
29 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
30 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
31 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
32 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
33 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
34 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
35 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
36 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
37 | * POSSIBILITY OF SUCH DAMAGE. |
---|
38 | */ |
---|
39 | |
---|
40 | #ifdef HAVE_CONFIG_H |
---|
41 | #include "config.h" |
---|
42 | #endif |
---|
43 | |
---|
44 | #include <rtems/score/threadcpubudget.h> |
---|
45 | #include <rtems/score/threadimpl.h> |
---|
46 | #include <rtems/score/statesimpl.h> |
---|
47 | #include <rtems/score/watchdogticks.h> |
---|
48 | |
---|
49 | void _Thread_CPU_budget_consume_and_yield( Thread_Control *the_thread ) |
---|
50 | { |
---|
51 | uint32_t budget_available; |
---|
52 | |
---|
53 | if ( !the_thread->is_preemptible ) { |
---|
54 | return; |
---|
55 | } |
---|
56 | |
---|
57 | if ( !_States_Is_ready( the_thread->current_state ) ) { |
---|
58 | return; |
---|
59 | } |
---|
60 | |
---|
61 | budget_available = the_thread->CPU_budget.available; |
---|
62 | |
---|
63 | if ( budget_available == 1 ) { |
---|
64 | the_thread->CPU_budget.available = _Watchdog_Ticks_per_timeslice; |
---|
65 | |
---|
66 | /* |
---|
67 | * A yield performs the ready chain mechanics needed when |
---|
68 | * resetting a timeslice. If no other thread's are ready |
---|
69 | * at the priority of the currently executing thread, then the |
---|
70 | * executing thread's timeslice is reset. Otherwise, the |
---|
71 | * currently executing thread is placed at the rear of the |
---|
72 | * FIFO for this priority and a new heir is selected. |
---|
73 | */ |
---|
74 | _Thread_Yield( the_thread ); |
---|
75 | } else { |
---|
76 | the_thread->CPU_budget.available = budget_available - 1; |
---|
77 | } |
---|
78 | } |
---|
79 | |
---|
80 | void _Thread_CPU_budget_set_to_ticks_per_timeslice( |
---|
81 | Thread_Control *the_thread |
---|
82 | ) |
---|
83 | { |
---|
84 | the_thread->CPU_budget.available = _Watchdog_Ticks_per_timeslice; |
---|
85 | } |
---|
86 | |
---|
87 | const Thread_CPU_budget_operations _Thread_CPU_budget_reset_timeslice = { |
---|
88 | .at_tick = _Thread_CPU_budget_consume_and_yield, |
---|
89 | .at_context_switch = _Thread_CPU_budget_set_to_ticks_per_timeslice, |
---|
90 | .initialize = _Thread_CPU_budget_set_to_ticks_per_timeslice |
---|
91 | }; |
---|