From d08e23770f827c9f71ef2ae30bfd52dcd25d0206 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 10 Sep 2010 11:29:24 +0200 Subject: Initialize and terminate mysql library --- odb/mysql/connection-factory.cxx | 60 ++++++++++++++++++++++++++++++++-------- 1 file changed, 49 insertions(+), 11 deletions(-) diff --git a/odb/mysql/connection-factory.cxx b/odb/mysql/connection-factory.cxx index 41ff2fd..645d03a 100644 --- a/odb/mysql/connection-factory.cxx +++ b/odb/mysql/connection-factory.cxx @@ -3,6 +3,8 @@ // copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC // license : GNU GPL v2; see accompanying LICENSE file +#include // abort + #include #include #include @@ -21,26 +23,62 @@ namespace odb { namespace { - struct mysql_init + static bool main_thread_init_; + + struct mysql_thread_init { #ifndef ODB_THREADS_NONE - mysql_init () + mysql_thread_init () + : init_ (false) { - if (mysql_thread_init ()) + if (!main_thread_init_) { - throw database_exception ( - CR_UNKNOWN_ERROR, "?????", "thread initialization failed"); + if (::mysql_thread_init ()) + { + throw database_exception ( + CR_UNKNOWN_ERROR, "?????", "thread initialization failed"); + } + + init_ = true; } } - ~mysql_init () + ~mysql_thread_init () { - mysql_thread_end (); + if (init_) + mysql_thread_end (); } + + private: + bool init_; #endif }; - static ODB_TLS_OBJECT (mysql_init) mysql_init_; + static ODB_TLS_OBJECT (mysql_thread_init) mysql_thread_init_; + + struct mysql_process_init + { + mysql_process_init () + { + if (mysql_library_init (0 ,0, 0)) + abort (); + + main_thread_init_ = true; + tls_get (mysql_thread_init_); + main_thread_init_ = false; + } + + ~mysql_process_init () + { + // Finalize the main thread now in case TLS destruction + // doesn't happen for the main thread. + // + tls_free (mysql_thread_init_); + mysql_library_end (); + } + }; + + static mysql_process_init mysql_process_init_; } // @@ -59,7 +97,7 @@ namespace odb shared_ptr new_connection_factory:: connect () { - tls_get (mysql_init_); + tls_get (mysql_thread_init_); return shared_ptr (new (shared) connection (*db_)); } @@ -92,7 +130,7 @@ namespace odb shared_ptr connection_pool_factory:: connect () { - tls_get (mysql_init_); + tls_get (mysql_thread_init_); lock l (mutex_); @@ -130,7 +168,7 @@ namespace odb void connection_pool_factory:: database (database_type& db) { - tls_get (mysql_init_); + tls_get (mysql_thread_init_); db_ = &db; -- cgit v1.1