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 {
00047 namespace internal
00048 {
00049 
00050 boost::thread_specific_ptr<st_my_thread_var> THR_KEY_mysys;
00051 boost::mutex THR_LOCK_threads;
00052 
00053 /*
00054   initialize thread environment
00055 
00056   SYNOPSIS
00057     my_thread_global_init()
00058 
00059   RETURN
00060     0  ok
00061     1  error (Couldn't create THR_KEY_mysys)
00062 */
00063 
00064 bool my_thread_global_init(void)
00065 {
00066   if (my_thread_init())
00067   {
00068     my_thread_global_end();     /* Clean up */
00069     return 1;
00070   }
00071   return 0;
00072 }
00073 
00074 
00075 void my_thread_global_end(void)
00076 {
00077 }
00078 
00079 static uint64_t thread_id= 0;
00080 
00081 /*
00082   Allocate thread specific memory for the thread, used by mysys
00083 
00084   SYNOPSIS
00085     my_thread_init()
00086 
00087   RETURN
00088     0  ok
00089     1  Fatal error; mysys/dbug functions can't be used
00090 */
00091 
00092 bool my_thread_init(void)
00093 {
00094   // We should mever see my_thread_init()  called twice
00095   if (THR_KEY_mysys.get())
00096     return 0;
00097 
00098   st_my_thread_var *tmp= new st_my_thread_var;
00099   if (tmp == NULL)
00100   {
00101     return true;
00102   }
00103   THR_KEY_mysys.reset(tmp);
00104 
00105   boost::mutex::scoped_lock scopedLock(THR_LOCK_threads);
00106   tmp->id= ++thread_id;
00107 
00108   return false;
00109 }
00110 
00111 
00112 /*
00113   Deallocate memory used by the thread for book-keeping
00114 
00115   SYNOPSIS
00116     my_thread_end()
00117 
00118   NOTE
00119     This may be called multiple times for a thread.
00120     This happens for example when one calls 'server_init()'
00121     server_end() and then ends with a end().
00122 */
00123 
00124 void my_thread_end(void)
00125 {
00126 }
00127 
00128 struct st_my_thread_var *_my_thread_var(void)
00129 {
00130   return THR_KEY_mysys.get();
00131 }
00132 
00133 } /* namespace internal */
00134 } /* namespace drizzled */