From: jjenista Date: Wed, 17 Jun 2009 18:32:07 +0000 (+0000) Subject: add a phtread implementation of semaphores for general runtime use X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=717e98a75dab152045b797496185d089a27366d7;p=IRC.git add a phtread implementation of semaphores for general runtime use --- diff --git a/Robust/src/Runtime/mlp_runtime.c b/Robust/src/Runtime/mlp_runtime.c index 285cf6b6..a737d12a 100644 --- a/Robust/src/Runtime/mlp_runtime.c +++ b/Robust/src/Runtime/mlp_runtime.c @@ -1,6 +1,9 @@ +#include "runtime.h" + #include #include #include + #include "mlp_runtime.h" #include "Queue.h" diff --git a/Robust/src/Runtime/mlp_runtime.h b/Robust/src/Runtime/mlp_runtime.h index ced76230..990500ac 100644 --- a/Robust/src/Runtime/mlp_runtime.h +++ b/Robust/src/Runtime/mlp_runtime.h @@ -12,8 +12,6 @@ struct invokeSESEargs_t; typedef struct SESEvar_t { - //unsigned char mode; - // the value when it is known will be placed // in this location, which can be accessed // as a variety of types @@ -27,15 +25,7 @@ typedef struct SESEvar_t { float sesetype_float; double sesetype_double; void* sesetype_object; - }; - - // a statically or dynamically known SESE - // to gather the variable's value from - // if source==NULL it indicates the root - // SESE, which has no record, just normal - // temp names - //struct SESErecord_t* source; - //unsigned int index; + }; } SESEvar; @@ -44,21 +34,14 @@ typedef struct SESErecord_t { // are instances of one particular static code block int classID; - - /* JUST USE POINTER TO SESErecord AS INSTANCE ID - // not globally unqiue, but each parent ensures that - // its children have unique identifiers, including to - // the parent itself - int instanceID; - - // used to give out IDs to children - int childInstanceIDs; - */ - // pointers to SESEs directly above or below // in the heirarchy - struct SESErecord_t* parent; - struct Queue* childrenList; + //struct SESErecord_t* parent; + //struct Queue* childrenList; + // IMPLEMENT THIS LIKE STALLS--EVERY PARENTS EXIT MUST + // "STALL" on COMPLETETION OF ALL ISSUED CHILDREN, SO + // ALWAYS GIVE A CHILD A SEMAPHORE THAT IS ON YOUR LIST + // OF THINGS TO BLOCK ON AT EXIT // for state of vars after issue SESEvar* vars; diff --git a/Robust/src/Runtime/psemaphore.c b/Robust/src/Runtime/psemaphore.c new file mode 100644 index 00000000..63f91eb0 --- /dev/null +++ b/Robust/src/Runtime/psemaphore.c @@ -0,0 +1,30 @@ +#include "psemaphore.h" + + +int psem_init( psemaphore* sem ) { + if( pthread_mutex_init( &(sem->lock), NULL ) == -1 ) { return -1; } + if( pthread_cond_init ( &(sem->cond), NULL ) == -1 ) { return -1; } + sem->signaled = 0; + return 0; +} + + +int psem_take( psemaphore* sem ) { + if( pthread_mutex_lock ( &(sem->lock) ) == -1 ) { return -1; } + while( !sem->signaled ) { + if( pthread_cond_wait ( &(sem->cond), &(sem->lock) ) == -1 ) { return -1; } + } + if( pthread_mutex_unlock( &(sem->lock) ) == -1 ) { return -1; } + return 0; +} + + +int psem_give( psemaphore* sem ) { + if( pthread_mutex_lock ( &(sem->lock) ) == -1 ) { return -1; } + sem->signaled = 1; + if( pthread_cond_signal ( &(sem->cond) ) == -1 ) { return -1; } + if( pthread_mutex_unlock( &(sem->lock) ) == -1 ) { return -1; } + +} + + diff --git a/Robust/src/Runtime/psemaphore.h b/Robust/src/Runtime/psemaphore.h new file mode 100644 index 00000000..1575ff87 --- /dev/null +++ b/Robust/src/Runtime/psemaphore.h @@ -0,0 +1,19 @@ +#ifndef ___PSEMAPHORE_H__ +#define ___PSEMAPHORE_H__ + +#include + + +typedef struct psemaphore_t { + pthread_mutex_t lock; + pthread_cond_t cond; + int signaled; +} psemaphore; + + +int psem_init( psemaphore* sem ); +int psem_take( psemaphore* sem ); +int psem_give( psemaphore* sem ); + + +#endif // ___PSEMAPHORE_H__ diff --git a/Robust/src/Tests/mlp/tinyTest/test.java b/Robust/src/Tests/mlp/tinyTest/test.java index 85d87fe3..a060d857 100644 --- a/Robust/src/Tests/mlp/tinyTest/test.java +++ b/Robust/src/Tests/mlp/tinyTest/test.java @@ -6,10 +6,10 @@ public class Test { int y = 1; sese fi { - if( true ) { + //if( true ) { x = y + 2; y = 3; - } + //} } @@ -23,9 +23,10 @@ public class Test { */ + /* // ADD BACK IN LATER, TO TEST STALLS // shouldn't cause a stall - //int z = x; + int z = x; // stall and get values for y and z x = x + 1; @@ -33,15 +34,15 @@ public class Test { // all of these should proceed without stall y = y + 1; x = x + 1; - //z = z + 1; - + z = z + 1; + */ // see that values from sese fi are // forwarded to this sibling //sese fo { // expecting x=5, y=4 System.out.println( "x="+x+", y="+y ); - //} + //}