1 /* src/threads/critical.c - restartable critical sections
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
8 This file is part of CACAO.
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.
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.
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
25 $Id: threads.c 4903 2006-05-11 12:48:43Z edwin $
36 #include "threads/critical.h"
38 #include "toolbox/avl.h"
40 #include "vm/jit/asmpart.h"
43 /* the AVL tree containing the critical sections */
45 static avl_tree *criticaltree;
48 /* prototypes *****************************************************************/
50 static s4 critical_compare(const void *pa, const void *pb);
51 static void critical_register_asm_critical_sections(void);
54 /* critical_init ***************************************************************
56 Init global data structures.
58 *******************************************************************************/
60 void critical_init(void)
62 criticaltree = avl_create(&critical_compare);
64 critical_register_asm_critical_sections();
68 /* critical_compare ************************************************************
70 Comparison function for AVL tree of critical section.
73 pa...............first node
74 pb...............second node
77 -1, 0, +1 for (pa <, ==, > pb)
79 *******************************************************************************/
81 static s4 critical_compare(const void *pa, const void *pb)
83 const critical_section_node_t *na = pa;
84 const critical_section_node_t *nb = pb;
86 if (na->mcodebegin < nb->mcodebegin)
88 if (na->mcodebegin > nb->mcodebegin)
94 /* critical_find ***************************************************************
96 Find the critical region the given pc is in.
102 pointer to critical region node, or
103 NULL if critical region was not found.
105 *******************************************************************************/
107 static const critical_section_node_t *critical_find(u1 *mcodeptr)
110 const critical_section_node_t *m;
112 n = criticaltree->root;
119 const critical_section_node_t *d = n->data;
121 if (mcodeptr == d->mcodebegin)
124 if (mcodeptr < d->mcodebegin) {
145 /* critical_register_critical_section ******************************************
147 Register a critical section.
150 n................node for the critical section
152 *******************************************************************************/
154 void critical_register_critical_section(critical_section_node_t *n)
156 (void) avl_insert(criticaltree, n);
160 /* critical_find_restart_point *************************************************
162 Find a restart point for the given PC, in case it is in a critical section.
168 PC of the restart point, or
169 NULL if the given mcodeptr is not in a critical section
171 *******************************************************************************/
173 u1 *critical_find_restart_point(u1 *mcodeptr)
175 const critical_section_node_t *n = critical_find(mcodeptr);
177 /* XXX should we check >= n->mcodebegin */
178 return (n && mcodeptr < n->mcodeend && mcodeptr > n->mcodebegin) ? n->mcoderestart : NULL;
182 /* critical_register_asm_critical_sections *************************************
184 Register critical sections defined in the array asm_criticalsections.
186 *******************************************************************************/
188 static void critical_register_asm_critical_sections(void)
190 /* XXX TWISTI: this is just a quick hack */
191 #if defined(ENABLE_JIT) && defined(ENABLE_THREADS)
192 critical_section_node_t *n = &asm_criticalsections;
194 while (n->mcodebegin)
195 critical_register_critical_section(n++);
201 * These are local overrides for various environment variables in Emacs.
202 * Please do not remove this and leave it at the end of the file, where
203 * Emacs will automagically detect them.
204 * ---------------------------------------------------------------------
207 * indent-tabs-mode: t
211 * vim:noexpandtab:sw=4:ts=4: