mypthread.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2006, Trent Waddington
00003  *
00004  * See the file "LICENSE.TERMS" for information on usage and
00005  * redistribution of this file, and for a DISCLAIMER OF ALL
00006  * WARRANTIES.
00007  *
00008  */
00009 
00010 
00011 /*
00012  * This file contains a truely diabolical hack to make Qt4 threads work 
00013  * nicely with the Boehm garbage collector.  It works by exporting a 
00014  * pthread_create symbol that Qt4 links to.  If the start routine is not
00015  * GC_start_routine, it calls the garbage collector to create the thread,
00016  * otherwise it dynamically loads libpthread and calls the real 
00017  * pthread_create.
00018  *
00019  * This prevents us from having to rebuild Qt4 with 
00020  *
00021  * #define GC_THREADS
00022  * #include <gc/gc.h>
00023  *
00024  * in qthread_unix.cpp, which would be the sane way to fix this problem.
00025  *
00026  *  - trentw
00027  */
00028 
00029 #include <stdlib.h>
00030 #include <stdio.h>
00031 #include <dlfcn.h>
00032 
00033 extern "C" void *GC_start_routine(void * arg);
00034 
00035 typedef void *pthread_create_type(void *a, void *b, void *c, void *d);
00036 
00037 extern "C" void *GC_pthread_create(void *, void *, void *, void *);
00038 
00039 void *pthread_lib = NULL;
00040 pthread_create_type *orig = NULL;
00041 
00042 extern "C" void *pthread_create(void *a, void *b, void *c, void *d)
00043 {
00044     if (c != (void*)GC_start_routine)
00045         return GC_pthread_create(a, b, c, d);
00046     else {
00047         if (pthread_lib == NULL) {
00048             pthread_lib = dlopen("libpthread.so.0", RTLD_LAZY);
00049             if (pthread_lib == NULL) {
00050                 printf("cannot dynamically open pthreads %s.\n", dlerror());
00051                 exit(1);
00052             }
00053             orig = (pthread_create_type*)dlsym(pthread_lib, "pthread_create");
00054             if (orig == NULL) {
00055                 printf("cannot find symbol pthread_create %s.\n", dlerror());
00056                 exit(1);
00057             }
00058         }
00059         return (*orig)(a, b, c, d);
00060     }
00061 }
00062 
00063 

Generated on Tue Sep 19 21:18:25 2006 for Boomerang by  doxygen 1.4.6