PR other/2873
[official-gcc.git] / boehm-gc / gc_dlopen.c
blobeafaa2b1b46c383abfebe682fd632e959353557d
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(GC_PTHREADS) || defined(GC_SOLARIS_THREADS)
29 # if defined(dlopen) && !defined(GC_USE_LD_WRAP)
30 /* To support various threads pkgs, gc.h interposes on dlopen by */
31 /* defining "dlopen" to be "GC_dlopen", which is implemented below. */
32 /* However, both GC_FirstDLOpenedLinkMap() and GC_dlopen() use the */
33 /* real system dlopen() in their implementation. We first remove */
34 /* gc.h's dlopen definition and restore it later, after GC_dlopen(). */
35 # undef dlopen
36 # endif
38 /* Make sure we're not in the middle of a collection, and make */
39 /* sure we don't start any. Returns previous value of GC_dont_gc. */
40 /* This is invoked prior to a dlopen call to avoid synchronization */
41 /* issues. We can't just acquire the allocation lock, since startup */
42 /* code in dlopen may try to allocate. */
43 /* This solution risks heap growth in the presence of many dlopen */
44 /* calls in either a multithreaded environment, or if the library */
45 /* initialization code allocates substantial amounts of GC'ed memory. */
46 /* But I don't know of a better solution. */
47 /* This can still deadlock if the client explicitly starts a GC */
48 /* during the dlopen. He shouldn't do that. */
49 static GC_bool disable_gc_for_dlopen()
51 GC_bool result;
52 LOCK();
53 result = GC_dont_gc;
54 while (GC_incremental && GC_collection_in_progress()) {
55 GC_collect_a_little_inner(1000);
57 GC_dont_gc = TRUE;
58 UNLOCK();
59 return(result);
62 /* Redefine dlopen to guarantee mutual exclusion with */
63 /* GC_register_dynamic_libraries. */
64 /* Should probably happen for other operating systems, too. */
66 #include <dlfcn.h>
68 #ifdef GC_USE_LD_WRAP
69 void * __wrap_dlopen(const char *path, int mode)
70 #else
71 void * GC_dlopen(path, mode)
72 GC_CONST char * path;
73 int mode;
74 #endif
76 void * result;
77 GC_bool dont_gc_save;
79 # ifndef USE_PROC_FOR_LIBRARIES
80 dont_gc_save = disable_gc_for_dlopen();
81 # endif
82 # ifdef GC_USE_LD_WRAP
83 result = (void *)__real_dlopen(path, mode);
84 # else
85 result = dlopen(path, mode);
86 # endif
87 # ifndef USE_PROC_FOR_LIBRARIES
88 GC_dont_gc = dont_gc_save;
89 # endif
90 return(result);
92 # endif /* GC_PTHREADS || GC_SOLARIS_THREADS ... */