An020 Mcs Mutex Implementation
An020 Mcs Mutex Implementation
Robert Bosch GmbH reserves all rights even in the event of industrial Property rights. We reserve all rights of disposal such as copying and
passing on to third parties.
v0.1 - 18.04.2018 - AE/EID5
GTM-IP Application note: AN020 – MCS Mutex implementation 2 | 13
Table of contents
1 Introduction ............................................................................................................................................................... 3
1.1 Problem description .......................................................................................................................................3
1.2 Outline ...........................................................................................................................................................3
3 References ................................................................................................................................................................ 10
Robert Bosch GmbH reserves all rights even in the event of industrial Property rights. We reserve all rights of disposal such as copying and
passing on to third parties.
v0.1 - 18.04.2018 - AE/EID5
GTM-IP Application note: AN020 – MCS Mutex implementation 3 | 13
1 Introduction
This application note describes methods to implement a mutual exclusion mechanism for concurrent running MCS
channels which share a common resource. The exclusive usage of this shared resource is established by the
implementation of a mutex, which has to be requested by an MCS channel, when he enters a code part (critical region),
where he wants to access the shared resource.
Actually, there is no dedicated HW-support to implement a semaphore mechanism for concurrent tasks within the MCS
itself. Therefore, an alternative approach is needed to establish access to shared resources.
1.2 Outline
This application note describes an alternative approach for accessing a shared resource from different MCS channels.
The solution uses a SW-mutex implemented with one dedicated MCS channel. This solution is described in section 2.
Robert Bosch GmbH reserves all rights even in the event of industrial Property rights. We reserve all rights of disposal such as copying and
passing on to third parties.
v0.1 - 18.04.2018 - AE/EID5
GTM-IP Application note: AN020 – MCS Mutex implementation 4 | 13
30
29
29
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
10
11
0
Reserved
TRG23
TRG22
TRG21
TRG20
TRG19
TRG18
TRG17
TRG16
TRG15
TRG14
TRG13
TRG12
TRG10
TRG11
TRG9
TRG8
TRG7
TRG6
TRG5
TRG4
TRG3
TRG2
TRG1
TRG0
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
R
0x00
Robert Bosch GmbH reserves all rights even in the event of industrial Property rights. We reserve all rights of disposal such as copying and
passing on to third parties.
v0.1 - 18.04.2018 - AE/EID5
GTM-IP Application note: AN020 – MCS Mutex implementation 5 | 13
The bits TRG4, TRG5, and TRG6 of register MCS[i]_STRG are used in this example by the MCS_Workers to request
access to a shared resource. Setting the corresponding bit to ‘1’ signals a request for accessing the resource. The
MCS_Gatekeeper reads the MCS[i]_STRG register to determine channels, requesting access.
Ownership of the shared resource has to be requested by setting this bit and the shared resource must not be used by
the MCS_Worker before the MCS_Gatekeeper granted the ownership.
30
29
29
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
10
11
0
Reserved
TRG23
TRG22
TRG21
TRG20
TRG19
TRG18
TRG17
TRG16
TRG15
TRG14
TRG13
TRG12
TRG10
TRG11
TRG9
TRG8
TRG7
TRG6
TRG5
TRG4
TRG3
TRG2
TRG1
TRG0
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
R
0x00
0
Table 2.3: Example of MCS[i]_CTRG resource consumption for three MCS_Workers
The bits TRG4, TRG5, and TRG6 of register MCS[i]_CTRG are used in this example by the MCS_Workers to return the
ownership of the shared resource to the MCS_Gatekeeper. By writing a ‘1’ to its corresponding trigger bit, the
MCS_Worker can free the shared resource. The trigger bit is also cleared in the MCS[i]_STRG register, which informs
the MCS_Gatekeeper, that he can give the shared resource to another MCS channel, requesting the resource.
The MCS[i]_CTRG register bit is to be set by a MCS_Worker, after leaving the critical region, where the shared resource
was accessed. Otherwise, the shared resource will never be assigned to another MCS_Worker by the MCS_Gatekeeper.
30
29
29
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
10
11
0
Bit 31
Bit 30
Bit 29
Bit 28
Bit 27
Bit 26
Bit 25
Bit 24
Bit 23
Bit 22
Bit 21
Bit 20
Bit 18
Bit 18
Bit 17
Bit 16
Bit 15
Bit 14
Bit 13
Bit 12
Bit 10
Bit 11
Bit 9
Bit 8
Bit 7
Bit 6
Bit 5
Bit 4
Bit 3
Bit 2
Bit 1
Bit 0
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
RW
0
Table 2.4: MCS Memory cell for usage with three MCS_Workers
Robert Bosch GmbH reserves all rights even in the event of industrial Property rights. We reserve all rights of disposal such as copying and
passing on to third parties.
v0.1 - 18.04.2018 - AE/EID5
GTM-IP Application note: AN020 – MCS Mutex implementation 6 | 13
Each MCS_Worker competing for a shared resource owns one bit of the MCS memory cell. The MCS_Gatekeeper uses
this memory location to inform the MCS_Workers that one of them is allowed to enter the critical region and access the
shared resource. This is signalled by the MCS_Gatekeeper by writing a ‘1’ to the corresponding memory cell bit. The
MCS_Workers have to poll this bit after setting the MCS[i]_STRG register and before they enter the critical region.
2.3 Implementation
2.3.1 MCS_Gatekeeper
The MCS_Gatekeeper is responsible for restricting the access to a shared resource for one and only one MCS_Worker
at one point in time. He does this with the help of the MCS[i]_STRG register and a dedicated memory cell in MCS RAM.
The MCS_Gatekeeper scans periodically the MCS[i]_STRG register for bits set. The MCS_Gatekeeper grants access to
the shared resource by setting the corresponding bit in the MCS RAM memory cell. The complete algorithm is shown in
Figure 2.1. The code is shown in Code 1.
2.3.1.1 Algorithm for MCS_Gatekeeper implementation
Robert Bosch GmbH reserves all rights even in the event of industrial Property rights. We reserve all rights of disposal such as copying and
passing on to third parties.
v0.1 - 18.04.2018 - AE/EID5
GTM-IP Application note: AN020 – MCS Mutex implementation 7 | 13
mcs_gatekeeper:
movl R2, 0x0
mtx_sched_start:
movl R1, (2**mtx_ofs_c) # mtx_ofs_c= 1. Bitposition
mtx_sched_test:
bt R1, STRG # test for mutex request
jbs STA, Z, mtx_sched_shift # current bit == requested bit
mov R6, R1 # prepare mask for wurmx
mwr R1, mtx_sta_v # assign mutex
wurmx R2, STRG # wait until mutex release
mwr R2, mtx_sta_v # remove mutex assign bit
mtx_sched_shift:
shl R1, 1 # try next request bit
btl R1, mtx_mask_c # check for overflow
jbc STA, Z, mtx_sched_test
jmp mtx_sched_start
Code 1: MCS_Gatekeeper sample source code
For the algorithm, the two variables mtx_ofs_c and mtx_len_c have to be initialized. mtx_ofs_c defines the
offset of the bit field used to implement the mutex mechanism (in the example shown in Table 2.2, Table 2.3, Table 2.4,
the first MCS_Worker resides at bit position four). mtx_len_c defines the number of workers, taking part in the
competition for the shared resource (in the example, there are three workers). By this, the bit field is defined, where the
MCS_Gatekeeper and MCS_Workers communicate with each other. It is important for the MCS_Gatekeeper algorithm,
that the bit positions in the registers and memory correspond to each other for each MCS_Worker.
Robert Bosch GmbH reserves all rights even in the event of industrial Property rights. We reserve all rights of disposal such as copying and
passing on to third parties.
v0.1 - 18.04.2018 - AE/EID5
GTM-IP Application note: AN020 – MCS Mutex implementation 8 | 13
In a first step, the MCS_Worker sets its corresponding TRGx bit in the MCS[i]_STRG register, to inform the
MCS_Gatekeeper, that he wants to enter the critical region. After the request, it is important for the requesting
MCS_Worker to wait some time, before checking if the access is granted by the MCS_Gatekeeper. The amount of time
depends on the algorithm used by the MCS_Gatekeeper to scan through and process pending requests. In this example,
there are three NOPs which implement the wait time. For a detailed discussion of this timing constraint, please refer to
section 2.3.3.
Robert Bosch GmbH reserves all rights even in the event of industrial Property rights. We reserve all rights of disposal such as copying and
passing on to third parties.
v0.1 - 18.04.2018 - AE/EID5
GTM-IP Application note: AN020 – MCS Mutex implementation 9 | 13
After this wait time, the MCS_Worker checks repeatedly if the access is granted to him by checking the corresponding
bit in the memory location. If the bit was set by the MCS_Gatekeeper, the MCS_Worker is allowed to enter the critical
region and access the shared resource.
When the MCS_Worker no longer requires the shared resource, he has to clear his dedicated corresponding trigger bit
in MCS[i]_STRG, by writing a ‘1’ to the associated bit in MCS[i]_CTRG. This frees the resource and allows the
MCS_Gatekeeper to continue scanning the MCS[i]_STRG register for new/pending requests.
Figure 2.3 shows the critical section, where the bit in the MCS memory can be set, while the MCS_Gatekeeper is
modifying the MCS memory content. The instructions behind these two activities of the algorithm are the two wurmx
R2, STRG and mwr R2, mtx_sta_v instructions.
The time for wurmx is one instruction cycle, while mwr takes two instruction cycles. Thus, a total of three instruction
cycles are necessary to update the MCS memory and clear the bit of the old requesting MCS_Worker.
Those three instruction cycles are inserted by three NOPs in the MCS_Worker code.
Robert Bosch GmbH reserves all rights even in the event of industrial Property rights. We reserve all rights of disposal such as copying and
passing on to third parties.
v0.1 - 18.04.2018 - AE/EID5
GTM-IP Application note: AN020 – MCS Mutex implementation 10 | 13
3 References
Robert Bosch GmbH reserves all rights even in the event of industrial Property rights. We reserve all rights of disposal such as copying and
passing on to third parties.
v0.1 - 18.04.2018 - AE/EID5
GTM-IP Application note: AN020 – MCS Mutex implementation 11 | 13
The content of this document is subject to continuous developments and improvements. All
particulars and its use contained in this document are given by BOSCH in good faith.
NO WARRANTIES: TO THE MAXIMUM EXTENT PERMITTED BY LAW, NEITHER THE INTELLECTUAL PROPERTY
OWNERS, COPYRIGHT HOLDERS AND CONTRIBUTORS, NOR ANY PERSON, EITHER EXPRESSLY OR
IMPLICITLY, WARRANTS ANY ASPECT OF THIS SPECIFICATION, SOFTWARE RELATED THERETO, CODE
AND/OR PROGRAM RELATED THERETO, INCLUDING ANY OUTPUT OR RESULTS OF THIS SPECIFICATION,
SOFTWARE RELATED THERETO, CODE AND/OR PROGRAM RELATED THERETO UNLESS AGREED TO IN
WRITING. THIS SPECIFICATION, SOFTWARE RELATED THERETO, CODE AND/OR PROGRAM RELATED
THERETO IS BEING PROVIDED "AS IS", WITHOUT ANY WARRANTY OF ANY TYPE OR NATURE, EITHER
EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE, AND ANY WARRANTY THAT THIS SPECIFICATION, SOFTWARE
RELATED THERETO, CODE AND/OR PROGRAM RELATED THERETO IS FREE FROM DEFECTS.
ASSUMPTION OF RISK: THE RISK OF ANY AND ALL LOSS, DAMAGE, OR UNSATISFACTORY PERFORMANCE OF
THIS SPECIFICATION (RESPECTIVELY THE PRODUCTS MAKING USE OF IT IN PART OR AS A WHOLE),
SOFTWARE RELATED THERETO, CODE AND/OR PROGRAM RELATED THERETO RESTS WITH YOU AS THE
USER. TO THE MAXIMUM EXTENT PERMITTED BY LAW, NEITHER THE INTELLECTUAL PROPERTY OWNERS,
COPYRIGHT HOLDERS AND CONTRIBUTORS, NOR ANY PERSON EITHER EXPRESSLY OR IMPLICITLY, MAKES
ANY REPRESENTATION OR WARRANTY REGARDING THE APPROPRIATENESS OF THE USE, OUTPUT, OR
RESULTS OF THE USE OF THIS SPECIFICATION, SOFTWARE RELATED THERETO, CODE AND/OR PROGRAM
RELATED THERETO IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY, BEING CURRENT OR
OTHERWISE. NOR DO THEY HAVE ANY OBLIGATION TO CORRECT ERRORS, MAKE CHANGES, SUPPORT THIS
SPECIFICATION, SOFTWARE RELATED THERETO, CODE AND/OR PROGRAM RELATED THERETO, DISTRIBUTE
UPDATES, OR PROVIDE NOTIFICATION OF ANY ERROR OR DEFECT, KNOWN OR UNKNOWN. IF YOU RELY
UPON THIS SPECIFICATION, SOFTWARE RELATED THERETO, CODE AND/OR PROGRAM RELATED THERETO,
YOU DO SO AT YOUR OWN RISK, AND YOU ASSUME THE RESPONSIBILITY FOR THE RESULTS. SHOULD THIS
SPECIFICATION, SOFTWARE RELATED THERETO, CODE AND/OR PROGRAM RELATED THERETO PROVE
DEFECTIVE, YOU ASSUME THE COST OF ALL LOSSES, INCLUDING, BUT NOT LIMITED TO, ANY NECESSARY
SERVICING, REPAIR OR CORRECTION OF ANY PROPERTY INVOLVED TO THE MAXIMUM EXTEND PERMITTED
BY LAW.
DISCLAIMER: IN NO EVENT, UNLESS REQUIRED BY LAW OR AGREED TO IN WRITING, SHALL THE
INTELLECTUAL PROPERTY OWNERS, COPYRIGHT HOLDERS OR ANY PERSON BE LIABLE FOR ANY LOSS,
EXPENSE OR DAMAGE, OF ANY TYPE OR NATURE ARISING OUT OF THE USE OF, OR INABILITY TO USE THIS
SPECIFICATION, SOFTWARE RELATED THERETO, CODE AND/OR PROGRAM RELATED THERETO, INCLUDING,
BUT NOT LIMITED TO, CLAIMS, SUITS OR CAUSES OF ACTION INVOLVING ALLEGED INFRINGEMENT OF
COPYRIGHTS, PATENTS, TRADEMARKS, TRADE SECRETS, OR UNFAIR COMPETITION.
INDEMNIFICATION: TO THE MAXIMUM EXTEND PERMITTED BY LAW YOU AGREE TO INDEMNIFY AND HOLD
HARMLESS THE INTELLECTUAL PROPERTY OWNERS, COPYRIGHT HOLDERS AND CONTRIBUTORS, AND
EMPLOYEES, AND ANY PERSON FROM AND AGAINST ALL CLAIMS, LIABILITIES, LOSSES, CAUSES OF ACTION,
DAMAGES, JUDGMENTS, AND EXPENSES, INCLUDING THE REASONABLE COST OF ATTORNEYS’ FEES AND
COURT COSTS, FOR INJURIES OR DAMAGES TO THE PERSON OR PROPERTY OF THIRD PARTIES, INCLUDING,
WITHOUT LIMITATIONS, CONSEQUENTIAL, DIRECT AND INDIRECT DAMAGES AND ANY ECONOMIC LOSSES,
THAT ARISE OUT OF OR IN CONNECTION WITH YOUR USE, MODIFICATION, OR DISTRIBUTION OF THIS
SPECIFICATION, SOFTWARE RELATED THERETO, CODE AND/OR PROGRAM RELATED THERETO, ITS OUTPUT,
OR ANY ACCOMPANYING DOCUMENTATION.
GOVERNING LAW: THE RELATIONSHIP BETWEEN YOU AND ROBERT BOSCH GMBH SHALL BE GOVERNED
SOLELY BY THE LAWS OF THE FEDERAL REPUBLIC OF GERMANY. THE STIPULATIONS OF INTERNATIONAL
CONVENTIONS REGARDING THE INTERNATIONAL SALE OF GOODS SHALL NOT BE APPLICABLE. THE
EXCLUSIVE LEGAL VENUE SHALL BE DUESSELDORF, GERMANY.
MANDATORY LAW SHALL BE UNAFFECTED BY THE FOREGOING PARAGRAPHS.
INTELLECTUAL PROPERTY OWNERS/COPYRIGHT OWNERS/CONTRIBUTORS: ROBERT BOSCH GMBH,
ROBERT BOSCH PLATZ 1, 70839 GERLINGEN, GERMANY AND ITS LICENSORS.
Robert Bosch GmbH reserves all rights even in the event of industrial Property rights. We reserve all rights of disposal such as copying and
passing on to third parties.
v0.1 - 18.04.2018 - AE/EID5
GTM-IP Application note: AN020 – MCS Mutex implementation 12 | 13
Robert Bosch GmbH reserves all rights even in the event of industrial Property rights. We reserve all rights of disposal such as copying and
passing on to third parties.
v0.1 - 18.04.2018 - AE/EID5
GTM-IP Application note: AN020 – MCS Mutex implementation 13 | 13
Robert Bosch GmbH reserves all rights even in the event of industrial Property rights. We reserve all rights of disposal such as copying and
passing on to third parties.
v0.1 - 18.04.2018 - AE/EID5