root/opal/threads/threads.h

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

INCLUDED FROM


   1 /*
   2  * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
   3  *                         University Research and Technology
   4  *                         Corporation.  All rights reserved.
   5  * Copyright (c) 2004-2006 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) 2010      Cisco Systems, Inc. All rights reserved.
  13  * Copyright (c) 2010      Oracle and/or its affiliates.  All rights reserved.
  14  * Copyright (c) 2015-2017 Research Organization for Information Science
  15  *                         and Technology (RIST). All rights reserved.
  16  * Copyright (c) 2017      Intel, Inc. All rights reserved.
  17  * $COPYRIGHT$
  18  *
  19  * Additional copyrights may follow
  20  *
  21  * $HEADER$
  22  */
  23 
  24 #ifndef OPAL_THREAD_H
  25 #define OPAL_THREAD_H 1
  26 
  27 #include "opal_config.h"
  28 
  29 #include <pthread.h>
  30 #include <signal.h>
  31 
  32 #include "opal/class/opal_object.h"
  33 #if OPAL_ENABLE_DEBUG
  34 #include "opal/util/output.h"
  35 #endif
  36 
  37 #include "mutex.h"
  38 #include "condition.h"
  39 
  40 BEGIN_C_DECLS
  41 
  42 typedef void *(*opal_thread_fn_t) (opal_object_t *);
  43 
  44 #define OPAL_THREAD_CANCELLED   ((void*)1);
  45 
  46 struct opal_thread_t {
  47     opal_object_t super;
  48     opal_thread_fn_t t_run;
  49     void* t_arg;
  50     pthread_t t_handle;
  51 };
  52 
  53 typedef struct opal_thread_t opal_thread_t;
  54 
  55 #if OPAL_ENABLE_DEBUG
  56 OPAL_DECLSPEC extern bool opal_debug_threads;
  57 #endif
  58 
  59 
  60 OPAL_DECLSPEC OBJ_CLASS_DECLARATION(opal_thread_t);
  61 
  62 #if OPAL_ENABLE_DEBUG
  63 #define OPAL_ACQUIRE_THREAD(lck, cnd, act)               \
  64     do {                                                 \
  65         OPAL_THREAD_LOCK((lck));                         \
  66         if (opal_debug_threads) {                        \
  67             opal_output(0, "Waiting for thread %s:%d",   \
  68                         __FILE__, __LINE__);             \
  69         }                                                \
  70         while (*(act)) {                                 \
  71             opal_condition_wait((cnd), (lck));           \
  72         }                                                \
  73         if (opal_debug_threads) {                        \
  74             opal_output(0, "Thread obtained %s:%d",      \
  75                         __FILE__, __LINE__);             \
  76         }                                                \
  77         *(act) = true;                                   \
  78     } while(0);
  79 #else
  80 #define OPAL_ACQUIRE_THREAD(lck, cnd, act)               \
  81     do {                                                 \
  82         OPAL_THREAD_LOCK((lck));                         \
  83         while (*(act)) {                                 \
  84             opal_condition_wait((cnd), (lck));           \
  85         }                                                \
  86         *(act) = true;                                   \
  87     } while(0);
  88 #endif
  89 
  90 
  91 #if OPAL_ENABLE_DEBUG
  92 #define OPAL_RELEASE_THREAD(lck, cnd, act)              \
  93     do {                                                \
  94         if (opal_debug_threads) {                       \
  95             opal_output(0, "Releasing thread %s:%d",    \
  96                         __FILE__, __LINE__);            \
  97         }                                               \
  98         *(act) = false;                                 \
  99         opal_condition_broadcast((cnd));                \
 100         OPAL_THREAD_UNLOCK((lck));                      \
 101     } while(0);
 102 #else
 103 #define OPAL_RELEASE_THREAD(lck, cnd, act)              \
 104     do {                                                \
 105         *(act) = false;                                 \
 106         opal_condition_broadcast((cnd));                \
 107         OPAL_THREAD_UNLOCK((lck));                      \
 108     } while(0);
 109 #endif
 110 
 111 
 112 #define OPAL_WAKEUP_THREAD(cnd, act)        \
 113     do {                                    \
 114         *(act) = false;                     \
 115         opal_condition_broadcast((cnd));    \
 116     } while(0);
 117 
 118 /* provide a macro for forward-proofing the shifting
 119  * of objects between libevent threads - at some point, we
 120  * may revamp that threading model */
 121 
 122 /* post an object to another thread - for now, we
 123  * only have a memory barrier */
 124 #define OPAL_POST_OBJECT(o)     opal_atomic_wmb()
 125 
 126 /* acquire an object from another thread - for now,
 127  * we only have a memory barrier */
 128 #define OPAL_ACQUIRE_OBJECT(o)  opal_atomic_rmb()
 129 
 130 
 131 
 132 OPAL_DECLSPEC int  opal_thread_start(opal_thread_t *);
 133 OPAL_DECLSPEC int  opal_thread_join(opal_thread_t *, void **thread_return);
 134 OPAL_DECLSPEC bool opal_thread_self_compare(opal_thread_t*);
 135 OPAL_DECLSPEC opal_thread_t *opal_thread_get_self(void);
 136 OPAL_DECLSPEC void opal_thread_kill(opal_thread_t *, int sig);
 137 OPAL_DECLSPEC void opal_thread_set_main(void);
 138 
 139 END_C_DECLS
 140 
 141 #endif /* OPAL_THREAD_H */

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