source: rtems/cpukit/rtems/src/tasks.c @ 52a0641

4.104.114.84.95
Last change on this file since 52a0641 was 98849f44, checked in by Joel Sherrill <joel.sherrill@…>, on 05/29/96 at 16:55:05

* empty log message *

  • Property mode set to 100644
File size: 27.0 KB
Line 
1/*
2 *  RTEMS Task Manager
3 *
4 *
5 *  COPYRIGHT (c) 1989, 1990, 1991, 1992, 1993, 1994.
6 *  On-Line Applications Research Corporation (OAR).
7 *  All rights assigned to U.S. Government, 1994.
8 *
9 *  This material may be reproduced by or for the U.S. Government pursuant
10 *  to the copyright license under the clause at DFARS 252.227-7013.  This
11 *  notice must appear in all copies of this file and its derivatives.
12 *
13 *  $Id$
14 */
15
16#include <rtems/system.h>
17#include <rtems/rtems/status.h>
18#include <rtems/rtems/support.h>
19#include <rtems/rtems/modes.h>
20#include <rtems/score/object.h>
21#include <rtems/score/stack.h>
22#include <rtems/score/states.h>
23#include <rtems/rtems/tasks.h>
24#include <rtems/score/thread.h>
25#include <rtems/score/threadq.h>
26#include <rtems/score/tod.h>
27#include <rtems/score/userext.h>
28#include <rtems/score/wkspace.h>
29#include <rtems/score/apiext.h>
30#include <rtems/score/sysstate.h>
31
32/*PAGE
33 *
34 *  _RTEMS_tasks_Create_extension
35 *
36 *  XXX
37 */
38
39boolean _RTEMS_tasks_Create_extension(
40  Thread_Control *executing,
41  Thread_Control *created
42)
43{
44  RTEMS_API_Control *api;
45
46  api = _Workspace_Allocate( sizeof( RTEMS_API_Control ) );
47
48  if ( !api )
49    return FALSE;
50
51  created->API_Extensions[ THREAD_API_RTEMS ] = api;
52 
53  api->pending_events = EVENT_SETS_NONE_PENDING;
54  _ASR_Initialize( &api->Signal );
55  return TRUE;
56}
57
58/*PAGE
59 *
60 *  _RTEMS_tasks_Start_extension
61 *
62 *  XXX
63 */
64 
65User_extensions_routine _RTEMS_tasks_Start_extension(
66  Thread_Control *executing,
67  Thread_Control *started
68)
69{
70  RTEMS_API_Control *api;
71
72  api = started->API_Extensions[ THREAD_API_RTEMS ];
73 
74  api->pending_events = EVENT_SETS_NONE_PENDING;
75
76  _ASR_Initialize( &api->Signal );
77}
78
79/*PAGE
80 *
81 *  _RTEMS_tasks_Delete_extension
82 *
83 *  XXX
84 */
85 
86User_extensions_routine _RTEMS_tasks_Delete_extension(
87  Thread_Control *executing,
88  Thread_Control *deleted
89)
90{
91  (void) _Workspace_Free( deleted->API_Extensions[ THREAD_API_RTEMS ] );
92 
93  deleted->API_Extensions[ THREAD_API_RTEMS ] = NULL;
94}
95
96/*PAGE
97 *
98 *  _RTEMS_tasks_Switch_extension
99 *
100 *  XXX
101 */
102 
103void _RTEMS_tasks_Switch_extension(
104  Thread_Control *executing
105)
106{
107  ISR_Level          level;
108  RTEMS_API_Control *api;
109  ASR_Information   *asr;
110  rtems_signal_set   signal_set;
111  Modes_Control      prev_mode;
112
113  api = executing->API_Extensions[ THREAD_API_RTEMS ];
114  asr = &api->Signal;
115 
116  _ISR_Disable( level );
117    signal_set = asr->signals_posted;
118    asr->signals_posted = 0;
119  _ISR_Enable( level );
120 
121 
122  if ( !signal_set ) /* similar to _ASR_Are_signals_pending( asr ) */
123    return;
124 
125  asr->nest_level += 1;
126  rtems_task_mode( asr->mode_set, RTEMS_ALL_MODE_MASKS, &prev_mode );
127 
128  (*asr->handler)( signal_set );
129
130  asr->nest_level -= 1;
131  rtems_task_mode( prev_mode, RTEMS_ALL_MODE_MASKS, &prev_mode );
132
133}
134
135API_extensions_Control _RTEMS_tasks_API_extensions = {
136  { NULL, NULL },
137  NULL,                                     /* predriver */
138  _RTEMS_tasks_Initialize_user_tasks,       /* postdriver */
139  _RTEMS_tasks_Switch_extension             /* post switch */
140};
141
142User_extensions_Control _RTEMS_tasks_User_extensions = {
143  { NULL, NULL },
144  { _RTEMS_tasks_Create_extension,            /* create */
145    _RTEMS_tasks_Start_extension,             /* start */
146    _RTEMS_tasks_Start_extension,             /* restart */
147    _RTEMS_tasks_Delete_extension,            /* delete */
148    NULL,                                     /* switch */
149    NULL,                                     /* begin */
150    NULL,                                     /* exitted */
151    NULL                                      /* fatal */
152  }
153};
154
155/*PAGE
156 *
157 *  _RTEMS_tasks_Manager_initialization
158 *
159 *  This routine initializes all Task Manager related data structures.
160 *
161 *  Input parameters:
162 *    maximum_tasks       - number of tasks to initialize
163 *
164 *  Output parameters:  NONE
165 */
166
167void _RTEMS_tasks_Manager_initialization(
168  unsigned32                        maximum_tasks,
169  unsigned32                        number_of_initialization_tasks,
170  rtems_initialization_tasks_table *user_tasks
171)
172{
173
174  _RTEMS_tasks_Number_of_initialization_tasks = number_of_initialization_tasks;
175  _RTEMS_tasks_User_initialization_tasks = user_tasks;
176
177  /*
178   *  There may not be any RTEMS initialization tasks configured.
179   */
180
181#if 0
182  if ( user_tasks == NULL || number_of_initialization_tasks == 0 )
183    _Internal_error_Occurred( INTERNAL_ERROR_RTEMS_API, TRUE, RTEMS_TOO_MANY );
184#endif
185
186  _Objects_Initialize_information(
187    &_RTEMS_tasks_Information,
188    OBJECTS_RTEMS_TASKS,
189    TRUE,
190    maximum_tasks,
191    sizeof( Thread_Control ),
192    FALSE,
193    RTEMS_MAXIMUM_NAME_LENGTH,
194    TRUE
195  );
196
197  /*
198   *  Add all the extensions for this API
199   */
200
201  _User_extensions_Add_API_set( &_RTEMS_tasks_User_extensions );
202
203  _API_extensions_Add( &_RTEMS_tasks_API_extensions );
204
205  /*
206   *  Register the MP Process Packet routine.
207   */
208
209  _MPCI_Register_packet_processor(
210    MP_PACKET_TASKS,
211    _RTEMS_tasks_MP_Process_packet
212  );
213
214}
215
216/*PAGE
217 *
218 *  rtems_task_create
219 *
220 *  This directive creates a thread by allocating and initializing a
221 *  thread control block and a stack.  The newly created thread is
222 *  placed in the dormant state.
223 *
224 *  Input parameters:
225 *    name             - user defined thread name
226 *    initial_priority - thread priority
227 *    stack_size       - stack size in bytes
228 *    initial_modes    - initial thread mode
229 *    attribute_set    - thread attributes
230 *    id               - pointer to thread id
231 *
232 *  Output parameters:
233 *    id                - thread id
234 *    RTEMS_SUCCESSFUL - if successful
235 *    error code        - if unsuccessful
236 */
237
238rtems_status_code rtems_task_create(
239  rtems_name           name,
240  rtems_task_priority  initial_priority,
241  unsigned32           stack_size,
242  rtems_mode           initial_modes,
243  rtems_attribute      attribute_set,
244  Objects_Id          *id
245)
246{
247  register Thread_Control *the_thread;
248  Objects_MP_Control      *the_global_object = NULL;
249  boolean                  is_fp;
250  boolean                  is_global;
251  boolean                  status;
252  rtems_attribute          the_attribute_set;
253  Priority_Control         core_priority;
254  RTEMS_API_Control       *api;
255  ASR_Information         *asr;
256 
257
258  if ( !rtems_is_name_valid( name ) )
259    return RTEMS_INVALID_NAME;
260
261  /*
262   *  Core Thread Initialize insures we get the minimum amount of
263   *  stack space.
264   */
265
266#if 0
267  if ( !_Stack_Is_enough( stack_size ) )
268    return RTEMS_INVALID_SIZE;
269#endif
270
271  /*
272   *  Validate the RTEMS API priority and convert it to the core priority range.
273   */
274
275  if ( !_RTEMS_tasks_Priority_is_valid( initial_priority ) )
276    return RTEMS_INVALID_PRIORITY;
277
278  core_priority = _RTEMS_tasks_Priority_to_Core( initial_priority );
279
280  /*
281   *  Fix the attribute set to match the attributes which
282   *  this processor (1) requires and (2) is able to support.
283   *  First add in the required flags for attribute_set
284   *  Typically this might include FP if the platform
285   *  or application required all tasks to be fp aware.
286   *  Then turn off the requested bits which are not supported.
287   */
288
289  the_attribute_set = _Attributes_Set( attribute_set, ATTRIBUTES_REQUIRED );
290  the_attribute_set =
291    _Attributes_Clear( the_attribute_set, ATTRIBUTES_NOT_SUPPORTED );
292
293  if ( _Attributes_Is_floating_point( the_attribute_set ) )
294    is_fp = TRUE;
295  else
296    is_fp = FALSE;
297
298  if ( _Attributes_Is_global( the_attribute_set ) ) {
299
300    is_global = TRUE;
301
302    if ( !_System_state_Is_multiprocessing )
303      return RTEMS_MP_NOT_CONFIGURED;
304
305  } else
306    is_global = FALSE;
307
308  /*
309   *  Make sure system is MP if this task is global
310   */
311
312  /*
313   *  Disable dispatch for protection
314   */
315
316  _Thread_Disable_dispatch();
317
318  /*
319   *  Allocate the thread control block and -- if the task is global --
320   *  allocate a global object control block.
321   *
322   *  NOTE:  This routine does not use the combined allocate and open
323   *         global object routine because this results in a lack of
324   *         control over when memory is allocated and can be freed in
325   *         the event of an error.
326   */
327
328  the_thread = _RTEMS_tasks_Allocate();
329
330  if ( !the_thread ) {
331    _Thread_Enable_dispatch();
332    return RTEMS_TOO_MANY;
333  }
334
335  if ( is_global ) {
336    the_global_object = _Objects_MP_Allocate_global_object();
337
338    if ( _Objects_MP_Is_null_global_object( the_global_object ) ) {
339      _RTEMS_tasks_Free( the_thread );
340      _Thread_Enable_dispatch();
341      return RTEMS_TOO_MANY;
342    }
343  }
344
345  /*
346   *  Initialize the core thread for this task.
347   */
348
349  status = _Thread_Initialize(
350    &_RTEMS_tasks_Information,
351    the_thread,
352    NULL,
353    stack_size,
354    is_fp,
355    core_priority,
356    _Modes_Is_preempt(initial_modes)   ? TRUE : FALSE,
357    _Modes_Is_timeslice(initial_modes) ? TRUE : FALSE,
358    _Modes_Get_interrupt_level(initial_modes),
359    &name
360  );
361
362  if ( !status ) {
363    if ( is_global )
364      _Objects_MP_Free_global_object( the_global_object );
365    _RTEMS_tasks_Free( the_thread );
366    _Thread_Enable_dispatch();
367    return RTEMS_UNSATISFIED;
368  }
369
370  api = the_thread->API_Extensions[ THREAD_API_RTEMS ];
371  asr = &api->Signal;
372 
373  asr->is_enabled = _Modes_Is_asr_disabled(initial_modes) ? FALSE : TRUE;
374
375  *id = the_thread->Object.id;
376
377  if ( is_global ) {
378
379    the_thread->is_global = TRUE;
380
381    _Objects_MP_Open(
382      &_RTEMS_tasks_Information,
383      the_global_object,
384      name,
385      the_thread->Object.id
386    );
387
388    _RTEMS_tasks_MP_Send_process_packet(
389      RTEMS_TASKS_MP_ANNOUNCE_CREATE,
390      the_thread->Object.id,
391      name
392    );
393
394   }
395
396  _Thread_Enable_dispatch();
397  return RTEMS_SUCCESSFUL;
398}
399
400/*PAGE
401 *
402 *  rtems_task_ident
403 *
404 *  This directive returns the system ID associated with
405 *  the thread name.
406 *
407 *  Input parameters:
408 *    name - user defined thread name
409 *    node - node(s) to be searched
410 *    id   - pointer to thread id
411 *
412 *  Output parameters:
413 *    *id               - thread id
414 *    RTEMS_SUCCESSFUL - if successful
415 *    error code        - if unsuccessful
416 */
417
418rtems_status_code rtems_task_ident(
419  rtems_name    name,
420  unsigned32    node,
421  Objects_Id   *id
422)
423{
424  Objects_Name_to_id_errors  status;
425
426  if ( name == OBJECTS_ID_OF_SELF ) {
427    *id = _Thread_Executing->Object.id;
428    return RTEMS_SUCCESSFUL;
429   }
430
431  status = _Objects_Name_to_id( &_RTEMS_tasks_Information, &name, node, id );
432
433  return _Status_Object_name_errors_to_status[ status ];
434}
435
436/*PAGE
437 *
438 *  rtems_task_start
439 *
440 *  This directive readies the thread identified by the "id"
441 *  based on its current priorty, to await execution.  A thread
442 *  can be started only from the dormant state.
443 *
444 *  Input parameters:
445 *    id          - thread id
446 *    entry_point - start execution address of thread
447 *    argument    - thread argument
448 *
449 *  Output parameters:
450 *    RTEMS_SUCCESSFUL - if successful
451 *    error code        - if unsuccessful
452 */
453
454rtems_status_code rtems_task_start(
455  rtems_id         id,
456  rtems_task_entry entry_point,
457  unsigned32       argument
458)
459{
460  register Thread_Control *the_thread;
461  Objects_Locations        location;
462
463  if ( entry_point == NULL )
464    return RTEMS_INVALID_ADDRESS;
465
466  the_thread = _Thread_Get( id, &location );
467  switch ( location ) {
468    case OBJECTS_ERROR:
469      return RTEMS_INVALID_ID;
470    case OBJECTS_REMOTE:
471      _Thread_Dispatch();
472      return RTEMS_ILLEGAL_ON_REMOTE_OBJECT;
473    case OBJECTS_LOCAL:
474      if ( _Thread_Start(
475             the_thread, THREAD_START_NUMERIC, entry_point, NULL, argument ) ) {
476        _Thread_Enable_dispatch();
477        return RTEMS_SUCCESSFUL;
478      }
479      _Thread_Enable_dispatch();
480      return RTEMS_INCORRECT_STATE;
481  }
482
483  return RTEMS_INTERNAL_ERROR;   /* unreached - only to remove warnings */
484}
485
486/*PAGE
487 *
488 *  rtems_task_restart
489 *
490 *  This directive readies the specified thread.  It restores
491 *  the thread environment to the original values established
492 *  at thread creation and start time.  A thread can be restarted
493 *  from any state except the dormant state.
494 *
495 *  Input parameters:
496 *    id       - thread id
497 *    argument - thread argument
498 *
499 *  Output parameters:
500 *    RTEMS_SUCCESSFUL - if successful
501 *    error code        - if unsuccessful
502 */
503
504rtems_status_code rtems_task_restart(
505  Objects_Id id,
506  unsigned32 argument
507)
508{
509  register Thread_Control *the_thread;
510  Objects_Locations        location;
511
512  the_thread = _Thread_Get( id, &location );
513  switch ( location ) {
514    case OBJECTS_ERROR:
515      return RTEMS_INVALID_ID;
516    case OBJECTS_REMOTE:
517      _Thread_Dispatch();
518      return RTEMS_ILLEGAL_ON_REMOTE_OBJECT;
519    case OBJECTS_LOCAL:
520      if ( _Thread_Restart( the_thread, NULL, argument ) ) {
521        _Thread_Enable_dispatch();
522        return RTEMS_SUCCESSFUL;
523      }
524      _Thread_Enable_dispatch();
525      return RTEMS_INCORRECT_STATE;
526  }
527
528  return RTEMS_INTERNAL_ERROR;   /* unreached - only to remove warnings */
529}
530
531/*PAGE
532 *
533 *  rtems_task_delete
534 *
535 *  This directive allows a thread to delete itself or the thread
536 *  identified in the id field.  The executive halts execution
537 *  of the thread and frees the thread control block.
538 *
539 *  Input parameters:
540 *    id - thread id
541 *
542 *  Output parameters:
543 *    nothing           - if id is the requesting thread (always succeeds)
544 *    RTEMS_SUCCESSFUL - if successful and id is
545 *                           not the requesting thread
546 *    error code        - if unsuccessful
547 */
548
549rtems_status_code rtems_task_delete(
550  Objects_Id id
551)
552{
553  register Thread_Control *the_thread;
554  Objects_Locations        location;
555
556  the_thread = _Thread_Get( id, &location );
557  switch ( location ) {
558    case OBJECTS_ERROR:
559      return RTEMS_INVALID_ID;
560    case OBJECTS_REMOTE:
561      _Thread_Dispatch();
562      return RTEMS_ILLEGAL_ON_REMOTE_OBJECT;
563    case OBJECTS_LOCAL:
564      _Thread_Close( &_RTEMS_tasks_Information, the_thread );
565
566      _RTEMS_tasks_Free( the_thread );
567
568      if ( the_thread->is_global ) {
569
570        _Objects_MP_Close( &_RTEMS_tasks_Information, the_thread->Object.id );
571
572        _RTEMS_tasks_MP_Send_process_packet(
573          RTEMS_TASKS_MP_ANNOUNCE_DELETE,
574          the_thread->Object.id,
575          0                                /* Not used */
576        );
577      }
578
579      _Thread_Enable_dispatch();
580      return RTEMS_SUCCESSFUL;
581  }
582
583  return RTEMS_INTERNAL_ERROR;   /* unreached - only to remove warnings */
584}
585
586/*PAGE
587 *
588 *  rtems_task_suspend
589 *
590 *  This directive will place the specified thread in the "suspended"
591 *  state.  Note that the suspended state can be in addition to
592 *  other waiting states.
593 *
594 *  Input parameters:
595 *    id - thread id
596 *
597 *  Output parameters:
598 *    RTEMS_SUCCESSFUL - if successful
599 *    error code        - if unsuccessful
600 */
601
602rtems_status_code rtems_task_suspend(
603  Objects_Id id
604)
605{
606  register Thread_Control *the_thread;
607  Objects_Locations               location;
608
609  the_thread = _Thread_Get( id, &location );
610  switch ( location ) {
611    case OBJECTS_ERROR:
612      return RTEMS_INVALID_ID;
613    case OBJECTS_REMOTE:
614      return _RTEMS_tasks_MP_Send_request_packet(
615        RTEMS_TASKS_MP_SUSPEND_REQUEST,
616        id,
617        0,          /* Not used */
618        0,          /* Not used */
619        0           /* Not used */
620      );
621    case OBJECTS_LOCAL:
622      if ( !_States_Is_suspended( the_thread->current_state ) ) {
623        _Thread_Set_state( the_thread, STATES_SUSPENDED );
624        _Thread_Enable_dispatch();
625        return RTEMS_SUCCESSFUL;
626      }
627      _Thread_Enable_dispatch();
628      return RTEMS_ALREADY_SUSPENDED;
629  }
630
631  return RTEMS_INTERNAL_ERROR;   /* unreached - only to remove warnings */
632}
633
634/*PAGE
635 *
636 *  rtems_task_resume
637 *
638 *  This directive will remove the specified thread
639 *  from the suspended state.
640 *
641 *  Input parameters:
642 *    id - thread id
643 *
644 *  Output parameters:
645 *    RTEMS_SUCCESSFUL - if successful
646 *    error code        - if unsuccessful
647 */
648
649rtems_status_code rtems_task_resume(
650  Objects_Id id
651)
652{
653  register Thread_Control *the_thread;
654  Objects_Locations        location;
655
656  the_thread = _Thread_Get( id, &location );
657  switch ( location ) {
658    case OBJECTS_ERROR:
659      return RTEMS_INVALID_ID;
660    case OBJECTS_REMOTE:
661      return(
662        _RTEMS_tasks_MP_Send_request_packet(
663          RTEMS_TASKS_MP_RESUME_REQUEST,
664          id,
665          0,          /* Not used */
666          0,          /* Not used */
667          0           /* Not used */
668        )
669      );
670    case OBJECTS_LOCAL:
671      if ( _States_Is_suspended( the_thread->current_state ) ) {
672        _Thread_Resume( the_thread );
673        _Thread_Enable_dispatch();
674        return RTEMS_SUCCESSFUL;
675      }
676      _Thread_Enable_dispatch();
677      return RTEMS_INCORRECT_STATE;
678  }
679
680  return RTEMS_INTERNAL_ERROR;   /* unreached - only to remove warnings */
681}
682
683/*PAGE
684 *
685 *  rtems_task_set_priority
686 *
687 *  This directive changes the priority of the specified thread.
688 *  The specified thread can be any thread in the system including
689 *  the requesting thread.
690 *
691 *  Input parameters:
692 *    id           - thread id (0 indicates requesting thread)
693 *    new_priority - thread priority (0 indicates current priority)
694 *    old_priority - pointer to previous priority
695 *
696 *  Output parameters:
697 *    old_priority      - previous priority
698 *    RTEMS_SUCCESSFUL - if successful
699 *    error code        - if unsuccessful
700 */
701
702rtems_status_code rtems_task_set_priority(
703  Objects_Id           id,
704  rtems_task_priority  new_priority,
705  rtems_task_priority *old_priority
706)
707{
708  register Thread_Control *the_thread;
709  Objects_Locations               location;
710
711  if ( new_priority != RTEMS_CURRENT_PRIORITY &&
712       !_RTEMS_tasks_Priority_is_valid( new_priority ) )
713    return RTEMS_INVALID_PRIORITY;
714
715  the_thread = _Thread_Get( id, &location );
716  switch ( location ) {
717    case OBJECTS_ERROR:
718      return RTEMS_INVALID_ID;
719    case OBJECTS_REMOTE:
720      _Thread_Executing->Wait.return_argument = old_priority;
721      return(
722        _RTEMS_tasks_MP_Send_request_packet(
723          RTEMS_TASKS_MP_SET_PRIORITY_REQUEST,
724          id,
725          new_priority,
726          0,          /* Not used */
727          0           /* Not used */
728        )
729      );
730    case OBJECTS_LOCAL:
731      *old_priority = the_thread->current_priority;
732      if ( new_priority != RTEMS_CURRENT_PRIORITY ) {
733        the_thread->real_priority = new_priority;
734        if ( the_thread->resource_count == 0 ||
735             the_thread->current_priority > new_priority )
736          _Thread_Change_priority( the_thread, new_priority );
737      }
738      _Thread_Enable_dispatch();
739      return RTEMS_SUCCESSFUL;
740  }
741
742  return RTEMS_INTERNAL_ERROR;   /* unreached - only to remove warnings */
743}
744
745/*PAGE
746 *
747 *  rtems_task_mode
748 *
749 *  This directive enables and disables several modes of
750 *  execution for the requesting thread.
751 *
752 *  Input parameters:
753 *    mode_set          - new mode
754 *    mask              - mask
755 *    previous_mode_set - address of previous mode set
756 *
757 *  Output:
758 *    *previous_mode_set - previous mode set
759 *     always return RTEMS_SUCCESSFUL;
760 */
761
762rtems_status_code rtems_task_mode(
763  rtems_mode  mode_set,
764  rtems_mode  mask,
765  rtems_mode *previous_mode_set
766)
767{
768  Thread_Control     *executing;
769  RTEMS_API_Control  *api;
770  ASR_Information    *asr;
771  boolean             is_asr_enabled = FALSE;
772  boolean             needs_asr_dispatching = FALSE;
773  rtems_mode          old_mode;
774
775  executing     = _Thread_Executing;
776  api = executing->API_Extensions[ THREAD_API_RTEMS ];
777  asr = &api->Signal;
778
779  old_mode  = (executing->is_preemptible) ? RTEMS_PREEMPT : RTEMS_NO_PREEMPT;
780  old_mode |= (executing->is_timeslice) ? RTEMS_TIMESLICE : RTEMS_NO_TIMESLICE;
781  old_mode |= (asr->is_enabled) ? RTEMS_ASR : RTEMS_NO_ASR;
782  old_mode |= _ISR_Get_level();
783
784  *previous_mode_set = old_mode;
785
786  /*
787   *  These are generic thread scheduling characteristics.
788   */
789
790  if ( mask & RTEMS_PREEMPT_MASK )
791    executing->is_preemptible = _Modes_Is_preempt(mode_set) ? TRUE : FALSE;
792
793  if ( mask & RTEMS_TIMESLICE_MASK )
794    executing->is_timeslice = _Modes_Is_timeslice(mode_set) ? TRUE : FALSE;
795
796  /*
797   *  Set the new interrupt level
798   */
799
800  if ( mask & RTEMS_INTERRUPT_MASK )
801    _Modes_Set_interrupt_level( mode_set );
802
803  /*
804   *  This is specific to the RTEMS API
805   */
806
807  is_asr_enabled = FALSE;
808  needs_asr_dispatching = FALSE;
809
810  if ( mask & RTEMS_ASR_MASK ) {
811    is_asr_enabled = _Modes_Is_asr_disabled( mode_set ) ? FALSE : TRUE;
812    if ( is_asr_enabled != asr->is_enabled ) {
813      asr->is_enabled = is_asr_enabled;
814      _ASR_Swap_signals( asr );
815      if ( _ASR_Are_signals_pending( asr ) ) {
816        needs_asr_dispatching = TRUE;
817        executing->do_post_task_switch_extension = TRUE;
818      }
819    }
820  }
821
822  if ( _Thread_Evaluate_mode() || needs_asr_dispatching )
823    _Thread_Dispatch();
824
825  return RTEMS_SUCCESSFUL;
826}
827
828/*PAGE
829 *
830 *  rtems_task_get_note
831 *
832 *  This directive obtains the note from the specified notepad
833 *  of the specified thread.
834 *
835 *  Input parameters:
836 *    id      - thread id
837 *    notepad - notepad number
838 *    note    - pointer to note
839 *
840 *  Output parameters:
841 *    note              - filled in if successful
842 *    RTEMS_SUCCESSFUL - if successful
843 *    error code        - if unsuccessful
844 */
845
846rtems_status_code rtems_task_get_note(
847  Objects_Id  id,
848  unsigned32  notepad,
849  unsigned32 *note
850)
851{
852  register Thread_Control *the_thread;
853  Objects_Locations        location;
854  RTEMS_API_Control       *api;
855
856  /*
857   *  NOTE:  There is no check for < RTEMS_NOTEPAD_FIRST because that would
858   *         be checking an unsigned number for being negative.
859   */
860
861  if ( notepad > RTEMS_NOTEPAD_LAST )
862    return RTEMS_INVALID_NUMBER;
863
864  /*
865   *  Optimize the most likely case to avoid the Thread_Dispatch.
866   */
867
868  if ( _Objects_Are_ids_equal( id, OBJECTS_ID_OF_SELF ) ||
869       _Objects_Are_ids_equal( id, _Thread_Executing->Object.id ) ) {
870      api = _Thread_Executing->API_Extensions[ THREAD_API_RTEMS ];
871      *note = api->Notepads[ notepad ];
872      return RTEMS_SUCCESSFUL;
873  }
874
875  the_thread = _Thread_Get( id, &location );
876  switch ( location ) {
877    case OBJECTS_ERROR:
878      return RTEMS_INVALID_ID;
879    case OBJECTS_REMOTE:
880      _Thread_Executing->Wait.return_argument = note;
881
882      return _RTEMS_tasks_MP_Send_request_packet(
883        RTEMS_TASKS_MP_GET_NOTE_REQUEST,
884        id,
885        0,          /* Not used */
886        notepad,
887        0           /* Not used */
888      );
889    case OBJECTS_LOCAL:
890      api = the_thread->API_Extensions[ THREAD_API_RTEMS ];
891      *note = api->Notepads[ notepad ];
892      _Thread_Enable_dispatch();
893      return RTEMS_SUCCESSFUL;
894  }
895
896  return RTEMS_INTERNAL_ERROR;   /* unreached - only to remove warnings */
897}
898
899/*PAGE
900 *
901 * rtems_task_set_note
902 *
903 *  This directive sets the specified notepad contents to the given
904 *  note.
905 *
906 *  Input parameters:
907 *    id      - thread id
908 *    notepad - notepad number
909 *    note    - note value
910 *
911 *  Output parameters:
912 *    RTEMS_SUCCESSFUL - if successful
913 *    error code        - if unsuccessful
914 */
915
916rtems_status_code rtems_task_set_note(
917  Objects_Id id,
918  unsigned32 notepad,
919  unsigned32 note
920)
921{
922  register Thread_Control *the_thread;
923  Objects_Locations        location;
924  RTEMS_API_Control       *api;
925
926  /*
927   *  NOTE:  There is no check for < RTEMS_NOTEPAD_FIRST because that would
928   *         be checking an unsigned number for being negative.
929   */
930
931  if ( notepad > RTEMS_NOTEPAD_LAST )
932    return RTEMS_INVALID_NUMBER;
933
934  /*
935   *  Optimize the most likely case to avoid the Thread_Dispatch.
936   */
937
938  if ( _Objects_Are_ids_equal( id, OBJECTS_ID_OF_SELF ) ||
939       _Objects_Are_ids_equal( id, _Thread_Executing->Object.id ) ) {
940      api = _Thread_Executing->API_Extensions[ THREAD_API_RTEMS ];
941      api->Notepads[ notepad ] = note;
942      return RTEMS_SUCCESSFUL;
943  }
944
945  the_thread = _Thread_Get( id, &location );
946  switch ( location ) {
947    case OBJECTS_ERROR:
948      return RTEMS_INVALID_ID;
949    case OBJECTS_REMOTE:
950      return _RTEMS_tasks_MP_Send_request_packet(
951        RTEMS_TASKS_MP_SET_NOTE_REQUEST,
952        id,
953        0,          /* Not used */
954        notepad,
955        note
956      );
957
958    case OBJECTS_LOCAL:
959      api = the_thread->API_Extensions[ THREAD_API_RTEMS ];
960      api->Notepads[ notepad ] = note;
961      _Thread_Enable_dispatch();
962      return RTEMS_SUCCESSFUL;
963  }
964
965  return RTEMS_INTERNAL_ERROR;   /* unreached - only to remove warnings */
966}
967
968/*PAGE
969 *
970 *  rtems_task_wake_after
971 *
972 *  This directive suspends the requesting thread for the given amount
973 *  of ticks.
974 *
975 *  Input parameters:
976 *    ticks - number of ticks to wait
977 *
978 *  Output parameters:
979 *    RTEMS_SUCCESSFUL - always successful
980 */
981
982rtems_status_code rtems_task_wake_after(
983  rtems_interval ticks
984)
985{
986  if ( ticks == 0 ) {
987    _Thread_Yield_processor();
988    _Thread_Dispatch();
989  } else {
990    _Thread_Disable_dispatch();
991      _Thread_Set_state( _Thread_Executing, STATES_DELAYING );
992      _Watchdog_Initialize(
993        &_Thread_Executing->Timer,
994        _Thread_Delay_ended,
995        _Thread_Executing->Object.id,
996        NULL
997      );
998      _Watchdog_Insert_ticks( &_Thread_Executing->Timer, ticks );
999    _Thread_Enable_dispatch();
1000  }
1001  return RTEMS_SUCCESSFUL;
1002}
1003
1004/*PAGE
1005 *
1006 *  rtems_task_wake_when
1007 *
1008 *  This directive blocks the requesting thread until the given date and
1009 *  time is reached.
1010 *
1011 *  Input parameters:
1012 *    time_buffer - pointer to the time and date structure
1013 *
1014 *  Output parameters:
1015 *    RTEMS_SUCCESSFUL - if successful
1016 *    error code        - if unsuccessful
1017 */
1018
1019rtems_status_code rtems_task_wake_when(
1020rtems_time_of_day *time_buffer
1021)
1022{
1023  Watchdog_Interval   seconds;
1024
1025  if ( !_TOD_Is_set() )
1026    return RTEMS_NOT_DEFINED;
1027
1028  time_buffer->ticks = 0;
1029
1030  if ( !_TOD_Validate( time_buffer ) )
1031    return RTEMS_INVALID_CLOCK;
1032
1033  seconds = _TOD_To_seconds( time_buffer );
1034
1035  if ( seconds <= _TOD_Seconds_since_epoch )
1036    return RTEMS_INVALID_CLOCK;
1037
1038  _Thread_Disable_dispatch();
1039    _Thread_Set_state( _Thread_Executing, STATES_WAITING_FOR_TIME );
1040    _Watchdog_Initialize(
1041      &_Thread_Executing->Timer,
1042      _Thread_Delay_ended,
1043      _Thread_Executing->Object.id,
1044      NULL
1045    );
1046    _Watchdog_Insert_seconds(
1047      &_Thread_Executing->Timer,
1048      seconds - _TOD_Seconds_since_epoch
1049    );
1050  _Thread_Enable_dispatch();
1051  return RTEMS_SUCCESSFUL;
1052}
1053
1054/*PAGE
1055 *
1056 *  _RTEMS_tasks_Initialize_user_tasks
1057 *
1058 *  This routine creates and starts all configured user
1059 *  initialzation threads.
1060 *
1061 *  Input parameters: NONE
1062 *
1063 *  Output parameters:  NONE
1064 */
1065
1066void _RTEMS_tasks_Initialize_user_tasks( void )
1067{
1068  unsigned32                        index;
1069  unsigned32                        maximum;
1070  rtems_id                          id;
1071  rtems_status_code                 return_value;
1072  rtems_initialization_tasks_table *user_tasks;
1073
1074  /*
1075   *  NOTE:  This is slightly different from the Ada implementation.
1076   */
1077
1078  user_tasks = _RTEMS_tasks_User_initialization_tasks;
1079  maximum    = _RTEMS_tasks_Number_of_initialization_tasks;
1080
1081  if ( !user_tasks || maximum == 0 )
1082    return;
1083
1084  for ( index=0 ; index < maximum ; index++ ) {
1085    return_value = rtems_task_create(
1086      user_tasks[ index ].name,
1087      user_tasks[ index ].initial_priority,
1088      user_tasks[ index ].stack_size,
1089      user_tasks[ index ].mode_set,
1090      user_tasks[ index ].attribute_set,
1091      &id
1092    );
1093
1094    if ( !rtems_is_status_successful( return_value ) )
1095      _Internal_error_Occurred( INTERNAL_ERROR_RTEMS_API, TRUE, return_value );
1096
1097    return_value = rtems_task_start(
1098      id,
1099      user_tasks[ index ].entry_point,
1100      user_tasks[ index ].argument
1101    );
1102
1103    if ( !rtems_is_status_successful( return_value ) )
1104      _Internal_error_Occurred( INTERNAL_ERROR_RTEMS_API, TRUE, return_value );
1105  }
1106}
1107
Note: See TracBrowser for help on using the repository browser.