root/opal/threads/condition.h

/* [<][>][^][v][top][bottom][index][help] */

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. opal_condition_wait
  2. opal_condition_timedwait
  3. opal_condition_signal
  4. opal_condition_broadcast

   1 /*
   2  * Copyright (c) 2004-2007 The Trustees of Indiana University and Indiana
   3  *                         University Research and Technology
   4  *                         Corporation.  All rights reserved.
   5  * Copyright (c) 2004-2005 The University of Tennessee and The University
   6  *                         of Tennessee Research Foundation.  All rights
   7  *                         reserved.
   8  * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
   9  *                         University of Stuttgart.  All rights reserved.
  10  * Copyright (c) 2004-2005 The Regents of the University of California.
  11  *                         All rights reserved.
  12  * Copyright (c) 2007      Los Alamos National Security, LLC.  All rights
  13  *                         reserved.
  14  * Copyright (c) 2015      Research Organization for Information Science
  15  *                         and Technology (RIST). All rights reserved.
  16  * $COPYRIGHT$
  17  *
  18  * Additional copyrights may follow
  19  *
  20  * $HEADER$
  21  */
  22 #ifndef OPAL_CONDITION_SPINLOCK_H
  23 #define OPAL_CONDITION_SPINLOCK_H
  24 
  25 #include "opal_config.h"
  26 #ifdef HAVE_SYS_TIME_H
  27 #include <sys/time.h>
  28 #endif
  29 #include <time.h>
  30 #include <pthread.h>
  31 
  32 #include "opal/threads/mutex.h"
  33 #include "opal/runtime/opal_progress.h"
  34 
  35 #include "opal/runtime/opal_cr.h"
  36 
  37 BEGIN_C_DECLS
  38 
  39 /*
  40  * Combine pthread support w/ polled progress to allow run-time selection
  41  * of threading vs. non-threading progress.
  42  */
  43 
  44 struct opal_condition_t {
  45     opal_object_t super;
  46     volatile int c_waiting;
  47     volatile int c_signaled;
  48 };
  49 typedef struct opal_condition_t opal_condition_t;
  50 
  51 OPAL_DECLSPEC OBJ_CLASS_DECLARATION(opal_condition_t);
  52 
  53 
  54 static inline int opal_condition_wait(opal_condition_t *c, opal_mutex_t *m)
  55 {
  56     int rc = 0;
  57     c->c_waiting++;
  58 
  59     if (opal_using_threads()) {
  60         if (c->c_signaled) {
  61             c->c_waiting--;
  62             opal_mutex_unlock(m);
  63             opal_progress();
  64             OPAL_CR_TEST_CHECKPOINT_READY_STALL();
  65             opal_mutex_lock(m);
  66             return 0;
  67         }
  68         while (c->c_signaled == 0) {
  69             opal_mutex_unlock(m);
  70             opal_progress();
  71             OPAL_CR_TEST_CHECKPOINT_READY_STALL();
  72             opal_mutex_lock(m);
  73         }
  74     } else {
  75         while (c->c_signaled == 0) {
  76             opal_progress();
  77             OPAL_CR_TEST_CHECKPOINT_READY_STALL();
  78         }
  79     }
  80 
  81     c->c_signaled--;
  82     c->c_waiting--;
  83     return rc;
  84 }
  85 
  86 static inline int opal_condition_timedwait(opal_condition_t *c,
  87                                            opal_mutex_t *m,
  88                                            const struct timespec *abstime)
  89 {
  90     struct timeval tv;
  91     struct timeval absolute;
  92     int rc = 0;
  93 
  94     c->c_waiting++;
  95     if (opal_using_threads()) {
  96         absolute.tv_sec = abstime->tv_sec;
  97         absolute.tv_usec = abstime->tv_nsec / 1000;
  98         gettimeofday(&tv,NULL);
  99         if (c->c_signaled == 0) {
 100             do {
 101                 opal_mutex_unlock(m);
 102                 opal_progress();
 103                 gettimeofday(&tv,NULL);
 104                 opal_mutex_lock(m);
 105                 } while (c->c_signaled == 0 &&
 106                          (tv.tv_sec <= absolute.tv_sec ||
 107                           (tv.tv_sec == absolute.tv_sec && tv.tv_usec < absolute.tv_usec)));
 108         }
 109     } else {
 110         absolute.tv_sec = abstime->tv_sec;
 111         absolute.tv_usec = abstime->tv_nsec / 1000;
 112         gettimeofday(&tv,NULL);
 113         if (c->c_signaled == 0) {
 114             do {
 115                 opal_progress();
 116                 gettimeofday(&tv,NULL);
 117                 } while (c->c_signaled == 0 &&
 118                          (tv.tv_sec <= absolute.tv_sec ||
 119                           (tv.tv_sec == absolute.tv_sec && tv.tv_usec < absolute.tv_usec)));
 120         }
 121     }
 122 
 123     if (c->c_signaled != 0) c->c_signaled--;
 124     c->c_waiting--;
 125     return rc;
 126 }
 127 
 128 static inline int opal_condition_signal(opal_condition_t *c)
 129 {
 130     if (c->c_waiting) {
 131         c->c_signaled++;
 132     }
 133     return 0;
 134 }
 135 
 136 static inline int opal_condition_broadcast(opal_condition_t *c)
 137 {
 138     c->c_signaled = c->c_waiting;
 139     return 0;
 140 }
 141 
 142 END_C_DECLS
 143 
 144 #endif
 145 

/* [<][>][^][v][top][bottom][index][help] */