00001 /* -*- mode:C++; c-basic-offset:4 -*- 00002 Shore-MT -- Multi-threaded port of the SHORE storage manager 00003 00004 Copyright (c) 2007-2009 00005 Data Intensive Applications and Systems Labaratory (DIAS) 00006 Ecole Polytechnique Federale de Lausanne 00007 00008 All Rights Reserved. 00009 00010 Permission to use, copy, modify and distribute this software and 00011 its documentation is hereby granted, provided that both the 00012 copyright notice and this permission notice appear in all copies of 00013 the software, derivative works or modified versions, and any 00014 portions thereof, and that both notices appear in supporting 00015 documentation. 00016 00017 This code is distributed in the hope that it will be useful, but 00018 WITHOUT ANY WARRANTY; without even the implied warranty of 00019 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS 00020 DISCLAIM ANY LIABILITY OF ANY KIND FOR ANY DAMAGES WHATSOEVER 00021 RESULTING FROM THE USE OF THIS SOFTWARE. 00022 */ 00023 00024 // -*- mode:c++; c-basic-offset:4 -*- 00025 /*<std-header orig-src='shore' incl-file-exclusion='TLS_CPP'> 00026 00027 $Id: tls.cpp,v 1.3 2010/06/23 23:42:57 nhall Exp $ 00028 00029 SHORE -- Scalable Heterogeneous Object REpository 00030 00031 Copyright (c) 1994-99 Computer Sciences Department, University of 00032 Wisconsin -- Madison 00033 All Rights Reserved. 00034 00035 Permission to use, copy, modify and distribute this software and its 00036 documentation is hereby granted, provided that both the copyright 00037 notice and this permission notice appear in all copies of the 00038 software, derivative works or modified versions, and any portions 00039 thereof, and that both notices appear in supporting documentation. 00040 00041 THE AUTHORS AND THE COMPUTER SCIENCES DEPARTMENT OF THE UNIVERSITY 00042 OF WISCONSIN - MADISON ALLOW FREE USE OF THIS SOFTWARE IN ITS 00043 "AS IS" CONDITION, AND THEY DISCLAIM ANY LIABILITY OF ANY KIND 00044 FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 00045 00046 This software was developed with support by the Advanced Research 00047 Project Agency, ARPA order number 018 (formerly 8230), monitored by 00048 the U.S. Army Research Laboratory under contract DAAB07-91-C-Q518. 00049 Further funding for this work was provided by DARPA through 00050 Rome Research Laboratory Contract No. F30602-97-2-0247. 00051 00052 */ 00053 #include "tls.h" 00054 using namespace tls_tricks; 00055 00056 #include <vector> 00057 #include <utility> 00058 00059 #define STR(x) STR2(x) 00060 #define STR2(x) #x 00061 00062 #define ATTEMPT(action) do { \ 00063 int err = action; \ 00064 if(err != 0) { \ 00065 std::printf("%s failed with error %d\n", #action, err); \ 00066 std::exit(-1); \ 00067 } \ 00068 } while(0) 00069 00070 typedef std::vector<std::pair<void(*)(), void(*)()> > tls_function_list; 00071 00072 static tls_function_list* registered_tls(NULL); 00073 static bool tls_manager_initialized = false; 00074 00075 void tls_manager::global_init() { 00076 if(tls_manager_initialized) 00077 return; 00078 tls_manager_initialized = true; 00079 registered_tls = new tls_function_list; 00080 } 00081 void tls_manager::global_fini() { 00082 if(tls_manager_initialized) { 00083 delete registered_tls; 00084 registered_tls = NULL; 00085 tls_manager_initialized = false; 00086 } 00087 } 00088 00089 void tls_manager::register_tls(void (*init)(), void (*fini)()) { 00090 /* init the tls for the current (main) thread, then add it to the 00091 pile for others which follow 00092 */ 00093 (*init)(); 00094 registered_tls->push_back(std::make_pair(init, fini)); 00095 } 00096 /**\var static __thread bool tls_manager::_thread_initialized; 00097 * \ingroup TLS 00098 */ 00099 __thread bool tls_manager::_thread_initialized(false); 00100 00101 // called from sthread.cpp just inside sthread_t::_start() 00102 void tls_manager::thread_init() { 00103 if(_thread_initialized) 00104 return; 00105 _thread_initialized = true; 00106 for(tls_function_list::iterator it=registered_tls->begin(); it != registered_tls->end(); ++it) 00107 (*it->first)(); 00108 } 00109 00110 // called from sthread.cpp at end of sthread_t::_start() 00111 void tls_manager::thread_fini() { 00112 if(!_thread_initialized) 00113 return; 00114 _thread_initialized = false; 00115 for(tls_function_list::iterator it=registered_tls->begin(); it != registered_tls->end(); ++it) 00116 (*it->second)(); 00117 }