[PATCH] New patch: Minimal stack sizes for threads



Soeren Sandmann <sandmann@daimi.au.dk> writes:

> On some systems there is a minimal stack size for threads. This size
> can be obtained with the sysconf function. The following patch changes
> gthread-posix.c and ghtread-solaris.c to use this.

> 
> Index: gthread/gthread-posix.c
> ===================================================================

This patch unnecessaryly checked the minimal stack size for *every*
g_thread_create.

Making sure that the thread size is above the minimum is necessary at
least on Irix, where the program

#include <glib.h>

static void
a (gpointer value)
{
  while (1)
    g_print ("a");
}

gint 
main ()
{
  g_thread_init (NULL);
  g_thread_create (a, NULL, 8192, FALSE, FALSE, G_THREAD_PRIORITY_LOW);
  return 0;
}

fails with

GThread-ERROR **: file gthread-posix.c: line 231 (g_thread_create_posix_impl): error Invalid argument during pthread_attr_setstacksize (&attr, stack_size)
aborting...
Trace/BPT trap

This patch only checks sysconf() once (in g_thread_init):

Index: gthread-impl.c
===================================================================
RCS file: /cvs/gnome/glib/gthread/gthread-impl.c,v
retrieving revision 1.2
diff -u -r1.2 gthread-impl.c
--- gthread-impl.c	1999/10/04 02:32:50	1.2
+++ gthread-impl.c	2000/02/21 15:34:29
@@ -34,6 +34,9 @@
 #ifdef HAVE_CONFIG_H
 #include <config.h>
 #endif
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
 
 #include <glib.h>
 
@@ -41,6 +44,7 @@
 static gint g_thread_map_priority (GThreadPriority priority);
 static gint g_thread_min_priority = 0;
 static gint g_thread_max_priority = 0;
+static gulong g_thread_min_stack_size = 0;
 
 #include G_THREAD_SOURCE
 
@@ -101,6 +105,12 @@
       else
 	g_error ("The supplied thread function vector is invalid.");
     }
+
+  /* on some sytems there is a minimum stack size for threads 
+   */
+#ifdef _SC_THREAD_STACK_MIN
+  g_thread_min_stack_size = MAX (sysconf (_SC_THREAD_STACK_MIN), 0);
+#endif
 
   /* now do any initialization stuff required by the implementation,
      but only if called with a NULL argument, of course. Otherwise it's
Index: gthread-posix.c
===================================================================
RCS file: /cvs/gnome/glib/gthread/gthread-posix.c,v
retrieving revision 1.13
diff -u -r1.13 gthread-posix.c
--- gthread-posix.c	1999/11/16 10:29:27	1.13
+++ gthread-posix.c	2000/02/21 15:34:30
@@ -228,7 +228,10 @@
   
 #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
   if (stack_size)
+    {
+      stack_size = MAX (g_thread_min_stack_size, stack_size);
       posix_check_for_error (pthread_attr_setstacksize (&attr, stack_size));
+    }
 #endif /* HAVE_PTHREAD_ATTR_SETSTACKSIZE */
 
 #ifdef PTHREAD_SCOPE_SYSTEM
Index: gthread-solaris.c
===================================================================
RCS file: /cvs/gnome/glib/gthread/gthread-solaris.c,v
retrieving revision 1.6
diff -u -r1.6 gthread-solaris.c
--- gthread-solaris.c	1999/11/16 10:29:27	1.6
+++ gthread-solaris.c	2000/02/21 15:34:30
@@ -193,6 +193,7 @@
   
   g_return_if_fail (thread_func);
   
+  stack_size = MAX (g_thread_min_stack_size, stack_size);
   solaris_check_for_error (thr_create (NULL, stack_size,  
 				       (void* (*)(void*))thread_func,
 				       arg, flags, thread));



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]