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