This is the mail archive of the libc-hacker@sources.redhat.com mailing list for the glibc project.

Note that libc-hacker is a closed list. You may look at the archives of this list, but subscription and posting are not open.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Fix getpid/raise


Hi!

The attached testcase fails with current CVS (when not linked against
-lpthread):

tgkill(32233, 32233, SIGUSR1)           = 0
--- SIGUSR1 (User defined signal 1) @ 0 (0) ---
sigreturn()                             = ? (mask now [])
tgkill(0, 32233, SIGUSR1)               = -1 EINVAL (Invalid argument)

Don't know where to put the tst-raise1.c testcase, signal/ or somewhere
else (nptl/ is bad, since it should not be linked against -lpthread)?

Additionally, I think there is a race.  If fork () is called when getpid ()
has never been called before in the program, then after the self->pid =
-parentpid; in fork.c but before the clone () syscall a signal is received
which calls getpid () and another signal is received in the child before
self->pid = self->tid setting, getpid () in that signal handler will return
incorrect value.

The attached patch should cure both.  Performance wise, I tried:
#include <stdio.h>
#include <dlfcn.h>

int main (void)
{
  int i;
  unsigned long long st, en, mi = ~0ULL;
  for (i = 0; i < 100; ++i)
    {
      asm volatile ("rdtsc" : "=A" (st));
      getpid ();
      asm volatile ("rdtsc" : "=A" (en));
      if (en - st < mi)
        mi = en - st;
    }
  printf ("%lld ticks\n", mi);
  dlopen ("libpthread.so.0", RTLD_LAZY);
  mi = ~0ULL;
  for (i = 0; i < 100; ++i)
    {
      asm volatile ("rdtsc" : "=A" (st));
      getpid ();
      asm volatile ("rdtsc" : "=A" (en));
      if (en - st < mi)
        mi = en - st;
    }
  printf ("%lld ticks\n", mi);
}
on my PIII, against the installed glibc which doesn't yet have any of the
getpid changes it prints
356 ticks
357 ticks

against CVS libc.so:
37 ticks
39 ticks

and against CVS+this patch:
41 ticks
38 ticks

I've moved pid field closer to tid, so that they share a cache line when
they are used so often together.

	Jakub

Attachment: P1
Description: Text document

Attachment: tst-raise1.c
Description: Text document


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]