* src/vm/jit/show.h (show_filters_init, show_filters_apply, show_filters_test_verbose...
[cacao.git] / src / threads / critical.c
1 /* src/threads/critical.c - restartable critical sections
2
3    Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25    $Id: threads.c 4903 2006-05-11 12:48:43Z edwin $
26
27 */
28
29
30 #include "config.h"
31
32 #include <stddef.h>
33
34 #include "vm/types.h"
35
36 #include "threads/critical.h"
37
38 #include "toolbox/avl.h"
39
40 #include "vm/jit/asmpart.h"
41
42
43 /* the AVL tree containing the critical sections */
44
45 static avl_tree_t *criticaltree;
46
47
48 /* prototypes *****************************************************************/
49
50 static s4 critical_comparator(const void *treenode, const void *node);
51 static void critical_register_asm_critical_sections(void);
52
53
54 /* critical_init ***************************************************************
55
56    Init global data structures.
57
58 *******************************************************************************/
59
60 void critical_init(void)
61 {
62     criticaltree = avl_create(&critical_comparator);
63
64         critical_register_asm_critical_sections();
65 }
66
67
68 /* critical_comparator *********************************************************
69
70    Comparison function for AVL tree of critical section.
71
72    IN:
73        treenode....node in the tree
74            node........node to compare with tree-node
75
76    RETURN VALUE:
77        -1, 0, +1 for (pa <, ==, > pb)
78
79 *******************************************************************************/
80
81 static s4 critical_comparator(const void *treenode, const void *node)
82 {
83         const critical_section_node_t *treecsn;
84         const critical_section_node_t *csn;
85
86         treecsn = treenode;
87         csn     = node;
88
89 #ifdef __S390__
90 #       define ADDR_MASK(x) ((u1 *)((s4)(x) & 0x7FFFFFFF))
91 #else
92 #       define ADDR_MASK(x) (x)
93 #endif
94
95         /* compare for avl_find if we have found an entry */
96
97         if (
98                 (ADDR_MASK(treecsn->start) <= ADDR_MASK(csn->start)) && 
99                 (ADDR_MASK(csn->start) < ADDR_MASK(treecsn->end))
100         )
101                 return 0;
102
103         /* these are for walking the tree */
104
105         if (ADDR_MASK(treecsn->start) < ADDR_MASK(csn->start))
106                 return -1;
107         else
108                 return 1;
109
110 #undef ADDR_MASK
111 }
112
113
114 /* critical_section_register ***************************************************
115  
116    Register a critical section.
117
118    IN:
119        csn....node for the critical section
120
121 *******************************************************************************/
122
123 void critical_section_register(critical_section_node_t *csn)
124 {
125         (void) avl_insert(criticaltree, csn);
126 }
127
128
129 /* critical_find_restart_point *************************************************
130
131    Find a restart point for the given PC, in case it is in a critical
132    section.
133
134    IN:
135        pc.........PC
136
137    OUT:
138        PC of the restart point, or
139            NULL if the given mcodeptr is not in a critical section
140
141 *******************************************************************************/
142
143 u1 *critical_find_restart_point(u1 *pc)
144 {
145         critical_section_node_t        csnpc;
146         const critical_section_node_t *csn;
147
148         /* fill the temporary node for comparison */
149
150         csnpc.start = pc;
151
152         /* see if there's an entry for that PC */
153
154         csn = avl_find(criticaltree, &csnpc);
155
156         if (csn == NULL)
157                 return NULL;
158
159         return csn->restart;
160 }
161
162
163 /* critical_register_asm_critical_sections *************************************
164
165    Register critical sections defined in the array asm_criticalsections.
166
167 *******************************************************************************/
168
169 static void critical_register_asm_critical_sections(void)
170 {
171         /* XXX TWISTI: this is just a quick hack */
172 #if defined(ENABLE_JIT) && defined(ENABLE_THREADS)
173         critical_section_node_t *n = &asm_criticalsections;
174
175         while (n->start)
176                 critical_section_register(n++);
177 #endif
178 }
179
180
181 /*
182  * These are local overrides for various environment variables in Emacs.
183  * Please do not remove this and leave it at the end of the file, where
184  * Emacs will automagically detect them.
185  * ---------------------------------------------------------------------
186  * Local variables:
187  * mode: c
188  * indent-tabs-mode: t
189  * c-basic-offset: 4
190  * tab-width: 4
191  * End:
192  * vim:noexpandtab:sw=4:ts=4:
193  */