a0d459561cd3dfb34687ac45b48e2fa63bc33567
[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         /* compare for avl_find if we have found an entry */
90
91         if ((treecsn->start <= csn->start) && (csn->start < treecsn->end))
92                 return 0;
93
94         /* these are for walking the tree */
95
96         if (treecsn->start < csn->start)
97                 return -1;
98         else
99                 return 1;
100 }
101
102
103 /* critical_section_register ***************************************************
104  
105    Register a critical section.
106
107    IN:
108        csn....node for the critical section
109
110 *******************************************************************************/
111
112 void critical_section_register(critical_section_node_t *csn)
113 {
114         (void) avl_insert(criticaltree, csn);
115 }
116
117
118 /* critical_find_restart_point *************************************************
119
120    Find a restart point for the given PC, in case it is in a critical
121    section.
122
123    IN:
124        pc.........PC
125
126    OUT:
127        PC of the restart point, or
128            NULL if the given mcodeptr is not in a critical section
129
130 *******************************************************************************/
131
132 u1 *critical_find_restart_point(u1 *pc)
133 {
134         critical_section_node_t        csnpc;
135         const critical_section_node_t *csn;
136
137         /* fill the temporary node for comparison */
138
139         csnpc.start = pc;
140
141         /* see if there's an entry for that PC */
142
143         csn = avl_find(criticaltree, &csnpc);
144
145         if (csn == NULL)
146                 return NULL;
147
148         return csn->restart;
149 }
150
151
152 /* critical_register_asm_critical_sections *************************************
153
154    Register critical sections defined in the array asm_criticalsections.
155
156 *******************************************************************************/
157
158 static void critical_register_asm_critical_sections(void)
159 {
160         /* XXX TWISTI: this is just a quick hack */
161 #if defined(ENABLE_JIT) && defined(ENABLE_THREADS)
162         critical_section_node_t *n = &asm_criticalsections;
163
164         while (n->start)
165                 critical_section_register(n++);
166 #endif
167 }
168
169
170 /*
171  * These are local overrides for various environment variables in Emacs.
172  * Please do not remove this and leave it at the end of the file, where
173  * Emacs will automagically detect them.
174  * ---------------------------------------------------------------------
175  * Local variables:
176  * mode: c
177  * indent-tabs-mode: t
178  * c-basic-offset: 4
179  * tab-width: 4
180  * End:
181  * vim:noexpandtab:sw=4:ts=4:
182  */