source: rtems/cpukit/posix/src/pthreadsetschedparam.c @ 04da96c7

5
Last change on this file since 04da96c7 was 9555341, checked in by Sebastian Huber <sebastian.huber@…>, on 04/06/16 at 14:26:22

posix: Use a dedicated lock for scheduler changes

Update #2555.

  • Property mode set to 100644
File size: 3.1 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/watchdogimpl.h>
32#include <rtems/config.h>
33
34int pthread_setschedparam(
35  pthread_t           thread,
36  int                 policy,
37  struct sched_param *param
38)
39{
40  Thread_Control                      *the_thread;
41  POSIX_API_Control                   *api;
42  Thread_CPU_budget_algorithms         budget_algorithm;
43  Thread_CPU_budget_algorithm_callout  budget_callout;
44  Objects_Locations                    location;
45  int                                  rc;
46  Priority_Control                     unused;
47  ISR_lock_Context                     lock_context;
48  Priority_Control                     new_priority;
49
50  /*
51   *  Check all the parameters
52   */
53  if ( !param )
54    return EINVAL;
55
56  rc = _POSIX_Thread_Translate_sched_param(
57    policy,
58    param,
59    &budget_algorithm,
60    &budget_callout
61  );
62  if ( rc )
63    return rc;
64
65  /*
66   *  Actually change the scheduling policy and parameters
67   */
68  the_thread = _Thread_Get( thread, &location );
69  switch ( location ) {
70
71    case OBJECTS_LOCAL:
72      api = the_thread->API_Extensions[ THREAD_API_POSIX ];
73
74      _POSIX_Threads_Scheduler_acquire( api, &lock_context );
75
76      if ( api->schedpolicy == SCHED_SPORADIC ) {
77        _Watchdog_Per_CPU_remove_relative( &api->Sporadic_timer );
78      }
79
80      api->schedpolicy = policy;
81      api->schedparam  = *param;
82      api->Attributes.schedpolicy = policy;
83      api->Attributes.schedparam  = *param;
84
85      the_thread->budget_algorithm = budget_algorithm;
86      the_thread->budget_callout   = budget_callout;
87
88      switch ( policy ) {
89        case SCHED_OTHER:
90        case SCHED_FIFO:
91        case SCHED_RR:
92          the_thread->cpu_time_budget =
93            rtems_configuration_get_ticks_per_timeslice();
94          new_priority =
95            _POSIX_Priority_To_core( api->schedparam.sched_priority );
96          break;
97
98        case SCHED_SPORADIC:
99          api->ss_high_priority = api->schedparam.sched_priority;
100          break;
101      }
102
103      _POSIX_Threads_Scheduler_release( api, &lock_context );
104
105      switch ( policy ) {
106        case SCHED_OTHER:
107        case SCHED_FIFO:
108        case SCHED_RR:
109          _Thread_Set_priority( the_thread, new_priority, &unused, true );
110          break;
111
112        case SCHED_SPORADIC:
113          _POSIX_Threads_Sporadic_budget_TSR( &api->Sporadic_timer );
114          break;
115      }
116
117      _Objects_Put( &the_thread->Object );
118      return 0;
119
120#if defined(RTEMS_MULTIPROCESSING)
121    case OBJECTS_REMOTE:
122#endif
123    case OBJECTS_ERROR:
124      break;
125  }
126
127  return ESRCH;
128}
Note: See TracBrowser for help on using the repository browser.