Thread XUser Guide
Thread XUser Guide
User Guide
http://www.expresslogic.com
©1997-2003 by Express Logic, Inc.
All rights reserved. This document and the associated ThreadX software are the sole property of
Express Logic, Inc. Each contains proprietary information of Express Logic, Inc. Reproduction or
duplication by any means of any portion of this document without the prior written consent of Express
Logic, Inc. is expressly forbidden.
Express Logic, Inc. reserves the right to make changes to the specifications described herein at any
time and without notice in order to improve design or reliability of ThreadX. The information in this
document has been carefully checked for accuracy; however, Express Logic, Inc. makes no warranty
pertaining to the correctness of this document.
Trademarks
ThreadX is a registered trademark of Express Logic, Inc., and picokernel, and preemption-threshold
are trademarks of Express Logic, Inc.
All other product and company names are trademarks or registered trademarks of their respective
holders.
Warranty Limitations
Express Logic, Inc. makes no warranty of any kind that the ThreadX products will meet the USER’s
requirements, or will operate in the manner specified by the USER, or that the operation of the
ThreadX products will operate uninterrupted or error free, or that any defects that may exist in the
ThreadX products will be corrected after the warranty period. Express Logic, Inc. makes no warranties
of any kind, either expressed or implied, including but not limited to the implied warranties of
merchantability and fitness for a particular purpose, with respect to the ThreadX products. No oral or
written information or advice given by Express Logic, Inc., its dealers, distributors, agents, or
employees shall create any other warranty or in any way increase the scope of this warranty, and
licensee may not rely on any such information or advice.
1 Introduction to ThreadX 21
1 ThreadX Unique Features 22
picokernel™ Architecture 22
ANSI C Source Code 22
Not A Black Box 22
A Potential Standard 23
1 Embedded Applications 23
Real-time Software 23
Multitasking 24
Tasks vs. Threads 24
1 ThreadX Benefits 25
Improved Responsiveness 25
Software Maintenance 26
Increased Throughput 26
Processor Isolation 26
Dividing the Application 27
Ease of Use 27
Improve
User Guide
4 ThreadX
Time-to-market 27
Protecting the Software Investment 27
User Guide
Contents 5
Thread Scheduling 52
Round-Robin Scheduling 52
Time-Slicing 53
Preemption 53
Preemption- Threshold™ 54
Priority Inheritance 55
Thread Creation 55
Thread Control Block TX_THREAD 55
Currently Executing Thread 57
Thread Stack Area 57
Memory Pitfalls 59
Reentrancy 60
Thread Priority Pitfalls 60
Priority Overhead 62
Debugging Pitfalls 63
1 Message Queues 63
Creating Message Queues 64
Message Size 64
Message Queue Capacity 64
Queue Memory Area 64
Thread Suspension 65
Queue Control Block TX_QUEUE 65
Message Destination Pitfall 66
1 Counting Semaphores 66
Mutual Exclusion 66
Event Notification 67
Creating Counting Semaphores 67
Thread Suspension 67
Semaphore Control Block TX_SEMAPHORE 68
Deadly Embrace 68
Priority Inversion 70
1 Mutexes 70
Mutex Mutual Exclusion 71
Creating Mutexes 71
Thread Suspension 71
Mutex Control Block TX_MUTEX 72
Deadly Embrace 72
Priority Inversion 72
1 Event Flags 73
Creating Event Flag Groups 74
Thread Suspension 74
Event Flag Group Control Block
TX_EVENT_FLAGS_GROUP 74
1 Memory Block Pools 75
Creating Memory Block Pools 75
Memory Block Size 76
Pool Capacity 76
Pool’s Memory Area 76
Thread Suspension 76
Memory Block Pool Control Block
TX_BLOCK_POOL 77
Overwriting Memory Blocks 77
1 Memory Byte Pools 77
Creating Memory Byte Pools 78
Pool Capacity 78
Pool’s Memory Area 79
Thread Suspension 79
Memory Byte Pool Control Block
TX_BYTE_POOL 80
Un-deterministic Behavior 80
Overwriting Memory Blocks 80
1 Application Timers 81
Timer Intervals 81
Timer Accuracy 82
Timer Execution 82
Creating Application Timers 82
Application Timer Control Block TX_TIMER 82
Excessive Timers 83
1 Relative Time 83
1 Interrupts 83
Interrupt Control 84
ThreadX Managed Interrupts 84
ISR Template 85
High-Frequency Interrupts 86
Interrupt Latency 86
User Guide
Contents 7
1 Thread 2 240
1 Threads 3 and 4 241
1 Thread 5 241
1 Threads 6 and 7 242
1 Observing the Demonstration 242
1 Distribution file: demo.c 243
User Guide
Contents 9
TX_TA.C 275
TX_TAA.C 276
TX_TD.C 276
TX_TDA.C 276
TX_TIMCH.C 276
TX_TIMCR.C 276
TX_TIMD.C 276
TX_TIMEG.C 276
TX_TIMES.C 276
TX_TIMI.C 277
TX_TIMIG.C 277
TX_TIMIN.[S,ASM] 277
TX_TTE.C 277
TXE_TAA.C 277
TXE_TDA.C 277
TXE_TIMD.C 277
TXE_TIMI.C 277
TXE_TMCH.C 278
TXE_TMCR.C 278
1 Queue Component 278
TX_QUE.H 278
TX_QC.C 278
TX_QCLE.C 279
TX_QD.C 279
TX_QF.C 279
TX_QFS.C 279
TX_QI.C 279
TX_QIG.C 279
TX_QP.C 279
TX_QR.C 279
TX_QS.C 280
TXE_QC.C 280
TXE_QD.C 280
TXE_QF.C 280
TXE_QFS.C 280
TXE_QIG.C 280
TXE_QP.C 280
TXE_QR.C 280
TXE_QS.C 281
1 Semaphore Component 281
TX_SEM.H 281
User Guide
Contents 11
TX_SC.C 281
TX_SCLE.C 282
TX_SD.C 282
TX_SG.C 282
TX_SI.C 282
TX_SIG.C 282
TX_SP.C 282
TX_SPRI.C 282
TXE_SC.C 282
TXE_SD.C 283
TXE_SG.C 283
TXE_SIG.C 283
TXE_SP.C 283
TXE_SPRI.C 283
1 Mutex Component 283
TX_MUT.H 283
TX_MC.C 284
TX_MCLE.C 284
TX_MD.C 284
TX_MG.C 284
TX_MI.C 284
TX_MIG.C 285
TX_MP.C 285
TX_MPC.C 285
TX_MPRI.C 285
TXE_MC.C 285
TXE_MD.C 285
TXE_MG.C 285
TXE_MIG.C 285
TXE_MP.C 286
TXE_MPRI.C 286
1 Event Flag Component 286
TX_EVE.H 286
TX_EFC.C 287
TX_EFCLE.C 287
TX_EFD.C 287
TX_EFG.C 287
TX_EFI.C 287
TX_EFIG.C 287
TX_EFS.C 287
TXE_EFC.C 287
TXE_EFD.C 288
TXE_EFG.C 288
TXE_EFIG.C 288
TXE_EFS.C 288
1 Block Memory Component 288
TX_BLO.H 288
TX_BA.C 289
TX_BPC.C 289
TX_BPCLE.C 289
TX_BPD.C 289
TX_BPI.C 289
TX_BPIG.C 289
TX_BPP.C 290
TX_BR.C 290
TXE_BA.C 290
TXE_BPC.C 290
TXE_BPD.C 290
TXE_BPIG.C 290
TXE_BPP.C 290
TXE_BR.C 290
1 Byte Memory Component 291
TX_BYT.H 291
TX_BYTA.C 291
TX_BYTC.C 291
TX_BYTCL.C 292
TX_BYTD.C 292
TX_BYTI.C 292
TX_BYTIG.C 292
TX_BYTPP.C 292
TX_BYTR.C 292
TX_BYTS.C 292
TXE_BTYA.C 293
TXE_BYTC.C 293
TXE_BYTD.C 293
TXE_BYTG.C 293
TXE_BYTP.C 293
TXE_BYTR.C 293
User Guide
Contents 13
Index 323
User Guide
Figures
Figure 1 Template for Application Development 35
Figure 2 Types of Program Execution 43
Figure 3 Memory Area Example 45
Figure 4 Initialization Process 49
Figure 5 Thread State Transition 51
Figure 6 Typical Thread Stack 58
Figure 7 Stack Preset to 0xEFEF 59
Figure 8 Example of Suspended Threads 69
Figure 9 Simple Driver Initialization 226
Figure 10 Simple Driver Input 227
Figure 11 Simple Driver Output 228
Figure 12 Logic for Circular Input Buffer 230
Figure 13 Logic for Circular Output Buffer 231
Figure 14 I/O Buffer 232
Figure 15 Input-Output Lists 234
Figure 16 ThreadX File Header Example 263
User Guide
16 ThreadX
User Guide
About This Guide
This guide provides comprehensive information
about ThreadX, the high-performance real-time
kernel from Express Logic, Inc.
It is intended for the embedded real-time software
developer. The developer should be familiar with
standard real-time operating system functions and
the C programming language.
Organization
Chapter 1 Provides a basic overview of
ThreadX and its relationship to
real-time embedded
development.
Chapter 2 Gives the basic steps to install
and use ThreadX in your
application right out of the box.
Chapter 3 Describes in detail the functional
operation of ThreadX, the high-
performance real-time kernel.
Chapter 4 Details the application’s
interface to ThreadX.
Chapter 5 Describes writing I/O drivers for
ThreadX applications.
Chapter 6 Describes the demonstration
application that is supplied with
every ThreadX processor
support package.
User Guide
18 ThreadX
Guide Conventions
Italics typeface denotes book titles,
emphasizes important words,
and indicates variables.
i attention to important or
additional information that could
affect performance or function.
User Guide
About This Guide 19
User Guide
CHAPTER 1
Introduction to ThreadX
User Guide
22 Introduction to ThreadX
User Guide
Embedded Applications 23
Embedded Applications
What is an embedded application? Embedded
applications are applications that execute on
microprocessors buried inside of products like
cellular phones, communication equipment,
automobile engines, laser printers, medical devices,
etc. Another distinction of embedded applications is
that their software and hardware have a dedicated
purpose.
Tasks vs. Threads Another distinction about tasks must be made. The
term task is used in a variety of ways. It sometimes
means a separately loadable program. In other
instances, it might refer to an internal program
segment.
User Guide
ThreadX Benefits 25
ThreadX Benefits
Using ThreadX provides many benefits to embedded
applications. Of course, the primary benefit rests in
how embedded application threads are allocated
processing time.
User Guide
ThreadX Benefits 27
User Guide
CHAPTER 2
User Guide
30 Installation and Use of ThreadX
Host Considerations
Embedded development is usually performed on
IBM-PC or Unix host computers. After the application
is compiled, linked, and located on the host, it is
downloaded to the target hardware for execution.
Target Considerations
ThreadX requires between 2 KBytes and 20 KBytes
of Read Only Memory (ROM) on the target. Another
1 to 2 KBytes of the target’s Random Access
Memory (RAM) are required for the ThreadX system
stack and other global data structures.
User Guide
Product Distribution 31
Product Distribution
ThreadX is shipped on a single CD-ROM compatible
disk. Two types of ThreadX packages are
available—standard and premium. The standard
package includes minimal source code; while the
premium package contains complete ThreadX
source code.
ThreadX Installation
Installation of ThreadX is straightforward. The
following instructions apply to virtually any
installation. However, examine the readme.txt file for
changes specific to the actual development tool
environment.
Backup the ThreadX distribution disk and store it in a
Step 1:
safe location.
On the host hard drive, make a directory called
Step 2: “threadx” or something similar. The ThreadX kernel
files will reside in this directory.
User Guide
Using ThreadX 33
Using ThreadX
Using ThreadX is easy. Basically, the application
code must include tx_api.h during compilation and
link with the ThreadX run-time library tx.lib.
User Guide
Small Example System 35
#include "tx_api.h"
main( )
{
/* Enter the ThreadX kernel. */
tx_kernel_enter( );
}
/* Create my_thread! */
tx_thread_create(&my_thread, "My Thread",
my_thread_entry, 0x1234, first_unused_memory, 1024,
3, 3, TX_NO_TIME_SLICE, TX_AUTO_START);
}
Troubleshooting
Each ThreadX port is delivered with a demonstration
application. It is always a good idea to first get the
demonstration system running—either on actual
target hardware or the specific demonstration
environment.
Configuration Options
There is really only one generic configuration option
for ThreadX, and it is used to bypass service call
error checking. If the condition compilation flag
TX_DISABLE_ERROR_CHECKING is defined
within an application C file, all basic parameter error
checking is disabled. This option is used to improve
performance (by as much as 30%). However, this
should be done only after the application is
thoroughly debugged.
User Guide
ThreadX Version ID 37
ThreadX Version ID
The current version of ThreadX is available to both
the user and the application software during run-time.
The programmer can find the ThreadX version in the
readme.txt file. This file also contains a version
history of the corresponding port. Application software
can obtain the ThreadX version by examining the
global string _tx_version_id.
User Guide
CHAPTER 3
Functional Components of
ThreadX
User Guide
40 Functional Components of ThreadX
User Guide
41
Execution Overview
There are four types of program execution within a
ThreadX application: Initialization, Thread Execution,
Interrupt Service Routines (ISRs), and Application
Timers.
User Guide
Execution Overview 43
Execution Overview
Hardware
Reset
Initialization
Thread
Execution
Interrupt Application
Service
Routines Timers
Memory Usage
ThreadX resides along with the application program.
As a result, the static memory (or fixed memory)
usage of ThreadX is determined by the development
tools; e.g., the compiler, linker, and locator. Dynamic
memory (or run-time memory) usage is under direct
control of the application.
User Guide
Memory Usage 45
0x00000000
Instruction Area
ROM
Constant Area
ROM
Indicates ThreadX
Usage
Initialization
Understanding the initialization process is very
important. The initial hardware environment is setup
here. In addition, this is where the application is
given its initial personality.
User Guide
Initialization 47
Thread Execution
Scheduling and executing application threads is the
most important activity of ThreadX. What exactly is a
thread? A thread is typically defined as semi-
User Guide
Thread Execution 49
entry point*
main( )
tx_kernel_enter( )
tx_application_define(mem_ptr)
Enter thread
scheduling loop
* denotes functions that are
development tool-specific
User Guide
Thread Execution 51
tx_thread_create
TX_AUTO_START TX_DONT_START
Services
Ready with Suspension Suspended
State State
Thread
Scheduling
Self
Suspend Terminate
Service
Executing
State
Self
Terminate
Return
From Thread
Entry Function
Completed Terminated
State State
User Guide
Thread Execution 53
User Guide
Thread Execution 55
TX_READY (0x00)
TX_COMPLETED (0x01)
TX_TERMINATED (0x02)
TX_SUSPENDED (0x03)
TX_SLEEP (0x04)
TX_QUEUE_SUSP (0x05)
TX_SEMAPHORE_SUSP (0x06)
TX_EVENT_FLAG (0x07)
TX_BLOCK_MEMORY (0x08)
TX_BYTE_MEMORY (0x09)
TX_MUTEX_SUSP (0x0D)
TX_IO_DRIVER (0x0A)
User Guide
Thread Execution 57
Thread Stack Area Each thread must have its own stack for saving the
context of its last execution and compiler use. Most C
compilers use the stack for making function calls and
for temporarily allocating local variables. Figure 6
shows a typical thread’s stack.
Typical
run-time
stack
growth
tx_stack_ptr
Thread’s last
execution context
User Guide
Thread Execution 59
Thread’s last
execution context
User Guide
Thread Execution 61
User Guide
Message Queues 63
Message Queues
Message queues are the primary means of inter-
thread communication in ThreadX. One or more
messages can reside in a message queue. A
message queue that holds a single message is
commonly called a mailbox.
User Guide
Message Queues 65
Counting Semaphores
ThreadX provides 32-bit counting semaphores that
range in value between 0 and 4,294,967,295. There
are two operations for counting semaphores:
tx_semaphore_get and tx_semaphore_put. The get
operation decreases the semaphore by one. If the
semaphore is 0, the get operation is not successful.
The inverse of the get operation is the put operation.
It increases the semaphore by one.
User Guide
Counting Semaphores 67
User Guide
Counting Semaphores 69
Deadly Embrace
(example)
First Second
Semaphore Semaphore
owned by owned by
first thread second thread
attempt to attempt to
get second get first
semaphore semaphore
Mutexes
In addition to semaphores, ThreadX also provides a
mutex object. A mutex is basically a binary
semaphore, which means that only one thread can
own a mutex at a time. In addition, the same thread
may perform a successful mutex get operation on an
owned mutex multiple times, 4,294,967,295 to be
exact. There are two operations on the mutex object,
namely tx_mutex_get and tx_mutex_put. The get
operation obtains a mutex not owned by another
thread, while the put operation releases a previously
obtained mutex. In order for a thread to release a
mutex, the number of put operations must equal the
number of prior get operations.
User Guide
Mutexes 71
User Guide
Event Flags 73
Event Flags
Event flags provide a powerful tool for thread
synchronization. Each event flag is represented by a
single bit. Event flags are arranged in groups of 32.
Event Flag Group The characteristics of each event flag group are
Control Block found in its control block. It contains information such
TX_EVENT_FLAGS_GROUP as the current event flag settings and the number of
threads suspended for events. This structure is
defined in the tx_api.h file.
User Guide
Memory Block Pools 75
Pool’s Memory As mentioned before, the memory area for the block
Area pool is specified during creation. Like other memory
areas in ThreadX, it can be located anywhere in the
target’s address space.
User Guide
Memory Byte Pools 77
User Guide
Memory Byte Pools 79
Pool’s Memory The memory area for a memory byte pool is specified
Area during creation. Like other memory areas in
ThreadX, it can be located anywhere in the target’s
address space.
Memory Byte Pool The characteristics of each memory byte pool are
Control Block found in its control block. It contains useful
TX_BYTE_POOL information such as the number of available bytes in
the pool. This structure is defined in the tx_api.h file.
User Guide
Application Timers 81
Application Timers
Fast response to asynchronous external events is
the most important function of real-time, embedded
applications. However, many of these applications
must also perform certain activities at pre-determined
intervals of time.
User Guide
Relative Time 83
Relative Time
In addition to the application timers mentioned
previously, ThreadX provides a single continuously
incrementing 32-bit tick counter. The tick counter or
time is increased by one on each timer interrupt.
Interrupts
Fast response to asynchronous events is the
principal function of real-time, embedded
applications. How does the application know such an
event is present? Typically, this is accomplished
through hardware interrupts.
User Guide
Interrupts 85
tx_event_flags_set
tx_interrupt_control
tx_queue_front_send
tx_queue_info_get
tx_queue_prioritize
tx_queue_receive
tx_queue_send
tx_semaphore_get
tx_semaphore_info_get
tx_semaphore_prioritize
tx_semaphore_put
tx_thread_identify
tx_thread_info_get
tx_thread_resume
tx_thread_wait_abort
tx_time_get
tx_time_set
tx_timer_activate
tx_timer_change
tx_timer_deactivate
tx_timer_info_get
_application_ISR_entry:
; Save context and prepare for
; ThreadX use by calling the ISR
; entry function.
CALL __tx_thread_context_save
User Guide
CHAPTER 4
Description of ThreadX
Services
User Guide
88 Description of ThreadX Services
tx_byte_pool_create
Create a memory pool of bytes 108
tx_byte_pool_delete
Delete a memory pool of bytes 110
tx_byte_pool_info_get
Retrieve information about byte pool 112
tx_byte_pool_prioritize
Prioritize the byte pool suspension list 114
tx_byte_release
Release bytes back to memory pool 116
tx_event_flags_create
Create an event flag group 118
tx_event_flags_delete
Delete an event flag group 120
tx_event_flags_get
Get event flags from event flag group 122
tx_event_flags_info_get
Retrieve information about event flags group 126
tx_event_flags_set
Set event flags in an event flag group 128
tx_interrupt_control
Enables and disables interrupts 130
tx_mutex_create
Create a mutual exclusion mutex 132
tx_mutex_delete
Delete a mutual exclusion mutex 134
tx_mutex_get
Obtain ownership of a mutex 136
tx_mutex_info_get
Retrieve information about a mutex 138
tx_mutex_prioritize
Prioritize mutex suspension list 140
User Guide
89
tx_mutex_put
Release ownership of mutex 142
tx_queue_create
Create a message queue 144
tx_queue_delete
Delete a message queue 146
tx_queue_flush
Empty messages in a message queue 148
tx_queue_front_send
Send a message to the front of queue 150
tx_queue_info_get
Retrieve information about a queue 152
tx_queue_prioritize
Prioritize queue suspension list 154
tx_queue_receive
Get a message from message queue 156
tx_queue_send
Send a message to message queue 160
tx_semaphore_create
Create a counting semaphore 162
tx_semaphore_delete
Delete a counting semaphore 164
tx_semaphore_get
Get instance from counting semaphore 166
tx_semaphore_info_get
Retrieve information about a semaphore 168
tx_semaphore_prioritize
Prioritize semaphore suspension list 170
tx_semaphore_put
Place an instance in counting semaphore 172
tx_thread_create
Create an application thread 174
tx_thread_delete
Delete an application thread 178
tx_thread_identify
Retrieves pointer to currently executing thread
180
tx_thread_info_get
Retrieve information about a thread 182
tx_thread_preemption_change
Change preemption-threshold of thread 186
tx_thread_priority_change
Change priority of an application thread 188
tx_thread_relinquish
Relinquish control to other application threads 190
tx_thread_resume
Resume suspended application thread 192
tx_thread_sleep
Suspended current thread for specified time 194
tx_thread_suspend
Suspend an application thread 196
tx_thread_terminate
Terminates an application thread 198
tx_thread_time_slice_change
Changes time-slice of application thread 200
tx_thread_wait_abort
Abort suspension of specified thread 202
tx_time_get
Retrieves the current time 204
tx_time_set
Sets the current time 206
tx_timer_activate
Activate an application timer 208
tx_timer_change
Change an application timer 210
User Guide
91
tx_timer_create
Create an application timer 212
tx_timer_deactivate
Deactivate an application timer 214
tx_timer_delete
Delete an application timer 216
tx_timer_info_get
Retrieve information about application timer 218
tx_block_allocate
Allocate a fixed-size block of memory
Memory Blocks
Prototype
UINT tx_block_allocate(TX_BLOCK_POOL *pool_ptr, VOID **block_ptr,
ULONG wait_option)
Description
This service allocates a fixed-size memory block from the specified
memory pool. The actual size of the memory block is determined during
memory pool creation.
Input Parameters
pool_ptr Pointer to a previously created memory block
pool.
block_ptr Pointer to a destination block pointer. On
successful allocation, the address of the
allocated memory block is placed where this
parameter points to.
wait_option Defines how the service behaves if there are no
memory blocks available. The wait options are
defined as follows:
TX_NO_WAIT (0x00000000)
TX_WAIT_FOREVER (0xFFFFFFFF)
timeout value (0x00000001 through
0xFFFFFFFE)
Selecting TX_NO_WAIT results in an immediate
return from this service regardless of whether or
not it was successful. This is the only valid option
if the service is called from a non-thread; e.g.,
Initialization, timer, or ISR.
Selecting TX_WAIT_FOREVER causes the
calling thread to suspend indefinitely until a
memory block is available.
Selecting a numeric value (1-0xFFFFFFFE)
specifies the maximum number of timer-ticks to
stay suspended while waiting for a memory
block.
User Guide
Memory Blocks 93
Return Values
TX_SUCCESS (0x00) Successful memory block allocation.
TX_DELETED (0x01) Memory block pool was deleted while
thread was suspended.
TX_NO_MEMORY (0x10) Service was unable to allocate a block
of memory.
TX_WAIT_ABORTED (0x1A) Suspension was aborted by another
thread, timer or ISR.
TX_POOL_ERROR (0x02) Invalid memory block pool pointer.
TX_PTR_ERROR (0x03) Invalid pointer to destination pointer.
TX_WAIT_ERROR (0x04) A wait option other than TX_NO_WAIT
was specified on a call from a non-
thread.
Allowed From
Initialization, threads, timers, and ISRs
Preemption Possible
Yes
Example
TX_BLOCK_POOL my_pool;
unsigned char *memory_ptr;
UINT status;
/* Allocate a memory block from my_pool. Assume that the
pool has already been created with a call to
tx_block_pool_create. */
status = tx_block_allocate(&my_pool, (VOID **) &memory_ptr,
TX_NO_WAIT);
See Also
tx_block_pool_create, tx_block_pool_delete, tx_block_pool_info_get,
tx_block_pool_prioritize, tx_block_release
tx_block_pool_create
Create a pool of fixed-size memory blocks
Prototype
UINT tx_block_pool_create(TX_BLOCK_POOL *pool_ptr,
CHAR *name_ptr, ULONG block_size,
VOID *pool_start, ULONG pool_size)
Description
This service creates a pool of fixed-size memory blocks. The memory
area specified is divided into as many fixed-size memory blocks as
possible using the formula:
total blocks = (total bytes) / (block size + sizeof(void *))
Input Parameters
pool_ptr Pointer to a memory block pool control block.
name_ptr Pointer to the name of the memory block pool.
block_size Number of bytes in each memory block.
pool_start Starting address of the memory block pool.
pool_size Total number of bytes available for the memory
block pool.
User Guide
Memory Blocks 95
Return Values
TX_SUCCESS (0x00) Successful memory block pool
creation.
TX_POOL_ERROR (0x02) Invalid memory block pool pointer.
Either the pointer is NULL or the pool
is already created.
TX_PTR_ERROR (0x03) Invalid starting address of the pool.
TX_SIZE_ERROR (0x05) Size of pool is invalid.
TX_CALLER_ERROR (0x13) Invalid caller of this service.
Allowed From
Initialization and threads
Preemption Possible
No
Example
TX_BLOCK_POOL my_pool;
UINT status;
/* Create a memory pool whose total size is 1000 bytes
starting at address 0x100000. Each block in this
pool is defined to be 50 bytes long. */
status = tx_block_pool_create(&my_pool, "my_pool_name",
50, (VOID *) 0x100000, 1000);
See Also
tx_block_allocate, tx_block_pool_delete, tx_block_pool_info_get,
tx_block_pool_prioritize, tx_block_release
tx_block_pool_delete
Delete fixed-size block of memory pool
Prototype
UINT tx_block_pool_delete(TX_BLOCK_POOL *pool_ptr)
Description
This service deletes the specified block-memory pool. All threads
suspended waiting for a memory block from this pool are resumed and
given a TX_DELETED return status.
i associated with the pool, which is available after this service completes.
In addition, the application must prevent use of a deleted pool or its
former memory blocks.
Input Parameters
pool_ptr Pointer to a previously created memory block
pool.
Return Values
TX_SUCCESS (0x00) Successful memory block pool
deletion.
TX_POOL_ERROR (0x02) Invalid memory block pool pointer.
TX_CALLER_ERROR (0x13) Invalid caller of this service.
Allowed From
Threads
Preemption Possible
Yes
User Guide
Memory Blocks 97
Example
TX_BLOCK_POOL my_pool;
UINT status;
/* Delete entire memory block pool. Assume that the pool
has already been created with a call to
tx_block_pool_create. */
status = tx_block_pool_delete(&my_pool);
See Also
tx_block_allocate, tx_block_pool_create, tx_block_pool_info_get,
tx_block_pool_prioritize, tx_block_release
tx_block_pool_info_get
Retrieve information about block pool
Memory Blocks
Prototype
UINT tx_block_pool_info_get(TX_BLOCK_POOL *pool_ptr, CHAR **name,
ULONG *available, ULONG *total_blocks,
TX_THREAD **first_suspended,
ULONG *suspended_count,
TX_BLOCK_POOL **next_pool)
Description
This service retrieves information about the specified block memory pool.
Input Parameters
pool_ptr Pointer to previously created memory block pool.
name Pointer to destination for the pointer to the block
pool’s name.
available Pointer to destination for the number of available
blocks in the block pool.
total_blocks Pointer to destination for the total number of
blocks in the block pool.
first_suspended Pointer to destination for the pointer to the thread
that is first on the suspension list of this block
pool.
suspended_count Pointer to destination for the number of threads
currently suspended on this block pool.
next_pool Pointer to destination for the pointer of the next
created block pool.
Return Values
TX_SUCCESS (0x00) Successful block pool information
retrieve.
TX_POOL_ERROR (0x02) Invalid memory block pool pointer.
TX_PTR_ERROR (0x03) Invalid pointer (NULL) for any
destination pointer.
User Guide
Memory Blocks 99
Allowed From
Initialization, threads, timers, and ISRs
Preemption Possible
No
Example
TX_BLOCK_POOL my_pool;
CHAR *name;
ULONG available;
ULONG total_blocks;
TX_THREAD *first_suspended;
ULONG suspended_count;
TX_BLOCK_POOL *next_pool;
UINT status;
/* Retrieve information about a the previously created
block pool "my_pool." */
status = tx_block_pool_info_get(&my_pool, &name,
&available,&total_packets,
&first_suspended, &suspended_count,
&next_pool);
See Also
tx_block_pool_allocate, tx_block_pool_create, tx_block_pool_delete,
tx_block_pool_prioritize, tx_block_release
tx_block_pool_prioritize
Prioritize block pool suspension list
Prototype
UINT tx_block_pool_prioritize(TX_BLOCK_POOL *pool_ptr)
Description
This service places the highest priority thread suspended for a block of
memory on this pool at the front of the suspension list. All other threads
remain in the same FIFO order they were suspended in.
Input Parameters
pool_ptr Pointer to a memory block pool control block.
Return Values
TX_SUCCESS (0x00) Successful block pool prioritize.
TX_POOL_ERROR (0x02) Invalid memory block pool pointer.
Allowed From
Initialization, threads, timers, and ISRs
Preemption Possible
No
User Guide
Memory Blocks 101
Example
TX_BLOCK_POOL my_pool;
UINT status;
See Also
tx_block_allocate, tx_block_pool_create, tx_block_pool_delete,
tx_block_pool_info_get, tx_block_release
tx_block_release
Release a fixed-size block of memory
Prototype
UINT tx_block_release(VOID *block_ptr)
Description
This service releases a previously allocated block back to its associated
memory pool. If there are one or more threads suspended waiting for
memory block from this pool, the first thread suspended is given this
memory block and resumed.
The application must prevent using a memory block area after it has been
Input Parameters
block_ptr Pointer to the previously allocated memory
block.
Return Values
TX_SUCCESS (0x00) Successful memory block release.
TX_PTR_ERROR (0x03) Invalid pointer to memory block.
Allowed From
Initialization, threads, timers, and ISRs
Preemption Possible
Yes
User Guide
Memory Blocks 103
Example
TX_BLOCK_POOL my_pool;
unsigned char *memory_ptr;
UINT status;
See Also
tx_block_allocate, tx_block_pool_create, tx_block_pool_delete,
tx_block_pool_info_get, tx_block_pool_prioritize
tx_byte_allocate
Allocate bytes of memory
Memory Bytes
Prototype
UINT tx_byte_allocate(TX_BYTE_POOL *pool_ptr,
VOID **memory_ptr, ULONG memory_size,
ULONG wait_option)
Description
This service allocates the specified number of bytes from the specified
byte-memory pool.
The performance of this service is a function of the block size and the
Input Parameters
pool_ptr Pointer to a previously created memory pool.
memory_ptr Pointer to a destination memory pointer. On
successful allocation, the address of the
allocated memory area is placed where this
parameter points to.
memory_size Number of bytes requested.
wait_option Defines how the service behaves if there is not
enough memory available. The wait options are
defined as follows:
TX_NO_WAIT (0x00000000)
TX_WAIT_FOREVER (0xFFFFFFFF)
timeout value (0x00000001 through
0xFFFFFFFE)
Selecting TX_NO_WAIT results in an immediate
return from this service regardless of whether or
not it was successful. This is the only valid option
if the service is called from initialization.
Selecting TX_WAIT_FOREVER causes the
calling thread to suspend indefinitely until
enough memory is available.
User Guide
Memory Bytes 105
Return Values
TX_SUCCESS (0x00) Successful memory allocation.
TX_DELETED (0x01) Memory pool was deleted while thread
was suspended.
TX_NO_MEMORY (0x10) Service was unable to allocate the
memory.
TX_WAIT_ABORTED (0x1A) Suspension was aborted by another
thread, timer, or ISR.
TX_POOL_ERROR (0x02) Invalid memory pool pointer.
TX_PTR_ERROR (0x03) Invalid pointer to destination pointer.
TX_WAIT_ERROR (0x04) A wait option other than TX_NO_WAIT
was specified on a call from a non-
thread.
TX_CALLER_ERROR (0x13) Invalid caller of this service.
Allowed From
Initialization and threads
Preemption Possible
Yes
Example
TX_BYTE_POOL my_pool;
unsigned char *memory_ptr;
UINT status;
/* Allocate a 112 byte memory area from my_pool. Assume
that the pool has already been created with a call to
tx_byte_pool_create. */
status = tx_byte_allocate(&my_pool, (VOID **) &memory_ptr,
112, TX_NO_WAIT);
See Also
tx_byte_pool_create, tx_byte_pool_delete, tx_byte_pool_info_get,
tx_byte_pool_prioritize, tx_byte_release
User Guide
Memory Bytes 107
tx_byte_pool_create
Create a memory pool of bytes
Prototype
UINT tx_byte_pool_create(TX_BYTE_POOL *pool_ptr,
CHAR *name_ptr, VOID *pool_start,
ULONG pool_size)
Description
This service creates a memory pool in the area specified. Initially the pool
consists of basically one very large free block. However, the pool is
broken into smaller blocks as allocations are made.
Input Parameters
pool_ptr Pointer to a memory pool control block.
name_ptr Pointer to the name of the memory pool.
pool_start Starting address of the memory pool.
pool_size Total number of bytes available for the memory
pool.
Return Values
TX_SUCCESS (0x00) Successful memory pool creation.
TX_POOL_ERROR (0x02) Invalid memory pool pointer. Either the
pointer is NULL or the pool is already
created.
TX_PTR_ERROR (0x03) Invalid starting address of the pool.
TX_SIZE_ERROR (0x05) Size of pool is invalid.
TX_CALLER_ERROR (0x13) Invalid caller of this service.
Allowed From
Initialization and threads
Preemption Possible
No
User Guide
Memory Bytes 109
Example
TX_BYTE_POOL my_pool;
UINT status;
See Also
tx_byte_allocate, tx_byte_pool_delete, tx_byte_pool_info_get,
tx_byte_pool_prioritize, tx_byte_release
tx_byte_pool_delete
Delete a memory pool of bytes
Prototype
UINT tx_byte_pool_delete(TX_BYTE_POOL *pool_ptr)
Description
This service deletes the specified memory pool. All threads suspended
waiting for memory from this pool are resumed and given a
TX_DELETED return status.
i associated with the pool, which is available after this service completes.
In addition, the application must prevent use of a deleted pool or memory
previously allocated from it.
Input Parameters
pool_ptr Pointer to a previously created memory pool.
Return Values
TX_SUCCESS (0x00) Successful memory pool deletion.
TX_POOL_ERROR (0x02) Invalid memory pool pointer.
TX_CALLER_ERROR (0x13) Invalid caller of this service.
Allowed From
Threads
Preemption Possible
Yes
User Guide
Memory Bytes 111
Example
TX_BYTE_POOL my_pool;
UINT status;
/* Delete entire memory pool. Assume that the pool has already
been created with a call to tx_byte_pool_create. */
status = tx_byte_pool_delete(&my_pool);
See Also
tx_byte_allocate, tx_byte_pool_create, tx_byte_pool_info_get,
tx_byte_pool_prioritize, tx_byte_release
tx_byte_pool_info_get
Retrieve information about byte pool
Memory Bytes
Prototype
UINT tx_byte_pool_info_get(TX_BYTE_POOL *pool_ptr, CHAR **name,
ULONG *available, ULONG *fragments,
TX_THREAD **first_suspended,
ULONG *suspended_count,
TX_BYTE_POOL **next_pool)
Description
This service retrieves information about the specified memory byte pool.
Input Parameters
pool_ptr Pointer to previously created memory pool.
name Pointer to destination for the pointer to the byte
pool’s name.
available Pointer to destination for the number of available
bytes in the pool.
fragments Pointer to destination for the total number of
memory fragments in the byte pool.
first_suspended Pointer to destination for the pointer to the thread
that is first on the suspension list of this byte
pool.
suspended_count Pointer to destination for the number of threads
currently suspended on this byte pool.
next_pool Pointer to destination for the pointer of the next
created byte pool.
Return Values
TX_SUCCESS (0x00) Successful pool information retrieve.
TX_POOL_ERROR (0x02) Invalid memory pool pointer.
TX_PTR_ERROR (0x03) Invalid pointer (NULL) for any
destination pointer.
User Guide
Memory Bytes 113
Allowed From
Initialization, threads, timers, and ISRs
Preemption Possible
No
Example
TX_BYTE_POOL my_pool;
CHAR *name;
ULONG available;
ULONG fragments;
TX_THREAD *first_suspended;
ULONG suspended_count;
TX_BYTE_POOL *next_pool;
UINT status;
See Also
tx_byte_allocate, tx_byte_pool_create, tx_byte_pool_delete,
tx_byte_pool_prioritize, tx_byte_release
tx_byte_pool_prioritize
Prioritize the byte pool suspension list
Prototype
UINT tx_byte_pool_prioritize(TX_BYTE_POOL *pool_ptr)
Description
This service places the highest priority thread suspended for memory on
this pool at the front of the suspension list. All other threads remain in the
same FIFO order they were suspended in.
Input Parameters
pool_ptr Pointer to a memory pool control block.
Return Values
TX_SUCCESS (0x00) Successful memory pool prioritize.
TX_POOL_ERROR (0x02) Invalid memory pool pointer.
Allowed From
Initialization, threads, timers, and ISRs
Preemption Possible
No
User Guide
Memory Bytes 115
Example
TX_BYTE_POOL my_pool;
UINT status;
See Also
tx_byte_allocate, tx_byte_pool_create, tx_byte_pool_delete,
tx_byte_pool_info_get, tx_byte_release
tx_byte_release
Release bytes back to memory pool
Prototype
UINT tx_byte_release(VOID *memory_ptr)
Description
This service releases a previously allocated memory area back to its
associated pool. If there are one or more threads suspended waiting for
memory from this pool, each suspended thread is given memory and
resumed until the memory is exhausted or until there are no more
suspended threads. This process of allocating memory to suspended
threads always begins with the first thread suspended.
i The application must prevent using the memory area after it is released.
Input Parameters
memory_ptr Pointer to the previously allocated memory area.
Return Values
TX_SUCCESS (0x00) Successful memory release.
TX_PTR_ERROR (0x03) Invalid memory area pointer.
TX_CALLER_ERROR (0x13) Invalid caller of this service.
Allowed From
Initialization and threads
Preemption Possible
Yes
User Guide
Memory Bytes 117
Example
unsigned char *memory_ptr;
UINT status;
See Also
tx_byte_allocate, tx_byte_pool_create, tx_byte_pool_delete,
tx_byte_pool_info_get, tx_byte_pool_prioritize
tx_event_flags_create
Create an event flag group
Event Flags
Prototype
UINT tx_event_flags_create(TX_EVENT_FLAGS_GROUP *group_ptr,
CHAR *name_ptr)
Description
This service creates a group of 32 event flags. All 32 event flags in the
group are initialized to zero. Each event flag is represented by a single bit.
Input Parameters
group_ptr Pointer to an event flags group control block.
name_ptr Pointer to the name of the event flags group.
Return Values
TX_SUCCESS (0x00) Successful event group creation.
TX_GROUP_ERROR (0x06) Invalid event group pointer. Either the
pointer is NULL or the event group is
already created.
TX_CALLER_ERROR (0x13) Invalid caller of this service.
Allowed From
Initialization and threads
Preemption Possible
No
User Guide
Event Flags 119
Example
TX_EVENT_FLAGS_GROUP my_event_group;
UINT status;
/* Create an event flag group. */
status = tx_event_flags_create(&my_event_group,
"my_event_group_name");
See Also
tx_event_flags_delete, tx_event_flags_get, tx_event_flags_info_get,
tx_event_flags_set
tx_event_flags_delete
Delete an event flag group
Prototype
UINT tx_event_flags_delete(TX_EVENT_FLAGS_GROUP *group_ptr)
Description
This service deletes the specified event flag group. All threads suspended
waiting for events from this group are resumed and given a
TX_DELETED return status.
Input Parameters
group_ptr Pointer to a previously created event flags group.
Return Values
TX_SUCCESS (0x00) Successful event flag group deletion.
TX_GROUP_ERROR (0x06) Invalid event flag group pointer.
TX_CALLER_ERROR (0x13) Invalid caller of this service.
Allowed From
Threads
Preemption Possible
Yes
User Guide
Event Flags 121
Example
TX_EVENT_FLAGS_GROUP my_event_flag_group;
UINT status;
See Also
tx_event_flags_create, tx_event_flags_get, tx_event_flags_info_get,
tx_event_flags_set
tx_event_flags_get
Get event flags from event flag group
Prototype
UINT tx_event_flags_get(TX_EVENT_FLAGS_GROUP *group_ptr,
ULONG requested_flags, UINT get_option,
ULONG *actual_flags_ptr, ULONG wait_option)
Description
This service retrieves event flags from the specified event flag group.
Each event flag group contains 32 event flags. Each flag is represented
by a single bit. This service can retrieve a variety of event flag
combinations, as selected by the input parameters.
Input Parameters
group_ptr Pointer to a previously created event flag group.
requested_flags 32-bit unsigned variable that represents the
requested event flags.
get_option Specifies whether all or any of the requested
event flagsare required. The following are valid
selections:
TX_AND (0x02)
TX_AND_CLEAR (0x03)
TX_OR (0x00)
TX_OR_CLEAR (0x01)
Selecting TX_AND or TX_AND_CLEAR
specifies that all event flags must be present in
the group. Selecting TX_OR or TX_OR_CLEAR
specifies that any event flag is satisfactory. Event
flags that satisfy the request are cleared (set to
zero) if TX_AND_CLEAR or TX_OR_CLEAR are
specified.
actual_flags_ptr Pointer to destination of where the retrieved
event flags are placed. Note that the actual flags
obtained may contain flags that were not
requested.
User Guide
Event Flags 123
Return Values
TX_SUCCESS (0x00) Successful event flags get.
TX_DELETED (0x01) Event flag group was deleted while
thread was suspended.
TX_NO_EVENTS (0x07) Service was unable to get the
specified events.
TX_WAIT_ABORTED (0x1A) Suspension was aborted by another
thread, timer, or ISR.
TX_GROUP_ERROR (0x06) Invalid event flags group pointer.
TX_PTR_ERROR (0x03) Invalid pointer for actual event flags.
TX_WAIT_ERROR (0x04) A wait option other than TX_NO_WAIT
was specified on a call from a non-
thread.
TX_OPTION_ERROR (0x08) Invalid get-option was specified.
Allowed From
Initialization, threads, timers, and ISRs
Preemption Possible
Yes
Example
TX_EVENT_FLAGS_GROUP my_event_flags_group;
ULONG actual_events;
UINT status;
See Also
tx_event_flags_create, tx_event_flags_delete, tx_event_flags_info_get,
tx_event_flags_set
User Guide
Event Flags 125
tx_event_flags_info_get
Retrieve information about event flags group
Event Flags
Prototype
UINT tx_event_flags_info_get(TX_EVENT_FLAGS_GROUP *group_ptr,
CHAR **name, ULONG *current_flags,
TX_THREAD **first_suspended,
ULONG *suspended_count,
TX_EVENT_FLAGS_GROUP **next_group)
Description
This service retrieves information about the specified event flags group.
Input Parameters
group_ptr Pointer to an event flags group control block.
name Pointer to destination for the pointer to the event
flag group’s name.
current_flags Pointer to destination for the current set flags in
the event flag group.
first_suspended Pointer to destination for the pointer to the thread
that is first on the suspension list of this event
flag group.
suspended_count Pointer to destination for the number of threads
currently suspended on this event flag group.
next_group Pointer to destination for the pointer of the next
created event flag group.
Return Values
TX_SUCCESS (0x00) Successful event group information
retrieval.
TX_GROUP_ERROR (0x06) Invalid event group pointer.
TX_PTR_ERROR (0x03) Invalid pointer (NULL) for any
destination pointer.
User Guide
Event Flags 127
Allowed From
Initialization, threads, timers, and ISRs
Preemption Possible
No
Example
TX_EVENT_FLAGS_GROUP my_event_group;
CHAR *name;
ULONG current_flags;
TX_THREAD *first_suspended;
ULONG suspended_count;
TX_EVENT_FLAGS_GROUP *next_group;
UINT status;
See Also
tx_event_flags_create, tx_event_flags_delete, tx_event_flags_get,
tx_event_flags_set
tx_event_flags_set
Set event flags in an event flag group
Prototype
UINT tx_event_flags_set(TX_EVENT_FLAGS_GROUP *group_ptr,
ULONG flags_to_set,UINT set_option)
Description
This service sets or clears event flags in an event flag group, depending
upon the specified set-option. All suspended threads whose event flag
request is now satisfied are resumed.
Input Parameters
group_ptr Pointer to the previously created event flag group
control block.
flags_to_set Specifies the event flags to set or clear based
upon the set option selected.
set_option Specifies whether the event flags specified are
ANDed or ORed into the current event flags of
the group. The following are valid selections:
TX_AND (0x02)
TX_OR (0x00)
Selecting TX_AND specifies that the specified
event flags are ANDed into the current event
flags in the group. This option is often used to
clear event flags in a group. Otherwise, if TX_OR
is specified, the specified event flags are ORed
with the current event in the group.
Return Values
TX_SUCCESS (0x00) Successful event flag set.
TX_GROUP_ERROR (0x06) Invalid pointer to event flags group.
TX_OPTION_ERROR (0x08) Invalid set-option specified.
User Guide
Event Flags 129
Allowed From
Initialization, threads, timers, and ISRs
Preemption Possible
Yes
Example
TX_EVENT_FLAGS_GROUP my_event_flags_group;
UINT status;
See Also
tx_event_flags_create, tx_event_flags_delete, tx_event_flags_get,
tx_event_flags_info_get
tx_interrupt_control
Enables and disables interrupts
Interrupt Control
Prototype
UINT tx_interrupt_control(UINT new_posture)
Description
This service enables or disables interrupts as specified by the input
parameter new_posture.
i remains part of that thread’s context. For example, if the thread calls this
routine to disable interrupts and then suspends, when it is resumed,
interrupts are disabled again.
Input Parameters
new_posture This parameter specifies whether interrupts are
disabled or enabled. Legal values include
TX_INT_DISABLE and TX_INT_ENABLE. The
actual values for these parameters are port
specific. In addition, some processing
architectures might support additional interrupt
disable postures. Please see the readme.txt
information supplied on the distribution disk for
more details.
Return Values
previous posture This service returns the previous interrupt
posture to the caller. This allows users of the
service to restore the previous posture after
interrupts are disabled.
User Guide
Interrupt Control 131
Allowed From
Threads, timers, and ISRs
Preemption Possible
No
Example
UINT my_old_posture;
/* Lockout interrupts */
my_old_posture = tx_interrupt_control(TX_INT_DISABLE);
See Also
None
tx_mutex_create
Create a mutual exclusion mutex
Mutex
Prototype
UINT tx_mutex_create(TX_MUTEX *mutex_ptr,
CHAR *name_ptr, UINT priority_inherit)
Description
This service creates a mutex for inter-thread mutual exclusion for
resource protection.
Input Parameters
mutex_ptr Pointer to a mutex control block.
name_ptr Pointer to the name of the mutex.
priority_inherit Specifies whether or not this mutex supports
priority inheritance. If this value is TX_INHERIT,
then priority inheritance is supported. However, if
TX_NO_INHERIT is specified, priority
inheritance is not supported by this mutex.
Return Values
TX_SUCCESS (0x00) Successful mutex creation.
TX_MUTEX_ERROR (0x1C) Invalid mutex pointer. Either the
pointer is NULL or the mutex is already
created.
TX_CALLER_ERROR (0x13) Invalid caller of this service.
TX_INHERIT_ERROR (0x1F) Invalid priority inherit parameter.
Allowed From
Initialization and threads
Preemption Possible
No
User Guide
Mutex 133
Example
TX_MUTEX my_mutex;
UINT status;
See Also
tx_mutex_delete, tx_mutex_get, tx_mutex_info_get, tx_mutex_prioritize,
tx_mutex_put
tx_mutex_delete
Delete a mutual exclusion mutex
Prototype
UINT tx_mutex_delete(TX_MUTEX *mutex_ptr)
Description
This service deletes the specified mutex. All threads suspended waiting
for the mutex are resumed and given a TX_DELETED return status.
Input Parameters
mutex_ptr Pointer to a previously created mutex.
Return Values
TX_SUCCESS (0x00) Successful mutex deletion.
TX_MUTEX_ERROR (0x1C) Invalid mutex pointer.
TX_CALLER_ERROR (0x13) Invalid caller of this service.
Allowed From
Threads
Preemption Possible
Yes
User Guide
Mutex 135
Example
TX_MUTEX my_mutex;
UINT status;
See Also
tx_mutex_create, tx_mutex_get, tx_mutex_info_get, tx_mutex_prioritize,
tx_mutex_put
tx_mutex_get
Obtain ownership of a mutex
Prototype
UINT tx_mutex_get(TX_MUTEX *mutex_ptr, ULONG wait_option)
Description
This service attempts to obtain exclusive ownership of the specified
mutex. If the calling thread already owns the mutex, an internal counter is
incremented and a successful status is returned.
If the mutex is owned by another thread and this thread is higher priority
and priority inheritance was specified at mutex create, the lower priority
thread’s priority will be temporarily raised to that of the calling thread.
Note that the priority of the lower-priority thread owning a mutex with
Input Parameters
mutex_ptr Pointer to a previously created mutex.
wait_option Defines how the service behaves if the mutex is
already owned by another thread. The wait
options are defined as follows:
TX_NO_WAIT (0x00000000)
TX_WAIT_FOREVER (0xFFFFFFFF)
timeout value (0x00000001 through
0xFFFFFFFE)
Selecting TX_NO_WAIT results in an immediate
return from this service regardless of whether or
not it was successful. This is the only valid option
if the service is called from Initialization.
Selecting TX_WAIT_FOREVER causes the
calling thread to suspend indefinitely until the
mutex is available.
Selecting a numeric value (1-0xFFFFFFFE)
specifies the maximum number of timer-ticks to
stay suspended while waiting for the mutex.
User Guide
Mutex 137
Return Values
TX_SUCCESS (0x00) Successful mutex get operation.
TX_DELETED (0x01) Mutex was deleted while thread was
suspended.
TX_NOT_AVAILABLE (0x1D) Service was unable to get ownership
of the mutex.
TX_WAIT_ABORTED (0x1A) Suspension was aborted by another
thread, timer, or ISR.
TX_MUTEX_ERROR (0x1C) Invalid mutex pointer.
TX_WAIT_ERROR (0x04) A wait option other than TX_NO_WAIT
was specified on a call from a non-
thread.
TX_CALLER_ERROR (0x13) Invalid caller of this service.
Allowed From
Initialization, threads, and timers
Preemption Possible
Yes
Example
TX_MUTEX my_mutex;
UINT status;
See Also
tx_mutex_create, tx_mutex_delete, tx_mutex_info_get,
tx_mutex_prioritize, tx_mutex_put
tx_mutex_info_get
Retrieve information about a mutex
Prototype
UINT tx_mutex_info_get(TX_MUTEX *mutex_ptr, CHAR **name,
ULONG *count, TX_THREAD **owner,
TX_THREAD **first_suspended,
ULONG *suspended_count, TX_MUTEX **next_mutex)
Description
This service retrieves information from the specified mutex.
Input Parameters
mutex_ptr Pointer to mutex control block.
name Pointer to destination for the pointer to the
mutex’s name.
count Pointer to destination for the ownership count of
the mutex.
owner Pointer to destination for the owning thread’s
pointer.
first_suspended Pointer to destination for the pointer to the thread
that is first on the suspension list of this mutex.
suspended_count Pointer to destination for the number of threads
currently suspended on this mutex.
next_mutex Pointer to destination for the pointer of the next
created mutex.
Return Values
TX_SUCCESS (0x00) Successful mutex information
retrieval.
TX_MUTEX_ERROR (0x1C) Invalid mutex pointer.
TX_PTR_ERROR (0x03) Invalid pointer (NULL) for any
destination pointer.
User Guide
Mutex 139
Allowed From
Initialization, threads, timers, and ISRs
Preemption Possible
No
Example
TX_MUTEX my_mutex;
CHAR *name;
ULONG count;
TX_THREAD *owner;
TX_THREAD *first_suspended;
ULONG suspended_count;
TX_MUTEX *next_mutex;
UINT status;
See Also
tx_mutex_create, tx_mutex_delete, tx_mutex_get, tx_mutex_prioritize,
tx_mutex_put
tx_mutex_prioritize
Prioritize mutex suspension list
Prototype
UINT tx_mutex_prioritize(TX_MUTEX *mutex_ptr)
Description
This service places the highest priority thread suspended for ownership of
the mutex at the front of the suspension list. All other threads remain in
the same FIFO order they were suspended in.
Input Parameters
mutex_ptr Pointer to the previously created mutex.
Return Values
TX_SUCCESS (0x00) Successful mutex prioritize.
TX_MUTEX_ERROR (0x1C) Invalid mutex pointer.
Allowed From
Initialization, threads, timers, and ISRs
Preemption Possible
No
User Guide
Mutex 141
Example
TX_MUTEX my_mutex;
UINT status;
See Also
tx_mutex_create, tx_mutex_delete, tx_mutex_get, tx_mutex_info_get,
tx_mutex_put
tx_mutex_put
Release ownership of mutex
Prototype
UINT tx_mutex_put(TX_MUTEX *mutex_ptr)
Description
This service decrements the ownership count of the specified mutex. If
the ownership count is zero, the mutex is made available.
i the releasing thread will be restored to the priority it had when it originally
obtained ownership of the mutex. Any other priority changes made to the
releasing thread during ownership of the mutex may be undone.
Input Parameters
mutex_ptr Pointer to the previously created mutex.
Return Values
TX_SUCCESS (0x00) Successful mutex release.
TX_NOT_OWNED (0x1E) Mutex is not owned by caller.
TX_MUTEX_ERROR (0x1C) Invalid pointer to mutex.
TX_CALLER_ERROR (0x13) Invalid caller of this service.
Allowed From
Initialization and threads
Preemption Possible
Yes
User Guide
Mutex 143
Example
TX_MUTEX my_mutex;
UINT status;
See Also
tx_mutex_create, tx_mutex_delete, tx_mutex_get, tx_mutex_info_get,
tx_mutex_prioritize
tx_queue_create
Create a message queue
Message Queues
Prototype
UINT tx_queue_create(TX_QUEUE *queue_ptr, CHAR *name_ptr,
UINT message_size,
VOID *queue_start, ULONG queue_size)
Description
This service creates a message queue that is typically used for inter-
thread communication. The total number of messages is calculated from
the specified message size and the total number of bytes in the queue.
If the total number of bytes specified in the queue’s memory area is not
i evenly divisible by the specified message size, the remaining bytes in the
memory area are not used.
Input Parameters
queue_ptr Pointer to a message queue control block.
name_ptr Pointer to the name of the message queue.
message_size Specifies the size of each message in the queue.
Message sizes range from 1 32-bit word to 16
32-bit words. Valid message size options are
defined as follows:
TX_1_ULONG (0x01)
TX_2_ULONG (0x02)
TX_4_ULONG (0x04)
TX_8_ULONG (0x08)
TX_16_ULONG (0x10)
queue_start Starting address of the message queue.
queue_size Total number of bytes available for the message
queue.
User Guide
Message Queues 145
Return Values
TX_SUCCESS (0x00) Successful message queue creation.
TX_QUEUE_ERROR (0x09) Invalid message queue pointer. Either
the pointer is NULL or the queue is
already created.
TX_PTR_ERROR (0x03) Invalid starting address of the
message queue.
TX_SIZE_ERROR (0x05) Size of message queue is invalid.
TX_CALLER_ERROR (0x13) Invalid caller of this service.
Allowed From
Initialization and threads
Preemption Possible
No
Example
TX_QUEUE my_queue;
UINT status;
See Also
tx_queue_delete, tx_queue_flush, tx_queue_front_send,
tx_queue_info_get, tx_queue_prioritize, tx_queue_receive,
tx_queue_send
tx_queue_delete
Delete a message queue
Prototype
UINT tx_queue_delete(TX_QUEUE *queue_ptr)
Description
This service deletes the specified message queue. All threads suspended
waiting for a message from this queue are resumed and given a
TX_DELETED return status.
Input Parameters
queue_ptr Pointer to a previously created message queue.
Return Values
TX_SUCCESS (0x00) Successful message queue deletion.
TX_QUEUE_ERROR (0x09) Invalid message queue pointer.
TX_CALLER_ERROR (0x13) Invalid caller of this service.
Allowed From
Threads
Preemption Possible
Yes
User Guide
Message Queues 147
Example
TX_QUEUE my_queue;
UINT status;
See Also
tx_queue_create, tx_queue_flush, tx_queue_front_send,
tx_queue_info_get, tx_queue_prioritize, tx_queue_receive,
tx_queue_send
tx_queue_flush
Empty messages in a message queue
Prototype
UINT tx_queue_flush(TX_QUEUE *queue_ptr)
Description
This service deletes all messages stored in the specified message queue.
If the queue is full, messages of all suspended threads are discarded.
Each suspended thread is then resumed with a return status that
indicates the message send was successful. If the queue is empty, this
service does nothing.
Input Parameters
queue_ptr Pointer to a previously created message queue.
Return Values
TX_SUCCESS (0x00) Successful message queue flush.
TX_QUEUE_ERROR (0x09) Invalid message queue pointer.
TX_CALLER_ERROR (0x13) Invalid caller of this service.
Allowed From
Initialization, threads, timers, and ISRs
Preemption Possible
Yes
User Guide
Message Queues 149
Example
TX_QUEUE my_queue;
UINT status;
See Also
tx_queue_create, tx_queue_delete, tx_queue_front_send,
tx_queue_info_get, tx_queue_prioritize, tx_queue_receive,
tx_queue_send
tx_queue_front_send
Send a message to the front of queue
Message Queues
Prototype
UINT tx_queue_front_send(TX_QUEUE *queue_ptr,
VOID *source_ptr, ULONG wait_option)
Description
This service sends a message to the front location of the specified
message queue. The message is copied to the front of the queue from
the memory area specified by the source pointer.
Input Parameters
queue_ptr Pointer to a message queue control block.
source_ptr Pointer to the message.
wait_option Defines how the service behaves if the message
queue is full. The wait options are defined as
follows:
TX_NO_WAIT (0x00000000)
TX_WAIT_FOREVER (0xFFFFFFFF)
timeout value (0x00000001 through
0xFFFFFFFE)
Selecting TX_NO_WAIT results in an immediate
return from this service regardless of whether or
not it was successful. This is the only valid option
if the service is called from a non-thread; e.g.,
Initialization, timer, or ISR.
Selecting TX_WAIT_FOREVER causes the
calling thread to suspend indefinitely until there is
room in the queue.
Selecting a numeric value (1-0xFFFFFFFE)
specifies the maximum number of timer-ticks to
stay suspended while waiting for room in the
queue.
User Guide
Message Queues 151
Return Values
TX_SUCCESS (0x00) Successful sending of message.
TX_DELETED (0x01) Message queue was deleted while
thread was suspended.
TX_QUEUE_FULL (0x0B) Service was unable to send message
because the queue was full.
TX_WAIT_ABORTED (0x1A) Suspension was aborted by another
thread, timer, or ISR.
TX_QUEUE_ERROR (0x09) Invalid message queue pointer.
TX_PTR_ERROR (0x03) Invalid source pointer for message.
TX_WAIT_ERROR (0x04) A wait option other than TX_NO_WAIT
was specified on a call from a non-
thread.
Allowed From
Initialization, threads, timers, and ISRs
Preemption Possible
Yes
Example
TX_QUEUE my_queue;
UINT status;
ULONG my_message[4];
/* Send a message to the front of "my_queue." Return
immediately, regardless of success. This wait
option is used for calls from initialization, timers,
and ISRs. */
status = tx_queue_front_send(&my_queue, my_message,
TX_NO_WAIT);
See Also
tx_queue_create, tx_queue_delete, tx_queue_flush, tx_queue_info_get,
tx_queue_prioritize, tx_queue_receive, tx_queue_send
tx_queue_info_get
Retrieve information about a queue
Prototype
UINT tx_queue_info_get(TX_QUEUE *queue_ptr, CHAR **name,
ULONG *enqueued, ULONG *available_storage
TX_THREAD **first_suspended, ULONG *suspended_count,
TX_QUEUE **next_queue)
Description
This service retrieves information about the specified message queue.
Input Parameters
queue_ptr Pointer to a previously created message queue.
name Pointer to destination for the pointer to the
queue’s name.
enqueued Pointer to destination for the number of
messages currently in the queue.
available_storage Pointer to destination for the number of
messages the queue currently has space for.
first_suspended Pointer to destination for the pointer to the thread
that is first on the suspension list of this queue.
suspended_count Pointer to destination for the number of threads
currently suspended on this queue.
next_queue Pointer to destination for the pointer of the next
created queue.
Return Values
TX_SUCCESS (0x00) Successful queue information get.
TX_QUEUE_ERROR (0x09) Invalid message queue pointer.
TX_PTR_ERROR (0x03) Invalid pointer (NULL) for any
destination pointer.
Allowed From
Initialization, threads, timers, and ISRs
User Guide
Message Queues 153
Preemption Possible
No
Example
TX_QUEUE my_queue;
CHAR *name;
ULONG enqueued;
TX_THREAD *first_suspended;
ULONG suspended_count;
TX_QUEUE *next_queue;
UINT status;
See Also
tx_queue_create, tx_queue_delete, tx_queue_flush,
tx_queue_front_send, tx_queue_prioritize, tx_queue_receive,
tx_queue_send
tx_queue_prioritize
Prioritize queue suspension list
Prototype
UINT tx_queue_prioritize(TX_QUEUE *queue_ptr)
Description
This service places the highest priority thread suspended for a message
(or to place a message) on this queue at the front of the suspension list.
All other threads remain in the same FIFO order they were suspended in.
Input Parameters
queue_ptr Pointer to a previously created message queue.
Return Values
TX_SUCCESS (0x00) Successful queue prioritize.
TX_QUEUE_ERROR (0x09) Invalid message queue pointer.
Allowed From
Initialization, threads, timers, and ISRs
Preemption Possible
No
User Guide
Message Queues 155
Example
TX_QUEUE my_queue;
UINT status;
See Also
tx_queue_create, tx_queue_delete, tx_queue_flush,
tx_queue_front_send, tx_queue_info_get, tx_queue_receive,
tx_queue_send
tx_queue_receive
Get a message from message queue
Prototype
UINT tx_queue_receive(TX_QUEUE *queue_ptr,
VOID *destination_ptr, ULONG wait_option)
Description
This service retrieves a message from the specified message queue. The
retrieved message is copied from the queue into the memory area
specified by the destination pointer. That message is then removed from
the queue.
The specified destination memory area must be large enough to hold the
Input Parameters
queue_ptr Pointer to a previously created message queue.
destination_ptr Location of where to copy the message.
wait_option Defines how the service behaves if the message
queue is empty. The wait options are defined as
follows:
TX_NO_WAIT (0x00000000)
TX_WAIT_FOREVER (0xFFFFFFFF)
timeout value (0x00000001
through
0xFFFFFFFE)
Selecting TX_NO_WAIT results in an immediate
return from this service regardless of whether or
not it was successful. This is the only valid option
if the service is called from a non-thread; e.g.,
Initialization, timer, or ISR.
Selecting TX_WAIT_FOREVER causes the
calling thread to suspend indefinitely until a
message is available.
User Guide
Message Queues 157
Return Values
TX_SUCCESS (0x00) Successful retrieval of message.
TX_DELETED (0x01) Message queue was deleted while
thread was suspended.
TX_QUEUE_EMPTY (0x0A) Service was unable to retrieve a
message because the queue was
empty.
TX_WAIT_ABORTED (0x1A) Suspension was aborted by another
thread, timer, or ISR.
TX_QUEUE_ERROR (0x09) Invalid message queue pointer.
TX_PTR_ERROR (0x03) Invalid destination pointer for
message.
TX_WAIT_ERROR (0x04) A wait option other than TX_NO_WAIT
was specified on a call from a non-
thread.
Allowed From
Initialization, threads, timers, and ISRs
Preemption Possible
Yes
Example
TX_QUEUE my_queue;
UINT status;
ULONG my_message[4];
/* Retrieve a message from "my_queue." If the queue is
empty, suspend until a message is present. Note that
this suspension is only possible from application
threads. */
status = tx_queue_receive(&my_queue, my_message,
TX_WAIT_FOREVER);
See Also
tx_queue_create, tx_queue_delete, tx_queue_flush,
tx_queue_front_send, tx_queue_info_get, tx_queue_prioritize,
tx_queue_send
User Guide
Message Queues 159
tx_queue_send
Send a message to message queue
Prototype
UINT tx_queue_send(TX_QUEUE *queue_ptr,
VOID *source_ptr, ULONG wait_option)
Description
This service sends a message to the specified message queue. The sent
message is copied to the queue from the memory area specified by the
source pointer.
Input Parameters
queue_ptr Pointer to a previously created message queue.
source_ptr Pointer to the message.
wait_option Defines how the service behaves if the message
queue is full. The wait options are defined as
follows:
TX_NO_WAIT (0x00000000)
TX_WAIT_FOREVER (0xFFFFFFFF)
timeout value (0x00000001 through
0xFFFFFFFE)
Selecting TX_NO_WAIT results in an immediate
return from this service regardless of whether or
not it was successful. This is the only valid option
if the service is called from a non-thread; e.g.,
Initialization, timer, or ISR.
Selecting TX_WAIT_FOREVER causes the
calling thread to suspend indefinitely until there is
room in the queue.
Selecting a numeric value (1-0xFFFFFFFE)
specifies the maximum number of timer-ticks to
stay suspended while waiting for room in the
queue.
User Guide
Message Queues 161
Return Values
TX_SUCCESS (0x00) Successful sending of message.
TX_DELETED (0x01) Message queue was deleted while
thread was suspended.
TX_QUEUE_FULL (0x0B) Service was unable to send message
because the queue was full.
TX_WAIT_ABORTED (0x1A) Suspension was aborted by another
thread, timer, or ISR.
TX_QUEUE_ERROR (0x09) Invalid message queue pointer.
TX_PTR_ERROR (0x03) Invalid source pointer for message.
TX_WAIT_ERROR (0x04) A wait option other than TX_NO_WAIT
was specified on a call from a non-
thread.
Allowed From
Initialization, threads, timers, and ISRs
Preemption Possible
Yes
Example
TX_QUEUE my_queue;
UINT status;
ULONG my_message[4];
/* Send a message to "my_queue." Return immediately,
regardless of success. This wait option is used for
calls from initialization, timers, and ISRs. */
status = tx_queue_send(&my_queue, my_message, TX_NO_WAIT);
See Also
tx_queue_create, tx_queue_delete, tx_queue_flush,
tx_queue_front_send, tx_queue_info_get, tx_queue_prioritize,
tx_queue_receive
tx_semaphore_create
Create a counting semaphore
Counting Semaphores
Prototype
UINT tx_semaphore_create(TX_SEMAPHORE *semaphore_ptr,
CHAR *name_ptr, ULONG initial_count)
Description
This service creates a counting semaphore for inter-thread
synchronization. The initial semaphore count is specified as an input
parameter.
Input Parameters
semaphore_ptr Pointer to a semaphore control block.
name_ptr Pointer to the name of the semaphore.
initial_count Specifies the initial count for this semaphore.
Legal values range from 0x00000000 through
0xFFFFFFFF.
Return Values
TX_SUCCESS (0x00) Successful semaphore
creation.
TX_SEMAPHORE_ERROR (0x0C) Invalid semaphore pointer.
Either the pointer is NULL or
the semaphore is already
created.
TX_CALLER_ERROR (0x13) Invalid caller of this service.
Allowed From
Initialization and threads
Preemption Possible
No
User Guide
Counting Semaphores 163
Example
TX_SEMAPHORE my_semaphore;
UINT status;
See Also
tx_semaphore_delete, tx_semaphore_get, tx_semaphore_info_get,
tx_semaphore_prioritize, tx_semaphore_put
tx_semaphore_delete
Delete a counting semaphore
Counting Semaphores
Prototype
UINT tx_semaphore_delete(TX_SEMAPHORE *semaphore_ptr)
Description
This service deletes the specified counting semaphore. All threads
suspended waiting for a semaphore instance are resumed and given a
TX_DELETED return status.
Input Parameters
semaphore_ptr Pointer to a previously created semaphore.
Return Values
TX_SUCCESS (0x00) Successful counting
semaphore deletion.
TX_SEMAPHORE_ERROR (0x0C) Invalid counting semaphore
pointer.
TX_CALLER_ERROR (0x13) Invalid caller of this service.
Allowed From
Threads
Preemption Possible
Yes
User Guide
Counting Semaphores 165
Example
TX_SEMAPHORE my_semaphore;
UINT status;
See Also
tx_semaphore_create, tx_semaphore_get, tx_semaphore_info_get,
tx_semaphore_prioritize, tx_semaphore_put
tx_semaphore_get
Get instance from counting semaphore
Prototype
UINT tx_semaphore_get(TX_SEMAPHORE *semaphore_ptr,
ULONG wait_option)
Description
This service retrieves an instance (a single count) from the specified
counting semaphore. As a result, the specified semaphore’s count is
decreased by one.
Input Parameters
semaphore_ptr Pointer to a previously created counting
semaphore.
wait_option Defines how the service behaves if there are no
instances of the semaphore available; i.e., the
semaphore count is zero. The wait options are
defined as follows:
TX_NO_WAIT (0x00000000)
TX_WAIT_FOREVER (0xFFFFFFFF)
timeout value (0x00000001 through
0xFFFFFFFE)
Selecting TX_NO_WAIT results in an immediate
return from this service regardless of whether or
not it was successful. This is the only valid option
if the service is called from a non-thread; e.g.,
initialization, timer, or ISR.
Selecting TX_WAIT_FOREVER causes the
calling thread to suspend indefinitely until a
semaphore instance is available.
Selecting a numeric value (1-0xFFFFFFFE)
specifies the maximum number of timer-ticks to
stay suspended while waiting for a semaphore
instance.
User Guide
Counting Semaphores 167
Return Values
TX_SUCCESS (0x00) Successful retrieval of a
semaphore instance.
TX_DELETED (0x01) Counting semaphore was
deleted while thread was
suspended.
TX_NO_INSTANCE (0x0D) Service was unable to
retrieve an instance of the
counting semaphore
(semaphore count is zero).
TX_WAIT_ABORTED (0x1A) Suspension was aborted by
another thread, timer, or
ISR.
TX_SEMAPHORE_ERROR (0x0C) Invalid counting semaphore
pointer.
TX_WAIT_ERROR (0x04) A wait option other than
TX_NO_WAIT was specified
on a call from a non-thread.
Allowed From
Initialization, threads, timers, and ISRs
Preemption Possible
Yes
Example
TX_SEMAPHORE my_semaphore;
UINT status;
See Also
tx_semaphore_create, tx_semahore_delete, tx_semaphore_info_get,
tx_semaphore_prioritize, tx_semaphore_put
tx_semaphore_info_get
Retrieve information about a semaphore
Counting Semaphores
Prototype
UINT tx_semaphore_info_get(TX_SEMAPHORE *semaphore_ptr,
CHAR **name, ULONG *current_value,
TX_THREAD **first_suspended,
ULONG *suspended_count,
TX_SEMAPHORE **next_semaphore)
Description
This service retrieves information about the specified semaphore.
Input Parameters
semaphore_ptr Pointer to semaphore control block.
name Pointer to destination for the pointer to the
semaphore’s name.
current_value Pointer to destination for the current
semaphore’s count.
first_suspended Pointer to destination for the pointer to the thread
that is first on the suspension list of this
semaphore.
suspended_count Pointer to destination for the number of threads
currently suspended on this semaphore.
next_semaphore Pointer to destination for the pointer of the next
created semaphore.
Return Values
TX_SUCCESS (0x00) Successful semaphore
information
retrieval.
TX_SEMAPHORE_ERROR (0x0C) Invalid semaphore pointer.
TX_PTR_ERROR (0x03) Invalid pointer (NULL) for
any destination pointer.
User Guide
Counting Semaphores 169
Allowed From
Initialization, threads, timers, and ISRs
Preemption Possible
No
Example
TX_SEMAPHORE my_semaphore;
CHAR *name;
ULONG current_value;
TX_THREAD *first_suspended;
ULONG suspended_count;
TX_SEMAPHORE *next_semaphore;
UINT status;
See Also
tx_semaphore_create, tx_semaphore_delete, tx_semaphore_get,
tx_semaphore_prioritize, tx_semaphore_put
tx_semaphore_prioritize
Prioritize semaphore suspension list
Counting Semaphores
Prototype
UINT tx_semaphore_prioritize(TX_SEMAPHORE *semaphore_ptr)
Description
This service places the highest priority thread suspended for an instance
of the semaphore at the front of the suspension list. All other threads
remain in the same FIFO order they were suspended in.
Input Parameters
semaphore_ptr Pointer to a previously created semaphore.
Return Values
TX_SUCCESS (0x00) Successful semaphore
prioritize.
TX_SEMAPHORE_ERROR (0x0C) Invalid counting semaphore
pointer.
Allowed From
Initialization, threads, timers, and ISRs
Preemption Possible
No
User Guide
Counting Semaphores 171
Example
TX_SEMAPHORE my_semaphore;
UINT status;
See Also
tx_semaphore_create, tx_semaphore_delete, tx_semaphore_get,
tx_semaphore_info_get, tx_semaphore_put
tx_semaphore_put
Place an instance in counting semaphore
Prototype
UINT tx_semaphore_put(TX_SEMAPHORE *semaphore_ptr)
Description
This service puts an instance into the specified counting semaphore,
which in reality increments the counting semaphore by one.
Input Parameters
semaphore_ptr Pointer to the previously created counting
semaphore control block.
Return Values
TX_SUCCESS (0x00) Successful semaphore put.
TX_SEMAPHORE_ERROR (0x0C) Invalid pointer to counting
semaphore.
Allowed From
Initialization, threads, timers, and ISRs
Preemption Possible
Yes
User Guide
Counting Semaphores 173
Example
TX_SEMAPHORE my_semaphore;
UINT status;
See Also
tx_semaphore_create, tx_semaphore_delete, tx_semaphore_info_get,
tx_semaphore_prioritize, tx_semaphore_get
tx_thread_create
Create an application thread
Thread Control
Prototype
UINT tx_thread_create(TX_THREAD *thread_ptr,
CHAR *name_ptr, VOID (*entry_function)(ULONG),
ULONG entry_input, VOID *stack_start,
ULONG stack_size, UINT priority,
UINT preempt_threshold, ULONG time_slice,
UINT auto_start)
Description
This service creates an application thread that starts execution at the
specified task entry function. The stack, priority, preemption-threshold,
and time-slice are among the attributes specified by the input parameters.
In addition, the initial execution state of the thread is also specified.
Input Parameters
thread_ptr Pointer to a thread control block.
name_ptr Pointer to the name of the thread.
entry_function Specifies the initial C function for thread
execution. When a thread returns from this entry
function, it is placed in a completed state and
suspended indefinitely.
entry_input A 32-bit value that is passed to the thread’s
entry function when it first executes. The use for
this input is determined exclusively by the
application.
stack_start Starting address of the stack’s memory area.
stack_size Number bytes in the stack memory area. The
thread’s stack area must be large enough to
handle its worst-case function call nesting and
local variable usage.
priority Numerical priority of thread. Legal values range
from 0 through 31, where a value of 0 represents
the highest priority.
User Guide
Thread Control 175
Return Values
TX_SUCCESS (0x00) Successful thread creation.
TX_THREAD_ERROR (0x0E) Invalid thread control
pointer. Either the pointer is
NULL or the thread is
already created.
TX_PTR_ERROR (0x03) Invalid starting address of
the entry point or the stack
area is invalid, usually
NULL.
TX_SIZE_ERROR (0x05) Size of stack area is invalid.
Threads must have at least
TX_MINIMUM_STACK
bytes to execute.
TX_PRIORITY_ERROR (0x0F) Invalid thread priority, which
is a value outside the range
of 0-31.
TX_THRESH_ERROR (0x18) Invalid preemption-
threshold specified. This
value must be a valid priority
less than or equal to the
initial priority of the thread.
TX_START_ERROR (0x10) Invalid auto-start selection.
TX_CALLER_ERROR (0x13) Invalid caller of this service.
Allowed From
Initialization and threads
Preemption Possible
Yes
User Guide
Thread Control 177
Example
TX_THREAD my_thread;
UINT status;
...
See Also
tx_thread_delete, tx_thread_identify, tx_thread_info_get,
tx_thread_preemption_change, tx_thread_priority_change,
tx_thread_relinquish, tx_thread_resume, tx_thread_sleep,
tx_thread_suspend, tx_thread_terminate, tx_thread_time_slice_change,
tx_thread_wait_abort
tx_thread_delete
Delete an application thread
Prototype
UINT tx_thread_delete(TX_THREAD *thread_ptr)
Description
This service deletes the specified application thread. Since the specified
thread must be in a terminated or completed state, this service cannot be
called from a thread attempting to delete itself.
i associated with the thread’s stack, which is available after this service
completes. In addition, the application must prevent use of a deleted
thread.
Input Parameters
thread_ptr Pointer to a previously created application
thread.
Return Values
TX_SUCCESS (0x00) Successful thread deletion.
TX_THREAD_ERROR (0x0E) Invalid application thread pointer.
TX_DELETE_ERROR (0x11) Specified thread is not in a terminated
or completed state.
TX_CALLER_ERROR (0x13) Invalid caller of this service.
Allowed From
Threads and timers
Preemption Possible
No
User Guide
Thread Control 179
Example
TX_THREAD my_thread;
UINT status;
See Also
tx_thread_create, tx_thread_identify, tx_thread_info_get,
tx_thread_preemption_change, tx_thread_priority_change,
tx_thread_relinquish, tx_thread_resume, tx_thread_sleep,
tx_thread_suspend, tx_thread_terminate, tx_thread_time_slice_change,
tx_thread_wait_abort
tx_thread_identify
Retrieves pointer to currently executing thread
Prototype
TX_THREAD* tx_thread_identify(VOID)
Description
This service returns a pointer to the currently executing thread. If no
thread is executing, this service returns a null pointer.
If this service is called from an ISR, the return value represents the thread
i running prior to the executing interrupt handler.
Input Parameters
None
Return Values
thread pointer Pointer to the currently executing thread. If no
thread is executing, the return value is
TX_NULL.
Allowed From
Threads and ISRs
Preemption Possible
No
User Guide
Thread Control 181
Example
TX_THREAD *my_thread_ptr;
See Also
tx_thread_create, tx_thread_delete, tx_thread_info_get,
tx_thread_preemption_change, tx_thread_priority_change,
tx_thread_relinquish, tx_thread_resume, tx_thread_sleep,
tx_thread_suspend, tx_thread_terminate, tx_thread_time_slice_change,
tx_thread_wait_abort
tx_thread_info_get
Retrieve information about a thread
Thread Control
Prototype
UINT tx_thread_info_get(TX_THREAD *thread_ptr, CHAR **name,
UINT *state, ULONG *run_count,
UINT *priority,
UINT *preemption_threshold,
ULONG *time_slice,
TX_THREAD **next_thread,
TX_THREAD **suspended_thread)
Description
This service retrieves information about the specified thread.
Input Parameters
thread_ptr Pointer to thread control block.
name Pointer to destination for the pointer to the
thread’s name.
state Pointer to destination for the thread’s current
execution state. Possible values are as follows:
TX_READY (0x00)
TX_COMPLETED (0x01)
TX_TERMINATED (0x02)
TX_SUSPENDED (0x03)
TX_SLEEP (0x04)
TX_QUEUE_SUSP (0x05)
TX_SEMAPHORE_SUSP (0x06)
TX_EVENT_FLAG (0x07)
TX_BLOCK_MEMORY (0x08)
TX_BYTE_MEMORY (0x09)
TX_MUTEX_SUSP (0x0D)
TX_IO_DRIVER (0x0A)
User Guide
Thread Control 183
Return Values
TX_SUCCESS (0x00) Successful thread information
retrieval.
TX_THREAD_ERROR (0x0E) Invalid thread control pointer.
TX_PTR_ERROR (0x03) Invalid pointer (NULL) for any
destination pointer.
Allowed From
Initialization, threads, timers, and ISRs
Preemption Possible
No
Example
TX_THREAD my_thread;
CHAR *name;
UINT state;
ULONG run_count;
UINT priority;
UINT preemption_threshold;
UINT time_slice;
TX_THREAD *next_thread;
TX_THREAD *suspended_thread;
UINT status;
See Also
tx_thread_create, tx_thread_delete, tx_thread_identify,
tx_thread_preemption_change, tx_thread_priority_change,
tx_thread_relinquish, tx_thread_resume, tx_thread_sleep,
tx_thread_suspend, tx_thread_terminate, tx_thread_time_slice_change,
tx_thread_wait_abort
User Guide
Thread Control 185
tx_thread_preemption_change
Change preemption-threshold of application thread
Prototype
UINT tx_thread_preemption_change(TX_THREAD *thread_ptr,
UINT new_threshold, UINT *old_threshold)
Description
This service changes the preemption-threshold of the specified thread.
The preemption-threshold prevents preemption of the specified thread by
threads equal to or less than the preemption-threshold value.
Input Parameters
thread_ptr Pointer to a previously created application
thread.
new_threshold New preemption-threshold priority level (0-31).
old_threshold Pointer to a location to return the previous
preemption-threshold.
Return Values
TX_SUCCESS (0x00) Successful preemption-threshold
change.
TX_THREAD_ERROR (0x0E) Invalid application thread pointer.
TX_THRESH_ERROR (0x18) Specified new preemption-threshold is
not a valid thread priority (a value other
than 0-31) or is greater than (lower
priority) than the current thread priority.
TX_PTR_ERROR (0x03) Invalid pointer to previous preemption-
threshold storage location.
TX_CALLER_ERROR (0x13) Invalid caller of this service.
Allowed From
Threads and timers
User Guide
Thread Control 187
Preemption Possible
Yes
Example
TX_THREAD my_thread;
UINT my_old_threshold;
UINT status;
See Also
tx_thread_create, tx_thread_delete, tx_thread_identify,
tx_thread_info_get, tx_thread_priority_change, tx_thread_relinquish,
tx_thread_resume, tx_thread_sleep, tx_thread_suspend,
tx_thread_terminate, tx_thread_time_slice_change, tx_thread_wait_abort
tx_thread_priority_change
Change priority of an application thread
Prototype
UINT tx_thread_priority_change(TX_THREAD *thread_ptr,
UINT new_priority, UINT *old_priority)
Description
This service changes the priority of the specified thread. Valid priorities
range from 0 through 31, where 0 represents the highest priority level.
Input Parameters
thread_ptr Pointer to a previously created application
thread.
new_priority New thread priority level (0-31).
old_priority Pointer to a location to return the thread’s
previous priority.
Return Values
TX_SUCCESS (0x00) Successful priority change.
TX_THREAD_ERROR (0x0E) Invalid application thread pointer.
TX_PRIORITY_ERROR (0x0F) Specified new priority is not valid (a
value other than 0-31).
TX_PTR_ERROR (0x03) Invalid pointer to previous priority
storage location.
TX_CALLER_ERROR (0x13) Invalid caller of this service.
User Guide
Thread Control 189
Allowed From
Threads and timers
Preemption Possible
Yes
Example
TX_THREAD my_thread;
UINT my_old_priority;
UINT status;
See Also
tx_thread_create, tx_thread_delete, tx_thread_identify,
tx_thread_info_get, tx_thread_preemption_change, tx_thread_relinquish,
tx_thread_resume, tx_thread_sleep, tx_thread_suspend,
tx_thread_terminate, tx_thread_time_slice_change, tx_thread_wait_abort
tx_thread_relinquish
Relinquish control to other application threads
Prototype
VOID tx_thread_relinquish(VOID)
Description
This service relinquishes processor control to other ready-to-run threads
at the same or higher priority.
Input Parameters
VOID
Return Values
VOID
Allowed From
Threads
Preemption Possible
Yes
User Guide
Thread Control 191
Example
ULONG run_counter_1 = 0;
ULONG run_counter_2 = 0;
See Also
tx_thread_create, tx_thread_delete, tx_thread_identify,
tx_thread_info_get, tx_thread_preemption_change,
tx_thread_priority_change, tx_thread_resume, tx_thread_sleep,
tx_thread_suspend, tx_thread_terminate, tx_thread_time_slice_change,
tx_thread_wait_abort
tx_thread_resume
Resume suspended application thread
Prototype
UINT tx_thread_resume(TX_THREAD *thread_ptr)
Description
This service resumes or prepares for execution a thread that was
previously suspended by a tx_thread_suspend call. In addition, this
service resumes threads that were created without an automatic start.
Input Parameters
thread_ptr Pointer to a suspended application thread.
Return Values
TX_SUCCESS (0x00) Successful thread resume.
TX_SUSPEND_LIFTED(0x19) Previously set delayed suspension
was lifted.
TX_THREAD_ERROR (0x0E) Invalid application thread pointer.
TX_RESUME_ERROR (0x12) Specified thread is not suspended or
was previously suspended by a
service other than
tx_thread_suspend.
Allowed From
Initialization, threads, timers, and ISRs
Preemption Possible
Yes
User Guide
Thread Control 193
Example
TX_THREAD my_thread;
UINT status;
See Also
tx_thread_create, tx_thread_delete, tx_thread_identify,
tx_thread_info_get, tx_thread_preemption_change,
tx_thread_priority_change, tx_thread_relinquish, tx_thread_sleep,
tx_thread_suspend, tx_thread_terminate, tx_thread_time_slice_change,
tx_thread_wait_abort
tx_thread_sleep
Suspended current thread for specified time
Prototype
UINT tx_thread_sleep(ULONG timer_ticks)
Description
This service causes the calling thread to suspend for the specified
number of timer ticks. The amount of physical time associated with a
timer tick is application specific. This service can only be called only from
an application thread.
Input Parameters
timer_ticks The number of timer ticks to suspend the calling
application thread, ranging from 0 through
0xFFFFFFFF. If 0 is specified, the service
returns immediately.
Return Values
TX_SUCCESS (0x00) Successful thread sleep.
TX_WAIT_ABORTED (0x1A) Suspension was aborted by another
thread, timer, or ISR.
TX_CALLER_ERROR (0x13) Service called from a non-thread.
Allowed From
Threads
Preemption Possible
Yes
User Guide
Thread Control 195
Example
UINT status;
See Also
tx_thread_create, tx_thread_delete, tx_thread_identify,
tx_thread_info_get, tx_thread_preemption_change,
tx_thread_priority_change, tx_thread_relinquish, tx_thread_resume,
tx_thread_suspend, tx_thread_terminate, tx_thread_time_slice_change,
tx_thread_wait_abort
tx_thread_suspend
Suspend an application thread
Prototype
UINT tx_thread_suspend(TX_THREAD *thread_ptr)
Description
This service suspends the specified application thread. A thread may call
this service to suspend itself.
i suspension is held internally until the prior suspension is lifted. When that
happens, this unconditional suspension of the specified thread is
performed. Further unconditional suspension requests have no effect.
Once suspended, the thread must be resumed by tx_thread_resume in
order to execute again.
Input Parameters
thread_ptr Pointer to an application thread.
Return Values
TX_SUCCESS (0x00) Successful thread suspend.
TX_THREAD_ERROR (0x0E) Invalid application thread
pointer.
TX_SUSPEND_ERROR (0x14) Specified thread is in a
terminated or
completed state.
TX_CALLER_ERROR (0x13) Invalid caller of this service.
Allowed From
Threads and timers
Preemption Possible
Yes
User Guide
Thread Control 197
Example
TX_THREAD my_thread;
UINT status;
See Also
tx_thread_create, tx_thread_delete, tx_thread_identify,
tx_thread_info_get, tx_thread_preemption_change,
tx_thread_priority_change, tx_thread_relinquish, tx_thread_resume,
tx_thread_sleep, tx_thread_terminate, tx_thread_time_slice_change,
tx_thread_wait_abort
tx_thread_terminate
Terminates an application thread
Prototype
UINT tx_thread_terminate(TX_THREAD *thread_ptr)
Description
This service terminates the specified application thread regardless of
whether the thread is suspended or not. A thread may call this service to
terminate itself.
Once terminated, the thread must be deleted and re-created in order for it
i to execute again.
Input Parameters
thread_ptr Pointer to application thread.
Return Values
TX_SUCCESS (0x00) Successful thread terminate.
TX_THREAD_ERROR (0x0E) Invalid application thread pointer.
TX_CALLER_ERROR (0x13) Invalid caller of this service.
Allowed From
Threads and timers
Preemption Possible
Yes
User Guide
Thread Control 199
Example
TX_THREAD my_thread;
UINT status;
See Also
tx_thread_create, tx_thread_delete, tx_thread_identify,
tx_thread_info_get, tx_thread_preemption_change,
tx_thread_priority_change, tx_thread_relinquish, tx_thread_resume,
tx_thread_sleep, tx_thread_suspend, tx_thread_time_slice_change,
tx_thread_wait_abort
tx_thread_time_slice_change
Changes time-slice of application thread
Prototype
UINT tx_thread_time_slice_change(TX_THREAD *thread_ptr,
ULONG new_time_slice, ULONG *old_time_slice)
Description
This service changes the time-slice of the specified application thread.
Selecting a time-slice for a thread insures that it won’t execute more than
the specified number of timer ticks before other threads of the same or
higher priorities have a chance to execute.
Input Parameters
thread_ptr Pointer to application thread.
new_time_slice New time slice value. Legal values include
TX_NO_TIME_SLICE and numeric values from
1 through 0xFFFFFFFF.
old_time_slice Pointer to location for storing the previous time-
slice value of the specified thread.
Return Values
TX_SUCCESS (0x00) Successful time-slice chance.
TX_THREAD_ERROR (0x0E) Invalid application thread pointer.
TX_PTR_ERROR (0x03) Invalid pointer to previous time-slice
storage location.
TX_CALLER_ERROR (0x13) Invalid caller of this service.
User Guide
Thread Control 201
Allowed From
Threads and timers
Preemption Possible
No
Example
TX_THREAD my_thread;
ULONG my_old_time_slice;
UINT status;
See Also
tx_thread_create, tx_thread_delete, tx_thread_identify,
tx_thread_info_get, tx_thread_preemption_change,
tx_thread_priority_change, tx_thread_relinquish, tx_thread_resume,
tx_thread_sleep, tx_thread_suspend, tx_thread_terminate,
tx_thread_wait_abort
tx_thread_wait_abort
Abort suspension of specified thread
Prototype
UINT tx_thread_wait_abort(TX_THREAD *thread_ptr)
Description
This service aborts sleep or any other object suspension of the specified
thread. If the wait is aborted, a TX_WAIT_ABORTED value is returned
from the service that the thread was waiting on.
Note that this service does not release pure suspension that is made by
i the tx_thread_suspend service.
Input Parameters
thread_ptr Pointer to a previously created application
thread.
Return Values
TX_SUCCESS (0x00) Successful thread wait
abort.
TX_THREAD_ERROR (0x0E) Invalid application thread
pointer.
TX_WAIT_ABORT_ERROR (0x1B) Specified thread is not in a
waiting state.
Allowed From
Initialization, threads, timers, and ISRs
Preemption Possible
Yes
User Guide
Thread Control 203
Example
TX_THREAD my_thread;
UINT status;
See Also
tx_thread_create, tx_thread_delete, tx_thread_identify,
tx_thread_info_get, tx_thread_preemption_change,
tx_thread_priority_change, tx_thread_relinquish, tx_thread_resume,
tx_thread_sleep, tx_thread_suspend, tx_thread_terminate,
tx_thread_time_slice_change
tx_time_get
Retrieves the current time
Time Ticks
Prototype
ULONG tx_time_get(VOID)
Description
This service returns the contents of the internal system clock. Each timer-
tick increases the internal system clock by one. The system clock is set to
zero during initialization and can be changed to a specific value by the
service tx_time_set.
Input Parameters
None
Return Values
system clock ticks Value of the internal, free running, system clock.
Allowed From
Initialization, threads, timers, and ISRs
Preemption Possible
No
User Guide
Time Ticks 205
Example
ULONG current_time;
See Also
tx_time_set
tx_time_set
Sets the current time
Prototype
VOID tx_time_set(ULONG new_time)
Description
This service sets the internal system clock to the specified value. Each
timer-tick increases the internal system clock by one.
Input Parameters
new_time New time to put in the system clock, legal values
range from 0 through 0xFFFFFFFF.
Return Values
None
Allowed From
Threads, timers, and ISRs
Preemption Possible
No
User Guide
Time Ticks 207
Example
/* Set the internal system time to 0x1234. */
tx_time_set(0x1234);
/* Current time now contains 0x1234 until the next timer
interrupt. */
See Also
tx_time_get
tx_timer_activate
Activate an application timer
Application Timers
Prototype
UINT tx_timer_activate(TX_TIMER *timer_ptr)
Description
This service activates the specified application timer. The expiration
routines of timers that expire at the same time are executed in the order
they were activated.
Input Parameters
timer_ptr Pointer to a previously created application timer.
Return Values
TX_SUCCESS (0x00) Successful application timer
activation.
TX_TIMER_ERROR (0x15) Invalid application timer
pointer.
TX_ACTIVATE_ERROR (0x17) Timer was already active.
Allowed From
Initialization, threads, timers, and ISRs
Preemption Possible
No
User Guide
Application Timers 209
Example
TX_TIMER my_timer;
UINT status;
See Also
tx_timer_change, tx_timer_create, tx_timer_deactivate, tx_timer_delete,
tx_timer_info_get
tx_timer_change
Change an application timer
Prototype
UINT tx_timer_change(TX_TIMER *timer_ptr,
ULONG initial_ticks, ULONG reschedule_ticks)
Description
This service changes the expiration characteristics of the specified
application timer. The timer must be deactivated prior to calling this
service.
Input Parameters
timer_ptr Pointer to a timer control block.
initial_ticks Specifies the initial number of ticks for timer
expiration. Legal values range from 1 through
0xFFFFFFFF.
reschedule_ticks Specifies the number of ticks for all timer
expirations after the first. A zero for this
parameter makes the timer a one-shot timer.
Otherwise, for periodic timers, legal values range
from 1 through 0xFFFFFFFF.
Return Values
TX_SUCCESS (0x00) Successful application timer change.
TX_TIMER_ERROR (0x15) Invalid application timer pointer.
TX_TICK_ERROR (0x16) Invalid value (a zero) supplied for initial
ticks.
TX_CALLER_ERROR (0x13) Invalid caller of this service.
User Guide
Application Timers 211
Allowed From
Threads, timers, and ISRs
Preemption Possible
No
Example
TX_TIMER my_timer;
UINT status;
See Also
tx_timer_activate, tx_timer_create, tx_timer_deactivate, tx_timer_delete,
tx_timer_info_get
tx_timer_create
Create an application timer
Prototype
UINT tx_timer_create(TX_TIMER *timer_ptr, CHAR *name_ptr,
VOID (*expiration_function)(ULONG),
ULONG expiration_input, ULONG initial_ticks,
ULONG reschedule_ticks, UINT auto_activate)
Description
This service creates an application timer with the specified expiration
function and periodic.
Input Parameters
timer_ptr Pointer to a timer control block
name_ptr Pointer to the name of the timer.
expiration_function Application function to call when the timer
expires.
expiration_input Input to pass to expiration function when timer
expires.
initial_ticks Specifies the initial number of ticks for timer
expiration. Legal values range from 1 through
0xFFFFFFFF.
reschedule_ticks Specifies the number of ticks for all timer
expirations after the first. A zero for this
parameter makes the timer a one-shot timer.
Otherwise, for periodic timers, legal values range
from 1 through 0xFFFFFFFF.
auto_activate Determines if the timer is automatically activated
during creation. If this value is
TX_AUTO_ACTIVATE (0x01) the timer is made
active. Otherwise, if the value
TX_NO_ACTIVATE (0x00) is selected, the timer
is created in a non-active state. In this case, a
subsequent tx_timer_activate service call is
necessary to get the timer actually started.
User Guide
Application Timers 213
Return Values
TX_SUCCESS (0x00) Successful application timer
creation.
TX_TIMER_ERROR (0x15) Invalid application timer pointer.
Either the pointer is NULL or the
timer is already created.
TX_TICK_ERROR (0x16) Invalid value (a zero) supplied for
initial ticks.
TX_ACTIVATE_ERROR (0x17) Invalid activation selected.
TX_CALLER_ERROR (0x13) Invalid caller of this service.
Allowed From
Initialization and threads
Preemption Possible
No
Example
TX_TIMER my_timer;
UINT status;
See Also
tx_timer_activate, tx_timer_change, tx_timer_deactivate, tx_timer_delete,
tx_timer_info_get
tx_timer_deactivate
Deactivate an application timer
Prototype
UINT tx_timer_deactivate(TX_TIMER *timer_ptr)
Description
This service deactivates the specified application timer. If the timer is
already deactivated, this service has no effect.
Input Parameters
timer_ptr Pointer to a previously created application timer.
Return Values
TX_SUCCESS (0x00) Successful application timer
deactivation.
TX_TIMER_ERROR (0x15) Invalid application timer pointer.
Allowed From
Initialization, threads, timers, and ISRs
Preemption Possible
No
User Guide
Application Timers 215
Example
TX_TIMER my_timer;
UINT status;
See Also
tx_timer_activate, tx_timer_change, tx_timer_create, tx_timer_delete,
tx_timer_info_get
tx_timer_delete
Delete an application timer
Prototype
UINT tx_timer_delete(TX_TIMER *timer_ptr)
Description
This service deletes the specified application timer.
Input Parameters
timer_ptr Pointer to a previously created application timer.
Return Values
TX_SUCCESS (0x00) Successful application timer deletion.
TX_TIMER_ERROR (0x15) Invalid application timer pointer.
TX_CALLER_ERROR (0x13) Invalid caller of this service.
Allowed From
Threads
Preemption Possible
No
User Guide
Application Timers 217
Example
TX_TIMER my_timer;
UINT status;
See Also
tx_timer_activate, tx_timer_change, tx_timer_create, tx_timer_deactivate,
tx_timer_info_get
tx_timer_info_get
Retrieve information about an application timer
Application Timers
Prototype
UINT tx_timer_info_get(TX_TIMER *timer_ptr, CHAR **name,
UINT *active, ULONG *remaining_ticks,
ULONG *reschedule_ticks,
TX_TIMER **next_timer)
Description
This service retrieves information about the specified application timer.
Input Parameters
timer_ptr Pointer to a previously created application timer.
name Pointer to destination for the pointer to the
timer’s name.
active Pointer to destination for the timer active
indication. If the timer is inactive or this service
is called from the timer itself, a TX_FALSE value
is returned. Otherwise, if the timer is active, a
TX_TRUE value is returned.
remaining_ticks Pointer to destination for the number of timer
ticks left before the timer expires.
reschedule_ticks Pointer to destination for the number of timer
ticks that will be used to automatically
reschedule this timer. If the value is zero, then
the timer is a one-shot and won’t be
rescheduled.
next_timer Pointer to destination for the pointer of the next
created application timer.
Return Values
TX_SUCCESS (0x00) Successful timer information retrieval.
TX_TIMER_ERROR (0x15) Invalid application timer pointer.
TX_PTR_ERROR (0x03) Invalid pointer (NULL) for any
destination pointer.
User Guide
Application Timers 219
Allowed From
Initialization, threads, timers, and ISRs
Preemption Possible
No
Example
TX_TIMER my_timer;
CHAR *name;
UINT active;
ULONG remaining_ticks;
ULONG reschedule_ticks;
TX_TIMER *next_timer;
UINT status;
See Also
tx_timer_activate, tx_timer_change, tx_timer_create, tx_timer_deactivate,
tx_timer_delete, tx_timer_info_get
User Guide
CHAPTER 5
User Guide
222 I/O Drivers for ThreadX
Driver Functions
ThreadX I/O drivers are composed of eight basic
functional areas, as follows:
Driver Initialization
Driver Control
Driver Access
Driver Input
Driver Output
Driver Interrupts
Driver Status
Driver Termination
User Guide
Driver Functions 223
Driver Control After the driver is initialized and ready for operation,
this functional area is responsible for run-time
control. Typically, run-time control consists of making
changes to the underlying hardware device.
Examples include changing the baud rate of a serial
device or seeking a new sector on a disk.
Driver Access Some I/O drivers are called only from a single
application thread. In such cases, this functional area
is not needed. However, in applications where
multiple threads need simultaneous driver access,
their interaction must be controlled by adding assign/
release facilities in the I/O driver. Alternatively, the
application may use a semaphore to control driver
access and avoid extra overhead and complication
inside the driver.
User Guide
Simple Driver Example 225
VOID tx_sdriver_initialize(VOID)
{
Simple Driver Input for the simple driver centers around the input
Input semaphore. When a serial device input interrupt is
received, the input semaphore is set. If one or more
threads are waiting for a character from the driver,
the thread waiting the longest is resumed. If no
threads are waiting, the semaphore simply remains
set until a thread calls the drive input function.
User Guide
Simple Driver Example 227
UCHAR tx_sdriver_input(VOID)
{
VOID tx_sdriver_input_ISR(VOID)
{
/* See if an input character notification is pending. */
if (!tx_sdriver_input_semaphore.tx_semaphore_count)
{
/* If not, notify thread of an input character. */
tx_semaphore_put(&tx_sdriver_input_semaphore);
}
}
VOID tx_sdriver_output_ISR(VOID)
{
/* Notify thread last character transmit is
complete. */
tx_semaphore_put(&tx_sdriver_output_semaphore);
}
Simple Driver This simple I/O driver example illustrates the basic
Shortcomings idea of a ThreadX device driver. However, because
the simple I/O driver does not address data buffering
or any overhead issues, it does not fully represent
real-world ThreadX drivers. The following section
describes some of the more advanced issues
associated with I/O drivers.
User Guide
Advanced Driver Issues 229
Circular Byte Circular byte buffers are typically used in drivers that
Buffers manage a simple serial hardware device like a
UART. Two circular buffers are most often used in
such situations—one for input and one for output.
Circular Buffer The input buffer is used to hold characters that arrive
Input before the application is ready for them. When an
input character is received (usually in an interrupt
service routine), the new character is retrieved from
the hardware device and placed into the input buffer
at the location pointed to by the write pointer. The
write pointer is then advanced to the next position in
UCHAR tx_input_buffer[MAX_SIZE];
UCHAR tx_input_write_ptr;
UCHAR tx_input_read_ptr;
/* Initialization. */
tx_input_write_ptr = &tx_input_buffer[0];
tx_input_read_ptr = &tx_input_buffer[0];
User Guide
Advanced Driver Issues 231
UCHAR tx_output_buffer[MAX_SIZE];
UCHAR tx_output_write_ptr;
UCHAR tx_output_read_ptr;
/* Initialization. */
tx_output_write_ptr = &tx_output_buffer[0];
tx_output_read_ptr = &tx_output_buffer[0];
TX_BLOCK_POOL tx_io_block_pool;
User Guide
Advanced Driver Issues 233
Input List
Input Head Pointer Input Tail Pointer
more buffers
in packet or
NULL
Output List
Output Head Pointer Output Tail Pointer
more buffers
in packet or
NULL
User Guide
Advanced Driver Issues 235
User Guide
CHAPTER 6
User Guide
238 Demonstration System for ThreadX
Overview
Each ThreadX product distribution contains a
demonstration system that runs on all supported
microprocessors.
Application Define
The tx_application_define function executes after
the basic ThreadX initialization is complete. It is
responsible for setting up all of the initial system
resources, including threads, queues, semaphores,
mutexes, event flags, and memory pools.
User Guide
Application Define 239
thread_7
queue_0
semaphore_0
event_flags_0
mutex_0
block_pool_0
Thread 0
The function thread_0_entry marks the entry point
of the thread (lines 167-190). Thread_0 is the first
thread in the demonstration system to execute. Its
processing is simple: it increments its counter, sleeps
for 10 timer ticks, sets an event flag to wake up
thread_5, then repeats the sequence.
Thread 1
The function thread_1_entry marks the entry point
of the thread (lines 193-216). Thread_1 is the
second-to-last thread in the demonstration system to
execute. Its processing consists of incrementing its
counter, sending a message to thread_2 (through
queue_0), and repeating the sequence. Notice that
thread_1 suspends whenever queue_0 becomes
full (line 207).
Thread 2
The function thread_2_entry marks the entry point
of the thread (lines 219-243). Thread_2 is the last
thread in the demonstration system to execute. Its
processing consists of incrementing its counter,
getting a message from thread_1 (through
queue_0), and repeating the sequence. Notice that
thread_2 suspends whenever queue_0 becomes
empty (line 233).
User Guide
Threads 3 and 4 241
are also the only threads that are ready for execution
most of the time. They are also the only threads
created with time-slicing (lines 74 and 82). Each
thread is allowed to execute for a maximum of 4
timer ticks before the other thread is executed.
Threads 3 and 4
The function thread_3_and_4_entry marks the
entry point of both thread_3 and thread_4 (lines
246-280). Both threads have a priority of 8, which
makes them the third and fourth threads in the
demonstration system to execute. The processing for
each thread is the same: incrementing its counter,
getting semaphore_0, sleeping for 2 timer ticks,
releasing semaphore_0, and repeating the
sequence. Notice that each thread suspends
whenever semaphore_0 is unavailable (line 264).
Thread 5
The function thread_5_entry marks the entry point
of the thread (lines 283-305). Thread_5 is the
second thread in the demonstration system to
execute. Its processing consists of incrementing its
Threads 6 and 7
The function thread_6_and_7_entry marks the
entry point of both thread_6 and thread_7 (lines
307-358). Both threads have a priority of 8, which
makes them the fifth and sixth threads in the
demonstration system to execute. The processing for
each thread is the same: incrementing its counter,
getting mutex_0 twice, sleeping for 2 timer ticks,
releasing mutex_0 twice, and repeating the
sequence. Notice that each thread suspends
whenever mutex_0 is unavailable (line 325).
Also both threads use the same function for their
main processing. This presents no problems
because they both have their own unique stack, and
C is naturally reentrant. Each thread determines
which one it is by examination of the thread input
parameter (line 319), which is setup when they are
created (lines 126 and 133).
User Guide
Distribution file: demo.c 243
thread_7_counter
000 /* This is a small demo of the high-performance ThreadX kernel. It includes examples of eight
001 threads of different priorities, using a message queue, semaphore, mutex, event flags group,
002 byte pool, and block pool. */
003
004 #include"tx_api.h"
005
006 #define DEMO_STACK_SIZE 1024
007 #define DEMO_BYTE_POOL_SIZE 9120
008 #define DEMO_BLOCK_POOL_SIZE 100
009 #define DEMO_QUEUE_SIZE 100
010
011 /* Define the ThreadX object control blocks... */
012
013 TX_THREAD thread_0;
014 TX_THREAD thread_1;
015 TX_THREAD thread_2;
016 TX_THREAD thread_3;
017 TX_THREAD thread_4;
018 TX_THREAD thread_5;
019 TX_THREAD thread_6;
020 TX_THREAD thread_7;
021 TX_QUEUE queue_0;
022 TX_SEMAPHORE semaphore_0;
023 TX_MUTEX mutex_0;
024 TX_EVENT_FLAGS_GROUP event_flags_0;
025 TX_BYTE_POOL byte_pool_0;
026 TX_BLOCK_POOL block_pool_0;
027
028 /* Define the counters used in the demo application... */
029
030 ULONG thread_0_counter;
031 ULONG thread_1_counter;
032 ULONG thread_1_messages_sent;
033 ULONG thread_2_counter;
034 ULONG thread_2_messages_received;
035 ULONG thread_3_counter;
036 ULONG thread_4_counter;
037 ULONG thread_5_counter;
038 ULONG thread_6_counter;
039 ULONG thread_7_counter;
040
041 /* Define thread prototypes. */
042
043 void thread_0_entry(ULONG thread_input);
044 void thread_1_entry(ULONG thread_input);
045 void thread_2_entry(ULONG thread_input);
046 void thread_3_and_4_entry(ULONG thread_input);
047 void thread_5_entry(ULONG thread_input);
048 void thread_6_and_7_entry(ULONG thread_input);
049
050
051 /* Define main entry point. */
052
053 int main()
054 {
055
056 /* Enter the ThreadX kernel. */
057 tx_kernel_enter();
058 }
059
060 /* Define what the initial system looks like. */
061 void tx_application_define(void *first_unused_memory)
062 {
063
064 CHAR *pointer;
065
066 /* Create a byte memory pool from which to allocate the thread stacks. */
067 tx_byte_pool_create(&byte_pool_0, "byte pool 0", first_unused_memory,
068 DEMO_BYTE_POOL_SIZE);
069
070 /* Put system definition stuff in here, e.g. thread creates and other assorted
071 create information. */
User Guide
Distribution file: demo.c 245
072
073 /* Allocate the stack for thread 0. */
074 tx_byte_allocate(&byte_pool_0, &pointer, DEMO_STACK_SIZE, TX_NO_WAIT);
075
076 /* Create the main thread. */
077 tx_thread_create(&thread_0, "thread 0", thread_0_entry, 0,
078 pointer, DEMO_STACK_SIZE,
079 1, 1, TX_NO_TIME_SLICE, TX_AUTO_START);
080
081 /* Allocate the stack for thread 1. */
082 tx_byte_allocate(&byte_pool_0, &pointer, DEMO_STACK_SIZE, TX_NO_WAIT);
083
084 /* Create threads 1 and 2. These threads pass information through a ThreadX
085 message queue. It is also interesting to note that these threads have a time
086 slice. */
087 tx_thread_create(&thread_1, "thread 1", thread_1_entry, 1,
088 pointer, DEMO_STACK_SIZE,
089 16, 16, 4, TX_AUTO_START);
090
091 /* Allocate the stack for thread 2. */
092 tx_byte_allocate(&byte_pool_0, &pointer, DEMO_STACK_SIZE, TX_NO_WAIT);
093 tx_thread_create(&thread_2, "thread 2", thread_2_entry, 2,
094 pointer, DEMO_STACK_SIZE,
095 16, 16, 4, TX_AUTO_START);
096
097 /* Allocate the stack for thread 3. */
098 tx_byte_allocate(&byte_pool_0, &pointer, DEMO_STACK_SIZE, TX_NO_WAIT);
099
100 /* Create threads 3 and 4. These threads compete for a ThreadX counting semaphore.
101 An interesting thing here is that both threads share the same instruction area. */
102 tx_thread_create(&thread_3, "thread 3", thread_3_and_4_entry, 3,
103 pointer, DEMO_STACK_SIZE,
104 8, 8, TX_NO_TIME_SLICE, TX_AUTO_START);
105
106 /* Allocate the stack for thread 4. */
107 tx_byte_allocate(&byte_pool_0, &pointer, DEMO_STACK_SIZE, TX_NO_WAIT);
108
109 tx_thread_create(&thread_4, "thread 4", thread_3_and_4_entry, 4,
110 pointer, DEMO_STACK_SIZE,
111 8, 8, TX_NO_TIME_SLICE, TX_AUTO_START);
112
113 /* Allocate the stack for thread 5. */
114 tx_byte_allocate(&byte_pool_0, &pointer, DEMO_STACK_SIZE, TX_NO_WAIT);
115
116 /* Create thread 5. This thread simply pends on an event flag which will be set
117 by thread_0. */
118 tx_thread_create(&thread_5, "thread 5", thread_5_entry, 5,
119 pointer, DEMO_STACK_SIZE,
120 4, 4, TX_NO_TIME_SLICE, TX_AUTO_START);
121
122 /* Allocate the stack for thread 6. */
123 tx_byte_allocate(&byte_pool_0, &pointer, DEMO_STACK_SIZE, TX_NO_WAIT);
124
125 /* Create threads 6 and 7. These threads compete for a ThreadX mutex. */
126 tx_thread_create(&thread_6, "thread 6", thread_6_and_7_entry, 6,
127 pointer, DEMO_STACK_SIZE,
128 8, 8, TX_NO_TIME_SLICE, TX_AUTO_START);
129
130 /* Allocate the stack for thread 7. */
131 tx_byte_allocate(&byte_pool_0, &pointer, DEMO_STACK_SIZE, TX_NO_WAIT);
132
133 tx_thread_create(&thread_7, "thread 7", thread_6_and_7_entry, 7,
134 pointer, DEMO_STACK_SIZE,
135 8, 8, TX_NO_TIME_SLICE, TX_AUTO_START);
136
137 /* Allocate the message queue. */
138 tx_byte_allocate(&byte_pool_0, &pointer, DEMO_QUEUE_SIZE*sizeof(ULONG), TX_NO_WAIT);
139
140 /* Create the message queue shared by threads 1 and 2. */
141 tx_queue_create(&queue_0, "queue 0", TX_1_ULONG, pointer, DEMO_QUEUE_SIZE*sizeof(ULONG));
142
143 /* Create the semaphore used by threads 3 and 4. */
User Guide
Distribution file: demo.c 247
216 }
217
218
219 void thread_2_entry(ULONG thread_input)
220 {
221
222 ULONG received_message;
223 UINT status;
224
225 /* This thread retrieves messages placed on the queue by thread 1. */
226 while(1)
227 {
228
229 /* Increment the thread counter. */
230 thread_2_counter++;
231
232 /* Retrieve a message from the queue. */
233 status = tx_queue_receive(&queue_0, &received_message, TX_WAIT_FOREVER);
234
235 /* Check completion status and make sure the message is what we
236 expected. */
237 if ((status != TX_SUCCESS) || (received_message != thread_2_messages_received))
238 break;
239
240 /* Otherwise, all is okay. Increment the received message count. */
241 thread_2_messages_received++;
242 }
243 }
244
245
246 void thread_3_and_4_entry(ULONG thread_input)
247 {
248
249 UINT status;
250
251
252 /* This function is executed from thread 3 and thread 4. As the loop
253 below shows, these function compete for ownership of semaphore_0. */
254 while(1)
255 {
256
257 /* Increment the thread counter. */
258 if (thread_input == 3)
259 thread_3_counter++;
260 else
261 thread_4_counter++;
262
263 /* Get the semaphore with suspension. */
264 status = tx_semaphore_get(&semaphore_0, TX_WAIT_FOREVER);
265
266 /* Check status. */
267 if (status != TX_SUCCESS)
268 break;
269
270 /* Sleep for 2 ticks to hold the semaphore. */
271 tx_thread_sleep(2);
272
273 /* Release the semaphore. */
274 status = tx_semaphore_put(&semaphore_0);
275
276 /* Check status. */
277 if (status != TX_SUCCESS)
278 break;
279 }
280 }
281
282
283 void thread_5_entry(ULONG thread_input)
284 {
285
286 UINT status;
287 ULONG actual_flags;
288
289
290 /* This thread simply waits for an event in a forever loop. */
291 while(1)
292 {
293
294 /* Increment the thread counter. */
295 thread_5_counter++;
296
297 /* Wait for event flag 0. */
298 status = tx_event_flags_get(&event_flags_0, 0x1, TX_OR_CLEAR,
299 &actual_flags, TX_WAIT_FOREVER);
300
301 /* Check status. */
302 if ((status != TX_SUCCESS) || (actual_flags != 0x1))
303 break;
304 }
305 }
306
307 void thread_6_and_7_entry(ULONG thread_input)
308 {
309
310 UINT status;
311
312
313 /* This function is executed from thread 6 and thread 7. As the loop
314 below shows, these function compete for ownership of mutex_0. */
315 while(1)
316 {
317
318 /* Increment the thread counter. */
319 if (thread_input == 6)
320 thread_6_counter++;
321 else
322 thread_7_counter++;
323
324 /* Get the mutex with suspension. */
325 status = tx_mutex_get(&mutex_0, TX_WAIT_FOREVER);
326
327 /* Check status. */
328 if (status != TX_SUCCESS)
329 break;
330
331 /* Get the mutex again with suspension. This shows
332 that an owning thread may retrieve the mutex it
333 owns multiple times. */
334 status = tx_mutex_get(&mutex_0, TX_WAIT_FOREVER);
335
336 /* Check status. */
337 if (status != TX_SUCCESS)
338 break;
339
340 /* Sleep for 2 ticks to hold the mutex. */
341 tx_thread_sleep(2);
342
343 /* Release the mutex. */
344 status = tx_mutex_put(&mutex_0);
345
346 /* Check status. */
347 if (status != TX_SUCCESS)
348 break;
349
350 /* Release the mutex again. This will actually
351 release ownership since it was obtained twice. */
352 status = tx_mutex_put(&mutex_0);
353
354 /* Check status. */
355 if (status != TX_SUCCESS)
356 break;
357 }
358 }
User Guide
CHAPTER 7
Internal Composition of
ThreadX
User Guide
250 Internal Composition of ThreadX
User Guide
251
TXE_TTSC.C 272
TXE_TWA.C 272
1 Timer Component 273
TX_TIM.H 273
TX_TA.C 275
TX_TAA.C 276
TX_TD.C 276
TX_TDA.C 276
TX_TIMCH.C 276
TX_TIMCR.C 276
TX_TIMD.C 276
TX_TIMEG.C 276
TX_TIMES.C 276
TX_TIMI.C 277
TX_TIMIG.C 277
TX_TIMIN.[S,ASM] 277
TX_TTE.C 277
TXE_TAA.C 277
TXE_TDA.C 277
TXE_TIMD.C 277
TXE_TIMI.C 277
TXE_TMCH.C 278
TXE_TMCR.C 278
1 Queue Component 278
TX_QUE.H 278
TX_QC.C 278
TX_QCLE.C 279
TX_QD.C 279
TX_QF.C 279
TX_QFS.C 279
TX_QI.C 279
TX_QIG.C 279
TX_QP.C 279
TX_QR.C 279
TX_QS.C 280
TXE_QC.C 280
TXE_QD.C 280
TXE_QF.C 280
TXE_QFS.C 280
TXE_QIG.C 280
TXE_QP.C 280
TXE_QR.C 280
TXE_QS.C 281
1 Semaphore Component 281
TX_SEM.H 281
TX_SC.C 281
TX_SCLE.C 282
TX_SD.C 282
TX_SG.C 282
TX_SI.C 282
TX_SIG.C 282
TX_SP.C 282
TX_SPRI.C 282
TXE_SC.C 282
TXE_SD.C 283
TXE_SG.C 283
TXE_SIG.C 283
TXE_SP.C 283
TXE_SPRI.C 283
1 Mutex Component 283
TX_MUT.H 283
TX_MC.C 284
TX_MCLE.C 284
TX_MD.C 284
TX_MG.C 284
TX_MI.C 284
TX_MIG.C 285
TX_MP.C 285
TX_MPC.C 285
TX_MPRI.C 285
TXE_MC.C 285
TXE_MD.C 285
TXE_MG.C 285
TXE_MIG.C 285
TXE_MP.C 286
TXE_MPRI.C 286
1 Event Flag Component 286
TX_EVE.H 286
TX_EFC.C 287
TX_EFCLE.C 287
TX_EFD.C 287
TX_EFG.C 287
User Guide
253
TX_EFI.C 287
TX_EFIG.C 287
TX_EFS.C 287
TXE_EFC.C 287
TXE_EFD.C 288
TXE_EFG.C 288
TXE_EFIG.C 288
TXE_EFS.C 288
1 Block Memory Component 288
TX_BLO.H 288
TX_BA.C 289
TX_BPC.C 289
TX_BPCLE.C 289
TX_BPD.C 289
TX_BPI.C 289
TX_BPIG.C 289
TX_BPP.C 290
TX_BR.C 290
TXE_BA.C 290
TXE_BPC.C 290
TXE_BPD.C 290
TXE_BPIG.C 290
TXE_BPP.C 290
TXE_BR.C 290
1 Byte Memory Component 291
TX_BYT.H 291
TX_BYTA.C 291
TX_BYTC.C 291
TX_BYTCL.C 292
TX_BYTD.C 292
TX_BYTI.C 292
TX_BYTIG.C 292
TX_BYTPP.C 292
TX_BYTR.C 292
TX_BYTS.C 292
TXE_BTYA.C 293
TXE_BYTC.C 293
TXE_BYTD.C 293
TXE_BYTG.C 293
TXE_BYTP.C 293
TXE_BYTR.C 293
User Guide
ThreadX Design Goals 255
System Entry From the application’s point of view, the entry point of
ThreadX is the function tx_kernel_enter. However,
this function is contained in the initialization file so its
real name is _tx_initialize_kernel_enter. Typically,
this function is called from the application main
routine with interrupts still disabled from the
hardware reset and compiler start-up processing.
Software Components
Express Logic utilizes a software component
methodology in its products. A software component
is somewhat similar to an object or class in C++.
Each component provides a set of action functions
that operate on the internal data of the component. In
general, components are not allowed access to the
User Guide
Software Components 257
Coding Conventions
All ThreadX software conforms to a strict set of
coding conventions. This makes it easier to
understand and maintain. In addition, it provides a
reasonable template for application software
conventions.
User Guide
Coding Conventions 259
User Guide
Coding Conventions 261
ThreadX Member ThreadX structure member names are all lower case
Names and take on the form
tx_c_name
_tx_c_name
User Guide
Coding Conventions 263
/**************************************************************************/
/** */
/** ThreadX Component */
/** */
/** Thread Control (THR) */
/** */
/**************************************************************************/
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _tx_thread_create PORTABLE C */
/* 3.0 */
/* AUTHOR */
/* */
/* William E. Lamie, Express Logic, Inc. */
/* */
/* DESCRIPTION */
/* */
/* This function creates a thread and places it on the list of created */
/* threads. */
/* */
/* INPUT */
/* */
/* thread_ptr Thread control block pointer */
/* name Pointer to thread name string */
/* entry_function Entry function of the thread */
/* entry_input 32-bit input value to thread */
/* stack_start Pointer to start of stack */
/* stack_size Stack size in bytes */
/* priority Priority of thread (0-31) */
/* preempt_threshold Preemption-threshold */
/* time_slice Thread time-slice value */
/* auto_start Automatic start selection */
/* */
/* OUTPUT */
/* */
/* return status Thread create return status */
/* */
/* CALLS */
/* */
/* _tx_thread_stack_build Build initial thread stack */
/* _tx_thread_resume Resume automatic start thread */
/* _tx_thread_system_return Return to system on preemption*/
/* */
/* CALLED BY */
/* */
/* Application Code */
/* _tx_timer_initialize Create system timer thread */
/* */
/* RELEASE HISTORY */
/* */
/* DATE NAME DESCRIPTION */
/* */
/* 12-31-1996 William E. Lamie Initial Version 3.0 */
/* */
/**************************************************************************/
Initialization Component
This component is responsible for performing all
ThreadX initialization. This processing includes
setting-up processor specific resources as well as
calling all of the other component initialization
functions. Once basic ThreadX initialization is
complete, the application tx_application_define
function is called to perform application specific
initialization. The thread scheduling loop is entered
after all initialization is complete.
User Guide
Thread Component 265
Thread Component
This component is responsible for all thread
management activities, including thread creation,
scheduling, and interrupt management. The thread
component is the most processor/compiler-specific
of all ThreadX components, hence, it has the most
assembly language files.
_tx_thread_current_ptr
This TX_THREAD pointer contains the
address of the currently running thread’s
control block. If this pointer is NULL, the
system is idle.
_tx_thread_execute_ptr
This TX_THREAD pointer contains the
address of the next thread to execute and is
User Guide
Thread Component 267
User Guide
Thread Component 269
User Guide
Thread Component 271
User Guide
Timer Component 273
Timer Component
This component is responsible for all timer
management activities, including thread time-slicing,
thread sleeps, API service time-outs, and application
timers. The timer component has one processor/
compiler-specific function that is responsible for
handling the physical timer interrupt.
_tx_timer_system_clock
This ULONG contains a tick counter that
increments on each timer interrupt.
_tx_timer_time_slice
This ULONG contains the time-slice of the
current thread. If this value is zero, no time-
slice is active.
_tx_timer_expired_time_slice
This UINT is set if a time-slice expiration is
detected in the timer interrupt handling. It is
cleared once the time-slice has been
processed in the ISR.
_tx_timer_list
This array of active timer linked-list head
pointers is indexed by the timer’s relative
time displacement from the current time
pointer. Each timer expiration list is
maintained in a doubly-linked, circular
fashion.
_tx_timer_list_start
This TX_INTERNAL_TIMER head pointer
contains the address of the first timer list. It is
used to reset the _tx_timer_current_ptr to
the beginning of _tx_timer_list when a wrap
condition is detected.
_tx_timer_list_end
This TX_INTERNAL_TIMER head pointer
contains the address of the end of the
_tx_timer_list array. It is used to signal when
to reset the _tx_timer_current_ptr to the
beginning of the _tx_timer_list.
_tx_timer_current_ptr
This TX_INTERNAL_TIMER head pointer
points to an active timer list in the
_tx_timer_list array. If a timer interrupt
occurs and this entry is non-NULL, one or
more timers have possibly expired. This
pointer is positioned to point at the next timer
list head pointer after each timer interrupt.
_tx_timer_expired
This UINT flag is set in the timer ISR when a
timer has expired. It is cleared in the timer
system thread after the expiration has been
processed.
_tx_timer_thread
This TX_THREAD structure is the control
block for the internal timer thread. This
thread is setup during initialization and is
used to process all timer expirations.
_tx_timer_stack_start
This VOID pointer represents the starting
address of the internal timer thread’s stack.
User Guide
Timer Component 275
_tx_timer_stack_size
This ULONG represents the size of the
internal timer thread’s stack. This variable
contains the value specified by
TX_TIMER_THREAD_STACK_SIZE, which
is defined inside of tx_port.h or on the
command line.
_tx_timer_priority
This UINT represents the priority of the
internal timer thread.
_tx_timer_created_ptr
This TX_TIMER pointer is the head pointer
of the created application timer list. The list is
a doubly-linked, circular list of all created
timer control blocks.
_tx_timer_created_count
This ULONG represents the number of
created application timers.
_tx_timer_thread_stack_area
This character array allocates space for the
system timer’s stack. The size of the array is
defined by
TX_TIMER_THREAD_STACK_SIZE, and
the _tx_timer_stack_start and
_tx_timer_stack_end pointers point to the
beginning and end of this array.
User Guide
Timer Component 277
Queue Component
This component is responsible for all queue
management activities, including queue creation,
deletion, and message sending/receiving.
_tx_queue_created_ptr
This TX_QUEUE pointer is the head pointer
of the created queue list. The list is a doubly-
linked, circular list of all created queue
control blocks.
_tx_queue_created_count
This ULONG represents the number of
created application queues.
User Guide
Queue Component 279
User Guide
Semaphore Component 281
Semaphore Component
This component is responsible for all semaphore
management activities, including semaphore
creation, deletion, semaphore gets, and semaphore
puts.
_tx_semaphore_created_ptr
This TX_SEMAPHORE pointer is the head
pointer of the created semaphore list. The
list is a doubly-linked, circular list of all
created semaphore control blocks.
_tx_semaphore_created_count
This ULONG represents the number of
created application semaphores.
User Guide
Mutex Component 283
Mutex Component
This component is responsible for all mutex
management activities, including mutex creation,
deletion, mutex gets, and mutex puts.
_tx_mutex_created_ptr
This TX_MUTEX pointer is the head pointer
of the created mutex list. The list is a doubly-
linked, circular list of all created mutex
control blocks.
_tx_mutex_created_count
This ULONG represents the number of
created application mutexes.
User Guide
Mutex Component 285
_tx_event_flags_created_ptr
This TX_EVENT_FLAGS_GROUP pointer is
the head pointer of the created event flags
list. The list is a doubly-linked, circular list of
all created event flags control blocks.
_tx_event_flags_created_count
This ULONG represents the number of
created application event flags.
User Guide
Event Flag Component 287
User Guide
Block Memory Component 289
_tx_block_pool_created_ptr
This TX_BLOCK_POOL pointer is the head
pointer of the created block memory pool list.
The list is a doubly-linked, circular list of all
created block pool control blocks.
_tx_block_pool_created_count
This ULONG represents the number of
created application block memory pools.
User Guide
Byte Memory Component 291
_tx_byte_pool_created_ptr
This TX_BYTE_POOL pointer is the head
pointer of the created byte memory pool list.
The list is a doubly-linked, circular list of all
created byte pool control blocks.
_tx_byte_pool_created_count
This ULONG represents the number of
created application byte memory pools.
User Guide
Byte Memory Component 293
User Guide
APPENDIX A
User Guide
296 ThreadX API Services
Function
User Guide
ThreadX API Services 297
Control
User Guide
ThreadX API Services 299
User Guide
APPENDIX B
ThreadX Constants
1 Alphabetic Listings 302
1 Listing by Value 304
User Guide
302 ThreadX Constants
Alphabetic TX_1_ULONG 1
Listings TX_2_ULONG 2
TX_4_ULONG 4
TX_8_ULONG 8
TX_16_ULONG 16
TX_ACTIVATE_ERROR 0x0017
TX_AND 2
TX_AUTO_ACTIVATE 1
TX_AND_CLEAR 3
TX_AUTO_START 1
TX_BLOCK_MEMORY 8
TX_BYTE_MEMORY 9
TX_CALLER_ERROR 0x0013
TX_COMPLETED 1
TX_DELETE_ERROR 0x0011
TX_DELETED 0x0001
TX_DONT_START 0
TX_EVENT_FLAG 7
TX_FALSE 0
TX_FILE 11
TX_FOREVER 1
TX_GROUP_ERROR 0x0006
TX_INHERIT 1
TX_INHERIT_ERROR 0x001F
TX_IO_DRIVER 10
TX_MAX_PRIORITIES 32
TX_MUTEX_ERROR 0x001F
TX_MUTEX_SUSP 13
TX_NO_ACTIVATE 0
TX_NO_EVENTS 0x0007
TX_NO_INHERIT 0
User Guide
303
TX_NO_INSTANCE 0x000D
TX_NO_MEMORY 0x0010
TX_NO_TIME_SLICE 0
TX_NO_WAIT 0
TX_NOT_AVAILABLE 0x001D
TX_NOT_OWNED 0x001E
TX_NULL 0
TX_OPTION_ERROR 0x0008
TX_OR 0
TX_OR_CLEAR 1
TX_POOL_ERROR 0x0002
TX_PRIORITY_ERROR 0x000F
TX_PTR_ERROR 0x0003
TX_QUEUE_EMPTY 0x000A
TX_QUEUE_ERROR 0x0009
TX_QUEUE_FULL 0x000B
TX_QUEUE_SUSP 5
TX_READY 0
TX_RESUME_ERROR 0x0012
TX_SEMAPHORE_ERROR 0x000C
TX_SEMAPHORE_SUSP 6
TX_SIZE_ERROR 0x0005
TX_SLEEP 4
TX_START_ERROR 0x0010
TX_SUCCESS 0x0000
TX_SUSPEND_ERROR 0x0014
TX_SUSPEND_LIFTED 0x0019
TX_SUSPENDED 3
TX_TCP_IP 12
TX_TERMINATED 2
TX_THREAD_ERROR 0x000E
TX_THRESH_ERROR 0x0018
TX_TICK_ERROR 0x0016
TX_TIMER_ERROR 0x0015
TX_TRUE 1
TX_WAIT_ABORT_ERROR 0x001B
TX_WAIT_ABORTED 0x001A
TX_WAIT_ERROR 0x0004
TX_WAIT_FOREVER FFFFFFFF
User Guide
305
TX_POOL_ERROR 0x0002
TX_TERMINATED 2
TX_AND_CLEAR 3
TX_PTR_ERROR 0x0003
TX_SUSPENDED 3
TX_4_ULONG 4
TX_SLEEP 4
TX_WAIT_ERROR 0x0004
TX_QUEUE_SUSP 5
TX_SIZE_ERROR 0x0005
TX_GROUP_ERROR 0x0006
TX_SEMAPHORE_SUSP 6
TX_EVENT_FLAG 7
TX_NO_EVENTS 0x0007
TX_8_ULONG 8
TX_BLOCK_MEMORY 8
TX_OPTION_ERROR 0x0008
TX_BYTE_MEMORY 9
TX_QUEUE_ERROR 0x0009
TX_IO_DRIVER 10
TX_QUEUE_EMPTY 0x000A
TX_FILE 11
TX_QUEUE_FULL 0x000B
TX_SEMAPHORE_ERROR 0x000C
TX_TCP_IP 12
TX_MUTEX_SUSP 13
TX_NO_INSTANCE 0x000D
TX_THREAD_ERROR 0x000E
TX_PRIORITY_ERROR 0x000F
TX_16_ULONG 16
TX_START_ERROR 0x0010
TX_NO_MEMORY 0x0010
TX_DELETE_ERROR 0x0011
TX_RESUME_ERROR 0x0012
TX_CALLER_ERROR 0x0013
TX_SUSPEND_ERROR 0x0014
TX_TIMER_ERROR 0x0015
TX_TICK_ERROR 0x0016
TX_ACTIVATE_ERROR 0x0017
TX_THRESH_ERROR 0x0018
TX_SUSPEND_LIFTED 0X0019
TX_WAIT_ABORTED 0x001A
TX_WAIT_ABORT_ERROR 0x001B
TX_MUTEX_ERROR 0x001C
TX_NOT_AVAILABLE 0x001D
TX_NOT_OWNED 0x001E
TX_INHERIT_ERROR 0x001F
TX_MAX_PRIORITIES 32
TX_WAIT_FOREVER FFFFFFFF
User Guide
APPENDIX C
User Guide
308 ThreadX Data Types
User Guide
309
} TX_THREAD;
*tx_semaphore_created_previous;
} TX_SEMAPHORE;
User Guide
311
} TX_MUTEX;
User Guide
APPENDIX D
User Guide
314 ThreadX Source Files
User Guide
ThreadX C Source Files 315
User Guide
ThreadX C Source Files 317
User Guide
ThreadX C Source Files 319
User Guide
APPENDIX E
User Guide
322 ASCII Character Codes
0_ 1_ 2_ 3_ 4_ 5_ 6_ 7_
_0 NUL DLE SP 0 @ P ' p
_1 SOH DC1 ! 1 A Q a q
_2 STX DC2 " 2 B R b r
_3 ETX DC3 # 3 C S c s
_4 EOT DC4 $ 4 D T d t
least significant nibble
_5 ENQ NAK % 5 E U e u
_6 ACK SYN & 6 F V f v
_7 BEL ETB ' 7 G W g w
_8 BS CAN ( 8 H X h x
_9 HT EM ) 9 I Y i y
_A LF SUB * : J Z j z
_B VT ESC + ; K [ K }
_C FF FS , < L \ l |
_D CR GS - = M ] m }
_E SO RS . > N ^ n ~
_F SI US / ? O _ o DEL
User Guide
Index
Symbols _tx_mutex_create 284
_tx_mutex_created_count 284
__tx_thread_context_restore 86 _tx_mutex_created_ptr 284
__tx_thread_context_save 86 _tx_mutex_delete 284
_application_ISR_entry 86 _tx_mutex_get 284
_tx_block_allocate 289 _tx_mutex_info_get 285
_tx_block_pool_cleanup 289 _tx_mutex_initialize 284
_tx_block_pool_create 289 _tx_mutex_prioritize 285
_tx_block_pool_created_count 289 _tx_mutex_priority_change 285
_tx_block_pool_created_ptr 289 _tx_mutex_put 285
_tx_block_pool_delete 289 _tx_queue_cleanup 279
_tx_block_pool_info_get 289 _tx_queue_create 278
_tx_block_pool_initialize 289 _tx_queue_created_count 278
_tx_block_pool_prioritize 290 _tx_queue_created_ptr 278
_tx_block_release 290 _tx_queue_delete 279
_tx_byte_allocate 291 _tx_queue_flush 279
_tx_byte_pool_cleanup 292 _tx_queue_front_send 279
_tx_byte_pool_create 291 _tx_queue_info_get 279
_tx_byte_pool_created_count 291 _tx_queue_initialize 279
_tx_byte_pool_created_ptr 291 _tx_queue_prioritize 279
_tx_byte_pool_delete 292 _tx_queue_receive 279
_tx_byte_pool_info_get 292 _tx_queue_send 280
_tx_byte_pool_initialize 292 _tx_semaphore_cleanup 282
_tx_byte_pool_prioritize 292 _tx_semaphore_create 281
_tx_byte_pool_search 292 _tx_semaphore_created_count 281
_tx_byte_release 292 _tx_semaphore_created_ptr 281
_tx_event_flags_cleanup 287 _tx_semaphore_delete 282
_tx_event_flags_create 287 _tx_semaphore_get 282
_tx_event_flags_created_count 286 _tx_semaphore_info_get 282
_tx_event_flags_created_ptr 286 _tx_semaphore_initialize 282
_tx_event_flags_delete 287 _tx_semaphore_prioritize 282
_tx_event_flags_get 287 _tx_semaphore_put 282
_tx_event_flags_info_get 287 _tx_thread_context_restore 225
_tx_event_flags_initialize 287 _tx_thread_context_save 225, 268
_tx_event_flags_set 287 _tx_thread_create 259, 262
_tx_initialize_high_level 264 _tx_thread_created_count 266
_tx_initialize_kernel_enter 256, 259, 264 _tx_thread_created_ptr 266
_tx_initialize_low_level 265 _tx_thread_current_ptr 261, 265, 268
_tx_initialize_unused_memory 264 _tx_thread_delete 268
_tx_mutex_cleanup 284
User Guide
_txe_semaphore_create 282 B
_txe_semaphore_delete 283
_txe_semaphore_get 283 Background Debug Monitor (BDM) 30
_txe_semaphore_info_get 283 binary semaphores 67, 70
_txe_semaphore_prioritize 283 black-box 22, 254
_txe_semaphore_put 283 block memory 257
_txe_thread_create 271 block memory component 288
_txe_thread_delete 271 block memory services 296
_txe_thread_info_get 271 block size 76
_txe_thread_preemption_change 271 Block TX_MUTEX 72
_txe_thread_priority_change 272 Block TX_QUEUE 65
_txe_thread_relinquish 272 Block TX_THREAD 55
_txe_thread_resume_api 272 buffer I/O management 232
_txe_thread_suspend_api 272 buffered driver responsibilities 233
_txe_thread_terminate 272 buffered I/O advantage 233
_txe_thread_time_slice_change 272 buffering messages 64
_txe_thread_wait_abort 272 build_ap.bat 32, 36
_txe_timer_activate_api 277 build_tx.bat 32, 33
_txe_timer_change 278 byte memory 257
_txe_timer_create 278 byte memory component 291
_txe_timer_deactivate_api 277 byte memory services 296
_txe_timer_delete 277
_txe_timer_info_get 277 C
C library 22, 254
A C main function 33
accelerated development 27 C pointers 76, 78
action functions 256 C source code 22
advanced driver issue 229 cache 75
after tx_application_define 239 circular buffer input 229
allocation algorithm 78 circular buffers 229, 231
allocation of processing 24 circular byte buffers 229
ANSI C 22 circular output buffer 231
application define 238 clock tick 34
application definition 256 coding conventions 258
application definition function 48 comments 262
application resources 66, 71 compiled 30
application specific modifications 23 compiler 44
application timer control block 82 completed 50, 51
application timers 30, 44, 81, 82 completed state 51
application’s entry point 47 component body functions 258
ASCII character codes in HEX 322 component constants 257
asynchronous events 83 component initialization 258
available 71 component methodology 254, 256
component specification file 257
configuration options 36
User Guide
first-in-first-out (FIFO) 52 in-line assembly 254
fixed-size 76 input bytes 224
fixed-size memory 75 input-output lists 234
fixed-sized messages 64 instruction 44
fragmentation 75, 78 instruction area 44
fragmented 79 interrupt control 84, 297
function calls 57 interrupt frequency 235
function prototypes 257 interrupt latency 86
interrupt management 235
G interrupt service routines 42, 43
interrupting 53
getting started 29 interrupts 42, 48, 83
global data 30, 258 invalid pointer 60
global variables 46 ISR template 85
globals 60 ISRs 42, 78, 227
H L
hardware devices 222 large local data 59
hardware interrupt 44 linked 30
heterogeneous 52 linker 44
hidden system thread 83 local storage 55
high performance 254 local variable 57
high throughput I/O 233 local variables 57
high-frequency interrupts 86 located 30
highly portable 27 locator 44
host considerations 30 logic for circular input buffer 230
host machines 30 logic for circular output buffer 231
logical AND/OR 73
I low-level initialization 47
User Guide
R simple driver shortcomings 228
simplicity 254
RAM 30, 45, 57, 65 slow memory 46
readme.txt 30, 31, 32, 35, 36, 37, 85 small example system 34
ready 50 software components 256
ready state 50 software maintenance 26
ready thread 42 source code indentation 262
real-time 23, 75, 80, 222 stack 43, 57
real-time software 23 stack area is too small 59
real-time systems 42, 54 stack memory area 59
re-created 52 stack pointer 56
recursive algorithms 59 stack size 63, 238
redundant polling 26 stack space 55
reentrancy 60 stacks 46, 48
reentrant 60 starvation 54, 61
reentrant function 60 starve 60
register 261 static memory 44
register variables 254 static memory usage 44
relative time 83 static variables 46
reset 47, 48 statics 60
responsive processing 54 suspended 50
re-started 52 suspended state 50
re-starting 52 suspension 85
ROM 30, 44, 45 system description 254
round-robin scheduling 52 system entry 256
run-time 54, 60, 78, 80 system include files 255
run-time behavior 27 system reset 47, 49
run-time environment 59 system stack 30, 44, 46
run-time image 22 system throughput 26
S T
scalability 254 target
scalable 22, 254, 257 interrupt source requirements 31
scheduling 48 ROM requirements 30
scheduling loop 57 target considerations 30
scheduling threads 42 target download interfaces 30
semaphore component 281 target’s address space 57, 64, 76, 79
semaphore control block 68, 72 tasks 24, 25
semaphore services 298 tasks vs. threads 24
semaphores 34, 48, 50, 70, 257 terminated 50
semi-independent program segment 48 terminated state 51
simple 228 Thread 0 240
simple driver initialization 224, 226 Thread 1 240
simple driver input 226 Thread 2 240
simple driver output 227, 228 Thread 5 241
User Guide
TX_BPCLE.C 289 TX_ILL 265
TX_BPD.C 289 tx_ill 31
TX_BPI.C 289 tx_ill assembly file 81
TX_BPIG.C 289 TX_INI.H 264
TX_BPP.C 290 TX_INITIALIZE_IN_PROGRESS 260
TX_BR.C 290 tx_interrupt_control 84, 85
TX_BYT.H 291 TX_IO_BUFFER 232
TX_BYTA.C 291 TX_IO_DRIVER (0x0A) 56
TX_BYTC.C 291 tx_kernel_enter 33, 35, 47, 49, 256, 264
TX_BYTCL.C 292 TX_MC.C 284
TX_BYTD.C 292 TX_MIG.C 285
tx_byte_allocate 104, 112 TX_MINIMUM_STACK 57
TX_BYTE_MEMORY (0x09) 56 TX_MPC.C 285
TX_BYTE_POOL 80 TX_MPRI.C 285
tx_byte_pool_create 108, 114 tx_mutex_create 132
tx_byte_pool_delete 110 tx_mutex_delete 134
tx_byte_pool_info_get 84 tx_mutex_get 70, 136
tx_byte_pool_prioritize 79, 84 tx_mutex_info_get 138
tx_byte_release 116 tx_mutex_prioritize 71, 140
TX_BYTI.C 292 tx_mutex_put 70, 142
TX_BYTIG.C 292 TX_MUTEX_SUSP (0x0D) 56
TX_BYTPP.C 292 tx_next_buffer 233
TX_BYTR.C 292 tx_next_packet 232
TX_BYTS.C 292 TX_OR_CONSUME 73
TX_COMPLETED (0x01) 56 tx_port.h 19, 32, 33, 255, 261
TX_DISABLE_ERROR_CHECKING 36, TX_QC.C 278
37, 255 TX_QCLE.C 279
TX_DISABLE_ERROR_CHECKNG 87 TX_QD.C 279
TX_EFC.C 287 TX_QF.C 279
TX_EFCLE.C 287 TX_QFS.C 279
TX_EFD.C 287 TX_QI.C 279
TX_EFG.C 287 TX_QIG.C 279
TX_EFI.C 287 TX_QP.C 279
TX_EFIG.C 287 TX_QR.C 279
TX_EFS.C’ 287 TX_QS.C 280
TX_EVE.H 286 TX_QUE.H 278
TX_EVENT_FLAG (0x07) 56 TX_QUEUE 260
TX_EVENT_FLAG_GROUP 74 tx_queue_create 144
tx_event_flags_create 118, 126 tx_queue_delete 146
tx_event_flags_delete 120 tx_queue_flush 148
tx_event_flags_get 73, 84, 122 tx_queue_front_send 85, 150
tx_event_flags_info_get 84 tx_queue_info_get 85, 152
tx_event_flags_set 73, 85, 128 tx_queue_prioritize 65, 85, 154
TX_IHL.C 264 tx_queue_receive 63, 85, 156
TX_IKE.C 264 tx_queue_send 62, 63, 85, 160
tx_ike.c 259 TX_QUEUE_SUSP (0x05) 56
User Guide
TX_TT.C 270 TXE_TIMI.C 277
TX_TTE.C 277 TXE_TMCH.C 278
TX_TTO.C 271 TXE_TMCR.C 278
TX_TTS.C 271 TXE_TPCH.C 271
TX_TTSC.C 271 TXE_TRA.C 272
TX_TWA.C 271 TXE_TREL.C 272
TXE_BA.C 290 TXE_TRPC.C 272
TXE_BPC.C 290 TXE_TSA.C 272
TXE_BPD.C 290 TXE_TT.C 272
TXE_BPIG.C 290 TXE_TTSC.C 272
TXE_BPP.C 290 TXE_TWA.C 272
TXE_BR.C 290 types of program execution 42
TXE_BTYA.C 293 typical thread stack 58
TXE_BYTC.C 293
TXE_BYTD.C 293 U
TXE_BYTG.C 293
TXE_BYTP.C 293 UART 229
TXE_BYTR.C 293 UINT 261
TXE_EFC.C 287 ULONG 261
TXE_EFD.C 288 un-deterministic 55, 75
TXE_EFG.C 288 un-deterministic behavior 80
TXE_EFIG.C 288 un-deterministic priority inversion 61, 73
TXE_EFS.C 288 uninitialized data 44, 46
TXE_MD.C 285 Unix 30, 32
TXE_MIG.C 285 unnecessary processing 26
TXE_MPRI.C 286 unpredictable behavior 48
TXE_QC.C 280 user-supplied main function 47
TXE_QD.C 280 using ThreadX 33
TXE_QF.C 280
TXE_QFS.C 280 V
TXE_QIG.C 280
TXE_QP.C 280 version ID 37
TXE_QR.C 280 VOID 261
TXE_QS.C 281
TXE_SC.C 282, 285 W
TXE_SD.C 283
TXE_SG.C 283, 285 watchdog services 44
TXE_SIG.C 283
TXE_SP.C 283, 286
TXE_SPRI.C 283
TXE_TAA.C 277
TXE_TC.C 271
TXE_TDA.C 277
TXE_TDEL.C 271
TXE_TIG.C 271
TXE_TIMD.C 277
User Guide