source: rtems/cpukit/posix/src/pthreadsetschedparam.c @ 9480815a

5
Last change on this file since 9480815a was 9480815a, checked in by Sebastian Huber <sebastian.huber@…>, on 12/21/17 at 13:36:52

score: Introduce new monotonic clock

Rename PER_CPU_WATCHDOG_MONOTONIC to PER_CPU_WATCHDOG_TICKS. Add new
PER_CPU_WATCHDOG_MONOTONIC which is based on the system uptime (measured
by timecounter).

Close #3264.

  • Property mode set to 100644
File size: 4.3 KB
Line 
1/**
2 * @file
3 *
4 * @brief Function sets scheduling policy and parameters of the thread
5 * @ingroup POSIXAPI
6 */
7
8/*
9 *  13.5.2 Dynamic Thread Scheduling Parameters Access,
10 *         P1003.1c/Draft 10, p. 124
11 */
12
13/*  COPYRIGHT (c) 1989-2014.
14 *  On-Line Applications Research Corporation (OAR).
15 *
16 *  The license and distribution terms for this file may be
17 *  found in the file LICENSE in this distribution or at
18 *  http://www.rtems.org/license/LICENSE.
19 */
20
21#if HAVE_CONFIG_H
22#include "config.h"
23#endif
24
25#include <pthread.h>
26#include <errno.h>
27
28#include <rtems/posix/pthreadimpl.h>
29#include <rtems/posix/priorityimpl.h>
30#include <rtems/score/threadimpl.h>
31#include <rtems/score/schedulerimpl.h>
32
33static int _POSIX_Set_sched_param(
34  Thread_Control                       *the_thread,
35  int                                   policy,
36  struct sched_param                   *param,
37  Thread_CPU_budget_algorithms          budget_algorithm,
38  Thread_CPU_budget_algorithm_callout   budget_callout,
39  Thread_queue_Context                 *queue_context
40)
41{
42  const Scheduler_Control *scheduler;
43  POSIX_API_Control       *api;
44  int                      normal_prio;
45  int                      low_prio;
46  bool                     valid;
47  Priority_Control         core_normal_prio;
48  Priority_Control         core_low_prio;
49
50  normal_prio = param->sched_priority;
51
52  scheduler = _Thread_Scheduler_get_home( the_thread );
53
54  core_normal_prio = _POSIX_Priority_To_core( scheduler, normal_prio, &valid );
55  if ( !valid ) {
56    return EINVAL;
57  }
58
59  if ( policy == SCHED_SPORADIC ) {
60    low_prio = param->sched_ss_low_priority;
61  } else {
62    low_prio = normal_prio;
63  }
64
65  core_low_prio = _POSIX_Priority_To_core( scheduler, low_prio, &valid );
66  if ( !valid ) {
67    return EINVAL;
68  }
69
70  api = the_thread->API_Extensions[ THREAD_API_POSIX ];
71
72  _Watchdog_Per_CPU_remove_ticks( &api->Sporadic.Timer );
73
74  _Priority_Node_set_priority( &the_thread->Real_priority, core_normal_prio );
75
76  if ( _Priority_Node_is_active( &api->Sporadic.Low_priority ) ) {
77    _Thread_Priority_add(
78      the_thread,
79      &the_thread->Real_priority,
80      queue_context
81    );
82    _Thread_Priority_remove(
83      the_thread,
84      &api->Sporadic.Low_priority,
85      queue_context
86    );
87    _Priority_Node_set_inactive( &api->Sporadic.Low_priority );
88  } else {
89    _Thread_Priority_changed(
90      the_thread,
91      &the_thread->Real_priority,
92      false,
93      queue_context
94    );
95  }
96
97  the_thread->budget_algorithm = budget_algorithm;
98  the_thread->budget_callout   = budget_callout;
99
100  _Priority_Node_set_priority( &api->Sporadic.Low_priority, core_low_prio );
101  api->Sporadic.sched_ss_repl_period = param->sched_ss_repl_period;
102  api->Sporadic.sched_ss_init_budget = param->sched_ss_init_budget;
103  api->Sporadic.sched_ss_max_repl = param->sched_ss_max_repl;
104
105  if ( policy == SCHED_SPORADIC ) {
106    _POSIX_Threads_Sporadic_timer_insert( the_thread, api );
107  } else {
108    the_thread->cpu_time_budget =
109      rtems_configuration_get_ticks_per_timeslice();
110  }
111
112  return 0;
113}
114
115int pthread_setschedparam(
116  pthread_t           thread,
117  int                 policy,
118  struct sched_param *param
119)
120{
121  Thread_CPU_budget_algorithms         budget_algorithm;
122  Thread_CPU_budget_algorithm_callout  budget_callout;
123  Thread_Control                      *the_thread;
124  Per_CPU_Control                     *cpu_self;
125  Thread_queue_Context                 queue_context;
126  int                                  error;
127
128  if ( param == NULL ) {
129    return EINVAL;
130  }
131
132  error = _POSIX_Thread_Translate_sched_param(
133    policy,
134    param,
135    &budget_algorithm,
136    &budget_callout
137  );
138  if ( error != 0 ) {
139    return error;
140  }
141
142  _Thread_queue_Context_initialize( &queue_context );
143  _Thread_queue_Context_clear_priority_updates( &queue_context );
144  the_thread = _Thread_Get( thread, &queue_context.Lock_context.Lock_context );
145
146  if ( the_thread == NULL ) {
147    return ESRCH;
148  }
149
150  _Thread_Wait_acquire_critical( the_thread, &queue_context );
151  error = _POSIX_Set_sched_param(
152    the_thread,
153    policy,
154    param,
155    budget_algorithm,
156    budget_callout,
157    &queue_context
158  );
159  cpu_self = _Thread_queue_Dispatch_disable( &queue_context );
160  _Thread_Wait_release( the_thread, &queue_context );
161  _Thread_Priority_update( &queue_context );
162  _Thread_Dispatch_enable( cpu_self );
163  return error;
164}
Note: See TracBrowser for help on using the repository browser.