Thread X Kernel APIs 4 1 Thread X

  • Slides: 81
Download presentation
Thread. X Kernel API’s 4 -1

Thread. X Kernel API’s 4 -1

Thread. X in General • • • Thread. X is delivered in binary format

Thread. X in General • • • Thread. X is delivered in binary format No MMU support Resides with Application System structures directly visible to Application Multilevel Queue Scheduler – 32 Thread priorities (0 highest, 31 lowest) – Timeslicing requires HW Timer • Small Footprint (1. 7 k. B to 11. 2 k. B text) • Unlimited Threads, Queues, Event Flags, … • Runs completely in Supervisor Mode 4 -2

Kernel API’s Overview 4. 1 Thread Management 4. 2 Memory Management 4. 3 Semaphores

Kernel API’s Overview 4. 1 Thread Management 4. 2 Memory Management 4. 3 Semaphores 4. 4 Event Flags 4. 5 Message Queues 4. 6 Timers 4. 7 Other Kernel Services

4. 1 Thread Management 4 -4

4. 1 Thread Management 4 -4

Thread. X™ RTOS • Thread Creation – Easy; One Function Call Spawns Thread •

Thread. X™ RTOS • Thread Creation – Easy; One Function Call Spawns Thread • Thread Management • Threads • Message Queues • Semaphores • Event Flags • Timers • Memory Management Under 25 Kbytes Instruction Area – Threads Execute Independently • Individual Stack Space – Execute Threads Based on Priority • 32 Levels of Prioritization – Time-Share Equal Priority Threads • Round-Robin • Run-Time Management – Locking/Unlocking System Resources – Thread-to-Thread Communication – Timing • Interrupt Handling 4 -5

Thread Introduction • NET+OS uses threads, not tasks – By definition threads share address

Thread Introduction • NET+OS uses threads, not tasks – By definition threads share address space, tasks and processes do not – In relative terms, overhead associated with thread management is minimal • NET+OS provides APIs which enable the user to: – Create and delete threads – Control the execution of threads – Control the scheduling of threads 4 -6

Thread. X™ Operation EXECUTE THREAD 1 COMPLETE SUSPEND CREATION MESSAGE QUEUE EVENT FLAGS SEMAPHORE

Thread. X™ Operation EXECUTE THREAD 1 COMPLETE SUSPEND CREATION MESSAGE QUEUE EVENT FLAGS SEMAPHORE THREAD 2 RESUME THREAD 4 KERNEL EVENT CREATION COMPLETE SLEEP CREATION COMPLETE ISR THREAD 5 KERNEL EVENT THREAD 3 4 -7 CREATION SUSPEND CREATION COMPLETE

Thread Parameters • Attributes used to define a thread include: – Thread Pointer: Pointer

Thread Parameters • Attributes used to define a thread include: – Thread Pointer: Pointer to thread’s control block – Name Pointer: Pointer to thread’s name – Entry Function: Pointer to thread’s entry-point routine – Entry Input: Optional argument passed to thread’s entry function. – Stack Start: Starting address of thread’s stack space in system memory 4 -8

Thread Parameters (continued) – Stack Size: Size, in bytes, of the thread’s stack space

Thread Parameters (continued) – Stack Size: Size, in bytes, of the thread’s stack space – Priority: Relative importance of the thread in relation to other threads – Preemption Threshold: Highest minimum priority another thread must be before interrupting this thread – Time Slice: Maximum number of ticks a thread can execute – Auto Start: Specifies whether the thread should start immediately, or be created in a suspended state 4 -9

Thread Creation CRITICAL PARAMETERS Entry Function Entry Parameter Stack Start Stack Size Priority Time

Thread Creation CRITICAL PARAMETERS Entry Function Entry Parameter Stack Start Stack Size Priority Time Slice 4 -10 tx_thread_create()

Example Thread Creation: Net+OS Root Thread From bsproot. c… 4 -11 #include tx_api. h

Example Thread Creation: Net+OS Root Thread From bsproot. c… 4 -11 #include tx_api. h

Thread Creation uint tx_thread_create(TX_THREAD *p. Thread, char *p. Name, void (*p. Entry. Func)(ulong), ulong

Thread Creation uint tx_thread_create(TX_THREAD *p. Thread, char *p. Name, void (*p. Entry. Func)(ulong), ulong entry. Input, void *p. Stack. Start, ulong stack. Size, uint priority, uint preempt. Threshold, ulong time. Slice, uint auto. Start) – Creates a thread using the specified attributes 4 -12

Thread Terminate/Delete uint tx_thread_terminate(TX_THREAD *p. Thread) – Terminates the specified thread regardless of state

Thread Terminate/Delete uint tx_thread_terminate(TX_THREAD *p. Thread) – Terminates the specified thread regardless of state – Threads are allowed to self terminate – Terminated threads must be deleted and re-created in order to execute again uint tx_thread_delete(TX_THREAD *p. Thread) – Deletes the specified thread, p. Thread – Only able to delete threads in the completed (TX_COMPLETED) or terminated (TX_TERMINATED) states – Application responsible for memory management/cleanup 4 -13

Thread Suspension / Resumption EXECUTE OUR THREAD SUSPEND CREATION Suspend On… tx_thread_suspend() tx_thread_sleep() tx_semaphore_get()

Thread Suspension / Resumption EXECUTE OUR THREAD SUSPEND CREATION Suspend On… tx_thread_suspend() tx_thread_sleep() tx_semaphore_get() tx_queue_receive() tx_byte_allocate() tx_event_flags_get() Resume On… tx_thread_resume() tx_semaphore_put() tx_queue_send() tx_byte_release() tx_event_flags_set() 4 -14 From Another Thread or Interrupt Service Routine

Thread Resume/Suspend uint tx_thread_resume(TX_THREAD *p. Thread) – Resumes previously suspended (TX_SUSPENDED) threads – Resumes

Thread Resume/Suspend uint tx_thread_resume(TX_THREAD *p. Thread) – Resumes previously suspended (TX_SUSPENDED) threads – Resumes threads created without an automatic start uint tx_thread_suspend(TX_THREAD *p. Thread) – Suspends the specified thread, p. Thread – Threads are allowed to suspend themselves – Allowed to suspend a currently suspended (TX_SUSPENDED) thread only once 4 -15

Thread Sleep/Relinquish uint tx_thread_sleep(ulong timer. Ticks) – Suspends the calling thread for the specified

Thread Sleep/Relinquish uint tx_thread_sleep(ulong timer. Ticks) – Suspends the calling thread for the specified number of timer ticks, timer. Ticks void tx_thread_relinquish (void) – Yields the processor to other threads in the ready (TX_READY) state 4 -16

Thread Identify TX_THREAD* tx_thread_identify (void) – Returns a pointer to the current thread’s control

Thread Identify TX_THREAD* tx_thread_identify (void) – Returns a pointer to the current thread’s control block – When called from an ISR the routine returns the control block of the thread that ran prior to the interrupt 4 -17

Thread Preemption/Priority/Time Slice Change uint tx_thread_preemption_change(TX_THREAD *p. Thread, uint new. Threshold, uint *p. Old.

Thread Preemption/Priority/Time Slice Change uint tx_thread_preemption_change(TX_THREAD *p. Thread, uint new. Threshold, uint *p. Old. Threshold) – Changes the thread’s preemption threshold attribute to the value of new. Threshold uint tx_thread_priority_change (TX_THREAD *p. Thread, uint new. Priority, uint *p. Old. Priority) – Changes the thread’s priority attribute to the value of new. Priority – User will need to adjust the preemption threshold value on their own uint tx_thread_time_slice_change(TX_THREAD *p. Thread, ulong new. Time. Slice, ulong *p. Old. Time. Slice) – Changes the thread’s time slice attribute to the value of new. Time. Slice 4 -18

Thread Summary • • 4 -19 Thread attributes – Maintained in thread control block,

Thread Summary • • 4 -19 Thread attributes – Maintained in thread control block, TX_THREAD – Originally specified in tx_thread_create() – Modified with tx_thread_xxx_change() routines Thread APIs – tx_thread_create() / tx_thread_terminate() / tx_thread_delete() – tx_thread_suspend() / tx_thread_resume() – tx_thread_sleep() / tx_thread_relinquish() – tx_thread_identify() – tx_thread_preemption_change() – tx_thread_priority_change() – tx_thread_time_slice_change()

Thread Summary (continued) • Thread APIs – tx_thread_create() / tx_thread_terminate() / tx_thread_delete() – tx_thread_suspend()

Thread Summary (continued) • Thread APIs – tx_thread_create() / tx_thread_terminate() / tx_thread_delete() – tx_thread_suspend() / tx_thread_resume() – tx_thread_sleep() / tx_thread_relinquish() – tx_thread_identify() – tx_thread_preemption_change() – tx_thread_priority_change() – tx_thread_time_slice_change() 4 -20

Thread. X Scheduler Multilevel Queue Scheduler Scenario: Thread A, Thread B, and Thread C

Thread. X Scheduler Multilevel Queue Scheduler Scenario: Thread A, Thread B, and Thread C are configured during creation w/ priority levels 0, 0, 30, respectively. Multilevel Scheduling Queue Priority 31 Thread C … Thread B 4 -21 Thread A Priority 0

Thread. X Scheduler (cont. ) Scheduler Thread Operation: Timeslice Thread A Deschedule Thread B

Thread. X Scheduler (cont. ) Scheduler Thread Operation: Timeslice Thread A Deschedule Thread B Preemption Deschedule Thread C Time 4 -22 Thread A

Thread. X Exception Handling Application Level Application Installed Exception Handler TX Kernel Level BSP

Thread. X Exception Handling Application Level Application Installed Exception Handler TX Kernel Level BSP Level __tx_irq_handler() MCIrq. Handler() Hardware Fetch IRQ Instruction at Address 0 x 18 IRQ 4 -23 Restore state

Thread. X Exception Handling cont. Thread A Timeslice Deschedule Thread B Preemption Deschedule Thread

Thread. X Exception Handling cont. Thread A Timeslice Deschedule Thread B Preemption Deschedule Thread C ISR Time 4 -24 Thread A

4. 2 Memory Management 4 -25

4. 2 Memory Management 4 -25

Memory Management - Block Pools • Block Pools – Deterministic allocation/free time – Not

Memory Management - Block Pools • Block Pools – Deterministic allocation/free time – Not subject to memory fragmentation – Pools must be sized to handle worst case memory scenario – Publicly available resource – Overhead associated with each block equal to a C-pointer – Pool control blocks, TX_BLOCK_POOL, often defined globally – Pools can be located anywhere in memory 4 -26

Memory Block Pools - Creation System Memory tx_block_pool_create() HEAP • Total Size in Bytes

Memory Block Pools - Creation System Memory tx_block_pool_create() HEAP • Total Size in Bytes • Divided Into Equal-size Sub-blocks • Can Suspend if Unsuccessful • Flexibility in Location • Programmer Chooses Start Address 4 -27

Memory Block Pools - Allocation Memory Block Pool block e block d block c

Memory Block Pools - Allocation Memory Block Pool block e block d block c block b block a 4 -28 tx_block_allocate() • Entire Block Allocated • Fragmentation Less Likely • More Deterministic than malloc() • Can Suspend if Unsuccessful -OR • Can Setup Time-Out Period • Wait for Another Thread to Free a Block

Block Pool Create/Delete uint tx_block_pool_create(TX_BLOCK_POOL *p. Pool, char *p. Name, ulong block. Size, void

Block Pool Create/Delete uint tx_block_pool_create(TX_BLOCK_POOL *p. Pool, char *p. Name, ulong block. Size, void *p. Pool. Start, ulong pool. Size) – Creates a pool of fixed size memory blocks, p. Pool – total blocks = (pool. Size)/ (block. Size + sizeof(void *)) uint tx_block_pool_delete(TX_BLOCK_POOL *p. Pool) – Deletes a pool of fixed size memory blocks, p. Pool – Threads (suspended) waiting for memory from this pool are resumed and given a TX_DELETED status – Application’s responsibility to prevent threads from using memory in the former block pool’s memory region 4 -29

Block Pool Allocate/Release uint tx_block_allocate(TX_BLOCK_POOL *p. Pool, void **p. Block, ulong wait. Option) –

Block Pool Allocate/Release uint tx_block_allocate(TX_BLOCK_POOL *p. Pool, void **p. Block, ulong wait. Option) – Allocates a fixed size memory block from the pool pointed to by p. Pool – Possible values for wait. Option include: • TX_NO_WAIT (0 x 0000) • TX_WAIT_FOREVER (0 x. FFFF) • time-out value, in ticks (0 x 0000 0001 - 0 x. FFFF FFFE) uint tx_block_release(void *p. Block) – Releases the previously allocated block, p. Block, into the memory pool – Application’s responsibility to prevent threads from using released memory 4 -30

Memory Management - Byte Pools • Byte Pools – Non-deterministic – Memory allocated using

Memory Management - Byte Pools • Byte Pools – Non-deterministic – Memory allocated using ‘First-fit’ algorithm – Suffers from memory fragmentation – Publicly available resource but should *NOT* be used in ISRs – Pool control blocks, TX_BYTE_POOL, often defined globally 4 -31

Memory Byte Pools - Creation System Memory tx_byte_pool_create() HEAP • Total Size in Bytes

Memory Byte Pools - Creation System Memory tx_byte_pool_create() HEAP • Total Size in Bytes • Can Suspend if Unsuccessful • Very Similar to Block Pools • Except no Fixed-Sized Blocks • Flexibility in Location • Programmer Chooses Start Address 4 -32

Memory Byte Pools - Allocation Memory Byte Pool tx_byte_allocate() • Allocation Specified in Bytes

Memory Byte Pools - Allocation Memory Byte Pool tx_byte_allocate() • Allocation Specified in Bytes • Very Similar Concept as malloc() • Can Suspend if Unsuccessful -OR • Can Setup Time-Out Period • Wait for Another Thread to Free a Block 4 -33

Byte Pool Create/Delete uint tx_byte_pool_create(TX_BYTE_POOL *p. Pool, char *p. Name, void *p. Pool. Start,

Byte Pool Create/Delete uint tx_byte_pool_create(TX_BYTE_POOL *p. Pool, char *p. Name, void *p. Pool. Start, ulong pool. Size) – Creates a memory pool in the specified area, p. Pool. Start – Initially consists of one memory block, of size pool. Size – Broken into smaller blocks during de-fragmentation process uint tx_byte_pool_delete(TX_BYTE_POOL *p. Pool) – Deletes the byte pool, p. Pool – Threads (suspended) waiting for memory from this pool are resumed and given a TX_DELETED status – Application’s responsibility to prevent threads from using memory in the former byte pool’s memory region 4 -34

Byte Pool Allocate/Release uint tx_byte_allocate(TX_BYTE_POOL *p. Pool, void **p. Memory, ulong memory. Size, ulong

Byte Pool Allocate/Release uint tx_byte_allocate(TX_BYTE_POOL *p. Pool, void **p. Memory, ulong memory. Size, ulong wait. Option) – Allocates memory. Size bytes from the pool pointed to by p. Pool – Possible values for wait. Option include: • TX_NO_WAIT (0 x 0000) • TX_WAIT_FOREVER (0 x. FFFF) • time-out value, in ticks (0 x 0000 0001 - 0 x. FFFF FFFE) – Performance a function of pool fragmentation, non-deterministic uint tx_byte_release(void *p. Memory) – Releases the previously allocated memory, p. Memory, back into the pool – Application’s responsibility to prevent threads from using released memory 4 -35

Memory Management Summary • • 4 -36 Memory Management attributes – Two different types

Memory Management Summary • • 4 -36 Memory Management attributes – Two different types of memory allocation, fixed-size blocks and heap – Memory allocation information maintained in memory control blocks, TX_BLOCK_POOL or TX_BYTE_POOL Memory Management APIs – tx_block_pool_create() / tx_block_pool_delete() – tx_block_allocate() / tx_block_release() – tx_byte_pool_create() / tx_byte_pool_delete() – tx_byte_allocate() / tx_byte_release()

4. 3 Semaphores 4 -37

4. 3 Semaphores 4 -37

Semaphores 4 -38 • Thread. X supports 32 -bit counting semaphores (4, 294, 967,

Semaphores 4 -38 • Thread. X supports 32 -bit counting semaphores (4, 294, 967, 296!) • Typically used for mutual exclusion, can also be applied to event notification • Initializing a semaphore’s initial count to 1 creates a ‘binary semaphore’ • Semaphores must be used carefully in order to avoid deadlocks or priority inversion • Semaphore control blocks, TX_SEMAPHORE, often defined globally • Semaphores can be located anywhere in memory

Counting Semaphores 32 Bit Unsigned Global Variable Semaphore with Value X tx_semaphore_get() Semaphore with

Counting Semaphores 32 Bit Unsigned Global Variable Semaphore with Value X tx_semaphore_get() Semaphore with Value (X – 1) 4 -39 tx_semaphore_put() Semaphore with Value (X + 1)

Semaphore Create/Delete uint tx_semaphore_create(TX_SEMAPHORE *p. Semaphore, char *p. Name, ulong initial. Count) – Creates

Semaphore Create/Delete uint tx_semaphore_create(TX_SEMAPHORE *p. Semaphore, char *p. Name, ulong initial. Count) – Creates a counting semaphore – Semaphore’s count initialized to initial. Count uint tx_semaphore_delete(TX_SEMAPHORE *p. Semaphore) – Deletes the specified semaphore, p. Semaphore – Application’s responsibility to prevent threads from using a deleted semaphore – Threads (suspended) waiting for this semaphore are resumed and given a TX_DELETED status 4 -40

Semaphore Get/Put uint tx_semaphore_get(TX_SEMAPHORE *p. Semaphore, ulong wait. Option) – Retrieves an instance of

Semaphore Get/Put uint tx_semaphore_get(TX_SEMAPHORE *p. Semaphore, ulong wait. Option) – Retrieves an instance of the specified semaphore, p. Semaphore, depending upon availability and wait. Option – Semaphore’s count decremented by 1 – Possible values for wait. Option include: • TX_NO_WAIT (0 x 0000) – Return Immediately • TX_WAIT_FOREVER (0 x. FFFF) – Block, waiting for the semaphore • time-out value, in ticks (0 x 0000 0001 - 0 x. FFFF FFFE) – Block, waiting for the specified time uint tx_semaphore_put (TX_SEMAPHORE *p. Semaphore) – Puts an instance of the specified semaphore, p. Semaphore – Semaphore’s count incremented by 1 – If a semaphore’s count is 0 x. FFFF, new count will be 0 4 -41

A Closer Look At tx_semaphore_get() Kernel State Change Suspend Calling Thread Until Semaphore 0

A Closer Look At tx_semaphore_get() Kernel State Change Suspend Calling Thread Until Semaphore 0 tx_semaphore_get(Semaphore *) Yes Semaphore = 0 No Return to Kernel* Semaphore = Semaphore - 1 *Kernel Decides which Thread to Resume 4 -42 Kernel State Change

Semaphores in Action EXECUTE THREAD 1 tx_semaphore_get() CREATION Granted tx_semaphore_put() Granted Suspension Denied EXECUTE

Semaphores in Action EXECUTE THREAD 1 tx_semaphore_get() CREATION Granted tx_semaphore_put() Granted Suspension Denied EXECUTE THREAD 2 4 -43 CREATION tx_semaphore_get() Usage of Shared Resource

Example Semaphore Usage Declaration TX_SEMAPHORE serial; Initial Value ‘ 1’ for Binary Creation status

Example Semaphore Usage Declaration TX_SEMAPHORE serial; Initial Value ‘ 1’ for Binary Creation status = tx_semaphore_create (&serial, "Serial Port Use", 1); Name Get tx_semaphore_get (&serial, TX_WAIT_FOREVER); Wait Condition Put 4 -44 tx_semaphore_put (&serial);

Semaphore Summary • Semaphore attributes – Semaphore information maintained in semaphore control blocks, TX_SEMAPHORE

Semaphore Summary • Semaphore attributes – Semaphore information maintained in semaphore control blocks, TX_SEMAPHORE – No built in support for deadlock avoidance or priority-inversion handling • Semaphore APIs – tx_semaphore_create() / tx_semaphore_delete() – tx_semaphore_get() / tx_semaphore_put() 4 -45

4. 4 Event Flags 4 -46

4. 4 Event Flags 4 -46

Event Flags 4 -47 • Event flags provide a means of thread synchronization •

Event Flags 4 -47 • Event flags provide a means of thread synchronization • Classified in groups of 32, making up a single word • When a group is created, all 32 flags are initialized to 0 • Multiple threads can use the same group • Event flag control blocks, TX_EVENT_FLAGS_GROUP, often defined globally • Event flag groups can be located anywhere in memory

Event Flags 32 Bit Unsigned Global Variable 31 Each Bit Can Represents an Event

Event Flags 32 Bit Unsigned Global Variable 31 Each Bit Can Represents an Event or Condition tx_event_flags_set() Set with an AND / OR 31 0 The State of Event Flags Retrieved with tx_event_flags_get() 4 -48 0

Event Flags - Conceptual EXECUTE THREAD 1 CREATION Suspension (Need Indication of Event(s)) Event

Event Flags - Conceptual EXECUTE THREAD 1 CREATION Suspension (Need Indication of Event(s)) Event Notification RESUME ISR Get Data Event Notification ISR 4 -49 Get Data COMPLETE

Event Flags-Implementation EXECUTE THREAD 1 tx_event_flags_get() CREATION tx_event_flags_set() RESUME ISR Get Data tx_event_flags_set() ISR

Event Flags-Implementation EXECUTE THREAD 1 tx_event_flags_get() CREATION tx_event_flags_set() RESUME ISR Get Data tx_event_flags_set() ISR 4 -50 Get Data COMPLETE Kernel State Change

Example Event Flags Declaration TX_EVENT_FLAGS_GROUP tcp_sleep_events; Name Creation tx_event_flags_create (&tcp_sleep_events, "TCP Sleep Event Flags");

Example Event Flags Declaration TX_EVENT_FLAGS_GROUP tcp_sleep_events; Name Creation tx_event_flags_create (&tcp_sleep_events, "TCP Sleep Event Flags"); Set tx_event_flags_set(&tcp_sleep_events, (1 << i), TX_OR); Flag Mask Get 4 -51 Flags Copied Here Modificatio n Option tx_event_flags_get(&tcp_sleep_events, (1 << i), TX_OR_CLEAR, &flags, TX_WAIT_FOREVER); Wait Option

Event Flags Create/Delete uint tx_event_flags_create(TX_EVENT_FLAGS_GROUP *p. Group, char *p. Name) – Creates a group

Event Flags Create/Delete uint tx_event_flags_create(TX_EVENT_FLAGS_GROUP *p. Group, char *p. Name) – Creates a group of 32 flags, all initialized to 0 uint tx_event_flags_delete(TX_EVENT_FLAGS_GROUP *p. Group) – Deletes the specified event flag group, p. Group – Application’s responsibility to prevent use of a deleted event flag group – Threads (suspended) waiting for events from this group are resumed and given a TX_DELETED status 4 -52

Event Flags Get uint tx_event_flags_get (TX_EVENT_FLAGS_GROUP *p. Group, ulong requested. Flags, uint get. Option,

Event Flags Get uint tx_event_flags_get (TX_EVENT_FLAGS_GROUP *p. Group, ulong requested. Flags, uint get. Option, ulong *p. Actual. Flags, ulong wait. Option) – Retrieves event flags from the specified group, p. Group – The get. Option parameter allows control over which flags are returned, and possibly cleared – Possible values for get. Option include: • • TX_AND_CLEAR TX_OR_CLEAR – Possible values for wait. Option include: • TX_NO_WAIT (0 x 0000) – Return Immediately • TX_WAIT_FOREVER (0 x. FFFF) – Block, waiting for the event • time-out value, in ticks (0 x 0000 0001 - 0 x. FFFF FFFE) – Block, waiting for the specified time 4 -53

Event Flags Set uint tx_event_flags_set (TX_EVENT_FLAGS_GROUP *p. Group, ulong flags. To. Set, uint set.

Event Flags Set uint tx_event_flags_set (TX_EVENT_FLAGS_GROUP *p. Group, ulong flags. To. Set, uint set. Option) – Sets/clears event flags in the specified group, p. Group, depending upon the set. Option – Performs the set. Option between the current flags and flags. To. Set – Possible values for set. Option include: • TX_AND • TX_OR 4 -54

Event Flags Summary • Event Flag attributes – Event Flag information maintained in event

Event Flags Summary • Event Flag attributes – Event Flag information maintained in event flag control blocks, TX_EVENT_FLAGS_GROUP – Multiple threads and use (and be suspended on) the same event flag group • Event Flags APIs – tx_event_flags_create() / tx_event_flags_delete() – tx_event_flags_get() / tx_event_flags_set () 4 -55

4. 5 Message Queues 4 -56

4. 5 Message Queues 4 -56

Message Queues • Primary means of inter-thread communication in Thread. X • Message queues

Message Queues • Primary means of inter-thread communication in Thread. X • Message queues that hold a single message referred to as a ‘mailbox’ • Queues support messages of 1, 2, 4, 8, or 16 32 -bit sizes – Anything larger should use pointers • Messages are copied to and from queues – In the case of a suspended thread, message sent directly to queue 4 -57 • Queue control blocks, TX_QUEUE, often defined globally • Queues can be located anywhere in memory • To prevent memory corruption the application *must* ensure that its receiving memory is at least as large as the message size

Message Queues THREAD 1 CREATION Data Exchange THREAD 2 CREATION Message Queues Provide a

Message Queues THREAD 1 CREATION Data Exchange THREAD 2 CREATION Message Queues Provide a Convenient Means of Inter. Thread Communication Data Exchange is Done by Posting and Receiving Data from Shared ‘Mailboxes’ 4 -58

Message Queues - Conceptual EXECUTE THREAD 1 CREATION Suspension (Need Data from Other Process)

Message Queues - Conceptual EXECUTE THREAD 1 CREATION Suspension (Need Data from Other Process) RESUME Post Message Interrupt Service Routine 4 -59 Get Data From External Source COMPLETE

Message Queues-Implementation EXECUTE THREAD 1 Kernel State Change Interrupt Service Routine 4 -60 tx_queue_receive()

Message Queues-Implementation EXECUTE THREAD 1 Kernel State Change Interrupt Service Routine 4 -60 tx_queue_receive() CREATION Kernel State Change RESUME tx_queue_send() Get Data From External Source COMPLETE

Example Message Queue Declaration TX_QUEUE Data_Queue; Name Creation Send status = tx_queue_create (&Data_Queue, "UDP

Example Message Queue Declaration TX_QUEUE Data_Queue; Name Creation Send status = tx_queue_create (&Data_Queue, "UDP Send Data", TX_2_ULONG, Message_Pointer, 8); Mailbox Location Message Size status = tx_queue_send (&Data_Queue, package, TX_WAIT_FOREVER); Data Receive 4 -61 Wait Condition tx_queue_receive(&Data_Queue, package, TX_WAIT_FOREVER);

Message Queue Send/Receive uint tx_queue_send(TX_QUEUE *p. Queue, void *p. Source, ulong wait. Option) –

Message Queue Send/Receive uint tx_queue_send(TX_QUEUE *p. Queue, void *p. Source, ulong wait. Option) – Sends a message to p. Queue – Possible values for wait. Option include: • TX_NO_WAIT (0 x 0000) • TX_WAIT_FOREVER (0 x. FFFF) • time-out value, in ticks (0 x 0000 0001 - 0 x. FFFF FFFE) uint tx_queue_receive(TX_QUEUE *p. Queue, void *p. Destination, ulong wait. Option) – Retrieves a message from p. Queue – Possible values for wait. Option include: • TX_NO_WAIT (0 x 0000) • TX_WAIT_FOREVER (0 x. FFFF) • time-out value, in ticks (0 x 0000 0001 - 0 x. FFFF FFFE) 4 -62

Message Queue Flush uint tx_queue_flush(TX_QUEUE *p. Queue) – Deletes all messages in the queue,

Message Queue Flush uint tx_queue_flush(TX_QUEUE *p. Queue) – Deletes all messages in the queue, p. Queue – Threads (suspended) waiting for message from this queue are resumed, and given a status indicating the message send was successful 4 -63

Queue Summary • Message Queue attributes – Queue information maintained in queue control blocks,

Queue Summary • Message Queue attributes – Queue information maintained in queue control blocks, TX_QUEUE – Size specified during call to tx_queue_create() – Queues implemented as FIFOs • Message Queue APIs – tx_queue_create() / tx_queue_delete() – tx_queue_send() / tx_queue_receive() – tx_queue_flush() 4 -64

4. 6 Timers 4 -65

4. 6 Timers 4 -65

Timers • • • Thread. X supports both one-shot and periodic timers Hardware must

Timers • • • Thread. X supports both one-shot and periodic timers Hardware must generate periodic interrupts for proper timer functionality Timers are executed in the order they become active Timer control blocks, TX_TIMER, often defined globally Timers can be located anywhere in memory A Hardware Timer is used to derive the System Tick Rate The Timer is fix and can not be changed The System Tick Rate determines how oftenthe Scheduler will be called The System Tick Rate is set with #define BSP_TICKS_PER_SECOND which defaults to 100 Note: Increasing the Tick Rate increases the CPU time spent inside the Scheduler 4 -66

Timer Functionality Background Information – Net+OS Operation Time 10 ms Hardware Timer 1 Periodic

Timer Functionality Background Information – Net+OS Operation Time 10 ms Hardware Timer 1 Periodic Interrupt This is How the Operating System Handles All Time-Related Functionality. • Timeouts • Sleeps • Time Slices 4 -67

Application Timers Time 10 ms Run Our Routine Every 5 Ticks 4 -68 CRITICAL

Application Timers Time 10 ms Run Our Routine Every 5 Ticks 4 -68 CRITICAL PARAMETERS C Routine Entry Parameter Start Delay Frequency tx_timer_create()

Timer Create/Delete uint tx_timer_create(TX_TIMER *p. Timer, char *p. Name, void *p. Expiration. Func(ulong), ulong

Timer Create/Delete uint tx_timer_create(TX_TIMER *p. Timer, char *p. Name, void *p. Expiration. Func(ulong), ulong expiration. Input, ulong initial. Ticks, ulong reschedule. Ticks, uint auto. Activate) – Creates a timer that executes p. Expiration. Func upon timer expiry – Initial ticks can range from 0 x 0000 0001 – 0 x. FFFF – Timer created in TX_AUTO_ACTIVATE or TX_NO_ACTIVATE states – Assign 0 to reschedule. Ticks to make the timer ‘one-shot’, otherwise reschedule. Ticks indicates the timer period after the first period uint tx_timer_delete(TX_TIMER *p. Timer) – Deletes the specified timer, p. Timer – Application’s responsibility to prevent threads from using a deleted timer 4 -69

Timer Activate/Deactivate/Change uint tx_timer_activate (TX_TIMER *p. Timer) – Activates the specified timer, p. Timer

Timer Activate/Deactivate/Change uint tx_timer_activate (TX_TIMER *p. Timer) – Activates the specified timer, p. Timer uint tx_timer_deactivate (TX_TIMER *p. Timer) – Deactivates the specified timer, p. Timer uint tx_timer_change(TX_TIMER *p. Timer, ulong initial. Ticks, ulong reschedule. Ticks) – Changes the expiration attributes of the specified timer, p. Timer – Timer must be deactivated before calling this routine, and activated later to restart 4 -70

Timer Summary • Timer attributes – Timer information maintained in timer control blocks, TX_TIMER

Timer Summary • Timer attributes – Timer information maintained in timer control blocks, TX_TIMER – Timers can have a different initial expiry time than their periodic rate • Timer APIs – tx_timer_create() / tx_timer_delete() – tx_timer_activate() / tx_time_deactivate() – tx_timer_change() 4 -71

System Tick Timer Implementation • Initialization takes place in bsptimer. c in functionnetos. Setup.

System Tick Timer Implementation • Initialization takes place in bsptimer. c in functionnetos. Setup. System. Clock() void netos. Setup. System. Clock (void) { /* Declaration for standard Thread. X timer ISR. */ extern int _tx_timer_interrupt (void *); #ifdef NS 9750 MCSet. Timer. Clock. Select(THREADX_TIMER, 0); /* set timer clock #0 select to CPU */ MCSet. Timer. Mode(THREADX_TIMER, 0); /* set timer clock #0 to internal timer */ MCSet. Timer. Interrupt. Select(THREADX_TIMER, 1); /* enable timer clock #0 interrupt */ MCSet. Timer. Up. Down. Select(THREADX_TIMER, 1); /* set timer clock #0 as a downcounter */ MCSet. Timer. Bit(THREADX_TIMER, 1); /* set timer as 32 bit timer */ MCSet. Timer. Reload. Enable(THREADX_TIMER, 1); /* set timer reload enable */ MCReload. Timer. Counter(THREADX_TIMER, THREADX_TIMER 0_RELOAD_VALUE); /* netos. Install. Timer 0 Isr (_tx_timer_interrupt); */ MCInstall. Isr(TIMER 0_INTERRUPT, _tx_timer_interrupt, NULL); MCEnable. Timer(THREADX_TIMER); #else. . . 4 -72

4. 7 Other Kernel Services 4 -73

4. 7 Other Kernel Services 4 -73

Other Kernel Services 4 -74 • Allows the user to initialize the system clock

Other Kernel Services 4 -74 • Allows the user to initialize the system clock to a known value • Allows the user to get the current value of the system clock • Allows the user to enable/disable interrupts

Time Get/Set ulong tx_time_get (void) – Returns the contents of the internal system clock

Time Get/Set ulong tx_time_get (void) – Returns the contents of the internal system clock – System clock initialized to 0 during system startup void tx_time_set(ulong new. Timer) – Sets the internal system clock – Valid ranges are 0 x 0000 – 0 x. FFFF 4 -75

Interrupt Control uint tx_interrupt_control(uint interrupt. Posture) – Enables or disables interrupts depending upon the

Interrupt Control uint tx_interrupt_control(uint interrupt. Posture) – Enables or disables interrupts depending upon the input, TX_INT_ENABLE or TX_INT_DISABLE – The interrupt. Posture is part of a thread’s context – This should NOT be called during system initialization 4 -76

Kernel Services Summary • Kernel Service APIs – tx_time_set() / tx_time_get() – tx_interrupt_control() 4

Kernel Services Summary • Kernel Service APIs – tx_time_set() / tx_time_get() – tx_interrupt_control() 4 -77

Summary 4 -78

Summary 4 -78

Bringing up the Thread. X Kernel … • … is quite easy • Assumes

Bringing up the Thread. X Kernel … • … is quite easy • Assumes a properly initialized C runtime environment (this is usually the case when entering main()) • Starting the Thread. X Kernel only needs a call to tx_kernel_enter() • Besides that a function of name tx_application_define() must be declared • tx_application_define is called from within the Thread. X library 4 -79

Bringing up the Thread. X Kernel (cont. ) tx_application_define() example implementation: void tx_application_define(void *first_unused_memory)

Bringing up the Thread. X Kernel (cont. ) tx_application_define() example implementation: void tx_application_define(void *first_unused_memory) { /* Setup Thread. X tick timer for a 1 ms tick period */ Setup. Os. Timer(OS_TICKRATE ); /* Create the root thread. */ tx_thread_create(&root. Thread. CB , "root. Thread", root. Thread, 0, root. Thread. Stack, THREAD_STACK_SIZE, 1, 1, 1, TX_AUTO_START); } 4 -80 /* /* /* control block for thread name entry function parameter start of stack size of stack priority preemption threshold time slice threshold start immediately */ */ */

Bringing up the Thread. X Kernel (cont. ) Setting up the System Tick Timer

Bringing up the Thread. X Kernel (cont. ) Setting up the System Tick Timer (NS 7520): int Setup. Os. Timer( unsigned long ticks. Per. Second ) { unsigned long tmp; if (ticks. Per. Second < 1) return (OS_TIMER_EINVAL); *(unsigned long *)0 xffb 00010 = 0; *(unsigned long *)0 xffb 00010 |= 0 x 08000000; /* use IRQ not FIQ to interrupt */ /* use sys clock as clock source */ tmp = (55000000 / ticks. Per. Second) - 1; *(unsigned long *)0 xffb 00010 |= tmp; /*--- set handler ---*/ Set. Int. Hdlr(TIMER 1_INT, _ tx_timer_interrupt); *(unsigned long *)0 xffb 00030 |= 0 x 00000020; *(unsigned long *)0 xffb 00010 |= 0 x 40000000; *(unsigned long *)0 xffb 00010 |= 0 x 80000000; return (OS_TIMER_ERR_OK); 4 -81 } /* enable the interrupt in the */ /* interrupt enable register. */ /* enable interrupt */ /* enable timer */