| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*********************************************************************************/ | ||
| 2 | /* Copyright 2009-2021 Barcelona Supercomputing Center */ | ||
| 3 | /* */ | ||
| 4 | /* This file is part of the DLB library. */ | ||
| 5 | /* */ | ||
| 6 | /* DLB is free software: you can redistribute it and/or modify */ | ||
| 7 | /* it under the terms of the GNU Lesser General Public License as published by */ | ||
| 8 | /* the Free Software Foundation, either version 3 of the License, or */ | ||
| 9 | /* (at your option) any later version. */ | ||
| 10 | /* */ | ||
| 11 | /* DLB is distributed in the hope that it will be useful, */ | ||
| 12 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ | ||
| 13 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ | ||
| 14 | /* GNU Lesser General Public License for more details. */ | ||
| 15 | /* */ | ||
| 16 | /* You should have received a copy of the GNU Lesser General Public License */ | ||
| 17 | /* along with DLB. If not, see <https://www.gnu.org/licenses/>. */ | ||
| 18 | /*********************************************************************************/ | ||
| 19 | |||
| 20 | #include "LB_core/spd.h" | ||
| 21 | |||
| 22 | #include "support/debug.h" | ||
| 23 | #include "support/gtree.h" | ||
| 24 | |||
| 25 | #include <stdlib.h> | ||
| 26 | #include <string.h> | ||
| 27 | #include <pthread.h> | ||
| 28 | |||
| 29 | /* TLS global variable to store the spd pointer */ | ||
| 30 | __thread subprocess_descriptor_t *thread_spd = NULL; | ||
| 31 | |||
| 32 | /* Global subprocess descriptor */ | ||
| 33 | static subprocess_descriptor_t global_spd = { 0 }; | ||
| 34 | |||
| 35 | /* GTree containing each subprocess descriptor */ | ||
| 36 | typedef struct SPDInfo { | ||
| 37 | const subprocess_descriptor_t *spd; | ||
| 38 | pthread_t pthread; | ||
| 39 | } spd_info_t; | ||
| 40 | static GTree *spd_tree = NULL; | ||
| 41 | static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; | ||
| 42 | |||
| 43 | |||
| 44 | /*********************************************************************************/ | ||
| 45 | /* TLS global variable */ | ||
| 46 | /*********************************************************************************/ | ||
| 47 | 1523 | void spd_enter_dlb(subprocess_descriptor_t *spd) { | |
| 48 |
2/2✓ Branch 0 taken 1415 times.
✓ Branch 1 taken 108 times.
|
1523 | thread_spd = spd ? spd : &global_spd; |
| 49 | 1523 | } | |
| 50 | |||
| 51 | ✗ | bool spd_is_global_spd(const subprocess_descriptor_t *spd) { | |
| 52 | ✗ | return spd == &global_spd; | |
| 53 | } | ||
| 54 | |||
| 55 | /*********************************************************************************/ | ||
| 56 | /* GTree modification functions */ | ||
| 57 | /*********************************************************************************/ | ||
| 58 | 79 | static gint key_compare_func(gconstpointer a, gconstpointer b) { | |
| 59 |
2/2✓ Branch 0 taken 63 times.
✓ Branch 1 taken 16 times.
|
79 | return (uintptr_t)a < (uintptr_t)b ? -1 : (uintptr_t)a > (uintptr_t)b; |
| 60 | } | ||
| 61 | |||
| 62 | 34 | void spd_register(subprocess_descriptor_t *spd) { | |
| 63 | 34 | spd_info_t *spd_info = malloc(sizeof(spd_info_t)); | |
| 64 | 34 | spd_info->spd = spd; | |
| 65 | 34 | spd_info->pthread = 0; | |
| 66 | |||
| 67 | 34 | pthread_mutex_lock(&mutex); | |
| 68 | { | ||
| 69 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 25 times.
|
34 | if (spd_tree == NULL) { |
| 70 | 9 | spd_tree = g_tree_new_full( | |
| 71 | (GCompareDataFunc)key_compare_func, | ||
| 72 | NULL, NULL, free); | ||
| 73 | } | ||
| 74 | 34 | g_tree_insert(spd_tree, spd, spd_info); | |
| 75 | } | ||
| 76 | 34 | pthread_mutex_unlock(&mutex); | |
| 77 | 34 | } | |
| 78 | |||
| 79 | 30 | void spd_unregister(const subprocess_descriptor_t *spd) { | |
| 80 | 30 | pthread_mutex_lock(&mutex); | |
| 81 | { | ||
| 82 | 30 | g_tree_remove(spd_tree, spd); | |
| 83 | } | ||
| 84 | 30 | pthread_mutex_unlock(&mutex); | |
| 85 | 30 | } | |
| 86 | |||
| 87 | __attribute__((destructor)) | ||
| 88 | 97 | static void spd_tree_dtor(void) { | |
| 89 | 97 | g_tree_destroy(spd_tree); | |
| 90 | 97 | } | |
| 91 | |||
| 92 | /*********************************************************************************/ | ||
| 93 | /* Setter and getter of the assigned pthread per spd */ | ||
| 94 | /*********************************************************************************/ | ||
| 95 | 2 | void spd_set_pthread(const subprocess_descriptor_t *spd, pthread_t pthread) { | |
| 96 | 2 | pthread_mutex_lock(&mutex); | |
| 97 | { | ||
| 98 | 2 | spd_info_t *spd_info = g_tree_lookup(spd_tree, spd); | |
| 99 | 2 | spd_info->pthread = pthread; | |
| 100 | } | ||
| 101 | 2 | pthread_mutex_unlock(&mutex); | |
| 102 | 2 | } | |
| 103 | |||
| 104 | 3 | pthread_t spd_get_pthread(const subprocess_descriptor_t *spd) { | |
| 105 | pthread_t pthread; | ||
| 106 | 3 | pthread_mutex_lock(&mutex); | |
| 107 | { | ||
| 108 | 3 | spd_info_t *spd_info = g_tree_lookup(spd_tree, spd); | |
| 109 | 3 | pthread = spd_info->pthread; | |
| 110 | } | ||
| 111 | 3 | pthread_mutex_unlock(&mutex); | |
| 112 | 3 | return pthread; | |
| 113 | } | ||
| 114 | |||
| 115 | /*********************************************************************************/ | ||
| 116 | /* Obtain a list of pointers (NULL terminated) of spds */ | ||
| 117 | /*********************************************************************************/ | ||
| 118 | 2 | static gint tree_to_list(gpointer key, gpointer value, gpointer data) { | |
| 119 | 2 | subprocess_descriptor_t *spd = key; | |
| 120 | 2 | subprocess_descriptor_t ***list_p = data; | |
| 121 | 2 | **list_p = spd; | |
| 122 | 2 | ++(*list_p); | |
| 123 | |||
| 124 | /* return false to not stop traversing */ | ||
| 125 | 2 | return false; | |
| 126 | } | ||
| 127 | |||
| 128 | 3 | const subprocess_descriptor_t** spd_get_spds(void) { | |
| 129 | const subprocess_descriptor_t **spds; | ||
| 130 | 3 | pthread_mutex_lock(&mutex); | |
| 131 | { | ||
| 132 | 3 | spds = malloc(sizeof(subprocess_descriptor_t *) * (g_tree_nnodes(spd_tree)+1)); | |
| 133 | 3 | const subprocess_descriptor_t **p = spds; | |
| 134 | 3 | g_tree_foreach(spd_tree, tree_to_list, &p); | |
| 135 | 3 | *p = NULL; | |
| 136 | } | ||
| 137 | 3 | pthread_mutex_unlock(&mutex); | |
| 138 | |||
| 139 | 3 | return spds; | |
| 140 | } | ||
| 141 | |||
| 142 | /*********************************************************************************/ | ||
| 143 | /* Fork handlers */ | ||
| 144 | /*********************************************************************************/ | ||
| 145 | 19 | void spd_atfork_prepare(void) { | |
| 146 | 19 | pthread_mutex_lock(&mutex); | |
| 147 | 19 | } | |
| 148 | |||
| 149 | 19 | void spd_atfork_parent(void) { | |
| 150 | 19 | pthread_mutex_unlock(&mutex); | |
| 151 | 19 | } | |
| 152 | |||
| 153 | ✗ | static gint deallocate_spd(gpointer key, gpointer value, gpointer data) { | |
| 154 | ✗ | subprocess_descriptor_t *spd = key; | |
| 155 | ✗ | free(spd->lewi_info); | |
| 156 | ✗ | free(spd->talp_info); | |
| 157 | ✗ | free(spd->barrier_info); | |
| 158 | ✗ | free(spd->mngo_info); | |
| 159 | ✗ | *spd = (subprocess_descriptor_t){0}; | |
| 160 | |||
| 161 | /* return false to not stop traversing */ | ||
| 162 | ✗ | return false; | |
| 163 | } | ||
| 164 | |||
| 165 | ✗ | void spd_atfork_child(void) { | |
| 166 | |||
| 167 | ✗ | pthread_mutex_init(&mutex, NULL); | |
| 168 | |||
| 169 | ✗ | deallocate_spd(&global_spd, NULL, NULL); | |
| 170 | |||
| 171 | ✗ | g_tree_foreach(spd_tree, deallocate_spd, NULL); | |
| 172 | ✗ | g_tree_destroy(spd_tree); | |
| 173 | ✗ | spd_tree = NULL; | |
| 174 | |||
| 175 | ✗ | thread_spd = NULL; | |
| 176 | } | ||
| 177 |