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

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
shintamainjp
Date:
Thu Sep 09 10:21:06 2010 +0000
Parent:
0:49c0ef6111e6
Commit message:

Changed in this revision

Semaphore.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/Semaphore.h	Wed Sep 08 12:53:38 2010 +0000
+++ b/Semaphore.h	Thu Sep 09 10:21:06 2010 +0000
@@ -8,25 +8,21 @@
 class Semaphore {
 public:
     Semaphore(): s(SemFree) {}
-
-    bool take(bool block = true) {
-        int oldval;
-        do {
-            oldval = __ldrex(&s);
-        } while ((block && oldval == SemTaken) || __strex(SemTaken, &s) != 0);
-        if (!block) {
-            __clrex();
+    bool try_enter() {
+        int oldval = __ldrex(&s);
+        if (oldval == SemTaken) {
+            return false;
         }
-        return (oldval == SemFree);
+        __strex(SemTaken, &s);
+        return true;
     }
-
     void release() {
+        __strex(SemFree, &s);
         s = SemFree;
     }
-
 private:
     enum { SemFree, SemTaken };
-    int s;
+    volatile int s;
 };
 
 #endif
\ No newline at end of file
--- a/main.cpp	Wed Sep 08 12:53:38 2010 +0000
+++ b/main.cpp	Thu Sep 09 10:21:06 2010 +0000
@@ -1,52 +1,56 @@
-/**
- * Test program for a bug. (http://mbed.org/forum/bugs-suggestions/topic/1074/)
- *
- * Copyright (C) 2010 Shinichiro Nakamura (CuBeatSystems)
- * http://shinta.main.jp/
- */
-
-#include "mbed.h"
-#include "Semaphore.h"
-
-Serial ser(USBTX, USBRX);
-Ticker ticker;
-BusOut led(LED4, LED3, LED2, LED1);
-Semaphore sem;
-
-char shared_resource;   // This is a shared resource for example.
-
-#define LOCK() sem.take()
-#define UNLOCK() sem.release()
-
-/**
- * a ticker.
- */
-void func_tick(void) {
-    led = led + 1;
-}
-
-/**
- * A call back function for serial interrupt.
- */
-void func_serial_interrupt(void) {
-    LOCK();
-    shared_resource = ser.getc();
-    UNLOCK();
-}
-
-/**
- * Entry point.
- */
-int main() {
-    ticker.attach_us(&func_tick, 100 * 1000);
-    ser.attach(&func_serial_interrupt);
-    while(1) {
-        /*
-         * Hung up your mbed system if you hit keys on console for Serial.
-         */
-        LOCK();
-        printf("0x%x\n", shared_resource);
-        UNLOCK();
-        wait_ms(100);
-    }
-}
+/**
+ * Test program for a bug. (http://mbed.org/forum/bugs-suggestions/topic/1074/)
+ *
+ * Copyright (C) 2010 Shinichiro Nakamura (CuBeatSystems)
+ * http://shinta.main.jp/
+ */
+
+#include "mbed.h"
+#include "Semaphore.h"
+
+Serial ser(USBTX, USBRX);
+Ticker ticker;
+BusOut led(LED4, LED3, LED2, LED1);
+Semaphore sem;
+
+volatile char shared_resource;   // This is a shared resource for example.
+
+#define LOCK() sem.try_enter()
+#define UNLOCK() sem.release()
+
+/**
+ * a ticker.
+ */
+void func_tick(void) {
+    led = led + 1;
+}
+
+/**
+ * A call back function for serial interrupt.
+ */
+void func_serial_interrupt(void) {
+    if (LOCK()) {
+        shared_resource = ser.getc();
+        UNLOCK();
+    }
+}
+
+/**
+ * Entry point.
+ */
+int main() {
+    ticker.attach_us(&func_tick, 100 * 1000);
+    ser.attach(&func_serial_interrupt);
+    while (1) {
+        /*
+         * Hung up serial communication if you hit keys on console for Serial.
+         */
+        if (LOCK()) {
+            if (ser.writeable()) {
+                ser.printf("0x%x\n", shared_resource);
+            }
+            UNLOCK();
+        }
+        wait_ms(100);
+    }
+}