diff --git a/mysys/my_thr_init.c b/mysys/my_thr_init.c index 5d1376e..f3a9fe7 100644 --- a/mysys/my_thr_init.c +++ b/mysys/my_thr_init.c @@ -141,6 +141,7 @@ my_bool my_thread_global_init(void) return 0; my_thread_global_init_done= 1; + my_sleep(1000); #ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP /* Set mutex type to "fast" a.k.a "adaptive" diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 8bd6320..4ef5406 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -27,4 +27,8 @@ IF(WITH_UNIT_TESTS) SET_TARGET_PROPERTIES(bug25714 PROPERTIES LINKER_LANGUAGE CXX) ENDIF() +ADD_EXECUTABLE(thread_test2 thread_test2.c) +TARGET_LINK_LIBRARIES(thread_test2 mysqlclient) +SET_TARGET_PROPERTIES(thread_test2 PROPERTIES LINKER_LANGUAGE CXX) + INSTALL(TARGETS mysql_client_test DESTINATION ${INSTALL_BINDIR} COMPONENT Test) diff --git a/tests/thread_test2.c b/tests/thread_test2.c new file mode 100644 index 0000000..678e5b1 --- /dev/null +++ b/tests/thread_test2.c @@ -0,0 +1,97 @@ +#include + +#include +#include +#include "mysql.h" +#include + +const char *user="root"; +const char *unix_socket="../mysql-test/var/tmp/mysqld.1.sock"; + +static uint thread_count,number_of_threads=2; +static pthread_cond_t COND_thread_count; +static pthread_mutex_t LOCK_thread_count; + +void *test_thread(void *arg __attribute__((unused))) +{ + MYSQL *mysql; + + mysql=mysql_init(NULL); + if (!mysql_real_connect(mysql,NULL,user,NULL,NULL,0,unix_socket,0)) + { + fprintf(stderr,"Couldn't connect to engine!\n%s\n\n",mysql_error(mysql)); + perror(""); + } + mysql_close(mysql); + pthread_mutex_lock(&LOCK_thread_count); + thread_count--; + pthread_cond_signal(&COND_thread_count); /* Tell main we are ready */ + pthread_mutex_unlock(&LOCK_thread_count); + pthread_exit(0); + return 0; +} + + +int main(int argc __attribute__((unused)), char **argv __attribute__((unused))) +{ + pthread_t tid; + pthread_attr_t thr_attr; + uint i; + int error; + + if ((error=pthread_cond_init(&COND_thread_count,NULL))) + { + fprintf(stderr,"Got error: %d from pthread_cond_init (errno: %d)", + error,errno); + exit(1); + } + pthread_mutex_init(&LOCK_thread_count,MY_MUTEX_INIT_FAST); + + if ((error=pthread_attr_init(&thr_attr))) + { + fprintf(stderr,"Got error: %d from pthread_attr_init (errno: %d)", + error,errno); + exit(1); + } + if ((error=pthread_attr_setdetachstate(&thr_attr,PTHREAD_CREATE_DETACHED))) + { + fprintf(stderr, + "Got error: %d from pthread_attr_setdetachstate (errno: %d)", + error,errno); + exit(1); + } + + printf("Init ok. Creating %d threads\n",number_of_threads); + for (i=1 ; i <= number_of_threads ; i++) + { + uint *param= &i; + + pthread_mutex_lock(&LOCK_thread_count); + if ((error=pthread_create(&tid,&thr_attr,test_thread,(void*) param))) + { + fprintf(stderr,"\nGot error: %d from pthread_create (errno: %d) when creating thread: %i\n", + error,errno,i); + pthread_mutex_unlock(&LOCK_thread_count); + exit(1); + } + thread_count++; + pthread_mutex_unlock(&LOCK_thread_count); + } + + printf("Waiting for threads to finnish\n"); + error=pthread_mutex_lock(&LOCK_thread_count); + while (thread_count) + { + if ((error=pthread_cond_wait(&COND_thread_count,&LOCK_thread_count))) + fprintf(stderr,"\nGot error: %d from pthread_cond_wait\n",error); + } + pthread_mutex_unlock(&LOCK_thread_count); + pthread_attr_destroy(&thr_attr); + printf("\nend\n"); + + return 0; + + exit(0); + return 0; /* Keep some compilers happy */ +} +