My understanding of what's going on under the hood of the mysqlclient_r library 
is fuzzy a best.  I based my code changes on some very poor documentation 
regarding threading from the MySQL folks.  The indication was that you call 
mysql_thread_start() when you begin using the library in a thread and then 
mysql_thread_end() when you are finished.  However, I'm told 
mysql_thread_start() is called for you when you open a connection on the _r 
version of the library so I figured calling mysql_thread_end() on connection 
close with in libdbi would balance that nicely if there are no other 
mysql_thread_start() calls.

The problem is, if you use the same connection on more than one thread, you 
need to call mysql_thread_start() at the start and mysql_thread_end() at the 
end of each threads usage, and there is no way for this to be done with in the 
lidbdi API.  There is no way to tell libdbi about the new thread usage such 
that it can pass that down to libmysqlclient, so I just deal with it at the 
connection level. 

Even with libdbi linked against the libmysqlclient_r library, the API is not 
thread safe with out the mysql_thread_start() and mysql_thread_end() calls on a 
**shared** connection, so including my change would not **hurt**.   Linking 
agains the non _r library and you can't even run different connection on 
different threads. 

Ethan...

On Sep 1, 2010, at 3:20 PM, Markus Hoenicka wrote:

> Ethan Funk writes:
>> 2. Modify the dbd_mysql.c source file, changing dbd_disconnect as follows:
>> 
>> int dbd_disconnect(dbi_conn_t *conn) {
>>      if (conn->connection) mysql_close((MYSQL *)conn->connection);
>>      // added the next three lines to resolve memory leak in threadsafe 
>> mysqlclient library: assumes each thread has it's own connection
>>      if(mysql_thread_safe())
>>              mysql_thread_end();
>>      return 0;
>> }
> 
> Is this generally applicable, or would that hurt if several threads
> share a connection? I wonder if this should be applied to the driver
> sources.
> 
>> 
>> 3. build and link the dbd_mysql driver.  It MUST be linked agains the _r 
>> version of the mysqlclientlib.  This was really tricky on OS X.
>> 
> 
> I've added an --enable-mysql-threadsafe option to ./configure which
> allows to optionally link against libmysqlclient_r instead of
> libmysqlclient.
> 
> regards,
> Markus
> 
> -- 
> Markus Hoenicka
> http://www.mhoenicka.de
> AQ score 38


------------------------------------------------------------------------------
This SF.net Dev2Dev email is sponsored by:

Show off your parallel programming skills.
Enter the Intel(R) Threading Challenge 2010.
http://p.sf.net/sfu/intel-thread-sfd
_______________________________________________
Libdbi-drivers-devel mailing list
Libdbi-drivers-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/libdbi-drivers-devel

Reply via email to