This is the mail archive of the
libc-alpha@sources.redhat.com
mailing list for the glibc project.
Re: Problem with stack size in pthreads
- To: drepper at cygnus dot com (Ulrich Drepper)
- Subject: Re: Problem with stack size in pthreads
- From: Andreas Jaeger <aj at suse dot de>
- Date: Fri, 13 Jul 2001 12:09:51 +0200
- Cc: libc-alpha at sources dot redhat dot com, Hui dot Huang at sun dot com
- References: <hod776crmw.fsf@gee.suse.de> <m3itgyvx9a.fsf@otr.mynet>
Ulrich Drepper <drepper@redhat.com> writes:
> Andreas Jaeger <aj@suse.de> writes:
>
>> If the stack pointer of the initial thread in function thread_self
>> (from internals.h) is below __pthread_initial_thread_bos, it's not
>> considered in the initial thread.
>
> This is not supposed to happen because we made a setrlimit() call.
> Testcase?
Here's one. It creates first some data on the stack before calling
the first thread.
With the LDT implementation on i686, I get:
$ ./jdk-thread
rlim.rlim_cur = RLIM_INFINITY
Thread 400: allocated key 1
Thread 400: allocating buffer at 0x8049da8
starting Process second
Thread 803: allocating buffer at 0x804c198
Thread 803: "Result of second thread"
Thread 803: freeing buffer at 0x804c198
starting Process first
Thread 402: allocating buffer at 0x804c198
Thread 402: "Result of first thread"
Thread 402: freeing buffer at 0x804c198
Thread 400: "Result of initial thread"
And without, on i686:
gee:~/tmp:[0]$ ./jdk-thread
rlim.rlim_cur = RLIM_INFINITY
Segmentation fault
or on powerpc:
cantaloupe:~/tmp:[139]$ gcc -Wall jdk-thread.c -O2 -lpthread -o jdk-thread -g
cantaloupe:~/tmp:[0]$ ./jdk-thread
rlim.rlim_cur = RLIM_INFINITY
Segmentation fault
cantaloupe:~/tmp:[139]$
Andreas
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <sys/time.h>
#include <sys/resource.h>
#include <unistd.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
pthread_key_t key;
/* This is a typical example of a library function that uses
static variables to accumulate results between calls.
Here, it just returns the concatenation of all string arguments
that were given to it. */
/* Of course, this cannot be used in a multi-threaded program
because all threads store "accu" at the same location.
So, we'll use thread-specific data to have a different "accu"
for each thread. */
/* Key identifying the thread-specific data */
static pthread_key_t str_key;
/* "Once" variable ensuring that the key for str_alloc will be allocated
exactly once. */
static pthread_once_t str_alloc_key_once = PTHREAD_ONCE_INIT;
/* Forward functions */
static void str_alloc_key (void);
static void str_alloc_destroy_accu (void *accu);
/* Thread-safe version of str_accumulate */
static char *
str_accumulate (const char *s)
{
char *accu;
/* Make sure the key is allocated */
pthread_once (&str_alloc_key_once, str_alloc_key);
/* Get the thread-specific data associated with the key */
accu = (char *) pthread_getspecific (str_key);
/* It's initially NULL, meaning that we must allocate the buffer first. */
if (accu == NULL)
{
accu = malloc (1024);
if (accu == NULL)
return NULL;
accu[0] = 0;
/* Store the buffer pointer in the thread-specific data. */
pthread_setspecific (str_key, (void *) accu);
printf ("Thread %lx: allocating buffer at %p\n", pthread_self (), accu);
}
/* Now we can use accu just as in the non thread-safe code. */
strcat (accu, s);
return accu;
}
/* Function to allocate the key for str_alloc thread-specific data. */
static void
str_alloc_key (void)
{
pthread_key_create (&str_key, str_alloc_destroy_accu);
printf ("Thread %lx: allocated key %d\n", pthread_self (), str_key);
}
/* Function to free the buffer when the thread exits. */
/* Called only when the thread-specific data is not NULL. */
static void
str_alloc_destroy_accu (void *accu)
{
printf ("Thread %lx: freeing buffer at %p\n", pthread_self (), accu);
free (accu);
}
/* Test program */
static void *
process (void *arg)
{
char *res;
printf ("starting Process %s\n", (char*) arg);
res = str_accumulate ("Result of ");
res = str_accumulate ((char *) arg);
res = str_accumulate (" thread");
printf ("Thread %lx: \"%s\"\n", pthread_self (), res);
return NULL;
}
static void
start_threading (void)
{
char *res;
pthread_t th1, th2;
res = str_accumulate ("Result of ");
pthread_create (&th1, NULL, process, (void *) "first");
pthread_create (&th2, NULL, process, (void *) "second");
res = str_accumulate ("initial thread");
printf ("Thread %lx: \"%s\"\n", pthread_self (), res);
pthread_join (th1, NULL);
pthread_join (th2, NULL);
}
void
enlarge_stack (int i)
{
if (i > 8192)
start_threading ();
else
{
char c[1024];
c [i %1024] = '\0';
enlarge_stack (i+1);
}
}
int
main (void)
{
struct rlimit rlim;
getrlimit(RLIMIT_STACK, &rlim);
if (rlim.rlim_cur == RLIM_INFINITY)
printf ("rlim.rlim_cur = RLIM_INFINITY\n");
else
printf("rlim_cur = %ldk, rlim_max = %ldk\n", rlim.rlim_cur/1024, rlim.rlim_max/1024);
pthread_key_create(&key, NULL);
pthread_setspecific(key, (void *)95);
enlarge_stack (0);
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--
Andreas Jaeger
SuSE Labs aj@suse.de
private aj@arthur.inka.de
http://www.suse.de/~aj