This is for http://mbed.org/forum/bugs-suggestions/topic/1074/

Dependencies:   mbed

Committer:
shintamainjp
Date:
Wed Sep 08 12:53:38 2010 +0000
Revision:
0:49c0ef6111e6
Child:
1:70466efca68e

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shintamainjp 0:49c0ef6111e6 1 #ifndef _SEMAPHORE_H_
shintamainjp 0:49c0ef6111e6 2 #define _SEMAPHORE_H_
shintamainjp 0:49c0ef6111e6 3
shintamainjp 0:49c0ef6111e6 4 /*
shintamainjp 0:49c0ef6111e6 5 * http://mbed.org/forum/mbed/topic/181/#comment-799
shintamainjp 0:49c0ef6111e6 6 */
shintamainjp 0:49c0ef6111e6 7
shintamainjp 0:49c0ef6111e6 8 class Semaphore {
shintamainjp 0:49c0ef6111e6 9 public:
shintamainjp 0:49c0ef6111e6 10 Semaphore(): s(SemFree) {}
shintamainjp 0:49c0ef6111e6 11
shintamainjp 0:49c0ef6111e6 12 bool take(bool block = true) {
shintamainjp 0:49c0ef6111e6 13 int oldval;
shintamainjp 0:49c0ef6111e6 14 do {
shintamainjp 0:49c0ef6111e6 15 oldval = __ldrex(&s);
shintamainjp 0:49c0ef6111e6 16 } while ((block && oldval == SemTaken) || __strex(SemTaken, &s) != 0);
shintamainjp 0:49c0ef6111e6 17 if (!block) {
shintamainjp 0:49c0ef6111e6 18 __clrex();
shintamainjp 0:49c0ef6111e6 19 }
shintamainjp 0:49c0ef6111e6 20 return (oldval == SemFree);
shintamainjp 0:49c0ef6111e6 21 }
shintamainjp 0:49c0ef6111e6 22
shintamainjp 0:49c0ef6111e6 23 void release() {
shintamainjp 0:49c0ef6111e6 24 s = SemFree;
shintamainjp 0:49c0ef6111e6 25 }
shintamainjp 0:49c0ef6111e6 26
shintamainjp 0:49c0ef6111e6 27 private:
shintamainjp 0:49c0ef6111e6 28 enum { SemFree, SemTaken };
shintamainjp 0:49c0ef6111e6 29 int s;
shintamainjp 0:49c0ef6111e6 30 };
shintamainjp 0:49c0ef6111e6 31
shintamainjp 0:49c0ef6111e6 32 #endif