dynamicly link pthread stack functions if they're not available at link time

Mike McCormack mike at codeweavers.com
Mon Apr 19 03:49:06 CDT 2004


ChangeLog:
* dynamicly link pthread stack functions if they're not available at 
link time
-------------- next part --------------
Index: loader/pthread.c
===================================================================
RCS file: /home/wine/wine/loader/pthread.c,v
retrieving revision 1.8
diff -u -r1.8 pthread.c
--- loader/pthread.c	30 Mar 2004 05:13:35 -0000	1.8
+++ loader/pthread.c	19 Apr 2004 07:49:57 -0000
@@ -25,6 +25,7 @@
 
 #include <assert.h>
 #include <stdlib.h>
+#include <stdio.h>
 #include <signal.h>
 #ifdef HAVE_UNISTD_H
 # include <unistd.h>
@@ -44,6 +45,23 @@
 static pthread_key_t teb_key;
 #endif
 
+#ifndef HAVE_PTHREAD_GETATTR_NP
+
+extern int pthread_getattr_np (pthread_t, pthread_attr_t *);
+extern int pthread_attr_getstack (const pthread_attr_t *, void **, size_t *);
+
+typeof (pthread_getattr_np) *fn_pthread_getattr_np;
+typeof (pthread_attr_getstack) *fn_pthread_attr_getstack;
+void * plibc;
+
+#else
+
+#define fn_pthread_getattr_np     pthread_getattr_np
+#define fn_pthread_attr_getstack  pthread_attr_getstack
+
+#endif
+
+
 /***********************************************************************
  *           wine_pthread_init_process
  *
@@ -52,6 +70,18 @@
 void wine_pthread_init_process( const struct wine_pthread_functions *functions )
 {
     memcpy( &funcs, functions, min(functions->size,sizeof(funcs)) );
+#ifndef HAVE_PTHREAD_GETATTR_NP
+    plibc = dlopen("libc.so",RTLD_LAZY);
+    if( plibc )
+    {
+        fn_pthread_getattr_np = (typeof (pthread_getattr_np)*) 
+                                dlsym(plibc, "pthread_getattr_np");
+        fn_pthread_attr_getstack = (typeof (pthread_attr_getstack)*) 
+                                dlsym(plibc, "pthread_attr_getstack");
+    }
+    if( ! ( plibc && fn_pthread_getattr_np && fn_pthread_attr_getstack ) )
+        fprintf(stderr,"Couldn't bind pthread stack functions\n");
+#endif
 }
 
 
@@ -65,17 +95,23 @@
     /* retrieve the stack info (except for main thread) */
     if (funcs.ptr_set_thread_data)
     {
-#ifdef HAVE_PTHREAD_GETATTR_NP
-        pthread_attr_t attr;
-        pthread_getattr_np( pthread_self(), &attr );
-        pthread_attr_getstack( &attr, &info->stack_base, &info->stack_size );
-#else
-        /* assume that the stack allocation is page aligned */
-        char dummy;
-        size_t page_size = getpagesize();
-        char *stack_top = (char *)((unsigned long)&dummy & ~(page_size - 1)) + page_size;
-        info->stack_base = stack_top - info->stack_size;
+#ifndef HAVE_PTHREAD_GETATTR_NP
+        if( ! (pthread_getattr_np&&pthread_attr_getstack) )
+        {
+            /* assume that the stack allocation is page aligned */
+            char dummy;
+            size_t page_size = getpagesize();
+            char *stack_top = (char *)((unsigned long)&dummy & ~(page_size - 1)) + page_size;
+            stack_top += page_size;  /* hack: add an extra page for the thread descriptor */
+            info->stack_base = stack_top - info->stack_size;
+        }
+        else
 #endif
+        {
+            pthread_attr_t attr;
+            fn_pthread_getattr_np( pthread_self(), &attr );
+            fn_pthread_attr_getstack( &attr, &info->stack_base, &info->stack_size );
+        }
     }
 }
 


More information about the wine-patches mailing list