Blame stdlib/test-dlclose-exit-race-helper.c

Packit 6c4009
/* Helper for exit/dlclose race test (Bug 22180).
Packit 6c4009
   Copyright (C) 2017-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdbool.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <semaphore.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <support/check.h>
Packit 6c4009
#include <support/xthread.h>
Packit 6c4009
Packit 6c4009
/* Semaphore defined in executable to ensure we have a happens-before
Packit 6c4009
   between the first function starting and exit being called.  */
Packit 6c4009
extern sem_t order1;
Packit 6c4009
Packit 6c4009
/* Semaphore defined in executable to ensure we have a happens-before
Packit 6c4009
   between the second function starting and the first function returning.  */
Packit 6c4009
extern sem_t order2;
Packit 6c4009
Packit 6c4009
/* glibc function for registering DSO-specific exit functions.  */
Packit 6c4009
extern int __cxa_atexit (void (*func) (void *), void *arg, void *dso_handle);
Packit 6c4009
Packit 6c4009
/* Hidden compiler handle to this shared object.  */
Packit 6c4009
extern void *__dso_handle __attribute__ ((__weak__));
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
first (void *start)
Packit 6c4009
{
Packit 6c4009
  /* Let the exiting thread run.  */
Packit 6c4009
  sem_post (&order1;;
Packit 6c4009
Packit 6c4009
  /* Wait for exiting thread to finish.  */
Packit 6c4009
  sem_wait (&order2;;
Packit 6c4009
Packit 6c4009
  printf ("first\n");
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
second (void *start)
Packit 6c4009
{
Packit 6c4009
  /* We may be called from different threads.
Packit 6c4009
     This lock protects called.  */
Packit 6c4009
  static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
Packit 6c4009
  static bool called = false;
Packit 6c4009
Packit 6c4009
  xpthread_mutex_lock (&mtx);
Packit 6c4009
  if (called)
Packit 6c4009
    FAIL_EXIT1 ("second called twice!");
Packit 6c4009
Packit 6c4009
  called = true;
Packit 6c4009
  xpthread_mutex_unlock (&mtx);
Packit 6c4009
Packit 6c4009
  printf ("second\n");
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
__attribute__ ((constructor)) static void
Packit 6c4009
constructor (void)
Packit 6c4009
{
Packit 6c4009
  sem_init (&order1, 0, 0);
Packit 6c4009
  sem_init (&order2, 0, 0);
Packit 6c4009
  __cxa_atexit (second, NULL, __dso_handle);
Packit 6c4009
  __cxa_atexit (first, NULL, __dso_handle);
Packit 6c4009
}