Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of LcdWindow by
semaphore.cpp@3:e5d5e2fe4bf6, 2010-11-28 (annotated)
- Committer:
- hlipka
- Date:
- Sun Nov 28 22:09:54 2010 +0000
- Revision:
- 3:e5d5e2fe4bf6
- Child:
- 9:2fe93daa2106
Made LCD driver thread safe (to allow usage from timers)
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| hlipka | 3:e5d5e2fe4bf6 | 1 | /** |
| hlipka | 3:e5d5e2fe4bf6 | 2 | * code from Igor Skochinsky |
| hlipka | 3:e5d5e2fe4bf6 | 3 | * taken from http://mbed.org/forum/mbed/post/799/ |
| hlipka | 3:e5d5e2fe4bf6 | 4 | */ |
| hlipka | 3:e5d5e2fe4bf6 | 5 | |
| hlipka | 3:e5d5e2fe4bf6 | 6 | #include "semaphore.h" |
| hlipka | 3:e5d5e2fe4bf6 | 7 | |
| hlipka | 3:e5d5e2fe4bf6 | 8 | Semaphore::Semaphore(): s(SemFree) {}; |
| hlipka | 3:e5d5e2fe4bf6 | 9 | |
| hlipka | 3:e5d5e2fe4bf6 | 10 | bool Semaphore::take(bool block) |
| hlipka | 3:e5d5e2fe4bf6 | 11 | { |
| hlipka | 3:e5d5e2fe4bf6 | 12 | int oldval; |
| hlipka | 3:e5d5e2fe4bf6 | 13 | #if defined(TARGET_LPC1768) // on Cortex-M3 we can use ldrex/strex |
| hlipka | 3:e5d5e2fe4bf6 | 14 | do { |
| hlipka | 3:e5d5e2fe4bf6 | 15 | // read the semaphore value |
| hlipka | 3:e5d5e2fe4bf6 | 16 | oldval = __ldrex(&s); |
| hlipka | 3:e5d5e2fe4bf6 | 17 | // loop again if it is locked and we are blocking |
| hlipka | 3:e5d5e2fe4bf6 | 18 | // or setting it with strex failed |
| hlipka | 3:e5d5e2fe4bf6 | 19 | } |
| hlipka | 3:e5d5e2fe4bf6 | 20 | while ( (block && oldval == SemTaken) || __strex(SemTaken, &s) != 0 ); |
| hlipka | 3:e5d5e2fe4bf6 | 21 | if ( !block ) __clrex(); // clear exclusive lock set by ldrex |
| hlipka | 3:e5d5e2fe4bf6 | 22 | #else // on arm7 there's only swp |
| hlipka | 3:e5d5e2fe4bf6 | 23 | do { |
| hlipka | 3:e5d5e2fe4bf6 | 24 | // swp sets the pointed data to the given value and returns the previous one |
| hlipka | 3:e5d5e2fe4bf6 | 25 | oldval = __swp(SemTaken, &s); |
| hlipka | 3:e5d5e2fe4bf6 | 26 | // if blocking, loop until the previous value becomes 0 |
| hlipka | 3:e5d5e2fe4bf6 | 27 | // which would mean we have successfully taken the lock |
| hlipka | 3:e5d5e2fe4bf6 | 28 | } |
| hlipka | 3:e5d5e2fe4bf6 | 29 | while (block && oldval == SemTaken); |
| hlipka | 3:e5d5e2fe4bf6 | 30 | #endif |
| hlipka | 3:e5d5e2fe4bf6 | 31 | return oldval == SemFree; |
| hlipka | 3:e5d5e2fe4bf6 | 32 | } |
| hlipka | 3:e5d5e2fe4bf6 | 33 | |
| hlipka | 3:e5d5e2fe4bf6 | 34 | // release the semaphore |
| hlipka | 3:e5d5e2fe4bf6 | 35 | void Semaphore::release() |
| hlipka | 3:e5d5e2fe4bf6 | 36 | { |
| hlipka | 3:e5d5e2fe4bf6 | 37 | s = SemFree; |
| hlipka | 3:e5d5e2fe4bf6 | 38 | } |
| hlipka | 3:e5d5e2fe4bf6 | 39 |
