* README: Replace with a cut-down and updated version of gcc/README.
[official-gcc.git] / boehm-gc / gc_dlopen.c
blob943c4b0c3eb9afa0741e4ed931b55b591cb9e629
1 /*
2 * Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
3 * Copyright (c) 1997 by Silicon Graphics. All rights reserved.
4 * Copyright (c) 2000 by Hewlett-Packard Company. All rights reserved.
6 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
7 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
9 * Permission is hereby granted to use or copy this program
10 * for any purpose, provided the above notices are retained on all copies.
11 * Permission to modify the code and to distribute modified code is granted,
12 * provided the above notices are retained, and a notice that the code was
13 * modified is included with the above copyright notice.
15 * Original author: Bill Janssen
16 * Heavily modified by Hans Boehm and others
20 * This used to be in dyn_load.c. It was extracted into a separate file
21 * to avoid having to link against libdl.{a,so} if the client doesn't call
22 * dlopen. -HB
25 #include "private/gc_priv.h"
27 # if defined(LINUX_THREADS) || defined(SOLARIS_THREADS) \
28 || defined(HPUX_THREADS) || defined(IRIX_THREADS)
30 # if defined(dlopen) && !defined(GC_USE_LD_WRAP)
31 /* To support various threads pkgs, gc.h interposes on dlopen by */
32 /* defining "dlopen" to be "GC_dlopen", which is implemented below. */
33 /* However, both GC_FirstDLOpenedLinkMap() and GC_dlopen() use the */
34 /* real system dlopen() in their implementation. We first remove */
35 /* gc.h's dlopen definition and restore it later, after GC_dlopen(). */
36 # undef dlopen
37 # endif
39 /* Make sure we're not in the middle of a collection, and make */
40 /* sure we don't start any. Returns previous value of GC_dont_gc. */
41 /* This is invoked prior to a dlopen call to avoid synchronization */
42 /* issues. We can't just acquire the allocation lock, since startup */
43 /* code in dlopen may try to allocate. */
44 /* This solution risks heap growth in the presence of many dlopen */
45 /* calls in either a multithreaded environment, or if the library */
46 /* initialization code allocates substantial amounts of GC'ed memory. */
47 /* But I don't know of a better solution. */
48 /* This can still deadlock if the client explicitly starts a GC */
49 /* during the dlopen. He shouldn't do that. */
50 static GC_bool disable_gc_for_dlopen()
52 GC_bool result;
53 LOCK();
54 result = GC_dont_gc;
55 while (GC_incremental && GC_collection_in_progress()) {
56 GC_collect_a_little_inner(1000);
58 GC_dont_gc = TRUE;
59 UNLOCK();
60 return(result);
63 /* Redefine dlopen to guarantee mutual exclusion with */
64 /* GC_register_dynamic_libraries. */
65 /* Should probably happen for other operating systems, too. */
67 #include <dlfcn.h>
69 #ifdef GC_USE_LD_WRAP
70 void * __wrap_dlopen(const char *path, int mode)
71 #else
72 void * GC_dlopen(path, mode)
73 GC_CONST char * path;
74 int mode;
75 #endif
77 void * result;
78 GC_bool dont_gc_save;
80 # ifndef USE_PROC_FOR_LIBRARIES
81 dont_gc_save = disable_gc_for_dlopen();
82 # endif
83 # ifdef GC_USE_LD_WRAP
84 result = (void *)__real_dlopen(path, mode);
85 # else
86 result = dlopen(path, mode);
87 # endif
88 # ifndef USE_PROC_FOR_LIBRARIES
89 GC_dont_gc = dont_gc_save;
90 # endif
91 return(result);
93 # endif /* LINUX_THREADS || SOLARIS_THREADS || ... */