Drizzled Public API Documentation

my_thr_init.cc
00001 /* Copyright (C) 2000 MySQL AB
00002 
00003    This program is free software; you can redistribute it and/or modify
00004    it under the terms of the GNU General Public License as published by
00005    the Free Software Foundation; version 2 of the License.
00006 
00007    This program is distributed in the hope that it will be useful,
00008    but WITHOUT ANY WARRANTY; without even the implied warranty of
00009    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010    GNU General Public License for more details.
00011 
00012    You should have received a copy of the GNU General Public License
00013    along with this program; if not, write to the Free Software
00014    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
00015 
00016 /*
00017   Functions to handle initializating and allocationg of all mysys & debug
00018   thread variables.
00019 */
00020 
00021 #include <config.h>
00022 
00023 #include <drizzled/internal/my_sys.h>
00024 #include <drizzled/internal/thread_var.h>
00025 #include <drizzled/internal/m_string.h>
00026 
00027 #include <cstdio>
00028 #include <signal.h>
00029 
00030 #if TIME_WITH_SYS_TIME
00031 # include <sys/time.h>
00032 # include <time.h>
00033 #else
00034 # if HAVE_SYS_TIME_H
00035 #  include <sys/time.h>
00036 # else
00037 #  include <time.h>
00038 # endif
00039 #endif
00040 
00041 #include <boost/thread/thread.hpp>
00042 #include <boost/thread/mutex.hpp>
00043 #include <boost/thread/tss.hpp>
00044 
00045 namespace drizzled {
00046 namespace internal {
00047 
00048 thread_local_st THR_KEY_mysys;
00049 boost::mutex THR_LOCK_threads;
00050 
00051 static uint64_t thread_id= 0;
00052 
00053 /*
00054   Allocate thread specific memory for the thread, used by mysys
00055 
00056   SYNOPSIS
00057     my_thread_init()
00058 
00059   RETURN
00060     0  ok
00061     1  Fatal error; mysys/dbug functions can't be used
00062 */
00063 
00064 void my_thread_init()
00065 {
00066   // We should never see my_thread_init() called twice
00067   if (THR_KEY_mysys.get())
00068   {
00069     abort();
00070   }
00071   boost::mutex::scoped_lock scopedLock(THR_LOCK_threads);
00072   THR_KEY_mysys.reset(new st_my_thread_var(++thread_id));
00073 }
00074 
00075 thread_local_st& my_thread_var2()
00076 {
00077   return THR_KEY_mysys;
00078 }
00079 
00080 } /* namespace internal */
00081 } /* namespace drizzled */