* src/threads/native/lock.c, src/threads/native/lock.h: Rewritten
[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: Stefan Ring
28
29    Changes: Christian Thalinger
30                         Edwin Steiner
31
32    $Id: threads.h 4866 2006-05-01 21:40:38Z edwin $
33
34 */
35
36
37 #ifndef _LOCK_H
38 #define _LOCK_H
39
40 #include "config.h"
41 #include "vm/types.h"
42
43 /* XXX Darwin */
44 #include <semaphore.h>
45
46 #include "vm/global.h"
47
48 /* typedefs *******************************************************************/
49
50 typedef struct lock_execution_env_t      lock_execution_env_t;
51 typedef struct lock_record_t             lock_record_t;
52 typedef struct lock_record_pool_header_t lock_record_pool_header_t;
53 typedef struct lock_record_pool_t        lock_record_pool_t;
54 typedef struct lock_waiter_t             lock_waiter_t;
55
56
57 /* lock_execution_env_t ********************************************************
58
59    Execution environment. Contains the lock record freelist and pools.
60
61 *******************************************************************************/
62
63 struct lock_execution_env_t {
64         lock_record_t         *firstfree;        /* lock record freelist          */
65         lock_record_pool_t    *lockrecordpools;  /* list of per-thread pools      */
66         int                    lockrecordcount;  /* # of records for this thread  */
67 };
68
69
70 /* lock_waiter_t ***************************************************************
71
72    List node for storing a waiting thread.
73
74 *******************************************************************************/
75
76 struct lock_waiter_t {
77         struct threadobject *waiter;         /* the waiting thread                */
78         lock_waiter_t       *next;           /* next in list                      */
79 };
80
81
82 /* lock_record_t ***************************************************************
83
84    Lock record struct representing an inflated ("fat") lock.
85
86 *******************************************************************************/
87
88 struct lock_record_t {
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 };
95
96
97 /* XXXXXXXXXXXXXXXXX ***********************************************************
98
99 *******************************************************************************/
100
101 struct lock_record_pool_header_t {
102         lock_record_pool_t *next;                /* next pool                     */
103         int                 size;                /* records in this pool          */
104 }; 
105
106
107 /* XXXXXXXXXXXXXXXXX ***********************************************************
108
109 *******************************************************************************/
110
111 struct lock_record_pool_t {
112         lock_record_pool_header_t header;        /* pool header (see above)       */
113         lock_record_t             lr[1];         /* variable array of records     */
114 };
115
116
117 /* functions ******************************************************************/
118
119 void lock_init(void);
120
121 void lock_init_execution_env(struct threadobject *thread);
122 void lock_record_free_pools(lock_record_pool_t *pool);
123
124 void lock_init_object_lock(java_objectheader *);
125 lock_record_t *lock_get_initial_lock_word(void);
126
127 lock_record_t *lock_monitor_enter(struct threadobject *, java_objectheader *);
128 bool lock_monitor_exit(struct threadobject *, java_objectheader *);
129
130 bool lock_does_thread_hold_lock(struct threadobject *t, java_objectheader *o);
131
132 void lock_wait_for_object(java_objectheader *o, s8 millis, s4 nanos);
133 void lock_notify_object(java_objectheader *o);
134 void lock_notify_all_object(java_objectheader *o);
135
136 #endif /* _LOCK_H */
137
138
139 /*
140  * These are local overrides for various environment variables in Emacs.
141  * Please do not remove this and leave it at the end of the file, where
142  * Emacs will automagically detect them.
143  * ---------------------------------------------------------------------
144  * Local variables:
145  * mode: c
146  * indent-tabs-mode: t
147  * c-basic-offset: 4
148  * tab-width: 4
149  * End:
150  * vim:noexpandtab:sw=4:ts=4:
151  */