source: rtems/cpukit/rtems/src/taskcreate.c @ d1f0701

4.104.114.84.95
Last change on this file since d1f0701 was d1f0701, checked in by Joel Sherrill <joel.sherrill@…>, on Sep 13, 2007 at 7:48:01 PM

2007-09-13 Joel Sherrill <joel.sherrill@…>

PR 1259/rtems

  • rtems/src/taskcreate.c: Initialize is_global in all cases since memory is not guaranteed to be zero.
  • Property mode set to 100644
File size: 5.7 KB
Line 
1/*
2 *  RTEMS Task Manager
3 *
4 *
5 *  COPYRIGHT (c) 1989-1999.
6 *  On-Line Applications Research Corporation (OAR).
7 *
8 *  The license and distribution terms for this file may be
9 *  found in the file LICENSE in this distribution or at
10 *  http://www.rtems.com/license/LICENSE.
11 *
12 *  $Id$
13 */
14
15#if HAVE_CONFIG_H
16#include "config.h"
17#endif
18
19#include <rtems/system.h>
20#include <rtems/rtems/status.h>
21#include <rtems/rtems/support.h>
22#include <rtems/rtems/modes.h>
23#include <rtems/score/object.h>
24#include <rtems/score/stack.h>
25#include <rtems/score/states.h>
26#include <rtems/rtems/tasks.h>
27#include <rtems/score/thread.h>
28#include <rtems/score/threadq.h>
29#include <rtems/score/tod.h>
30#include <rtems/score/userext.h>
31#include <rtems/score/wkspace.h>
32#include <rtems/score/apiext.h>
33#include <rtems/score/sysstate.h>
34
35/*PAGE
36 *
37 *  rtems_task_create
38 *
39 *  This directive creates a thread by allocating and initializing a
40 *  thread control block and a stack.  The newly created thread is
41 *  placed in the dormant state.
42 *
43 *  Input parameters:
44 *    name             - user defined thread name
45 *    initial_priority - thread priority
46 *    stack_size       - stack size in bytes
47 *    initial_modes    - initial thread mode
48 *    attribute_set    - thread attributes
49 *    id               - pointer to thread id
50 *
51 *  Output parameters:
52 *    id               - thread id
53 *    RTEMS_SUCCESSFUL - if successful
54 *    error code       - if unsuccessful
55 */
56
57rtems_status_code rtems_task_create(
58  rtems_name           name,
59  rtems_task_priority  initial_priority,
60  size_t               stack_size,
61  rtems_mode           initial_modes,
62  rtems_attribute      attribute_set,
63  Objects_Id          *id
64)
65{
66  register Thread_Control *the_thread;
67  boolean                  is_fp;
68#if defined(RTEMS_MULTIPROCESSING)
69  Objects_MP_Control      *the_global_object = NULL;
70  boolean                  is_global;
71#endif
72  boolean                  status;
73  rtems_attribute          the_attribute_set;
74  Priority_Control         core_priority;
75  RTEMS_API_Control       *api;
76  ASR_Information         *asr;
77
78
79  if ( !id )
80   return RTEMS_INVALID_ADDRESS;
81
82  if ( !rtems_is_name_valid( name ) )
83    return RTEMS_INVALID_NAME;
84
85  /*
86   *  Core Thread Initialize insures we get the minimum amount of
87   *  stack space.
88   */
89
90  /*
91   *  Fix the attribute set to match the attributes which
92   *  this processor (1) requires and (2) is able to support.
93   *  First add in the required flags for attribute_set
94   *  Typically this might include FP if the platform
95   *  or application required all tasks to be fp aware.
96   *  Then turn off the requested bits which are not supported.
97   */
98
99  the_attribute_set = _Attributes_Set( attribute_set, ATTRIBUTES_REQUIRED );
100  the_attribute_set =
101    _Attributes_Clear( the_attribute_set, ATTRIBUTES_NOT_SUPPORTED );
102
103  if ( _Attributes_Is_floating_point( the_attribute_set ) )
104    is_fp = TRUE;
105  else
106    is_fp = FALSE;
107
108  /*
109   *  Validate the RTEMS API priority and convert it to the core priority range.
110   */
111
112  if ( !_Attributes_Is_system_task( the_attribute_set ) ) {
113    if ( !_RTEMS_tasks_Priority_is_valid( initial_priority ) )
114      return RTEMS_INVALID_PRIORITY;
115  }
116
117  core_priority = _RTEMS_tasks_Priority_to_Core( initial_priority );
118
119#if defined(RTEMS_MULTIPROCESSING)
120  if ( _Attributes_Is_global( the_attribute_set ) ) {
121
122    is_global = TRUE;
123
124    if ( !_System_state_Is_multiprocessing )
125      return RTEMS_MP_NOT_CONFIGURED;
126
127  } else
128    is_global = FALSE;
129#endif
130
131  /*
132   *  Make sure system is MP if this task is global
133   */
134
135  /*
136   *  Disable dispatch for protection
137   */
138
139  _Thread_Disable_dispatch();
140
141  /*
142   *  Allocate the thread control block and -- if the task is global --
143   *  allocate a global object control block.
144   *
145   *  NOTE:  This routine does not use the combined allocate and open
146   *         global object routine because this results in a lack of
147   *         control over when memory is allocated and can be freed in
148   *         the event of an error.
149   */
150
151  the_thread = _RTEMS_tasks_Allocate();
152
153  if ( !the_thread ) {
154    _Thread_Enable_dispatch();
155    return RTEMS_TOO_MANY;
156  }
157
158#if defined(RTEMS_MULTIPROCESSING)
159  if ( is_global ) {
160    the_global_object = _Objects_MP_Allocate_global_object();
161
162    if ( _Objects_MP_Is_null_global_object( the_global_object ) ) {
163      _RTEMS_tasks_Free( the_thread );
164      _Thread_Enable_dispatch();
165      return RTEMS_TOO_MANY;
166    }
167  }
168#endif
169
170  /*
171   *  Initialize the core thread for this task.
172   */
173
174  status = _Thread_Initialize(
175    &_RTEMS_tasks_Information,
176    the_thread,
177    NULL,
178    stack_size,
179    is_fp,
180    core_priority,
181    _Modes_Is_preempt(initial_modes)   ? TRUE : FALSE,
182    _Modes_Is_timeslice(initial_modes) ?
183      THREAD_CPU_BUDGET_ALGORITHM_RESET_TIMESLICE :
184      THREAD_CPU_BUDGET_ALGORITHM_NONE,
185    NULL,        /* no budget algorithm callout */
186    _Modes_Get_interrupt_level(initial_modes),
187    (Objects_Name) name
188  );
189
190  if ( !status ) {
191#if defined(RTEMS_MULTIPROCESSING)
192    if ( is_global )
193      _Objects_MP_Free_global_object( the_global_object );
194#endif
195    _RTEMS_tasks_Free( the_thread );
196    _Thread_Enable_dispatch();
197    return RTEMS_UNSATISFIED;
198  }
199
200  api = the_thread->API_Extensions[ THREAD_API_RTEMS ];
201  asr = &api->Signal;
202
203  asr->is_enabled = _Modes_Is_asr_disabled(initial_modes) ? FALSE : TRUE;
204
205  *id = the_thread->Object.id;
206
207#if defined(RTEMS_MULTIPROCESSING)
208  the_thread->is_global = is_global;
209  if ( is_global ) {
210
211    _Objects_MP_Open(
212      &_RTEMS_tasks_Information,
213      the_global_object,
214      name,
215      the_thread->Object.id
216    );
217
218    _RTEMS_tasks_MP_Send_process_packet(
219      RTEMS_TASKS_MP_ANNOUNCE_CREATE,
220      the_thread->Object.id,
221      name
222    );
223
224   }
225#endif
226
227  _Thread_Enable_dispatch();
228  return RTEMS_SUCCESSFUL;
229}
Note: See TracBrowser for help on using the repository browser.