Based on the sample code that you listed earlier I would say no. The typedef for spinlock doesn't provide the compiler with sufficient information to know that its state may be changed by threads other than the current one. Therefore variables of type spinlock will be subject to compiler optimisations. For regular variables this is precisely what you want because it will have a positive effect on codesize and speed. For semaphores the story is a different one. Those must be explicitly written to and read from such as you code it. The way to do this is by adding the volatile keyword to your type definition. So rather than using 'typedef int spinlock' you will have to use 'typedef volatile int spinlock'.

Another advise I want to give you is to drop STM1_Handler() from your lock() function definition. To me it makes no sense to repeatedly call it while waiting to acquire the lock. The lock() function must only be used to acquire the semaphore. Once it is in your possession you become the sole owner of the resource that you're sharing. And only at that point you call the resource. Combining both remarks, this is the preferred approach:

Bookmarks

Bookmarks

All content and materials on this site are provided “as is“. Infineon makes no warranties or
representations with regard to this content and these materials of any kind, whether express or
implied, including without limitation, warranties or representations of merchantability, fitness for
a particular purpose, title and non-infringement of any third party intellectual property right. No
license, whether express or implied, is granted by Infineon. Use of the information on this site may
require a license from a third party, or a license from Infineon.

Infineon accepts no liability for the content and materials on this site being accurate, complete or up-
to-date or for the contents of external links. Infineon distances itself expressly from the contents of
the linked pages, over the structure of which Infineon has no control.

Content on this site may contain or be subject to specific guidelines or limitations on use. All postings
and use of the content on this site are subject to the Usage Terms of the site; third parties using
this content agree to abide by any limitations or guidelines and to comply with the Usage Terms of
this site. Infineon reserves the right to make corrections, deletions, modifications, enhancements,
improvements and other changes to the content and materials, its products, programs and services
at any time or to move or discontinue any content, products, programs, or services without notice.