d49ca068da14a2073ab6046fce609d086d738c48
[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 #include <stdint.h>
34
35 #include "vm/types.h"
36
37 #include "threads/critical.h"
38
39 #include "toolbox/avl.h"
40
41
42 /* the AVL tree containing the critical sections */
43
44 static avl_tree_t *criticaltree;
45
46
47 /* prototypes *****************************************************************/
48
49 static int critical_comparator(const void *treenode, const void *node);
50
51
52 /* critical_init ***************************************************************
53
54    Init global data structures.
55
56 *******************************************************************************/
57
58 void critical_init(void)
59 {
60     criticaltree = avl_create(&critical_comparator);
61 }
62
63
64 /* critical_comparator *********************************************************
65
66    Comparison function for AVL tree of critical section.
67
68    IN:
69        treenode....node in the tree
70            node........node to compare with tree-node
71
72    RETURN VALUE:
73        -1, 0, +1 for (pa <, ==, > pb)
74
75 *******************************************************************************/
76
77 static int critical_comparator(const void *treenode, const void *node)
78 {
79         const critical_section_node_t *treecsn;
80         const critical_section_node_t *csn;
81
82         treecsn = treenode;
83         csn     = node;
84
85 #ifdef __S390__
86 #       define ADDR_MASK(x) ((u1 *)((s4)(x) & 0x7FFFFFFF))
87 #else
88 #       define ADDR_MASK(x) (x)
89 #endif
90
91         /* compare for avl_find if we have found an entry */
92
93         if (
94                 (ADDR_MASK(treecsn->start) <= ADDR_MASK(csn->start)) && 
95                 (ADDR_MASK(csn->start) < ADDR_MASK(treecsn->end))
96         )
97                 return 0;
98
99         /* these are for walking the tree */
100
101         if (ADDR_MASK(treecsn->start) < ADDR_MASK(csn->start))
102                 return -1;
103         else
104                 return 1;
105
106 #undef ADDR_MASK
107 }
108
109
110 /* critical_section_register ***************************************************
111  
112    Register a critical section.
113
114    IN:
115        csn....node for the critical section
116
117 *******************************************************************************/
118
119 void critical_section_register(critical_section_node_t *csn)
120 {
121         (void) avl_insert(criticaltree, csn);
122 }
123
124
125 /* critical_find_restart_point *************************************************
126
127    Find a restart point for the given PC, in case it is in a critical
128    section.
129
130    IN:
131        pc.........PC
132
133    OUT:
134        PC of the restart point, or
135            NULL if the given mcodeptr is not in a critical section
136
137 *******************************************************************************/
138
139 u1 *critical_find_restart_point(u1 *pc)
140 {
141         critical_section_node_t        csnpc;
142         const critical_section_node_t *csn;
143
144         /* fill the temporary node for comparison */
145
146         csnpc.start = pc;
147
148         /* see if there's an entry for that PC */
149
150         csn = avl_find(criticaltree, &csnpc);
151
152         if (csn == NULL)
153                 return NULL;
154
155         return csn->restart;
156 }
157
158
159 /*
160  * These are local overrides for various environment variables in Emacs.
161  * Please do not remove this and leave it at the end of the file, where
162  * Emacs will automagically detect them.
163  * ---------------------------------------------------------------------
164  * Local variables:
165  * mode: c
166  * indent-tabs-mode: t
167  * c-basic-offset: 4
168  * tab-width: 4
169  * End:
170  * vim:noexpandtab:sw=4:ts=4:
171  */