root/opal/runtime/opal_finalize.c

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

DEFINITIONS

This source file includes following definitions.
  1. opal_cleanup_fn_item_construct
  2. opal_cleanup_fn_item_destruct
  3. opal_finalize_domain_construct
  4. opal_finalize_domain_destruct
  5. opal_finalize_append_cleanup
  6. opal_finalize_domain_init
  7. opal_finalize_set_domain
  8. opal_finalize_cleanup_domain
  9. opal_finalize_util
  10. opal_finalize
  11. warn_fork_cb
  12. opal_warn_fork

   1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
   2 /*
   3  * Copyright (c) 2004-2010 The Trustees of Indiana University and Indiana
   4  *                         University Research and Technology
   5  *                         Corporation.  All rights reserved.
   6  * Copyright (c) 2004-2005 The University of Tennessee and The University
   7  *                         of Tennessee Research Foundation.  All rights
   8  *                         reserved.
   9  * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
  10  *                         University of Stuttgart.  All rights reserved.
  11  * Copyright (c) 2004-2005 The Regents of the University of California.
  12  *                         All rights reserved.
  13  * Copyright (c) 2008-2015 Cisco Systems, Inc.  All rights reserved.
  14  * Copyright (c) 2010-2015 Los Alamos National Security, LLC.
  15  *                         All rights reserved.
  16  * Copyright (c) 2013-2017 Intel, Inc. All rights reserved.
  17  * Copyright (c) 2016-2017 Research Organization for Information Science
  18  *                         and Technology (RIST). All rights reserved.
  19  * Copyright (c) 2017      Amazon.com, Inc. or its affiliates.
  20  *                         All Rights reserved.
  21  * Copyright (c) 2018      Triad National Security, LLC. All rights
  22  *                         reserved.
  23  * $COPYRIGHT$
  24  *
  25  * Additional copyrights may follow
  26  *
  27  * $HEADER$
  28  */
  29 
  30 /** @file **/
  31 
  32 #include "opal_config.h"
  33 
  34 #include "opal/class/opal_object.h"
  35 #include "opal/util/output.h"
  36 #include "opal/util/malloc.h"
  37 #include "opal/util/proc.h"
  38 #include "opal/util/show_help.h"
  39 #include "opal/memoryhooks/memory.h"
  40 #include "opal/runtime/opal.h"
  41 #include "opal/constants.h"
  42 #include "opal/threads/tsd.h"
  43 #include "opal/runtime/opal_cr.h"
  44 #include "opal/runtime/opal_progress.h"
  45 
  46 extern int opal_initialized;
  47 extern int opal_util_initialized;
  48 extern bool opal_init_called;
  49 
  50 static opal_mutex_t opal_finalize_cleanup_fns_lock = OPAL_MUTEX_STATIC_INIT;
  51 opal_list_t opal_finalize_cleanup_fns = {{0}};
  52 
  53 struct opal_cleanup_fn_item_t {
  54     opal_list_item_t  super;
  55     opal_cleanup_fn_t cleanup_fn;
  56     void             *user_data;
  57 #if OPAL_ENABLE_DEBUG
  58     char *cleanup_fn_name;
  59 #endif
  60 };
  61 
  62 typedef struct opal_cleanup_fn_item_t opal_cleanup_fn_item_t;
  63 OBJ_CLASS_DECLARATION(opal_cleanup_fn_item_t);
  64 
  65 static void opal_cleanup_fn_item_construct (opal_cleanup_fn_item_t *item)
  66 {
  67 #if OPAL_ENABLE_DEBUG
  68     item->cleanup_fn_name = NULL;
  69 #endif
  70 }
  71 
  72 static void opal_cleanup_fn_item_destruct (opal_cleanup_fn_item_t *item)
  73 {
  74 #if OPAL_ENABLE_DEBUG
  75     free (item->cleanup_fn_name);
  76     item->cleanup_fn_name = NULL;
  77 #endif
  78 }
  79 
  80 
  81 OBJ_CLASS_INSTANCE(opal_cleanup_fn_item_t, opal_list_item_t,
  82                    opal_cleanup_fn_item_construct, opal_cleanup_fn_item_destruct);
  83 
  84 static void opal_finalize_domain_construct (opal_finalize_domain_t *domain)
  85 {
  86     domain->domain_name = NULL;
  87 }
  88 
  89 static void opal_finalize_domain_destruct (opal_finalize_domain_t *domain)
  90 {
  91     free (domain->domain_name);
  92     domain->domain_name = NULL;
  93 }
  94 
  95 OBJ_CLASS_INSTANCE(opal_finalize_domain_t, opal_list_t, opal_finalize_domain_construct,
  96                    opal_finalize_domain_destruct);
  97 
  98 static opal_finalize_domain_t *current_finalize_domain;
  99 opal_finalize_domain_t opal_init_util_domain = {{{0}}};
 100 opal_finalize_domain_t opal_init_domain = {{{0}}};
 101 
 102 void opal_finalize_append_cleanup (opal_cleanup_fn_t cleanup_fn, const char *fn_name, void *user_data)
 103 {
 104     opal_cleanup_fn_item_t *cleanup_item = OBJ_NEW(opal_cleanup_fn_item_t);
 105     assert (NULL != cleanup_item);
 106     cleanup_item->cleanup_fn = cleanup_fn;
 107     cleanup_item->user_data = user_data;
 108 #if OPAL_ENABLE_DEBUG
 109     cleanup_item->cleanup_fn_name = strdup (fn_name);
 110     assert (NULL != cleanup_item->cleanup_fn_name);
 111 #else
 112     (void) fn_name;
 113 #endif
 114 
 115     opal_mutex_lock (&opal_finalize_cleanup_fns_lock);
 116     opal_list_append (&current_finalize_domain->super, &cleanup_item->super);
 117     opal_mutex_unlock (&opal_finalize_cleanup_fns_lock);
 118 }
 119 
 120 void opal_finalize_domain_init (opal_finalize_domain_t *domain, const char *domain_name)
 121 {
 122     free (domain->domain_name);
 123     domain->domain_name = domain_name ? strdup (domain_name) : NULL;
 124 }
 125 
 126 void opal_finalize_set_domain (opal_finalize_domain_t *domain)
 127 {
 128     current_finalize_domain = domain;
 129 }
 130 
 131 void opal_finalize_cleanup_domain (opal_finalize_domain_t *domain)
 132 {
 133     opal_cleanup_fn_item_t *cleanup_item, *next;
 134     /* call any registered cleanup functions before tearing down OPAL */
 135     OPAL_LIST_FOREACH_SAFE_REV(cleanup_item, next, &domain->super, opal_cleanup_fn_item_t) {
 136         cleanup_item->cleanup_fn (cleanup_item->user_data);
 137         opal_list_remove_item (&domain->super, &cleanup_item->super);
 138         OBJ_RELEASE(cleanup_item);
 139     }
 140 }
 141 
 142 int opal_finalize_util (void)
 143 {
 144     if (--opal_util_initialized != 0) {
 145         if (opal_util_initialized < 0) {
 146             return OPAL_ERROR;
 147         }
 148         return OPAL_SUCCESS;
 149     }
 150 
 151     opal_finalize_cleanup_domain (&opal_init_util_domain);
 152     OBJ_DESTRUCT(&opal_init_util_domain);
 153 
 154     /* finalize the class/object system */
 155     opal_class_finalize();
 156 
 157     free (opal_process_info.nodename);
 158     opal_process_info.nodename = NULL;
 159 
 160     return OPAL_SUCCESS;
 161 }
 162 
 163 
 164 int opal_finalize(void)
 165 {
 166     if (--opal_initialized != 0) {
 167         if (opal_initialized < 0) {
 168             return OPAL_ERROR;
 169         }
 170         return OPAL_SUCCESS;
 171     }
 172 
 173     opal_finalize_cleanup_domain (&opal_init_domain);
 174     OBJ_DESTRUCT(&opal_init_domain);
 175 
 176     /* finalize util code */
 177     opal_finalize_util();
 178 
 179     return OPAL_SUCCESS;
 180 }
 181 
 182 static bool fork_warning_issued = false;
 183 static bool atfork_called = false;
 184 
 185 static void warn_fork_cb(void)
 186 {
 187     if (opal_initialized && !fork_warning_issued) {
 188         opal_show_help("help-opal-runtime.txt", "opal_init:warn-fork", true,
 189                        OPAL_NAME_PRINT(OPAL_PROC_MY_NAME), getpid());
 190         fork_warning_issued = true;
 191     }
 192 }
 193 
 194 void opal_warn_fork(void)
 195 {
 196     if (opal_warn_on_fork && !atfork_called) {
 197         pthread_atfork(warn_fork_cb, NULL, NULL);
 198         atfork_called = true;
 199     }
 200 }

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