root/opal/mca/pmix/pmix4x/pmix/src/threads/thread.c

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

DEFINITIONS

This source file includes following definitions.
  1. pmix_thread_construct
  2. pmix_thread_start
  3. pmix_thread_join
  4. pmix_thread_self_compare
  5. pmix_thread_get_self
  6. pmix_thread_kill
  7. pmix_tsd_key_create
  8. pmix_tsd_keys_destruct
  9. pmix_thread_set_main

   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-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) 2010      Cisco Systems, Inc. All rights reserved.
  13  * Copyright (c) 2015-2017 Research Organization for Information Science
  14  *                         and Technology (RIST). All rights reserved.
  15  * Copyright (c) 2017      Intel, Inc. All rights reserved.
  16  * $COPYRIGHT$
  17  *
  18  * Additional copyrights may follow
  19  *
  20  * $HEADER$
  21  */
  22 
  23 #include "pmix_config.h"
  24 
  25 #include "src/threads/threads.h"
  26 #include "src/threads/tsd.h"
  27 #include "pmix_common.h"
  28 
  29 bool pmix_debug_threads = false;
  30 
  31 static void pmix_thread_construct(pmix_thread_t *t);
  32 
  33 static pthread_t pmix_main_thread;
  34 
  35 struct pmix_tsd_key_value {
  36     pmix_tsd_key_t key;
  37     pmix_tsd_destructor_t destructor;
  38 };
  39 
  40 static struct pmix_tsd_key_value *pmix_tsd_key_values = NULL;
  41 static int pmix_tsd_key_values_count = 0;
  42 
  43 PMIX_EXPORT PMIX_CLASS_INSTANCE(pmix_thread_t,
  44                                 pmix_object_t,
  45                                 pmix_thread_construct, NULL);
  46 
  47 
  48 /*
  49  * Constructor
  50  */
  51 static void pmix_thread_construct(pmix_thread_t *t)
  52 {
  53     t->t_run = 0;
  54     t->t_handle = (pthread_t) -1;
  55 }
  56 
  57 int pmix_thread_start(pmix_thread_t *t)
  58 {
  59     int rc;
  60 
  61     if (PMIX_ENABLE_DEBUG) {
  62         if (NULL == t->t_run || t->t_handle != (pthread_t) -1) {
  63             return PMIX_ERR_BAD_PARAM;
  64         }
  65     }
  66 
  67     rc = pthread_create(&t->t_handle, NULL, (void*(*)(void*)) t->t_run, t);
  68 
  69     return (rc == 0) ? PMIX_SUCCESS : PMIX_ERROR;
  70 }
  71 
  72 
  73 int pmix_thread_join(pmix_thread_t *t, void **thr_return)
  74 {
  75     int rc = pthread_join(t->t_handle, thr_return);
  76     t->t_handle = (pthread_t) -1;
  77     return (rc == 0) ? PMIX_SUCCESS : PMIX_ERROR;
  78 }
  79 
  80 
  81 bool pmix_thread_self_compare(pmix_thread_t *t)
  82 {
  83     return t->t_handle == pthread_self();
  84 }
  85 
  86 
  87 pmix_thread_t *pmix_thread_get_self(void)
  88 {
  89     pmix_thread_t *t = PMIX_NEW(pmix_thread_t);
  90     t->t_handle = pthread_self();
  91     return t;
  92 }
  93 
  94 void pmix_thread_kill(pmix_thread_t *t, int sig)
  95 {
  96     pthread_kill(t->t_handle, sig);
  97 }
  98 
  99 int pmix_tsd_key_create(pmix_tsd_key_t *key,
 100                     pmix_tsd_destructor_t destructor)
 101 {
 102     int rc;
 103     rc = pthread_key_create(key, destructor);
 104     if ((0 == rc) && (pthread_self() == pmix_main_thread)) {
 105         pmix_tsd_key_values = (struct pmix_tsd_key_value *)realloc(pmix_tsd_key_values, (pmix_tsd_key_values_count+1) * sizeof(struct pmix_tsd_key_value));
 106         pmix_tsd_key_values[pmix_tsd_key_values_count].key = *key;
 107         pmix_tsd_key_values[pmix_tsd_key_values_count].destructor = destructor;
 108         pmix_tsd_key_values_count ++;
 109     }
 110     return rc;
 111 }
 112 
 113 int pmix_tsd_keys_destruct()
 114 {
 115     int i;
 116     void * ptr;
 117     for (i=0; i<pmix_tsd_key_values_count; i++) {
 118         if(PMIX_SUCCESS == pmix_tsd_getspecific(pmix_tsd_key_values[i].key, &ptr)) {
 119             if (NULL != pmix_tsd_key_values[i].destructor) {
 120                 pmix_tsd_key_values[i].destructor(ptr);
 121                 pmix_tsd_setspecific(pmix_tsd_key_values[i].key, NULL);
 122             }
 123         }
 124     }
 125     if (0 < pmix_tsd_key_values_count) {
 126         free(pmix_tsd_key_values);
 127         pmix_tsd_key_values_count = 0;
 128     }
 129     return PMIX_SUCCESS;
 130 }
 131 
 132 void pmix_thread_set_main() {
 133     pmix_main_thread = pthread_self();
 134 }

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