* src/native/native.c: Moved to .cpp.
[cacao.git] / src / threads / lock.hpp
1 /* src/threads/lock.hpp - lock implementation
2
3    Copyright (C) 1996-2005, 2006, 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #ifndef _LOCK_HPP
27 #define _LOCK_HPP
28
29 #include <stdint.h>
30
31 #include "native/llni.h"
32
33 #include "threads/mutex.hpp"
34
35 #include "toolbox/list.hpp"
36
37 #include "vm/global.h"
38
39
40 /* typedefs *******************************************************************/
41
42 typedef struct lock_record_t    lock_record_t;
43 typedef struct lock_hashtable_t lock_hashtable_t;
44
45
46 /* lock_record_t ***************************************************************
47
48    Lock record struct representing an inflated ("fat") lock.
49
50 *******************************************************************************/
51
52 struct lock_record_t {
53         java_object_t       *object;             /* object for which this lock is */
54         struct threadobject *owner;              /* current owner of this monitor */
55         s4                   count;              /* recursive lock count          */
56         Mutex*               mutex;              /* mutex for synchronizing       */
57 #ifdef __cplusplus
58         List<threadobject*>* waiters;            /* list of threads waiting       */
59 #else
60         List* waiters;
61 #endif
62         lock_record_t       *hashlink;           /* next record in hash chain     */
63 };
64
65
66 /* lock_hashtable_t ************************************************************
67  
68    The global hashtable mapping objects to lock records.
69
70 *******************************************************************************/
71
72 struct lock_hashtable_t {
73     Mutex*               mutex;       /* mutex for synch. access to the table */
74         u4                   size;        /* number of slots                      */
75         u4                   entries;     /* current number of entries            */
76         lock_record_t      **ptr;         /* the table of slots, uses ext. chain. */
77 };
78
79
80 /* functions ******************************************************************/
81
82 #ifdef __cplusplus
83 extern "C" {
84 #endif
85
86 void lock_init(void);
87
88 void lock_init_object_lock(java_object_t *);
89
90 ptrint lock_pre_compute_thinlock(s4 index);
91
92 bool lock_monitor_enter(java_handle_t *);
93 bool lock_monitor_exit(java_handle_t *);
94
95 #define LOCK_monitor_enter    (functionptr) lock_monitor_enter
96 #define LOCK_monitor_exit     (functionptr) lock_monitor_exit
97
98 bool lock_is_held_by_current_thread(java_handle_t *o);
99
100 void lock_wait_for_object(java_handle_t *o, s8 millis, s4 nanos);
101 void lock_notify_object(java_handle_t *o);
102 void lock_notify_all_object(java_handle_t *o);
103
104 #ifdef __cplusplus
105 }
106 #endif
107
108
109 /* defines ********************************************************************/
110
111 /* only define the following stuff with thread enabled ************************/
112
113 #if defined(ENABLE_THREADS)
114
115 #define LOCK_INIT_OBJECT_LOCK(o) lock_init_object_lock((java_object_t *) (o))
116
117 #define LOCK_MONITOR_ENTER(o)    lock_monitor_enter((java_handle_t *) LLNI_QUICKWRAP(o))
118 #define LOCK_MONITOR_EXIT(o)     lock_monitor_exit((java_handle_t *) LLNI_QUICKWRAP(o))
119
120 #endif
121
122 #endif // _LOCK_HPP
123
124
125 /*
126  * These are local overrides for various environment variables in Emacs.
127  * Please do not remove this and leave it at the end of the file, where
128  * Emacs will automagically detect them.
129  * ---------------------------------------------------------------------
130  * Local variables:
131  * mode: c++
132  * indent-tabs-mode: t
133  * c-basic-offset: 4
134  * tab-width: 4
135  * End:
136  * vim:noexpandtab:sw=4:ts=4:
137  */