summaryrefslogblamecommitdiffstats
path: root/cpukit/score/include/rtems/score/thread.h
blob: 9c5d0b040b9ba6db4215ec2f0c7723ae530ca51f (plain) (tree)
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714





























































                                                                              

                                                     




















































                                                                              
                                                                             
















                                                                           
                                         

                                            


                                          
                                          














                                                                  
                                          

                                             


                                           
                                           


                                                                  

                                           

                                   

                                          

                                       
                                                              






















































                                                                    

















                                                                        
















































































































































































































































































































































































































































































                                                                             
/*  thread.h
 *
 *  This include file contains all constants and structures associated
 *  with the thread control block.
 *
 *  COPYRIGHT (c) 1989, 1990, 1991, 1992, 1993, 1994.
 *  On-Line Applications Research Corporation (OAR).
 *  All rights assigned to U.S. Government, 1994.
 *
 *  This material may be reproduced by or for the U.S. Government pursuant
 *  to the copyright license under the clause at DFARS 252.227-7013.  This
 *  notice must appear in all copies of this file and its derivatives.
 *
 *  $Id$
 */

#ifndef __RTEMS_THREAD_h
#define __RTEMS_THREAD_h

#ifdef __cplusplus
extern "C" {
#endif

#include <rtems/asr.h>
#include <rtems/attr.h>
#include <rtems/context.h>
#include <rtems/cpu.h>
#include <rtems/eventset.h>
#include <rtems/modes.h>
#include <rtems/mppkt.h>
#include <rtems/object.h>
#include <rtems/options.h>
#include <rtems/priority.h>
#include <rtems/stack.h>
#include <rtems/states.h>
#include <rtems/tod.h>
#include <rtems/tqdata.h>
#include <rtems/watchdog.h>

/*
 *  Notepads constants (indices into notepad array)
 */

#define RTEMS_NOTEPAD_FIRST 0             /* lowest numbered notepad */
#define RTEMS_NOTEPAD_0    0              /* notepad location 0  */
#define RTEMS_NOTEPAD_1    1              /* notepad location 1  */
#define RTEMS_NOTEPAD_2    2              /* notepad location 2  */
#define RTEMS_NOTEPAD_3    3              /* notepad location 3  */
#define RTEMS_NOTEPAD_4    4              /* notepad location 4  */
#define RTEMS_NOTEPAD_5    5              /* notepad location 5  */
#define RTEMS_NOTEPAD_6    6              /* notepad location 6  */
#define RTEMS_NOTEPAD_7    7              /* notepad location 7  */
#define RTEMS_NOTEPAD_8    8              /* notepad location 8  */
#define RTEMS_NOTEPAD_9    9              /* notepad location 9  */
#define RTEMS_NOTEPAD_10   10             /* notepad location 10 */
#define RTEMS_NOTEPAD_11   11             /* notepad location 11 */
#define RTEMS_NOTEPAD_12   12             /* notepad location 12 */
#define RTEMS_NOTEPAD_13   13             /* notepad location 13 */
#define RTEMS_NOTEPAD_14   14             /* notepad location 14 */
#define RTEMS_NOTEPAD_15   15             /* notepad location 15 */
#define RTEMS_NOTEPAD_LAST RTEMS_NOTEPAD_15     /* highest numbered notepad */

#define RTEMS_NUMBER_NOTEPADS  (RTEMS_NOTEPAD_LAST+1)

/*
 *  The following defines the "return type" of an RTEMS thread.
 *
 *  NOTE:  Keep both types for internal threads.
 */

typedef void rtems_task;
typedef void Thread;

/*
 *  The following defines the argument to an RTEMS thread.
 */

typedef unsigned32 rtems_task_argument;
typedef unsigned32 Thread_Argument;

/*
 *  The following defines the type for the entry point of an RTEMS thread.
 */

typedef rtems_task ( *rtems_task_entry )(
                      rtems_task_argument
                   );

typedef Thread ( *Thread_Entry )(
                      Thread_Argument
                   );

/*
 *  The following structure contains the information which defines
 *  the starting state of a thread.
 */

typedef struct {
  Thread_Entry         entry_point;      /* starting thread address         */
  unsigned32           initial_argument; /* initial argument                */
  rtems_mode           initial_modes;    /* initial mode                    */
  rtems_task_priority  initial_priority; /* initial priority                */
  void                *fp_context;       /* initial FP context area address */
  Stack_Control        Initial_stack;    /* stack information               */
}   Thread_Start_information;

/*
 *  The following structure contains the information necessary to manage
 *  a thread which it is  waiting for a resource.
 */

typedef struct {
  Objects_Id            id;              /* waiting on this object       */
  rtems_option          option_set;      /* wait mode                    */
  union {
    unsigned32          segment_size;    /* size of segment requested    */
    rtems_event_set     event_condition;
    unsigned32         *message_size_p;  /* ptr for return size of message */
  } Extra;
  void                 *return_argument; /* address of user return param */
  rtems_status_code     return_code;     /* status for thread awakened   */
  Chain_Control         Block2n;         /* 2 - n priority blocked chain */
  Thread_queue_Control *queue;           /* pointer to thread queue      */
}   Thread_Wait_information;

/*
 *  The following defines the control block used to manage
 *  each thread proxy.
 *
 *  NOTE: It is critical that proxies and threads have identical
 *        memory images for the shared part.
 */

typedef struct {
  Objects_Control          Object;
  States_Control           current_state;
  rtems_task_priority      current_priority;
  rtems_task_priority      real_priority;
  unsigned32               resource_count;
  Thread_Wait_information  Wait;
  Watchdog_Control         Timer;
  rtems_packet_prefix     *receive_packet;
     /****************** end of common block ********************/
  Chain_Node               Active;
}   Thread_Proxy_control;


/*
 *  The following record defines the control block used
 *  to manage each thread.
 *
 *  NOTE: It is critical that proxies and threads have identical
 *        memory images for the shared part.
 */

typedef struct {
  Objects_Control           Object;
  States_Control            current_state;
  rtems_task_priority       current_priority;
  rtems_task_priority       real_priority;
  unsigned32                resource_count;
  Thread_Wait_information   Wait;
  Watchdog_Control          Timer;
  rtems_packet_prefix      *receive_packet;
     /****************** end of common block ********************/
  Chain_Control            *ready;
  Priority_Information      Priority_map;
  rtems_event_set           pending_events;
  rtems_event_set           events_out;
  Thread_Start_information  Start;
  ASR_Information           Signal;
  rtems_mode                current_modes;
  rtems_attribute           attribute_set;
  Context_Control           Registers;
  void                     *fp_context;
  unsigned32                Notepads[ RTEMS_NUMBER_NOTEPADS ];
  void                     *extension;
}   Thread_Control;

/*
 *  External API name for Thread_Control
 */

typedef Thread_Control rtems_tcb;

/*
 *  The following declares the dispatch critical section nesting
 *  counter which is used to prevent context switches at inopportune
 *  moments.
 */

EXTERN unsigned32 _Thread_Dispatch_disable_level;

/*
 *  The following data items are used to manage timeslicing.
 */

EXTERN unsigned32 _Thread_Ticks_remaining_in_timeslice;
EXTERN unsigned32 _Thread_Ticks_per_timeslice;

/*
 *  The following points to the array of FIFOs used to manage the
 *  set of ready threads.
 */

EXTERN Chain_Control *_Thread_Ready_chain;

/*
 *  The following points to the thread which is currently executing.
 *  This thread is implicitly manipulated by numerous directives.
 */

EXTERN Thread_Control *_Thread_Executing;

/*
 *  The following points to the highest priority ready thread
 *  in the system.  Unless the current thread is RTEMS_NO_PREEMPT,
 *  then this thread will be context switched to when the next
 *  dispatch occurs.
 */

EXTERN Thread_Control *_Thread_Heir;

/*
 *  The following points to the thread whose floating point
 *  context is currently loaded.
 */

EXTERN Thread_Control *_Thread_Allocated_fp;

/*
 *  The following context area contains the context of the "thread"
 *  which invoked rtems_initialize_executive.  This context is restored
 *  as the last action of the rtems_shutdown_executive directive.  Thus
 *  control of the processor can be returned to the environment
 *  which initiated RTEMS.
 */

EXTERN Context_Control _Thread_BSP_context;

/*
 *  _Thread_Handler_initialization
 *
 *  DESCRIPTION:
 *
 *  This routine performs the initialization necessary for this handler.
 */

void _Thread_Handler_initialization (
  unsigned32 ticks_per_timeslice,
  unsigned32 maximum_proxies
);

/*
 *  _Thread_Start_multitasking
 *
 *  DESCRIPTION:
 *
 *  This routine initiates multitasking.  It is invoked only as
 *  part of initialization and its invocation is the last act of
 *  the rtems_initialize_executive directive.
 */

void _Thread_Start_multitasking (
  Thread_Control *system_thread,
  Thread_Control *idle_thread
);

/*
 *  _Thread_Stop_multitasking
 *
 *  DESCRIPTION:
 *
 *  This routine halts multitasking and returns control to
 *  the "thread" which initially invoked the rtems_initialize_executive
 *  directive.
 */

STATIC INLINE void _Thread_Stop_multitasking( void );

/*
 *  _Thread_Dispatch_initialization
 *
 *  DESCRIPTION:
 *
 *  This routine initializes the thread dispatching subsystem.
 */

STATIC INLINE void _Thread_Dispatch_initialization( void );

/*
 *  _Thread_Dispatch
 *
 *  DESCRIPTION:
 *
 *  This routine is responsible for transferring control of the
 *  processor from the executing thread to the heir thread.  As part
 *  of this process, it is responsible for the following actions:
 *
 *     + saving the context of the executing thread
 *     + restoring the context of the heir thread
 *     + dispatching any signals for the resulting executing thread
 */

void _Thread_Dispatch( void );

/*
 *  _Thread_Ready
 *
 *  DESCRIPTION:
 *
 *  This routine removes any set states for the_thread.  It performs
 *  any necessary scheduling operations including the selection of
 *  a new heir thread.
 */

void _Thread_Ready(
  Thread_Control *the_thread
);

/*
 *  _Thread_Clear_state
 *
 *  DESCRIPTION:
 *
 *  This routine clears the indicated STATES for the_thread.  It performs
 *  any necessary scheduling operations including the selection of
 *  a new heir thread.
 */

void _Thread_Clear_state(
  Thread_Control *the_thread,
  States_Control  state
);

/*
 *  _Thread_Set_state
 *
 *  DESCRIPTION:
 *
 *  This routine sets the indicated states for the_thread.  It performs
 *  any necessary scheduling operations including the selection of
 *  a new heir thread.
 *
 */

void _Thread_Set_state(
  Thread_Control *the_thread,
  States_Control  state
);

/*
 *  _Thread_Set_transient
 *
 *  DESCRIPTION:
 *
 *  This routine sets the TRANSIENT state for the_thread.  It performs
 *  any necessary scheduling operations including the selection of
 *  a new heir thread.
 */

void _Thread_Set_transient(
  Thread_Control *the_thread
);

/*
 *  _Thread_Reset_timeslice
 *
 *  DESCRIPTION:
 *
 *  This routine is invoked upon expiration of the currently
 *  executing thread's timeslice.  If no other thread's are ready
 *  at the priority of the currently executing thread, then the
 *  executing thread's timeslice is reset.  Otherwise, the
 *  currently executing thread is placed at the rear of the
 *  RTEMS_FIFO for this priority and a new heir is selected.
 */

void _Thread_Reset_timeslice( void );

/*
 *  _Thread_Tickle_timeslice
 *
 *  DESCRIPTION:
 *
 *  This routine is invoked as part of processing each clock tick.
 *  It is responsible for determining if the current thread allows
 *  timeslicing and, if so, when its timeslice expires.
 */

void _Thread_Tickle_timeslice( void );

/*
 *  _Thread_Yield_processor
 *
 *  DESCRIPTION:
 *
 *  This routine is invoked when a thread wishes to voluntarily
 *  transfer control of the processor to another thread of equal
 *  or greater priority.
 */

void _Thread_Yield_processor( void );

/*
 *  _Thread_Is_executing
 *
 *  DESCRIPTION:
 *
 *  This function returns TRUE if the_thread is the currently executing
 *  thread, and FALSE otherwise.
 */

STATIC INLINE boolean _Thread_Is_executing (
  Thread_Control *the_thread
);

/*
 *  _Thread_Is_heir
 *
 *  DESCRIPTION:
 *
 *  This function returns TRUE if the_thread is the heir
 *  thread, and FALSE otherwise.
 */

STATIC INLINE boolean _Thread_Is_executing (
  Thread_Control *the_thread
);

/*
 *  _Thread_Is_executing_also_the_heir
 *
 *  DESCRIPTION:
 *
 *  This function returns TRUE if the currently executing thread
 *  is also the heir thread, and FALSE otherwise.
 */

STATIC INLINE boolean _Thread_Is_executing_also_the_heir( void );

/*
 *  _Thread_Load_environment
 *
 *  DESCRIPTION:
 *
 *  This routine initializes the context of the_thread to its
 *  appropriate starting state.
 */

void _Thread_Load_environment(
  Thread_Control *the_thread
);

/*
 *  _Thread_Handler
 *
 *  DESCRIPTION:
 *
 *  This routine is the wrapper function for all threads.  It is
 *  the starting point for all threads.  The user provided thread
 *  entry point is invoked by this routine.  Operations
 *  which must be performed immediately before and after the user's
 *  thread executes are found here.
 */

void _Thread_Handler( void );

/*
 *  _Thread_Delay_ended
 *
 *  DESCRIPTION:
 *
 *  This routine is invoked when a thread must be unblocked at the
 *  end of a delay such as the rtems_task_wake_after and rtems_task_wake_when
 *  directives.
 */

void _Thread_Delay_ended(
  Objects_Id  id,
  void       *ignored
);

/*
 *  _Thread_Change_priority
 *
 *  DESCRIPTION:
 *
 *  This routine changes the current priority of the_thread to
 *  new_priority.  It performs any necessary scheduling operations
 *  including the selection of a new heir thread.
 */

void _Thread_Change_priority (
  Thread_Control   *the_thread,
  rtems_task_priority  new_priority
);

/*
 *  _Thread_Set_priority
 *
 *  DESCRIPTION:
 *
 *  This routine updates the priority related fields in the_thread
 *  control block to indicate the current priority is now new_priority.
 */

void _Thread_Set_priority(
  Thread_Control   *the_thread,
  rtems_task_priority  new_priority
);

/*
 *  _Thread_Change_mode
 *
 *  DESCRIPTION:
 *
 *  This routine changes the current values of the modes
 *  indicated by mask of the calling thread are changed to that
 *  indicated in mode_set.  The former mode of the thread is
 *  returned in mode_set.  If the changes in the current mode
 *  indicate that a thread dispatch operation may be necessary,
 *  then need_dispatch is TRUE, otherwise it is FALSE.
 */

boolean _Thread_Change_mode(
  rtems_mode  new_mode_set,
  rtems_mode  mask,
  rtems_mode *old_mode_set
);

/*
 *  _Thread_Resume
 *
 *  DESCRIPTION:
 *
 *  This routine clears the SUSPENDED state for the_thread.  It performs
 *  any necessary scheduling operations including the selection of
 *  a new heir thread.
 */

STATIC INLINE void _Thread_Resume (
  Thread_Control *the_thread
);

/*
 *  _Thread_Unblock
 *
 *  DESCRIPTION:
 *
 *  This routine clears any blocking state for the_thread.  It performs
 *  any necessary scheduling operations including the selection of
 *  a new heir thread.
 */

STATIC INLINE void _Thread_Unblock (
  Thread_Control *the_thread
);

/*
 *  _Thread_Restart_self
 *
 *  DESCRIPTION:
 *
 *  This routine resets the current context of the calling thread
 *  to that of its initial state.
 */

STATIC INLINE void _Thread_Restart_self( void );

/*
 *  _Thread_Calculate_heir
 *
 *  DESCRIPTION:
 *
 *  This function returns a pointer to the highest priority
 *  ready thread.
 */

STATIC INLINE void _Thread_Calculate_heir( void );

/*
 *  _Thread_Is_allocated_fp
 *
 *  DESCRIPTION:
 *
 *  This function returns TRUE if the floating point context of
 *  the_thread is currently loaded in the floating point unit, and
 *  FALSE otherwise.
 */

STATIC INLINE boolean _Thread_Is_allocated_fp (
  Thread_Control *the_thread
);

/*
 *  _Thread_Deallocate_fp
 *
 *  DESCRIPTION:
 *
 *  This routine is invoked when the currently loaded floating
 *  point context is now longer associated with an active thread.
 */

STATIC INLINE void _Thread_Deallocate_fp( void );

/*
 *  _Thread_Disable_dispatch
 *
 *  DESCRIPTION:
 *
 *  This routine prevents dispatching.
 */

STATIC INLINE void _Thread_Disable_dispatch( void );

/*
 *  _Thread_Enable_dispatch
 *
 *  DESCRIPTION:
 *
 *  This routine allows dispatching to occur again.  If this is
 *  the outer most dispatching critical section, then a dispatching
 *  operation will be performed and, if necessary, control of the
 *  processor will be transferred to the heir thread.
 */

#if ( CPU_INLINE_ENABLE_DISPATCH == TRUE )

STATIC INLINE void _Thread_Enable_dispatch();

#endif

#if ( CPU_INLINE_ENABLE_DISPATCH == FALSE )

void _Thread_Enable_dispatch( void );

#endif

/*
 *  _Thread_Unnest_dispatch
 *
 *  DESCRIPTION:
 *
 *  This routine allows dispatching to occur again.  However,
 *  no dispatching operation is performed even if this is the outer
 *  most dispatching critical section.
 */

STATIC INLINE void _Thread_Unnest_dispatch( void );

/*
 *  _Thread_Is_dispatching_enabled
 *
 *  DESCRIPTION:
 *
 *  This function returns TRUE if dispatching is disabled, and FALSE
 *  otherwise.
 */

STATIC INLINE boolean _Thread_Is_dispatching_enabled( void );

/*
 *  _Thread_Is_context_switch_necessary
 *
 *  DESCRIPTION:
 *
 *  This function returns TRUE if dispatching is disabled, and FALSE
 *  otherwise.
 */

STATIC INLINE boolean _Thread_Is_context_switch_necessary( void );

/*
 *  _Thread_Is_null
 *
 *  DESCRIPTION:
 *
 *  This function returns TRUE if the_thread is NULL and FALSE otherwise.
 */

STATIC INLINE boolean _Thread_Is_null (
  Thread_Control *the_thread
);

/*
 *  _Thread_Get
 *
 *  DESCRIPTION:
 *
 *  This function maps thread IDs to thread control
 *  blocks.  If ID corresponds to a local thread, then it
 *  returns the_thread control pointer which maps to ID
 *  and location is set to OBJECTS_LOCAL.  If the thread ID is
 *  global and resides on a remote node, then location is set
 *  to OBJECTS_REMOTE, and the_thread is undefined.
 *  Otherwise, location is set to OBJECTS_ERROR and
 *  the_thread is undefined.
 */

STATIC INLINE Thread_Control *_Thread_Get (
  Objects_Id         id,
  Objects_Locations *location
);

#include <rtems/thread.inl>
#include <rtems/threadmp.h>

#ifdef __cplusplus
}
#endif

#endif
/* end of include file */