Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
This change is about the refactoring of the source code of the objects use int the...
authorcherierm <cherierm@48e7efb5-ca39-0410-a469-dd3cf9ba447f>
Tue, 23 Oct 2007 09:37:14 +0000 (09:37 +0000)
committercherierm <cherierm@48e7efb5-ca39-0410-a469-dd3cf9ba447f>
Tue, 23 Oct 2007 09:37:14 +0000 (09:37 +0000)
  - the functions xbt_os_mutex_lock() and xbt_os_mutex_unlock() are renamed xbt_os_mutex_acquire() and xbt_os_mutex_release() respectively
  - the functions xbt_os_sem_wait() and xbt_os_sem_post() are renamed xbt_os_sem_acquire() and xbt_os_sem_release() respectively
This renaming impacts the folowing files :

  - xbt_os_thread.h and xbt_os_thread.c
  - context.c
  - xbt_rl_synchro.c
  - run_context.c

In addition the function unschedule() declared an defined in the file context.c is renamed yield().

The prototype of the function xbt_os_sem_init() takes now only one parameter : the initial value of the semaphore.
The second parameter of the function xbt_os_sem_timedacquire() which replace the function xbt_os_sem_timedwait() is now a double (desired timeout in seconds).
To finish, the semaphore interface of the xbt portability layer does not expose the functions xbt_os_sem_open() and xbt_sem_close().

git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@4848 48e7efb5-ca39-0410-a469-dd3cf9ba447f

src/include/xbt/xbt_os_thread.h
src/xbt/context.c
src/xbt/xbt_os_thread.c
src/xbt/xbt_rl_synchro.c
tools/tesh/run_context.c

index b39fcec..f69276c 100644 (file)
@@ -49,8 +49,8 @@ SG_BEGIN_DECL()
   typedef struct xbt_os_mutex_ *xbt_os_mutex_t;
 
   XBT_PUBLIC(xbt_os_mutex_t) xbt_os_mutex_init(void);
-  XBT_PUBLIC(void)           xbt_os_mutex_lock(xbt_os_mutex_t mutex);
-  XBT_PUBLIC(void)           xbt_os_mutex_unlock(xbt_os_mutex_t mutex);
+  XBT_PUBLIC(void)           xbt_os_mutex_acquire(xbt_os_mutex_t mutex);
+  XBT_PUBLIC(void)           xbt_os_mutex_release(xbt_os_mutex_t mutex);
   XBT_PUBLIC(void)           xbt_os_mutex_destroy(xbt_os_mutex_t mutex);
 
 
@@ -70,12 +70,10 @@ SG_BEGIN_DECL()
    /** \brief Semaphore data type (opaque structure) */
   typedef struct xbt_os_sem_* xbt_os_sem_t;
 
-  XBT_PUBLIC(xbt_os_sem_t) xbt_os_sem_init(int pshared, unsigned int value);
-  XBT_PUBLIC(xbt_os_sem_t) xbt_os_sem_open(const char *name, int oflag, mode_t mode, unsigned int value);
-  XBT_PUBLIC(void) xbt_os_sem_wait(xbt_os_sem_t sem);
-  XBT_PUBLIC(void) xbt_os_sem_timedwait(xbt_os_sem_t sem,const struct timespec* abs_timeout);
-  XBT_PUBLIC(void) xbt_os_sem_post(xbt_os_sem_t sem);
-  XBT_PUBLIC(void) xbt_os_sem_close(xbt_os_sem_t sem);
+  XBT_PUBLIC(xbt_os_sem_t) xbt_os_sem_init(unsigned int value);
+  XBT_PUBLIC(void) xbt_os_sem_acquire(xbt_os_sem_t sem);
+  XBT_PUBLIC(void) xbt_os_sem_timedacquire(xbt_os_sem_t sem,double timeout);
+  XBT_PUBLIC(void) xbt_os_sem_release(xbt_os_sem_t sem);
   XBT_PUBLIC(void) xbt_os_sem_destroy(xbt_os_sem_t sem);
   XBT_PUBLIC(void) xbt_os_sem_get_value(xbt_os_sem_t sem, int* svalue);
  
index c862be6..136411c 100644 (file)
@@ -55,19 +55,19 @@ static void
 schedule(xbt_context_t c);
 
 static void 
-unschedule(xbt_context_t c);
+yield(xbt_context_t c);
 
 static void 
 schedule(xbt_context_t c) 
 {
-       xbt_os_sem_post(c->begin);      
-       xbt_os_sem_wait(c->end);        
+       xbt_os_sem_release(c->begin);   
+       xbt_os_sem_acquire(c->end);     
 }
 
-static void unschedule(xbt_context_t c) 
+static void yield(xbt_context_t c) 
 {
-       xbt_os_sem_post(c->end);                
-       xbt_os_sem_wait(c->begin);
+       xbt_os_sem_release(c->end);             
+       xbt_os_sem_acquire(c->begin);
 }
 #endif
 
@@ -161,11 +161,11 @@ xbt_context_new(const char *name,
 
 #ifdef CONTEXT_THREADS
   /* 
-   * initialize the semaphores used to schedule/unschedule 
+   * initialize the semaphores used to schedule/yield 
    * the process associated to the newly created context 
    */
-  res->begin = xbt_os_sem_init(0,0);
-  res->end = xbt_os_sem_init(0,0);
+  res->begin = xbt_os_sem_init(0);
+  res->end = xbt_os_sem_init(0);
 #else
 
   xbt_assert2(getcontext(&(res->uc)) == 0,
@@ -273,7 +273,7 @@ static void *__context_wrapper(void *c)
   /*context->thread = xbt_os_thread_self();*/
 
   /* signal its starting to the maestro and wait to start its job*/
-  unschedule(context);
+  yield(context);
 
 #endif
 
@@ -298,7 +298,7 @@ void xbt_context_start(xbt_context_t context)
   context->thread = xbt_os_thread_create(context->name,__context_wrapper, context);
        
   /* wait the starting of the newly created process */
-  xbt_os_sem_wait(context->end);
+  xbt_os_sem_acquire(context->end);
 #else
   makecontext(&(context->uc), (void (*)(void)) __context_wrapper, 1, context);
 #endif
@@ -322,7 +322,7 @@ static void xbt_context_stop(int retvalue)
 
 #ifdef CONTEXT_THREADS
   /* signal to the maestro that it has finished */
-       xbt_os_sem_post(current_context->end);
+       xbt_os_sem_release(current_context->end);
        /* exit*/
        xbt_os_thread_exit(NULL);       /* We should provide return value in case other wants it */
 #else
@@ -380,7 +380,7 @@ static void __xbt_context_yield(xbt_context_t context)
                current_context = context;
                
                /* yield itself */
-               unschedule(context);
+               yield(context);
                
                /* restore the current context to the previously saved context */
                current_context = self;
index 0405622..023848e 100644 (file)
@@ -182,7 +182,7 @@ xbt_os_mutex_t xbt_os_mutex_init(void) {
    return res;
 }
 
-void xbt_os_mutex_lock(xbt_os_mutex_t mutex) {
+void xbt_os_mutex_acquire(xbt_os_mutex_t mutex) {
    int errcode;
    
    if ((errcode=pthread_mutex_lock(&(mutex->m))))
@@ -190,7 +190,7 @@ void xbt_os_mutex_lock(xbt_os_mutex_t mutex) {
            mutex, strerror(errcode));
 }
 
-void xbt_os_mutex_unlock(xbt_os_mutex_t mutex) {
+void xbt_os_mutex_release(xbt_os_mutex_t mutex) {
    int errcode;
    
    if ((errcode=pthread_mutex_unlock(&(mutex->m))))
@@ -289,31 +289,25 @@ void *xbt_os_thread_getparam(void) {
 
 typedef struct xbt_os_sem_ {
    sem_t s;
-   int pshared;
-   unsigned int value;
-   const char* name;
 }s_xbt_os_sem_t ;
 
 xbt_os_sem_t
-xbt_os_sem_init(int pshared, unsigned int value)
+xbt_os_sem_init(unsigned int value)
 {
        xbt_os_sem_t res = xbt_new(s_xbt_os_sem_t,1);
        
-       if(sem_init(&(res->s),pshared,value) < 0)
+       if(sem_init(&(res->s),0,value) < 0)
                THROW1(system_error,errno,"sem_init() failed: %s",
            strerror(errno));
    
-   res->pshared = pshared;
-   res->value = value;
-   
    return res;
 }
 
 void 
-xbt_os_sem_wait(xbt_os_sem_t sem)
+xbt_os_sem_acquire(xbt_os_sem_t sem)
 {
        if(!sem)
-               THROW1(arg_error,EINVAL,"xbt_os_sem_wait() failed: %s",
+               THROW1(arg_error,EINVAL,"xbt_os_sem_acquire() failed: %s",
            strerror(EINVAL));
        
        if(sem_wait(&(sem->s)) < 0)
@@ -321,25 +315,44 @@ xbt_os_sem_wait(xbt_os_sem_t sem)
            strerror(errno));            
 }
 
-void xbt_os_sem_timedwait(xbt_os_sem_t sem,const struct timespec* abs_timeout)
+void xbt_os_sem_timedacquire(xbt_os_sem_t sem,double timeout)
 {
+       int errcode;
+       struct timespec ts_end;
+       double end = timeout + xbt_os_time();
+       
        if(!sem)
-               THROW1(arg_error,EINVAL,"xbt_os_sem_timedwait() failed: %s",
-           strerror(EINVAL));
+               THROW1(arg_error,EINVAL,"xbt_os_sem_timedacquire() failed: %s",strerror(EINVAL));
        
-       /* only throw an exception if the global variable errno is different than ETIMEDOUT :
-        * (the semaphore could not be locked before the specified timeout expired)
-        */
-       if((sem_timedwait(&(sem->s),abs_timeout) < 0) && (ETIMEDOUT != errno))
-               THROW1(system_error,errno,"sem_wait() failed: %s",
-           strerror(errno));   
+       if (timeout < 0) 
+       {
+               xbt_os_sem_acquire(sem);
+       } 
+       else 
+       {
+               ts_end.tv_sec = (time_t) floor(end);
+               ts_end.tv_nsec = (long)  ( ( end - ts_end.tv_sec) * 1000000000);
+               DEBUG2("sem_timedwait(%p,%p)",&(sem->s),&ts_end);
+       
+               switch ((errcode=sem_timedwait(&(sem->s),&ts_end)))
+               {
+                       case 0:
+                       return;
+                       
+                       case ETIMEDOUT:
+                       THROW2(timeout_error,errcode,"semaphore %p wasn't signaled before timeout (%f)",sem,timeout);
+                       
+                       default:
+                       THROW3(system_error,errcode,"sem_timedwait(%p,%f) failed: %s",sem,timeout, strerror(errcode));
+               }   
+       }
 }
 
 void 
-xbt_os_sem_post(xbt_os_sem_t sem)
+xbt_os_sem_release(xbt_os_sem_t sem)
 {
        if(!sem)
-               THROW1(arg_error,EINVAL,"xbt_os_sem_post() failed: %s",
+               THROW1(arg_error,EINVAL,"xbt_os_sem_release() failed: %s",
            strerror(EINVAL));
        
        if(sem_post(&(sem->s)) < 0)
@@ -347,34 +360,6 @@ xbt_os_sem_post(xbt_os_sem_t sem)
            strerror(errno));            
 }
 
-void
-xbt_os_sem_close(xbt_os_sem_t sem)
-{
-       if(!sem)
-               THROW1(arg_error,EINVAL,"xbt_os_sem_close() failed: %s",
-           strerror(EINVAL));
-           
-       if(sem_close(&(sem->s)) < 0)
-               THROW1(system_error,errno,"sem_close() failed: %s",
-           strerror(errno));
-}
-
-xbt_os_sem_t 
-xbt_os_sem_open(const char *name, int oflag, mode_t mode, unsigned int value)
-{
-       sem_t* ps;
-       xbt_os_sem_t res = xbt_new(s_xbt_os_sem_t,1);
-       
-       if(SEM_FAILED == (ps = sem_open(name,oflag, mode, value)))
-               THROW1(system_error,errno,"sem_open() failed: %s",
-           strerror(errno));
-       
-   res->s = *ps;
-   res->value = value;
-   
-   return res; 
-}
-
 void
 xbt_os_sem_destroy(xbt_os_sem_t sem)
 {
@@ -403,6 +388,8 @@ xbt_os_sem_get_value(xbt_os_sem_t sem, int* svalue)
 
 #elif defined(WIN32)
 
+#include <math.h>
+
 typedef struct xbt_os_thread_ {
   char *name;
   HANDLE handle;                  /* the win thread handle        */
@@ -530,12 +517,12 @@ xbt_os_mutex_t xbt_os_mutex_init(void) {
    return res;
 }
 
-void xbt_os_mutex_lock(xbt_os_mutex_t mutex) {
+void xbt_os_mutex_acquire(xbt_os_mutex_t mutex) {
 
    EnterCriticalSection(& mutex->lock);
 }
 
-void xbt_os_mutex_unlock(xbt_os_mutex_t mutex) {
+void xbt_os_mutex_release(xbt_os_mutex_t mutex) {
 
    LeaveCriticalSection (& mutex->lock);
 
@@ -737,19 +724,14 @@ void xbt_os_cond_destroy(xbt_os_cond_t cond){
 typedef struct xbt_os_sem_ {
    HANDLE h;
    unsigned int value;
-   const char* name;
    CRITICAL_SECTION value_lock;  /* protect access to value of the semaphore  */
 }s_xbt_os_sem_t ;
 
 xbt_os_sem_t
-xbt_os_sem_init(int pshared, unsigned int value)
+xbt_os_sem_init(unsigned int value)
 {
        xbt_os_sem_t res;
        
-       if(0 != pshared)
-       THROW1(arg_error,EPERM,"xbt_os_sem_init() failed: %s",
-           strerror(EPERM));
-           
        if(value > INT_MAX)
        THROW1(arg_error,EINVAL,"xbt_os_sem_init() failed: %s",
            strerror(EINVAL));
@@ -770,10 +752,10 @@ xbt_os_sem_init(int pshared, unsigned int value)
 }
 
 void 
-xbt_os_sem_wait(xbt_os_sem_t sem)
+xbt_os_sem_acquire(xbt_os_sem_t sem)
 {
        if(!sem)
-               THROW1(arg_error,EINVAL,"xbt_os_sem_wait() failed: %s",
+               THROW1(arg_error,EINVAL,"xbt_os_sem_acquire() failed: %s",
            strerror(EINVAL));  
 
        /* wait failure */
@@ -785,49 +767,48 @@ xbt_os_sem_wait(xbt_os_sem_t sem)
        LeaveCriticalSection(&(sem->value_lock));
 }
 
-void xbt_os_sem_timedwait(xbt_os_sem_t sem,const struct timespec* abs_timeout)
+void xbt_os_sem_timedacquire(xbt_os_sem_t sem, double timeout)
 {
-       long timeout;
-       struct timeval tv;
+       long seconds;
+       long milliseconds;
+       double end = timeout + xbt_os_time();
        
        if(!sem)
-               THROW1(arg_error,EINVAL,"xbt_os_sem_timedwait() failed: %s",
+               THROW1(arg_error,EINVAL,"xbt_os_sem_timedacquire() failed: %s",
            strerror(EINVAL));  
-           
-       if(!abs_timeout)
-               timeout = INFINITE;
-       else
-       {
-               if(gettimeofday(&tv, NULL) < 0) 
-                       THROW1(system_error,errno,"gettimeofday() failed: %s",
-               strerror(errno));       
-                       
-                timeout = ((long) (abs_timeout->tv_sec - tv.tv_sec) * 1e3 + (long)((abs_timeout->tv_nsec / 1e3) - tv.tv_usec) / 1e3);          
-       }
        
-       switch(WaitForSingleObject(sem->h,timeout))
+        if (timeout < 0) 
+       {
+               xbt_os_sem_acquire(sem);
+       } 
+       else 
        {
-               case WAIT_OBJECT_0:
-               EnterCriticalSection(&(sem->value_lock));
-               sem->value--;
-               LeaveCriticalSection(&(sem->value_lock));
-               return;
                
-               case WAIT_TIMEOUT:
-               /* it's not an exception : 
-                * (semaphore could not be locked before the specified timeout expired)
-                */
-               return;
+               seconds = (long) floor(end);
+               milliseconds = (long)( ( end - seconds) * 1000);
+               milliseconds += (seconds * 1000);
                
-               default:
+               switch(WaitForSingleObject(sem->h,milliseconds))
+               {
+                       case WAIT_OBJECT_0:
+                       EnterCriticalSection(&(sem->value_lock));
+                       sem->value--;
+                       LeaveCriticalSection(&(sem->value_lock));
+                       return;
+               
+                       case WAIT_TIMEOUT:
+                       THROW2(timeout_error,GetLastError(),"semaphore %p wasn't signaled before timeout (%f)",sem,timeout);
+                       return;
+               
+                       default:
                        
-               THROW1(system_error,GetLastError(),"WaitForSingleObject() failed: %s",
-               strerror(GetLastError()));
+                       THROW3(system_error,GetLastError(),"WaitForSingleObject(%p,%f) failed: %s",sem,timeout, strerror(GetLastError()));
+               }
        }
 }
 
 void 
-xbt_os_sem_post(xbt_os_sem_t sem)
+xbt_os_sem_release(xbt_os_sem_t sem)
 {
        if(!sem)
                THROW1(arg_error,EINVAL,"xbt_os_sem_post() failed: %s",
@@ -841,18 +822,6 @@ xbt_os_sem_post(xbt_os_sem_t sem)
        LeaveCriticalSection(&(sem->value_lock));
 }
 
-xbt_os_sem_t 
-xbt_os_sem_open(const char *name, int oflag, mode_t mode, unsigned int value)
-{
-       THROW_UNIMPLEMENTED;
-}
-
-void
-xbt_os_sem_close(xbt_os_sem_t sem)
-{
-       THROW_UNIMPLEMENTED;
-}
-
 void
 xbt_os_sem_destroy(xbt_os_sem_t sem)
 {
index abd6e96..c273ccf 100644 (file)
@@ -95,12 +95,12 @@ xbt_mutex_t xbt_mutex_init(void) {
 
 void xbt_mutex_lock(xbt_mutex_t mutex) {
    DEBUG1("Lock mutex %p", mutex);
-   xbt_os_mutex_lock( (xbt_os_mutex_t)mutex );
+   xbt_os_mutex_acquire( (xbt_os_mutex_t)mutex );
 }
 
 void xbt_mutex_unlock(xbt_mutex_t mutex) {
    DEBUG1("Unlock mutex %p", mutex);
-   xbt_os_mutex_unlock( (xbt_os_mutex_t)mutex );
+   xbt_os_mutex_release( (xbt_os_mutex_t)mutex );
 }
 
 void xbt_mutex_destroy(xbt_mutex_t mutex) {
index f9fbdae..36547cc 100644 (file)
@@ -67,25 +67,25 @@ void rctx_armageddon(rctx_t initiator, int exitcode) {
   rctx_t rctx;
 
   DEBUG2("Armageddon request by <%s> (exit=%d)",initiator->filepos,exitcode);
-  xbt_os_mutex_lock(armageddon_mutex);
+  xbt_os_mutex_acquire(armageddon_mutex);
   if (armageddon_initiator != NULL) {
     VERB0("Armageddon already started. Let it go");
-    xbt_os_mutex_unlock(initiator->interruption);
-    xbt_os_mutex_unlock(armageddon_mutex);
+    xbt_os_mutex_release(initiator->interruption);
+    xbt_os_mutex_release(armageddon_mutex);
     return;
   }
   DEBUG1("Armageddon request by <%s> got the lock. Let's go amok",initiator->filepos);
   armageddon_initiator = initiator;
-  xbt_os_mutex_unlock(armageddon_mutex);
+  xbt_os_mutex_release(armageddon_mutex);
 
   /* Kill any background commands */
   while (xbt_dynar_length(bg_jobs)) {
     xbt_dynar_pop(bg_jobs,&rctx);
     if (rctx != initiator) {
       INFO2("Kill <%s> because <%s> failed",rctx->filepos,initiator->filepos);
-      xbt_os_mutex_lock(rctx->interruption);
+      xbt_os_mutex_acquire(rctx->interruption);
       rctx->interrupted = 1;
-      xbt_os_mutex_unlock(rctx->interruption);
+      xbt_os_mutex_release(rctx->interruption);
       if (!rctx->reader_done) {
        kill(rctx->pid,SIGTERM);
        usleep(100);
@@ -414,7 +414,7 @@ void *rctx_wait(void* r) {
     now = time(NULL);
   }
    
-  xbt_os_mutex_lock(rctx->interruption);
+  xbt_os_mutex_acquire(rctx->interruption);
   if (!rctx->interrupted && rctx->end_time > 0 && rctx->end_time < now) {    
     INFO1("<%s> timeouted. Kill the process.",rctx->filepos);
     rctx->timeout = 1;
@@ -430,10 +430,10 @@ void *rctx_wait(void* r) {
   xbt_os_thread_join(rctx->writer,NULL);
   xbt_os_thread_join(rctx->reader,NULL);
 
-  /*  xbt_os_mutex_unlock(rctx->interruption);
+  /*  xbt_os_mutex_release(rctx->interruption);
   if (rctx->interrupted)
     return NULL;
-    xbt_os_mutex_lock(rctx->interruption);*/
+    xbt_os_mutex_acquire(rctx->interruption);*/
  
   xbt_strbuff_chomp(rctx->output_got);
   xbt_strbuff_chomp(rctx->output_wanted);
@@ -547,7 +547,7 @@ void *rctx_wait(void* r) {
     }
   }
 
-  xbt_os_mutex_unlock(rctx->interruption);
+  xbt_os_mutex_release(rctx->interruption);
   return NULL;
 }