summaryrefslogtreecommitdiff
path: root/lib/libpthread/uthread/uthread_autoinit.c
blob: 5ac4592e61c9f5c941cbecfe5423c06b4e73d0a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
 * David Leonard, 1998. Public Domain. <david.leonard@csee.uq.edu.au>
 *
 * $OpenBSD: uthread_autoinit.c,v 1.4 1999/01/10 23:07:59 d Exp $
 */

#include <stdio.h>
#include <pthread.h>
#include "pthread_private.h"

__BEGIN_DECLS
extern void _thread_init __P((void));
__END_DECLS

#ifdef DEBUG
#define init_debug(m)	stderr_debug(m)
#else
#define init_debug(m)	/* nothing */
#endif

/*
 * Use C++'s static instance constructor to initialise threads.
 */
#ifdef __cplusplus
class Init {
public:
	Init() { 
		init_debug("init: C++\n");
		_thread_init();
	}
};
Init _thread_initialiser;
#endif /* C++ */

/*
 * The a.out ld.so dynamic linker calls the function
 * at symbol ".init" if it exists, just after linkage.
 */
extern void _thread_dot_init __P((void)) asm(".init");
void 
_thread_dot_init()
{ 
	init_debug("init: a.out .init\n");
	_thread_init();
}

/*
 * The GNU ELF loader will place a function called _init
 * found in the .dynamic section into the _INIT field. This then gets
 * automatically run by GNU ELF's ld.so.
 */
#ifdef mips
extern int _init() __attribute__((constructor,section (".dynamic")));
int 
_init()
{ 
	init_debug("init: elf _init\n");
	_thread_init();
	return 0; 
}
#endif /* mips */

/*
 * A GNU C installation may know how to automatically run
 * constructors for other architectures. (It doesn't matter if 
 * we initialise multiple times.)  This construct places
 * the function in the __CTOR_LIST__ entry in the object, and later
 * the collect2 stage of linkage will inform __main (from libgcc.a)
 * to call it.
 */
#ifdef _GNUC_
void _thread_init_constructor __P((void)) __attribute__((constructor));
void
_thread_init_constructor()
{
	init_debug("init: GNU constructor");
	_thread_init();
}
#endif /* GNU C */

/*
 * Dummy symbol referenced by uthread_init.o so this compilation unit 
 * is always loaded.
 */
int _thread_autoinit_dummy_decl = 0;