Vector Microsar Os Spinlock module detailed design (incomplete version)

Directory

1. The concept of Spinlock

2. Properties of Spinlock

2.1 Common properties of AUTOSAR Spinlock and OPTIMIZED

2.2 Properties of AUTOSAR Spinlock

2.3 Properties of OPTIMIZED Spinlock

3. The data structure of Spinlock

3.1 Structure

3.2 Enumeration variables

3.3 A graph of data structure

4. Function design of Spinlock

4.1 Os_Api_GetSpinlock

4.2 Os_SpinlockInternalGet

4.3 Os_SpinlockInternalTryGet

4.4 Os_SpinlockGet

5. Service content and contact information

6. Reference link


1. The concept of Spinlock

Spinlock, spin lock, is a cross-core synchronization mechanism, in which TASK/two-type ISR waits in a loop (ie “spin”), repeatedly checking whether the shared variable becomes a certain value. This value indicates whether the lock is free.

In a multi-core system, some variables are shared by TASK/type 2 interrupts. Comparisons and changes to these variables typically require atomic operations. A spinlock is a busy-waiting mechanism, since the TASK/Type 2 interrupt remains active during the spin but does nothing useful.

Readers are asked to think, why can’t Spinlock be used in TASK/Type 2 ISR of a single-core system?

Second, the attribute of Spinlock

Section 7.9.28 of the AUTOSAR specification “AUTOSAR_SWS_OS.pdf” puts forward the design requirements of Spinlock. In Vector Microsar, in order to optimize the design of Spinlock and reduce the program execution time, Spinlock is subdivided into two categories: AUTOSAR Spinlock and OPTIMIZED Spinlock.

2.1 Common attributes of AUTOSAR Spinlock and OPTIMIZED

(1) [SWS_Os_00648] The OS provides Spinlock, which is a mutual exclusion mechanism for multi-core systems, to prevent conflicts when TASK/ISR2 in a multi-core system accesses specific shared data across cores.

(2) [SWS_Os_00649] The OS provides GetSpinlock() to occupy a specific Spinlock. If the Spinlock is already occupied by other Core’s Task/ISR2, then GetSpinlock() will keep trying to occupy it until it succeeds.

(3) [SWS_Os_00652] The OS provides TryToGetSpinlock() to occupy a specific Spinlock. If the Spinlock is already occupied by other Core’s Task/ISR2, then TryToGetSpinlock() will return immediately.

(4) [SWS_Os_00655] The OS provides ReleaseSpinlock() to release a specific Spinlock. If the Spinlock is not already occupied, ReleaseSpinlock() will generate an error.

2.2 Attributes of AUTOSAR Spinlock

(1) [SWS_Os_00658] If TASK tries to occupy a Spinlock, but other TASK/ISR2 already occupies this Spinlock, and this TASK belongs to the same Core as other TASK/ISR, then the OS generates an OS_STATUS_INTERFERENCE_DEADLOCK error.

(2) [SWS_Os_00659] If ISR2 tries to occupy a Spinlock, but other TASK/ISR2s already occupy this Spinlock, and this ISR2 and other TASK/ISRs belong to the same Core, then the OS generates an OS_STATUS_INTERFERENCE_DEADLOCK error.

(3) [SWS_Os_00660][SWS_Os_00661] Each Spinlock has its own Successor (that is, Order). On the same Core, different TASK/ISR2 can occupy different Spinlocks. In order to prevent TASK/ISR2 from occupying these Spinlocks in an improper order and causing “crossover deadlock”, the order in which these Spinlocks are occupied by TASK/ISR2 is specific: it must be occupied from the Spinlock with a smaller Order number, and gradually go to the Order number Larger Spinlock Orientation Occupancy. If the order of these Spinlocks occupied by Task/ISR2 appears “countercurrent”, then the OS generates an OS_STATUS_NESTING_DEADLOCK error.

Figure 1. Nesting causes deadlock (unique comments are blocked, welcome to communicate privately)

Figure 2. Reasonable configuration of Successor can prevent deadlock (unique comments are blocked, welcome to communicate privately)

(4) [SWS_Os_00650][SWS_Os_00651]GetSpinlock() can only be executed in TASK/ISR2.

(5) [SWS_Os_00653][SWS_Os_00654] TryToGetSpinlock() can only be executed in TASK/ISR2.

(6) [SWS_Os_00656][SWS_Os_00657]ReleaseSpinlock() can only be executed in TASK/ISR2.

(7) In the properties of Spinlock, it is necessary to clearly specify that it can access the Application. If the Spinlock is occupied by TASK/ISR2 in the Application that does not have access rights, then the OS will generate an OS_STATUS_ACCESSRIGHTS_1 error.

2.3 Attributes of OPTIMIZED Spinlock

(1) The Mode and Type attributes of the OPTIMIZED Spinlock type are determined by the Killing and LockMethod options.

(2) When the value of Killing is disable, and the value of LockMethod is NOTHING, the kernel mode that executes the “possession, attempt to possess and release Spinlock” program is in the normal user (USER) authority, and does not need to execute the Trace function.

(3) When the value of Killing is disable, and the value of LockMethod is NOT NOTHING, the kernel mode that executes the “possession, attempt to possess and release Spinlock” program is in the super administrator (SUPER.) authority, and there is no need to execute the Trace function (due to The Killing function cannot be executed, so the Trace data cannot be retained).

(4) When the value of Killing is enable, no matter what the value of LockMethod is, the kernel mode that executes the “possession, attempt to possess and release Spinlock” program is in the super administrator (SUPER.) authority, and executes the Trace function.

(5) The Check attribute of OPTIMIZED Spinlock is Disable, that is, no AUTOSAR Spinlock check is performed.

3. Data structure of Spinlock

3.1 Structure

struct Os_SpinlockType
{
   Os_LockType Lock;
   P2CONST(Os_ThreadConfigType, TYPEDEF, OS_CONST) OwnerThread;
   P2CONST(Os_SpinlockConfigType, TYPEDEF, OS_CONST) PreviousSpinlock;
   volatile Os_Hal_SpinlockType Spinlock;
   Os_TaskPrioType PreviousPriority;
}

Here, the role of Os_SpinlockType is implicitly expressed: the internal member variables of the structure obtain specific values and output specific values during the running of the function. For details on how to use it, welcome to communicate in private.

struct Os_SpinlockConfigType
{
   Os_LockConfigType Lock;
   Os_SpinlockMethodType Method;
   Os_TaskPrioType CeilingPriority;
   Os_SpinlockModeType Mode;
   Os_SpinlockTraceType Trace;
   Os_SpinlockCheckType Checks;
   Os_SpinlockOrderType Order;
  P2CONST(Os_TraceSpinlockConfigType, TYPEDEF, OS_CONST) TimingHookTrace;
}

Here, the role of Os_SpinlockConfigType is implicitly expressed: the variables of this structure define the initial value in Os_Spinlock_Lcfg.c. For details on how to use it, welcome to communicate in private.

3.2 Enumeration variables

enum Os_SpinlockMethodType
{
OS_SPINLOCKMETHOD_ALL_INT,
OS_SPINLOCKMETHOD_CAT2,
OS_SPINLOCKMETHOD_SCHEDULER,
OS_SPINLOCKMETHOD_NOTHING
}
  • The value of SpinlockMethodType is determined by the Lock Type option of Os/Spinlock. Different enumeration values indicate different impacts on the operating system OS during the execution of GetSpinlock()/TryToGetSpinlock()/ReleaseSpinlock(). Among them, OS_SPINLOCKMETHOD_ALL_INT means to turn off all interrupts of the OS, and OS_SPINLOCKMETHOD_CAT2 means to turn off the second type of interrupts of the OS.
enum Os_SpinlockModeType
{
OS_SPINLOCKMODE_USER,
OS_SPINLOCKMODE_SUPERVISOR
}
  • The value of Os_SpinlockModeType is determined by Lock Method and forcible Termination(Killing). Different enumeration values indicate different states of the chip core. Among them, OS_SPINLOCKMODE_USER indicates that the chip core is in user mode, and OS_SPINLOCKMODE_SUPERVISOR indicates that the chip core is in SUPERVISOR state. Different enumeration values will execute different functions during the execution of GetSpinlock()/TryToGetSpinlock()/ReleaseSpinlock().
enum Os_SpinlockTraceType
{
OS_SPINLOCKTRACE_DISABLED,
OS_SPINLOCKTRACE_ENABLED
}
  • The value of Os_SpinlockTraceType is determined by Lock Method and forcible Termination (Killing). When the Trace function is enabled, during the execution of GetSpinlock()/TryToGetSpinlock()/ReleaseSpinlock(), the Locks linked list and the spinlocks linked list record and delete the current spinlock.
enum Os_SpinlockCheckType
{
    OS_SPINLOCKCHECK_DISABLED,
    OS_SPINLOCKCHECK_ENABLED
}
  • The value of Os_SpinlockCheckType is selected by the type of Spinlock. When Spinlock is AUTOSAR type, CheckType is Enable, when Spinlock is OPTIMIZED type, CheckType is Disable.

3.3 A picture of data structure

When the structure and enumeration variables are marked on a picture, you can clearly see the whole picture of Spinlock, so that you can easily understand many complex phenomena, such as the internal logical relationship of different types of functions in Spinlock.

Figure 3. A picture of Spinlock’s data structure (unique comments are blocked, private communication is welcome)

Fourth, Spinlock function design

To put it bluntly, Spinlock’s function types are mainly divided into four types. The first type is a public Static Inline function, which is responsible for attribute operations; the second type is a public Static Inline function, which is responsible for member functions; the third type is a public function. Responsible for member functions; the fourth is the public interface, which mainly provides external interfaces.

Here, several novel and unique function flowcharts are shown for readers’ reference and enlightenment.

As for the detailed function design methodology, private communication is welcome.

4.1 Os_Api_GetSpinlock

4.2 Os_SpinlockInternalGet

4.3 Os_SpinlockInternalTryGet

4.4 Os_SpinlockGet

V. Service content

Welcome leaders and colleagues from auto parts companies and MCU companies to inquire.

Vector Microsar analysis, domestic Mcu adaptation and self-developed CP – 哔哩哔哩 (bilibili.com)

(9 messages) Vector Microsar analysis, domestic Mcu adaptation and self-developed CP AUTOSAR_chenchaocai’s blog-CSDN blog

Vector Microsar analysis, domestic Mcu adaptation and self-developed CP AUTOSAR – Zhihu (zhihu.com)