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