A multi-thread contention issue when using lt_dlinit function
xiaonan <[email protected]> Thu, 11 Apr 2013 14:24:47 +0800 (CST)
| Newsgroups | gmane.comp.db.unixodbc.devel |
|---|---|
| Message-ID | <[email protected]> |
Hi, Nick:
Sorry for interrupting you again!
Because libltdl functions are not thread-safe (http://www.gnu.org/software/libtool/manual/html_node/Thread-Safety-in-libltdl.html), we should add lock before using them.
In __connect_part_one function:
{
......
/*
* initialize libtool
*/
lt_dlinit();
......
if ( !(connection -> dl_handle = odbc_dlopen( driver_lib )))
{
......
}
}
int lt_dlinit (void)
{
......
if (++initialized == 1)
{
......
}
}
In multi-thread environment, there will be a scenario: Thread 1 calls lt_dlinit(), and the initialized's value is 1. Then Thread 2 calls lt_dlinit(), and the initialized's value is changed to 2. Thread 2 thinks the initializtion is OK, so it will continue executing and calls odbc_dlopen(). But in fact, the initialization isn't done! So odbc_dlopen() will return NULL.
So I think we should add lock before using lt_dlinit(), such as:
mutex_lib_entry();
lt_dlinit();
mutex_lib_exit();
Is it OK? Thanks very much!
Best Regards
Nan Xiao
_______________________________________________
unixODBC-dev mailing list
[email protected]
http://mailman.unixodbc.org/mailman/listinfo/unixodbc-dev