source: rtems/cpukit/posix/src/pthreadsetschedparam.c @ 77fbbd6

5
Last change on this file since 77fbbd6 was 77fbbd6, checked in by Sebastian Huber <sebastian.huber@…>, on 07/03/18 at 09:50:53

posix: Check for new <pthread.h> prototypes

Update #3342.
Update #3343.

  • Property mode set to 100644
File size: 4.4 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  const 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#ifdef HAVE_PTHREAD_SETSCHEDPARAM_CONST
119  const struct sched_param *param
120#else
121  struct sched_param       *param
122#endif
123)
124{
125  Thread_CPU_budget_algorithms         budget_algorithm;
126  Thread_CPU_budget_algorithm_callout  budget_callout;
127  Thread_Control                      *the_thread;
128  Per_CPU_Control                     *cpu_self;
129  Thread_queue_Context                 queue_context;
130  int                                  error;
131
132  if ( param == NULL ) {
133    return EINVAL;
134  }
135
136  error = _POSIX_Thread_Translate_sched_param(
137    policy,
138    param,
139    &budget_algorithm,
140    &budget_callout
141  );
142  if ( error != 0 ) {
143    return error;
144  }
145
146  _Thread_queue_Context_initialize( &queue_context );
147  _Thread_queue_Context_clear_priority_updates( &queue_context );
148  the_thread = _Thread_Get( thread, &queue_context.Lock_context.Lock_context );
149
150  if ( the_thread == NULL ) {
151    return ESRCH;
152  }
153
154  _Thread_Wait_acquire_critical( the_thread, &queue_context );
155  error = _POSIX_Set_sched_param(
156    the_thread,
157    policy,
158    param,
159    budget_algorithm,
160    budget_callout,
161    &queue_context
162  );
163  cpu_self = _Thread_queue_Dispatch_disable( &queue_context );
164  _Thread_Wait_release( the_thread, &queue_context );
165  _Thread_Priority_update( &queue_context );
166  _Thread_Dispatch_enable( cpu_self );
167  return error;
168}
Note: See TracBrowser for help on using the repository browser.