00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include <pthread.h>
00034 #include <stdlib.h>
00035 #include "reincarnation_callback.h"
00036 #include "segment.h"
00037 #include "log/log_i.h"
00038 #include "thrdesc.h"
00039 #include "debug.h"
00040 #include "config.h"
00041
00042
00043 static pthread_mutex_t global_init_lock = PTHREAD_MUTEX_INITIALIZER;
00044
00045
00046
00047 volatile uint32_t mnemosyne_initialized = 0;
00048
00049
00050 __thread mnemosyne_thrdesc_t *_mnemosyne_thr;
00051
00052 static void do_global_init(void) __attribute__(( constructor ));
00053 static void do_global_fini(void) __attribute__(( destructor ));
00054
00055 void
00056 do_global_init(void)
00057 {
00058 pcm_storeset_t* pcm_storeset;
00059 #ifdef _M_STATS_BUILD
00060 struct timeval start_time;
00061 struct timeval stop_time;
00062 unsigned long long op_time;
00063 #endif
00064
00065 if (mnemosyne_initialized) {
00066 return;
00067 }
00068
00069 pcm_storeset = pcm_storeset_get ();
00070
00071 pthread_mutex_lock(&global_init_lock);
00072 if (!mnemosyne_initialized) {
00073 mcore_config_init();
00074 #ifdef _M_STATS_BUILD
00075 gettimeofday(&start_time, NULL);
00076 #endif
00077 m_segmentmgr_init();
00078 mnemosyne_initialized = 1;
00079 mnemosyne_reincarnation_callback_execute_all();
00080 #ifdef _M_STATS_BUILD
00081 gettimeofday(&stop_time, NULL);
00082 op_time = 1000000 * (stop_time.tv_sec - start_time.tv_sec) +
00083 stop_time.tv_usec - start_time.tv_usec;
00084 fprintf(stderr, "reincarnation_latency = %llu (us)\n", op_time);
00085 #endif
00086 m_logmgr_init(pcm_storeset);
00087 M_WARNING("Initialize\n");
00088 }
00089 pthread_mutex_unlock(&global_init_lock);
00090 }
00091
00092
00093 void
00094 do_global_fini(void)
00095 {
00096 if (!mnemosyne_initialized) {
00097 return;
00098 }
00099
00100 pthread_mutex_lock(&global_init_lock);
00101 if (mnemosyne_initialized) {
00102 m_logmgr_fini();
00103 m_segmentmgr_fini();
00104 mtm_fini_global();
00105 mnemosyne_initialized = 0;
00106
00107 M_WARNING("Shutdown\n");
00108 }
00109 pthread_mutex_unlock(&global_init_lock);
00110 }
00111
00112
00113 void
00114 mnemosyne_init_global(void)
00115 {
00116 do_global_init();
00117 }
00118
00119
00120 void
00121 mnemosyne_fini_global(void)
00122 {
00123 do_global_fini();
00124 }
00125
00126
00127 mnemosyne_thrdesc_t *
00128 mnemosyne_init_thread(void)
00129 {
00130 mnemosyne_thrdesc_t *thr = NULL;
00131
00132 if (thr) {
00133 M_WARNING("mnemosyne_init_thread:1: thr = %p\n", thr);
00134 return thr;
00135 }
00136
00137 mnemosyne_init_global();
00138 thr = (mnemosyne_thrdesc_t *) malloc(sizeof(mnemosyne_thrdesc_t));
00139
00140
00141
00142 M_WARNING("mnemosyne_init_thread:2: thr = %p\n", thr);
00143 return thr;
00144 }
00145
00146
00147 void
00148 mnemosyne_fini_thread(void)
00149 {
00150 M_WARNING("mnemosyne_fini_thread:2\n");
00151 }