public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Ulrich Drepper <drepper@cygnus.com>
To: egcs@cygnus.com
Cc: drepper@cygnus.com
Subject: mutex in frame code
Date: Sun, 31 Jan 1999 23:58:00 -0000	[thread overview]
Message-ID: <r2r9sjas6h.fsf@happy.cygnus.com> (raw)
Message-ID: <19990131235800.aNky2kgMwuO-6YxOVn9AXBy4KVtJ2Q-xS25eijzljRk@z> (raw)

We've recently adding to the frame installation functions in frame.c
code to protect the installation of the frame element to a global list
by an semaphore.  E.g., the __register_frame_info is

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void
__register_frame_info (void *begin, struct object *ob)
{
  ob->fde_begin = begin;

  ob->pc_begin = ob->pc_end = 0;
  ob->fde_array = 0;
  ob->count = 0;

  init_object_mutex_once ();
  __gthread_mutex_lock (&object_mutex);

  ob->next = objects;
  objects = ob;

  __gthread_mutex_unlock (&object_mutex);
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

There is a major flaw in this code.  It could use the thread functions
before the thread library is initialized.  E.g., a simple program
using pthreads could would use the libraries libpthread.so and
libc.so.  Due to the dependencies the C library is initialized first.
Part of the initialization is registration of a frame.  But now the C
library initialization will use the thread library though it is not
yet initialized.

This so far lead to no problems since in most thread implementations
the mutex functions do not need special initialization but others do.
In fact, I found this problem because the library I'm writing has this
problem.


Looking at the code one can see that the lock is used to insert into a
single-linked list.  This can be done safely without locks with an
compare & exchange instruction:

	do
	  {
	    struct object *last = objects;
	    ob->next = objects;
	  }
	while (compxchg (ob, last, ob) != 0);

with an appropriate compxchg function.  This function would be very
system-specific so Jason proposed to have a __builtin_cmpxchg function
and put the system specific bits into the .md files.


Now there is the problem that not all processors have such an opcode.
This is where the problems start.  E.g., the i386 processor has no
such opcode while i486 and up do.  I think this is the situation for
some other processor families as well.  Using the opcode nevertheless
would mean that there is not anymore full backward compatibility.
I.e., a gcc compiled for i486 will not work on i386 anymore.  I don't
think this is a problem personally.  We already have similar
restrictions (i686 code cannot be used on earlier processors) and i386
are also not anymore the commonly found platform.


One way to work around not having a compare & exchange opcode is using
spinlocks.  This works in most situations so at least one can assume
these processors still be supported.  The only situations where this
fails is with threads of different priority (priority inversion).


What I suggest is to add the __builtin_cmpxchg code.  It will be using
the asm opcode if allowed and fall back to a spinlock implementations
in the case where there is no such opcode.  If compiled without thread
support the opcode even compiles to no code at all.


Comments?

-- 
---------------.      drepper at gnu.org  ,-.   1325 Chesapeake Terrace
Ulrich Drepper  \    ,-------------------'   \  Sunnyvale, CA 94089 USA
Cygnus Solutions `--' drepper at cygnus.com   `------------------------

             reply	other threads:[~1999-01-31 23:58 UTC|newest]

Thread overview: 70+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-01-25 14:44 Ulrich Drepper [this message]
1999-01-26  5:06 ` Jeffrey A Law
1999-01-26  7:48   ` Ulrich Drepper
1999-01-31 23:58     ` Ulrich Drepper
1999-01-26  9:54   ` Joe Buck
1999-01-26  9:57     ` Jeffrey A Law
1999-01-26 10:23       ` Joe Buck
1999-01-26 10:27         ` David Edelsohn
1999-01-26 10:42           ` Joe Buck
1999-01-26 10:56             ` David Edelsohn
1999-01-26 11:28               ` Joe Buck
1999-01-26 11:44                 ` David Edelsohn
1999-01-31 23:58                   ` David Edelsohn
1999-01-31 23:58                 ` Joe Buck
1999-01-31 23:58               ` David Edelsohn
1999-01-26 11:02             ` Nick Ing-Simmons
1999-01-31 23:58               ` Nick Ing-Simmons
1999-01-31 23:58             ` Joe Buck
1999-01-31 23:58           ` David Edelsohn
1999-01-26 13:17         ` Gabriel Dos Reis
1999-01-31 23:58           ` Gabriel Dos Reis
1999-01-31 23:58         ` Joe Buck
     [not found]       ` <36AE692E.A5DD45E6@manhattanproject.com>
1999-01-26 17:27         ` gperf switch Clark Evans
1999-01-26 18:18           ` Alexandre Oliva
1999-01-26 19:57             ` Donn Terry
1999-01-31 23:58               ` Donn Terry
1999-01-31 23:58             ` Alexandre Oliva
1999-01-31 23:58           ` Clark Evans
1999-01-31 23:58       ` mutex in frame code Jeffrey A Law
1999-01-31 23:58     ` Joe Buck
1999-01-31 23:58   ` Jeffrey A Law
1999-01-31 23:58 ` Ulrich Drepper
1999-01-26 13:20 Mike Stump
1999-01-31 23:58 ` Mike Stump
1999-02-01  7:38 ` Richard Earnshaw
     [not found]   ` < 199902011536.PAA05753@sun52.NIS.cambridge >
1999-02-01  9:35     ` Richard Henderson
     [not found]       ` < 19990201093544.A18419@cygnus.com >
1999-02-01  9:53         ` David Edelsohn
     [not found]           ` < 9902011752.AA89026@marc.watson.ibm.com >
1999-02-01 10:07             ` Joe Buck
     [not found]               ` < 199902011806.KAA02061@atrus.synopsys.com >
1999-02-01 10:16                 ` David Edelsohn
     [not found]                   ` < 9902011816.AA33418@marc.watson.ibm.com >
1999-02-01 10:48                     ` Zack Weinberg
     [not found]                       ` < 199902011848.NAA27796@blastula.phys.columbia.edu >
1999-02-01 10:54                         ` David Edelsohn
     [not found]                           ` < 9902011854.AA89036@marc.watson.ibm.com >
1999-02-01 11:09                             ` Joe Buck
     [not found]                               ` < 199902011907.LAA05591@atrus.synopsys.com >
1999-02-01 11:25                                 ` David Edelsohn
1999-02-28 22:53                                   ` David Edelsohn
1999-02-28 22:53                               ` Joe Buck
1999-02-28 22:53                           ` David Edelsohn
1999-02-28 22:53                       ` Zack Weinberg
1999-02-01 10:54                     ` Joe Buck
1999-02-28 22:53                       ` Joe Buck
1999-02-01 23:00                     ` Matthias Urlichs
     [not found]                       ` < 19990202080023.B15962@noris.de >
1999-02-02 10:04                         ` David Edelsohn
1999-02-28 22:53                           ` David Edelsohn
1999-02-28 22:53                       ` Matthias Urlichs
1999-02-28 22:53                   ` David Edelsohn
1999-02-28 22:53               ` Joe Buck
1999-02-01 10:25             ` Zack Weinberg
     [not found]               ` < 199902011825.NAA27565@blastula.phys.columbia.edu >
1999-02-01 10:32                 ` David Edelsohn
1999-02-28 22:53                   ` David Edelsohn
1999-02-28 22:53               ` Zack Weinberg
1999-02-28 22:53           ` David Edelsohn
1999-02-01  9:54         ` Richard Earnshaw
1999-02-28 22:53           ` Richard Earnshaw
1999-02-28 22:53       ` Richard Henderson
1999-02-28 22:53   ` Richard Earnshaw
1999-02-02 10:24 Mike Stump
     [not found] ` < 199902021823.KAA13030@kankakee.wrs.com >
1999-02-02 12:40   ` Gavin Romig-Koch
     [not found]     ` < 14007.25134.285874.916089@cetus.cygnus.com >
1999-02-03  3:30       ` Richard Earnshaw
1999-02-28 22:53         ` Richard Earnshaw
1999-02-28 22:53     ` Gavin Romig-Koch
1999-02-28 22:53 ` Mike Stump

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=r2r9sjas6h.fsf@happy.cygnus.com \
    --to=drepper@cygnus.com \
    --cc=egcs@cygnus.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).