public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* eh-mt problems
@ 1997-12-18  3:28 Andrey Slepuhin
  1997-12-18 10:05 ` scott snyder
  1998-01-15 16:30 ` H.J. Lu
  0 siblings, 2 replies; 3+ messages in thread
From: Andrey Slepuhin @ 1997-12-18  3:28 UTC (permalink / raw)
  To: tot; +Cc: egcs

Hi Teemu,

Here are two letters from Sai-Lai Lo ( mailto:S.Lo@orl.co.uk ), the
maintainer
of omniORB project. They might me interesting for you:

------------------------------------------------------------
Subject: help with egcs-1.0
   Date: Wed, 17 Dec 1997 17:07:25 GMT
   From: Sai-Lai Lo <S.Lo@orl.co.uk>
     To: Andrey Slepuhin <pooh@msu.ru>

Hi Andrey,

I could use some help from you.

I'm trying to get egcs-1.0 running on my alpha linux. I got the eh
thread safe
patch from the mailing list and have modified a couple of configuration
files 
for alpha linux. Now I try the new compiler and have run into trouble
with
the process SEGV when a thread exit.

I've isolated the problem to thread exit with this simple example
attached.

The example spawns three threads, and each of them throw an exception,
catch and then exit. Looking at the output, I always get SEGV when the
second thread exit. Here is the output I got from the program. The
tracing
inside omnithread has been turned on:

% ./prog
omni_thread::init: posix 1003.4a/1003.1c (draft 10) implementation
initialising
initial thread 0
A ctor
B ctor
A ctor
omni_thread_wrapper: thread 1 started
omni_thread_wrapper: thread 2 started
omni_thread_wrapper: thread 3 started
g1 throw exc 10
f1 got exc 10
A got exc 10
omni_thread::exit: thread 1 detached 1 return value (nil)
A dtor
destructor called for thread 1
g1 throw exc 10
f1 got exc 10
A got exc 10
omni_thread::exit: thread 3 detached 1 return value (nil)
A dtor
destructor called for thread 3
zsh: segmentation fault  ./prog
% 


It looks to me the cleanup code in the libgcc runtime is crashing when
it
is called the second time for a different thread.

Can you run the example on AIX with egcs and see if you get the same
result?

Any idea as to where I should look for the problem is appreciated.

By the way, if there is no exception throw or catch in the threads,
everything is fine.

Sai-Lai



------- prog.c ----------
#include <iostream.h>
#include <omnithread.h>

static void f1();
static void f2();
static void g1();
static void g2();

class A : public omni_thread {
public:
  A() {
    cerr << "A ctor" << endl;
    start();
  }
  ~A() {
    cerr << "A dtor" << endl;
  }
  virtual void run(void* arg);
};

class B : public omni_thread {
public:
  B() {
    cerr << "B ctor" << endl;
    start();
  }
  ~B() {
    cerr << "B dtor" << endl;
  }
  virtual void run(void* arg);
};

typedef int except_t;

void
A::run(void* arg)
{
  int die = 0;
  while (!die) {
    try {
      f1();
    }
    catch (const except_t& exc) {
      cerr << "A got exc " << exc << endl;
      die = 1;
    }
  }
}


void
B::run(void* arg)
{
  int die = 0;
  while (!die) {
    try {
      f2();
    }
    catch (const except_t& exc) {
      cerr << "B got exc " << exc << endl;
      die = 1;
    }
  }
}


static
void
f1()
{
  try {
    g1();
  }
  catch (const except_t& exc) {
    cerr << "f1 got exc " << exc << endl;
    throw;
  }
}

static
void
f2()
{
  try {
    g2();
  }
  catch (const except_t& exc) {
    cerr << "f2 got exc " << exc << endl;
    throw;
  }
}

static
void
g1() {
  omni_thread::sleep(2);
  int exc = 10;
  cerr << "g1 throw exc " << exc << endl;
  throw exc;
}

static 
void
g2() {
  omni_thread::sleep(5);
  int exc = 5;
  cerr << "g2 throw exc " << exc << endl;
  throw exc;
}


int
main(int, char**)
{
  A* a = new A;
  B* b = new B;
  A* a2 = new A;
  omni_thread::sleep(30);
  return 0;
}

------------------------------------------------------------------
------------------------------------------------------------------
Subject: Re: help with egcs-1.0
   Date: Wed, 17 Dec 1997 19:51:21 GMT
   From: Sai-Lai Lo <S.Lo@orl.co.uk>
     To: Andrey Slepuhin <pooh@msu.ru>


I think I found the bug. The destructor of eh_context in libgcc2.c-
eh_context_free(), is called whenever a thread exit. According to posix
thread standard, the destructor should reset the key value to NULL or it
will be called again by the thread system. Linux thread in glibc-2
exhibits
this (correct) behaviour whereas older versions don't. Older version
just
call the destructor once.

The fix is simple:

static void
eh_context_free (void *ptr)
{
  pthread_setspecific(eh_context_key,NULL); <----- new
  if (ptr)
    free (ptr);
}

I guess pthread on AIX is similar to old version of linux thread, i.e.
it
calls the destructor only once. That is why you are not seeing the
problem
I have.

I now seem to be able to run all the tests. Looks pretty promising.


Sai-Lai
---------------------------------------------------------------------------

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: eh-mt problems
  1997-12-18  3:28 eh-mt problems Andrey Slepuhin
@ 1997-12-18 10:05 ` scott snyder
  1998-01-15 16:30 ` H.J. Lu
  1 sibling, 0 replies; 3+ messages in thread
From: scott snyder @ 1997-12-18 10:05 UTC (permalink / raw)
  To: egcs, Andrey Slepuhin

>I'm trying to get egcs-1.0 running on my alpha linux. I got the eh
>thread safe
>patch from the mailing list and have modified a couple of configuration
>files 
>for alpha linux. Now I try the new compiler and have run into trouble
>with
>the process SEGV when a thread exit.

If you're using version 0.6 of linuxthreads, there's a bug in the
destructor feature of the thread specific data stuff, which can cause
the destructor to be called multiple times.  This causes the mt-eh
code to call free() multiple times on the same block of memory,
corrupting the heap.

It looks like this problem is fixed in the development version
of linuxthreads.  In the meantime, here's a patch to linuxthreads 0.6
which fixes this problem for me.  (I've only tested it on a i386
platform, though.)

sss

--- specific.c-orig     Thu Dec  4 16:45:30 1997
+++ specific.c  Thu Dec  4 16:47:05 1997
@@ -113,8 +113,9 @@
          data = self->p_specific[i];
          if (destr != NULL && data != NULL)
            {
+              self->p_specific[i] = NULL;
              destr (data);
-             found_nonzero |= self->p_specific[i] != NULL;
+             found_nonzero |= 1;
            }
        }
 

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: eh-mt problems
  1997-12-18  3:28 eh-mt problems Andrey Slepuhin
  1997-12-18 10:05 ` scott snyder
@ 1998-01-15 16:30 ` H.J. Lu
  1 sibling, 0 replies; 3+ messages in thread
From: H.J. Lu @ 1998-01-15 16:30 UTC (permalink / raw)
  To: egcs; +Cc: tot, S.Lo

> 
> Hi Teemu,
> 
> Here are two letters from Sai-Lai Lo ( mailto:S.Lo@orl.co.uk ), the
> maintainer
> of omniORB project. They might me interesting for you:
> 
> 
> % ./prog
> omni_thread::init: posix 1003.4a/1003.1c (draft 10) implementation
> initialising
> initial thread 0
> A ctor
> B ctor
> A ctor
> omni_thread_wrapper: thread 1 started
> omni_thread_wrapper: thread 2 started
> omni_thread_wrapper: thread 3 started
> g1 throw exc 10
> f1 got exc 10
> A got exc 10
> omni_thread::exit: thread 1 detached 1 return value (nil)
> A dtor
> destructor called for thread 1
> g1 throw exc 10
> f1 got exc 10
> A got exc 10
> omni_thread::exit: thread 3 detached 1 return value (nil)
> A dtor
> destructor called for thread 3
> zsh: segmentation fault  ./prog
> % 
> 

FWIW, I tried this code on Linux/x86/glibc 2. I got

# gcc -I/opt/omni/include prog.cc -L/opt/omni/lib -lomnithread -lstdc++
# ldd a.out
        libomnithread.so.2 => /opt/omni/lib/libomnithread.so.2 (0x40016000)
        libstdc++.so.2.8 => /usr/lib/libstdc++.so.2.8 (0x4001c000)
        libc.so.6 => /lib/libc.so.6 (0x40069000)
        /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)
        libpthread.so.0 => /lib/libpthread.so.0 (0x40142000)
        libm.so.6 => /lib/libm.so.6 (0x4014e000)
# a.out
A ctor
B ctor
A ctor
g1 throw exc 10
f1 got exc 10
A got exc 10
A dtor
g1 throw exc 10
f1 got exc 10
A got exc 10
A dtor
g2 throw exc 5
f2 got exc 5
B got exc 5
B dtor
#

BTW, I am using egcs 980113 in the egcs CVS repository.


H.J.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~1998-01-15 16:30 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-12-18  3:28 eh-mt problems Andrey Slepuhin
1997-12-18 10:05 ` scott snyder
1998-01-15 16:30 ` H.J. Lu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).