2010-07-10 Mark Probst <mark.probst@gmail.com>
[mono.git] / mono / metadata / sgen-gc.h
1 /*
2  * Copyright 2001-2003 Ximian, Inc
3  * Copyright 2003-2010 Novell, Inc.
4  * 
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  * 
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  * 
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 #ifndef __MONO_SGENGC_H__
25 #define __MONO_SGENGC_H__
26
27 /* pthread impl */
28 #include "config.h"
29 #include <glib.h>
30 #include <pthread.h>
31 #include <signal.h>
32 #include <mono/utils/mono-compiler.h>
33 #include <mono/metadata/class-internals.h>
34
35 #define THREAD_HASH_SIZE 11
36
37 #define ARCH_THREAD_TYPE pthread_t
38 #define ARCH_GET_THREAD pthread_self
39 #define ARCH_THREAD_EQUALS(a,b) pthread_equal (a, b)
40
41 #if SIZEOF_VOID_P == 4
42 typedef guint32 mword;
43 #else
44 typedef guint64 mword;
45 #endif
46
47 /* for use with write barriers */
48 typedef struct _RememberedSet RememberedSet;
49 struct _RememberedSet {
50         mword *store_next;
51         mword *end_set;
52         RememberedSet *next;
53         mword data [MONO_ZERO_LEN_ARRAY];
54 };
55
56 /* eventually share with MonoThread? */
57 typedef struct _SgenThreadInfo SgenThreadInfo;
58
59 struct _SgenThreadInfo {
60         SgenThreadInfo *next;
61         ARCH_THREAD_TYPE id;
62         unsigned int stop_count; /* to catch duplicate signals */
63         int signal;
64         int skip;
65         volatile int in_critical_region;
66         void *stack_end;
67         void *stack_start;
68         void *stack_start_limit;
69         char **tlab_next_addr;
70         char **tlab_start_addr;
71         char **tlab_temp_end_addr;
72         char **tlab_real_end_addr;
73         gpointer **store_remset_buffer_addr;
74         long *store_remset_buffer_index_addr;
75         RememberedSet *remset;
76         gpointer runtime_data;
77         gpointer stopped_ip;    /* only valid if the thread is stopped */
78         MonoDomain *stopped_domain; /* ditto */
79         gpointer *stopped_regs;     /* ditto */
80 #ifndef HAVE_KW_THREAD
81         char *tlab_start;
82         char *tlab_next;
83         char *tlab_temp_end;
84         char *tlab_real_end;
85         gpointer *store_remset_buffer;
86         long store_remset_buffer_index;
87 #endif
88 };
89
90 #ifdef __APPLE__
91 static int suspend_signal_num = SIGXFSZ;
92 #else
93 static int suspend_signal_num = SIGPWR;
94 #endif
95 static int restart_signal_num = SIGXCPU;
96
97 /*
98  * Recursion is not allowed for the thread lock.
99  */
100 #define LOCK_DECLARE(name) pthread_mutex_t name = PTHREAD_MUTEX_INITIALIZER
101 #define LOCK_INIT(name)
102 #define LOCK_GC pthread_mutex_lock (&gc_mutex)
103 #define UNLOCK_GC pthread_mutex_unlock (&gc_mutex)
104 #define LOCK_INTERRUPTION pthread_mutex_lock (&interruption_mutex)
105 #define UNLOCK_INTERRUPTION pthread_mutex_unlock (&interruption_mutex)
106
107 /* non-pthread will need to provide their own version of start/stop */
108 #define USE_SIGNAL_BASED_START_STOP_WORLD 1
109 /* we intercept pthread_create calls to know which threads exist */
110 #define USE_PTHREAD_INTERCEPT 1
111
112 #define MAX_DEBUG_LEVEL 2
113 #define DEBUG(level,a) do {if (G_UNLIKELY ((level) <= MAX_DEBUG_LEVEL && (level) <= gc_debug_level)) a;} while (0)
114
115 int mono_sgen_thread_handshake (int signum) MONO_INTERNAL;
116 SgenThreadInfo* mono_sgen_thread_info_lookup (ARCH_THREAD_TYPE id) MONO_INTERNAL;
117 SgenThreadInfo** mono_sgen_get_thread_table (void) MONO_INTERNAL;
118 void mono_sgen_wait_for_suspend_ack (int count) MONO_INTERNAL;
119
120 gboolean mono_sgen_is_worker_thread (pthread_t thread) MONO_INTERNAL;
121
122 #endif /* __MONO_SGENGC_H__ */
123