Repository navigation
Expand file tree
/
Copy pathapi-threads.k
More file actions
379 lines (338 loc) · 12.7 KB
/
Copy pathapi-threads.k
File metadata and controls
379 lines (338 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
//@ \section{Module API-THREADS}
module API-THREADS
imports CORE-SORTS
imports METHOD-INVOKE-REST //for invokeImpl
imports VAR-LOOKUP //for typedLookup
/*@Java API related to threads and locks. Just the core part.*/
//@ \subsection{Method Thread.start()}
rule [thread-start]:
<thread>
<k>
invokeImpl(
methodRef(sig(MethodName:Id, _), Class:ClassType),
objectRef(OL:Int,_)::_,
[TId:Int :: _]
) => nothing::void
...
</k>
...
</thread>
(. =>
<thread>
<k>
//typedLookup(OL).runnable.run()
'Field(
cast(
class String2Id("java.lang.Thread"),
typedLookup(OL)
),,
String2Id("runnable")
)
.String2Id("run") (.TypedVals) ;
</k>
<tid> TId </tid>
...
</thread>
)
when
Class ==K class String2Id("java.lang.Thread") andBool Id2String(MethodName) ==String "startImpl"
//@ \subsection{Synchronized statement}
rule [Synchronized-first-time]:
<k>
synchronized (objectRef(OId:Int,_)::_) Block:K
=> try Block .CatchClauses finally releaseLock(OId)
...
</k>
<holds>... (.Map => OId |-> 1) ...</holds>
<busy> Busy:Set (.Set => SetItem(OId)) </busy>
when
notBool (OId in Busy)
[transition-threading, transition-sync]
rule [Synchronized-nested]:
<k>
synchronized (objectRef(OId:Int,_)::_) Block:K
=> try Block .CatchClauses finally releaseLock(OId)
...
</k>
<holds>... OId |-> (Level:Int => Level +Int 1) ...</holds>
[transition-threading, transition-sync]
rule [Synchronized-on-null]:
synchronized (null::_) _ => throw new classNullPointerException(null::classString);
/*@Release one level of lock for the given object.*/
syntax KItem ::= releaseLock ( Int )
rule [releaseLock]:
<k>
releaseLock(OL:Int) => .
...
</k>
<holds>... OL |-> (Level:Int => Level -Int 1) ...</holds>
[transition-threading]
/*
K performance evaluation with the rule below active and commented:
helloWorld vanilla: 22.5s
helloWorld rule commented: 22.15s
22_array_separated 42 tests, vanilla: 336s
22_array_separated 42 tests, rule commented: 335s
Conclusion: the rule below have no impact on performance.
*/
//todo mapItemByValue/solution
//rule [monitor-completely-released]:
// <holds>... (OL:Int |-> 0 => .Map) ...</holds>
// <busy>... (SetItem(OL) => .Set) ...</busy>
rule [monitor-completely-released]:
<holds> M:Map => removeMapItemsByValue(M,0) </holds>
<busy> S:Set => S -Set keys(M -Map removeMapItemsByValue(M,0)) </busy>
//@ \subsection{Thread.join()}
rule [thread-join]:
<k>
invokeImpl(
methodRef(sig(MethodName:Id, _), Class:ClassType), _,
[TId:Int :: _]
) => nothing::void
...
</k>
<interrupted> false </interrupted>
<terminated>... SetItem(TId) ...</terminated>
when
Class ==K class String2Id("java.lang.Thread") andBool Id2String(MethodName) ==String "joinImpl"
[transition-threading, transition-sync]
rule [thread-join-interrupted]:
<k>
invokeImpl(
methodRef(sig(MethodName:Id, _), Class:ClassType), _,
[TId:Int :: _]
)
=> throw new class String2Id("java.lang.InterruptedException") (null::classString);
...
</k>
<interrupted> true => false </interrupted>
when
Class ==K class String2Id("java.lang.Thread") andBool Id2String(MethodName) ==String "joinImpl"
[transition-threading, transition-sync]
//@ \subsection{Object.wait()}
rule [object-wait-notify-notifyAll-without-sync]:
<k>
objectRef(OL:Int,_)::_ . methodRef(sig(MethodName:Id, _), Class:ClassType) (.TypedVals)
=> throw new class String2Id("java.lang.IllegalMonitorStateException") (null::classString);
...
</k>
<holds> Holds:Map </holds>
when
Class ==K classObject
andBool (
Id2String(MethodName) ==String "wait"
orBool Id2String(MethodName) ==String "notify"
orBool Id2String(MethodName) ==String "notifyAll"
)
andBool notBool OL in keys(Holds)
/*@Second part of a wait. Waiting was already registered to <waitingThreads>.
When the record in <waitingThreads> will be deleted, waiting itself shall end.
*/
syntax KItem ::= waitImpl (
Int //Monitor object location
)
rule [waitImpl-interrupted]:
<k>
waitImpl(OL:Int) => throw new class String2Id("java.lang.InterruptedException") (null::classString);
...
</k>
<tid> TId:Int </tid>
<busy> Busy:Set (.Set => SetItem(OL)) </busy>
<interrupted> true => false </interrupted>
when
notBool OL in Busy
[transition-threading, transition-sync]
//@ \subsection{Object.notify(), Object.notifyAll()}
/*@Implementation of Object.notify(), extracted here to avoid rule superfluousness*/
syntax KItem ::= notifyImpl (
Int //store key of the monitor object
)
//todo mapItemByValue/solution
rule [notifyImpl-someone-waiting]:
<k>
notifyImpl(OL:Int) => nothing::void
...
</k>
<waitingThreads> M:Map => removeMapItemsByValue(M,OL) </waitingThreads>
[transition-threading]
rule [notifyImpl-no-one-waiting]:
<k>
notifyImpl(OL:Int) => nothing::void
...
</k>
<waitingThreads> WT:Map </waitingThreads>
when
notBool OL in values(WT)
[transition-threading]
rule [object-notifyAll]:
<k>
objectRef(OL:Int,_)::_ . methodRef(sig(MethodName:Id, _), Class:ClassType) (.TypedVals)
=> objectNotifyAllImpl(OL)
...
</k>
<holds>... OL |-> HoldLevel:Int ...</holds>
when
Class ==K classObject
andBool Id2String(MethodName) ==String "notifyAll"
andBool HoldLevel >=Int 1
/*@Implementation of Object.notifyAll(), extracted here to avoid rule superfluousness*/
syntax KItem ::= objectNotifyAllImpl (
Int //store key of the monitor object
)
//todo mapItemByValue/solution
rule [objectNotifyAllImpl-someone-waiting]:
<k>
objectNotifyAllImpl(OL:Int)
...
</k>
<waitingThreads> M:Map => removeMapItemsByValue(M,OL) </waitingThreads>
[transition-threading]
rule [objectNotifyAllImpl-no-one-waiting]:
<k>
objectNotifyAllImpl(OL:Int) => nothing::void
...
</k>
<waitingThreads> WT:Map </waitingThreads>
when
notBool OL in values(WT)
[transition-threading]
//@ \subsection{Thread.interrupt()}
rule [thread-interrupt]:
<thread>
<k>
invokeImpl(
methodRef(sig(MethodName:Id, _), Class:ClassType),
objectRef(OL:Int,_)::_,
[TId:Int :: _]
) => nothing::void
...
</k>
...
</thread>
<thread>
<tid> TId </tid>
<interrupted> _ => true </interrupted>
...
</thread>
when
Class ==K class String2Id("java.lang.Thread") andBool Id2String(MethodName) ==String "interruptImpl"
[transition-threading, transition-sync]
rule [thread-interrupt-ended-thread]:
<k>
invokeImpl(
methodRef(sig(MethodName:Id, _), Class:ClassType),
objectRef(OL:Int,_)::_,
[TId:Int :: _]
) => nothing::void
...
</k>
<terminated>... SetItem(TId) ...</terminated>
when
Class ==K class String2Id("java.lang.Thread") andBool Id2String(MethodName) ==String "interruptImpl"
[transition-threading, transition-sync]
//@ \subsection{Thread termination}
rule [ThreadTermination]:
( <thread>
<k> .K </k>
<holds> H:Map </holds>
<tid> TId:Int </tid>
...
</thread>
=> .)
<busy> Busy:Set => Busy -Set keys(H) </busy>
<terminated>... .Set => SetItem(TId) ...</terminated>
<globalPhase> ExecutionPhase </globalPhase>
<dissolveEmptyK> false </dissolveEmptyK>
//@ \subsection{Debug aids}
/*@ Dissolve all cells except <out> for the purpose of model checking. We will avoid duplicates caused by the same
<out> but different order of content in other cells.*/
rule [DissolveAllExceptOut]:
<T>
<out> Out:List </out>
<threads> .Bag </threads>
<globalPhase> ExecutionPhase </globalPhase>
<dissolveAllExceptOut> true </dissolveAllExceptOut>
...
</T>
=> <T> <out> Out </out> </T>
//@ \section{Methods wait() and notify()}
/*@
When \verb|object.wait()| is called the method call expression is replaced by \verb|waitImpl()|
-- an auxiliary function that is used later to exit from the waiting state.
The id of the current thread (\verb|TId|) has to be registered in the set inside $\kall{waitingThreads}{}$.
The cell $\kall{holds}{}$ attached to each thread stores the number of times the current thread
acquired the lock on each object. Here we use it to make sure that the current thread acquired the lock at least once
(see the side condition). Otherwise calling the method \verb|wait()| is illegal and we have to throw an exception, according
to Java API. The exceptional case is covered by other rules. Another cell matched here is $\kall{busy}{}$. It stores
the set of objects that serve as synchronization monitors -- arguments of blocks \verb|synchronized|. When
an object enters the waiting state it have to release the ownership of this monitor, this is reflected by deleting
the entry \verb|OL| from the set. In this rule the cell $\kall{thread}{}$ is used solely for clarity, to separate
the cells that are attached to a thread from global cells. In fact, if we would delete $\kall{thread}{}$ here and
keep just what is inside, the semantics of the rule would not change. This is because configuration abstraction
mechanism of \K would infer the surrounding context for each of $\kall{k}{}$ $\kall{tid}{}$ and $\kall{holds}{}$.
It would know to match them under the same $\kall{thread}{}$, even if there are multiple $\kall{thread}{}$ cells.
*/
rule [object-wait]:
<thread> //added just for clarity, to illustrate the distinctive context of <holds> and <busy>
<k>
objectRef(OId:Int, _)::_ . methodRef(sig(Method:Id, _), Class:ClassType) (.TypedVals)
=> waitImpl(OId)
...
</k>
<tid> TId:Int </tid>
<holds>... OId |-> HoldLevel:Int ...</holds>
...
</thread>
<busy>... (SetItem(OId) => .Set) ...</busy>
<waitingThreads>... (.Map => TId |-> OId ) ...</waitingThreads>
when
Class ==K classObject
andBool Id2String(Method) ==String "wait"
andBool HoldLevel >=Int 1
[transition-threading]
/*@
When another thread calls \verb|notify()| we have to make sure that the thread holds the monitor on the target object
(the side condition). Otherwise we have to throw an exception. The actual logic of \verb|notify()| is delegated
to \verb|notifyImpl()|, in order to avoid duplication. The construct \verb|notifyImpl()| requires two rules for two
cases -- the case when there is at least one thread waiting on the target object, and the case when there is no one.
*/
rule [object-notify]:
<k>
objectRef(OId:Int, _)::_ . methodRef(sig(Method:Id, _), Class:ClassType) (.TypedVals)
=> notifyImpl(OId)
...
</k>
<holds>... OId |-> HoldLevel:Int ...</holds>
when
Class ==K classObject
andBool Id2String(Method) ==String "notify"
andBool HoldLevel >=Int 1
/*@
Here we present the rule for the first case. If there is a thread waiting on the current object, then the object
identifier \verb|OId| will be present among the map values of $\kall{waitingThreads}{}$. By deleting the whole entry
associated to that value we enable the waiting thread to proceed. If there is no thread waiting for this object then
the term \verb|notifyImpl()| is simply consumed.
*/
//todo mapItemByValue/solution
rule [notifyImpl-someone-waiting]:
<k> notifyImpl(OId:Int) => nothing::void ...</k>
<waitingThreads> M:Map => removeMapItemsByValue(M,OId) </waitingThreads>
[transition-threading]
/*@
At this stage the rule for \verb|waitImpl()| could match. The rule checks in its side conditions that the current
thread id \verb|TId| is not among the waiting threads anymore. It also checks that the target object, represented by
\verb|OId| is not busy. This is required because the thread exiting from waiting state have to reacquire the monitor
on the target object. Finally, the rule have to make sure that the thread was not interrupted while it was waiting.
Otherwise another rule will match and will throw the appropriate exception.
*/
rule [waitImpl-main]:
<k> waitImpl(OId:Int) => nothing::void ...</k>
<tid> TId:Int </tid>
<busy> Busy:Set (.Set => SetItem(OId)) </busy>
<interrupted> false </interrupted>
<waitingThreads> WT:Map </waitingThreads>
when
(notBool TId in keys(WT))
andBool (notBool OId in Busy)
[transition-threading, transition-sync]
endmodule