MISRA C:2012 Rule 22.20
Thread-specific storage pointers shall be created before being accessed
Since R2025b
Description
This checker is deactivated in a default Polyspace® as You Code analysis. See Checkers Deactivated in Polyspace as You Code Analysis (Polyspace Access).
Rule Definition
Thread-specific storage pointers shall be created before being accessed.1
Rationale
The C standard does not define order of thread execution. If your code defines thread-specific storage pointers within threads, then the states of the pointers are dependent on thread execution order. Determining the validity of the pointer state in such a case is difficult.
Create thread-specific storage by calling tss_create()
before they
are accessed in threads. This pattern allows checking the validity of the thread-specific
storage pointers in a deterministic way.
Polyspace Implementation
Polyspace reports a violation of this rule if either of these conditions is true:
A
tss_t
object is used in the code but it is not explicitly created usingtss_create()
.Multiple functions call the function
tss_create()
. Polyspace expects eithermain()
or another unique function to calltss_create()
.The
main()
function callstss_create()
after the threads are created.The unique thread-specific storage creation function is called by
main()
after the threads are created.
Extend Checker
By default, Polyspace looks for the unique functions that create the thread-specific storage and the threads. In a Datalog file, you can specify either or both:
The unique thread-specific storage creation function.
The unique thread-creation function.
For example, this Datalog code designates tss_create_caller()
as the
function that calls tss_create()
and thrd_maker()
as
the function that creates the threads. Then, it instructs Polyspace to report a violation when the function thrd_maker()
is
called before the function tss_create()
. The function names are specified
without the trailing
parentheses.
Concurrency.Basic.is_unique_function_using_tss_create("tss_create_caller"). Concurrency.Basic.is_unique_function_starting_threads("thrd_maker"). Concurrency.Basic.raise_if_thread_start_before_tss_create().
.dl
) file and use it as an input to the option -code-behavior-specifications
. For more details about modifying checker
behavior using Datalog, see Modify Bug Finder Checkers Through Code Behavior Specifications.Troubleshooting
If you expect a rule violation but do not see it, refer to Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
Group: Resources |
Category: Mandatory |
AGC Category: Mandatory |
Version History
Introduced in R2025b
1 All MISRA coding rules and directives are © Copyright The MISRA Consortium Limited 2021.
The MISRA coding standards referenced in the Polyspace Bug Finder™ documentation are from the following MISRA standards:
MISRA C:2004
MISRA C:2012
MISRA C:2023
MISRA C++:2008
MISRA C++:2023
MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.