Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b0d68ee

Browse files
committed
Updated the spinlock semaphore to a cooperative semaphore
1 parent 8187d8f commit b0d68ee

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

Semaphore/README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
# Spinlock Semaphore
1717

18-
- Wait for a signal to be raised to gain access to a shared resource
18+
- Wait for a signal to be sent to gain access to a shared resource
1919
- During a **wait** this shared resource burns CPU cycles till it gets a signal from another thread.
2020

2121
![Spinlock Semaphore for UART](images/example2_spinlock_semaphore.PNG)
@@ -27,4 +27,7 @@
2727

2828
# Cooperative Semaphore
2929

30-
> TODO,
30+
- Wait for a signal to be sent to gain access to a shared resource
31+
- During a **wait** this shared resource **yields** to a different thread if it does not have access to the shared resource.
32+
33+
**NOTE** The output and working of the Cooperative Semaphore is similar to the Spinlock Semaphore. However the cooperative semaphore is more efficient since it does not burn unnecessary CPU cycles.

Semaphore/application/osSemaphore.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "osSemaphore.h"
22

3+
#include "osKernel.h"
4+
35
#include "stm32l4xx.h"
46

57
void osSemaphoreInit(uint32_t *semaphore, uint32_t value) {
@@ -8,6 +10,11 @@ void osSemaphoreInit(uint32_t *semaphore, uint32_t value) {
810

911
void osSemaphoreWait(uint32_t *semaphore) {
1012
while (*semaphore <= 0) {
13+
// This yield operation is required for a `cooperative semaphore`
14+
// If the 3 lines below are disabled we have a `spinlock semaphore`
15+
__disable_irq();
16+
osKernelYield();
17+
__enable_irq();
1118
}
1219

1320
__disable_irq();

0 commit comments

Comments
 (0)