* src/vm/builtin.c (builtin_monitorenter): Removed.
[cacao.git] / src / threads / native / lock.h
1 /* src/threads/native/lock.h - lock implementation
2
3    Copyright (C) 1996-2005, 2006 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    Contact: cacao@cacaojvm.org
26
27    Authors: Edwin Steiner
28
29    Changes: Christian Thalinger
30
31    $Id: threads.h 4866 2006-05-01 21:40:38Z edwin $
32
33 */
34
35
36 #ifndef _LOCK_H
37 #define _LOCK_H
38
39 #include "config.h"
40 #include "vm/types.h"
41 #include "vm/global.h"
42
43 #include <pthread.h>
44
45
46 /* typedefs *******************************************************************/
47
48 typedef struct lock_execution_env_t      lock_execution_env_t;
49 typedef struct lock_record_t             lock_record_t;
50 typedef struct lock_record_pool_header_t lock_record_pool_header_t;
51 typedef struct lock_record_pool_t        lock_record_pool_t;
52 typedef struct lock_waiter_t             lock_waiter_t;
53 typedef struct lock_hashtable_t          lock_hashtable_t;
54
55
56 /* lock_execution_env_t ********************************************************
57
58    Execution environment. Contains the lock record freelist and pools.
59
60 *******************************************************************************/
61
62 struct lock_execution_env_t {
63         lock_record_t         *firstfree;        /* lock record freelist          */
64         lock_record_pool_t    *lockrecordpools;  /* list of per-thread pools      */
65         int                    lockrecordcount;  /* # of records for this thread  */
66 };
67
68
69 /* lock_waiter_t ***************************************************************
70
71    List node for storing a waiting thread.
72
73 *******************************************************************************/
74
75 struct lock_waiter_t {
76         struct threadobject *waiter;         /* the waiting thread                */
77         lock_waiter_t       *next;           /* next in list                      */
78 };
79
80
81 /* lock_record_t ***************************************************************
82
83    Lock record struct representing an inflated ("fat") lock.
84
85 *******************************************************************************/
86
87 struct lock_record_t {
88         java_objectheader   *obj;                /* object for which this lock is */
89         struct threadobject *owner;              /* current owner of this monitor */
90         s4                   count;              /* recursive lock count          */
91         pthread_mutex_t      mutex;              /* mutex for synchronizing       */
92         lock_waiter_t       *waiters;            /* list of threads waiting       */
93         lock_record_t       *nextfree;           /* next in free list             */
94         lock_record_t       *hashlink;           /* next record in hash chain     */
95 };
96
97
98 /* lock_hashtable_t ************************************************************
99  
100    The global hashtable mapping objects to lock records.
101
102 *******************************************************************************/
103
104 struct lock_hashtable_t {
105         pthread_mutex_t      mutex;       /* mutex for synch. access to the table */
106         u4                   size;        /* number of slots                      */
107         u4                   entries;     /* current number of entries            */
108         lock_record_t      **ptr;         /* the table of slots, uses ext. chain. */
109 };
110
111
112 /* lock_record_pool_header_t ***************************************************
113  
114    Lock records are allocated in pools. Each pool has on of these headers.
115
116 *******************************************************************************/
117
118 struct lock_record_pool_header_t {
119         lock_record_pool_t *next;                /* next pool                     */
120         int                 size;                /* records in this pool          */
121 }; 
122
123
124 /* lock_record_pool_t **********************************************************
125  
126    Lock records are allocated in such pools.
127
128 *******************************************************************************/
129
130 struct lock_record_pool_t {
131         lock_record_pool_header_t header;        /* pool header (see above)       */
132         lock_record_t             lr[1];         /* variable array of records     */
133 };
134
135 #if defined(ENABLE_JVMTI)
136 extern pthread_mutex_t lock_global_pool_lock;
137 extern lock_record_pool_t *lock_global_pool;
138 #endif
139
140
141 /* functions ******************************************************************/
142
143 void lock_init(void);
144
145 void lock_init_execution_env(struct threadobject *thread);
146 void lock_record_free_pools(lock_record_pool_t *pool);
147
148 void lock_init_object_lock(java_objectheader *);
149 lock_record_t *lock_get_initial_lock_word(void);
150
151 ptrint lock_pre_compute_thinlock(s4 index);
152
153 void lock_monitor_enter(java_objectheader *);
154 bool lock_monitor_exit(java_objectheader *);
155
156 #define LOCK_monitor_enter    (functionptr) lock_monitor_enter
157 #define LOCK_monitor_exit     (functionptr) lock_monitor_exit
158
159 #define LOCK_MONITOR_ENTER(o)    lock_monitor_enter((java_objectheader *) (o))
160 #define LOCK_MONITOR_EXIT(o)     lock_monitor_exit((java_objectheader *) (o))
161
162 bool lock_is_held_by_current_thread(java_objectheader *o);
163
164 void lock_wait_for_object(java_objectheader *o, s8 millis, s4 nanos);
165 void lock_notify_object(java_objectheader *o);
166 void lock_notify_all_object(java_objectheader *o);
167
168 #endif /* _LOCK_H */
169
170
171 /*
172  * These are local overrides for various environment variables in Emacs.
173  * Please do not remove this and leave it at the end of the file, where
174  * Emacs will automagically detect them.
175  * ---------------------------------------------------------------------
176  * Local variables:
177  * mode: c
178  * indent-tabs-mode: t
179  * c-basic-offset: 4
180  * tab-width: 4
181  * End:
182  * vim:noexpandtab:sw=4:ts=4:
183  */