gc_pthread_redirects.h raw

   1  /*
   2   * Copyright (c) 1994 by Xerox Corporation.  All rights reserved.
   3   * Copyright (c) 1996 by Silicon Graphics.  All rights reserved.
   4   * Copyright (c) 1998 by Fergus Henderson.  All rights reserved.
   5   * Copyright (c) 2000-2010 by Hewlett-Packard Development Company.
   6   * All rights reserved.
   7   *
   8   * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
   9   * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
  10   *
  11   * Permission is hereby granted to use or copy this program
  12   * for any purpose, provided the above notices are retained on all copies.
  13   * Permission to modify the code and to distribute modified code is granted,
  14   * provided the above notices are retained, and a notice that the code was
  15   * modified is included with the above copyright notice.
  16   */
  17  
  18  /*
  19   * The collector `pthreads` support normally needs to intercept a number
  20   * of thread calls.  We arrange to do that here, if appropriate.
  21   */
  22  
  23  #ifndef GC_PTHREAD_REDIRECTS_H
  24  #define GC_PTHREAD_REDIRECTS_H
  25  
  26  /*
  27   * Included from `gc.h` file only.  Included only if `GC_PTHREADS` macro
  28   * is defined.
  29   */
  30  #if defined(GC_H) && defined(GC_PTHREADS)
  31  
  32  /*
  33   * We need to intercept calls to many of the threads' primitives, so
  34   * that we can locate thread stacks and stop the world.
  35   *
  36   * Note also that the collector cannot always see thread-specific data.
  37   * Such data should generally consist of pointers to uncollectible
  38   * objects (allocated with `GC_malloc_uncollectable`, not the system
  39   * `malloc`), which are deallocated using the destructor facility in
  40   * `pthread_key_create()`.  Alternatively, keep a redundant pointer
  41   * to thread-specific data on the thread stack.
  42   */
  43  
  44  #  ifndef GC_PTHREAD_REDIRECTS_ONLY
  45  
  46  #    include <pthread.h>
  47  #    ifndef GC_NO_DLOPEN
  48  #      include <dlfcn.h>
  49  #    endif
  50  #    ifndef GC_NO_PTHREAD_SIGMASK
  51  /* This is needed anyway for proper redirection. */
  52  #      include <signal.h>
  53  #    endif
  54  
  55  #    ifdef __cplusplus
  56  extern "C" {
  57  #    endif
  58  
  59  #    ifndef GC_SUSPEND_THREAD_ID
  60  #      define GC_SUSPEND_THREAD_ID pthread_t
  61  #    endif
  62  
  63  #    ifndef GC_NO_DLOPEN
  64  GC_API void *GC_dlopen(const char * /* `path` */, int /* `mode` */);
  65  #    endif
  66  
  67  #    ifndef GC_NO_PTHREAD_SIGMASK
  68  #      if defined(GC_PTHREAD_SIGMASK_NEEDED) || defined(__COSMOPOLITAN__) \
  69            || defined(GC_HAVE_PTHREAD_SIGMASK) || defined(_BSD_SOURCE)     \
  70            || defined(_GNU_SOURCE) || defined(_NETBSD_SOURCE)              \
  71            || (_POSIX_C_SOURCE >= 199506L) || (_XOPEN_SOURCE >= 500)       \
  72            || (__POSIX_VISIBLE >= 199506) /*< xBSD internal macro */
  73  
  74  GC_API int GC_pthread_sigmask(int /* `how` */, const sigset_t *,
  75                                sigset_t * /* `oset` */);
  76  #      else
  77  #        define GC_NO_PTHREAD_SIGMASK
  78  #      endif
  79  #    endif /* !GC_NO_PTHREAD_SIGMASK */
  80  
  81  #    ifndef GC_PTHREAD_CREATE_CONST
  82  /* This is used for `pthread_create()` only. */
  83  #      define GC_PTHREAD_CREATE_CONST const
  84  #    endif
  85  
  86  GC_API int GC_pthread_create(pthread_t *,
  87                               GC_PTHREAD_CREATE_CONST pthread_attr_t *,
  88                               void *(*)(void *), void * /* `arg` */);
  89  GC_API int GC_pthread_join(pthread_t, void ** /* `retval` */);
  90  GC_API int GC_pthread_detach(pthread_t);
  91  
  92  #    ifndef GC_NO_PTHREAD_CANCEL
  93  GC_API int GC_pthread_cancel(pthread_t);
  94  #    endif
  95  
  96  #    if defined(GC_HAVE_PTHREAD_EXIT) && !defined(GC_PTHREAD_EXIT_DECLARED)
  97  #      define GC_PTHREAD_EXIT_DECLARED
  98  GC_API void GC_pthread_exit(void *) GC_PTHREAD_EXIT_ATTRIBUTE;
  99  #    endif
 100  
 101  #    ifdef __cplusplus
 102  } /* extern "C" */
 103  #    endif
 104  
 105  #  endif /* !GC_PTHREAD_REDIRECTS_ONLY */
 106  
 107  #  if !defined(GC_NO_THREAD_REDIRECTS) && !defined(GC_USE_LD_WRAP)
 108  /*
 109   * Unless the compiler supports `#pragma extern_prefix`, the Tru64 UNIX
 110   * platform `pthread.h` file redefines some POSIX thread functions to use
 111   * mangled names.  Anyway, it is safe to `#undef` them before redefining.
 112   */
 113  #    undef pthread_create
 114  #    undef pthread_join
 115  #    undef pthread_detach
 116  #    define pthread_create GC_pthread_create
 117  #    define pthread_join GC_pthread_join
 118  #    define pthread_detach GC_pthread_detach
 119  
 120  #    ifndef GC_NO_PTHREAD_SIGMASK
 121  #      undef pthread_sigmask
 122  #      define pthread_sigmask GC_pthread_sigmask
 123  #    endif
 124  #    ifndef GC_NO_DLOPEN
 125  #      undef dlopen
 126  #      define dlopen GC_dlopen
 127  #    endif
 128  #    ifndef GC_NO_PTHREAD_CANCEL
 129  #      undef pthread_cancel
 130  #      define pthread_cancel GC_pthread_cancel
 131  #    endif
 132  #    ifdef GC_HAVE_PTHREAD_EXIT
 133  #      undef pthread_exit
 134  #      define pthread_exit GC_pthread_exit
 135  #    endif
 136  #  endif /* !GC_NO_THREAD_REDIRECTS */
 137  
 138  #endif /* GC_PTHREADS */
 139  
 140  #endif /* GC_PTHREAD_REDIRECTS_H */
 141