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