Umut Aradağ / Mbed 2 deprecated Semaphores

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
umutaradag
Date:
Sat Aug 06 04:40:56 2011 +0000
Parent:
2:ac3746d21033
Commit message:

Changed in this revision

semaphore.c Show annotated file Show diff for this revision Revisions of this file
semaphore.h Show annotated file Show diff for this revision Revisions of this file
--- a/semaphore.c	Thu Aug 04 10:00:02 2011 +0000
+++ b/semaphore.c	Sat Aug 06 04:40:56 2011 +0000
@@ -4,32 +4,34 @@
 
 void semInit(semaphore* s, int initVal)
 {
-    *s = initVal;
+    s->count = initVal;
 }
 void semPost(semaphore* s)
 {
     __disable_irq();
-    (*s)++;
+    s->count++;
     //printf("sem released %d\n",*s);
     __enable_irq();
 }
-int semPend(semaphore* s, int trials)
+int semPend(semaphore* s, int maxNumOfTrials)
 {
-    int* counter = (int*)malloc(sizeof(int));
+    s->trials = 0;
     do
     {
         __disable_irq();
         //printf("sem testing %d\n",*s);
-        if(*s>0)
+        if(s->count > 0)
         {
-            (*s)--;
+            s->count--;
             __enable_irq();
             //printf("sem acquired\n");
-            free(counter);
             return SEM_OK;
         }
         __enable_irq();
-    }while(trials > (*counter)++);
-    free(counter);
+    }while(maxNumOfTrials > s->trials++);
     return SEM_ERR;
+}
+void semSet(semaphore* s, int val)
+{
+    s->count = val;
 }
\ No newline at end of file
--- a/semaphore.h	Thu Aug 04 10:00:02 2011 +0000
+++ b/semaphore.h	Sat Aug 06 04:40:56 2011 +0000
@@ -1,9 +1,15 @@
 
-typedef int semaphore;
+typedef struct
+{
+    int count;
+    int trials;
+}
+semaphore;
 
 #define SEM_OK 1
 #define SEM_ERR 0
 
 void semInit(semaphore* s, int initVal);
 void semPost(semaphore* s);
+void semSet(semaphore* s, int val);
 int semPend(semaphore* s, int trials);
\ No newline at end of file