Skip to content

Commit 2c171bf

Browse files
dcattaruzzapeterschrammel
authored andcommitted
Add basic concurrency support
1 parent 5fd9020 commit 2c171bf

File tree

5 files changed

+2240
-2
lines changed

5 files changed

+2240
-2
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (c) 1994, 2008, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
package java.lang;
27+
28+
public class IllegalThreadStateException extends IllegalArgumentException {
29+
private static final long serialVersionUID = -7626246362397460174L;
30+
31+
public IllegalThreadStateException() {
32+
super();
33+
}
34+
35+
public IllegalThreadStateException(String s) {
36+
super(s);
37+
}
38+
39+
// DIFFBLUE MODEL LIBRARY
40+
// While Object.getClass() is not modelled, we can get the same
41+
// functionality by adding one toString() method per subclass of
42+
// Throwable.
43+
@Override
44+
public String toString() {
45+
String message = getLocalizedMessage();
46+
return (message != null)
47+
? ("java.lang.IllegalThreadStateException: " + message)
48+
: "java.lang.IllegalThreadStateException";
49+
}
50+
}

src/main/java/java/lang/Object.java

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,20 @@
2626
package java.lang;
2727

2828
import org.cprover.CProver;
29+
import java.lang.NullPointerException;
30+
import java.lang.IllegalMonitorStateException;
2931

3032
public class Object {
33+
34+
// lock needed for synchronization in cbmc
35+
// used by monitorenter, monitorexit, wait, and notify
36+
// Not present in the original Object class
37+
public int monitorCount;
38+
39+
public Object() {
40+
monitorCount = 0;
41+
}
42+
3143
public final Class<?> getClass() {
3244
/*
3345
* MODELS LIBRARY {
@@ -57,11 +69,20 @@ public String toString() {
5769
return getClass().getName() + "@" + Integer.toHexString(hashCode());
5870
}
5971

60-
public final void notify() {}
72+
public final void notify()
73+
{
74+
// FIXME: the thread must own the lock when it calls notify
75+
}
6176

62-
public final void notifyAll() {}
77+
// See implementation of notify
78+
public final void notifyAll()
79+
{
80+
// FIXME: the thread must own the lock when it calls notifyAll
81+
}
6382

6483
public final void wait(long timeout) throws InterruptedException {
84+
// FIXME: the thread must own the lock when it calls wait
85+
// FIXME: should only throw if the interrupted flag in Thread is enabled
6586
throw new InterruptedException();
6687
}
6788

@@ -87,4 +108,51 @@ public final void wait() throws InterruptedException {
87108
}
88109

89110
protected void finalize() throws Throwable { }
111+
112+
/**
113+
* This method is not present in the original Objecct class.
114+
* It will be called by JBMC when the monitor in this instance
115+
* is being acquired as a result of either the execution of a
116+
* monitorenter bytecode instruction or the call to a synchronized
117+
* method. It uses a counter to enable reentrance and an atomic section
118+
* to ensure multiple threads do not race in the access/modification of
119+
* the counter.
120+
*/
121+
public static void monitorenter(Object object)
122+
{
123+
//FIXME: we shoud remove the call to this method from the call
124+
// stack appended to the thrown exception
125+
if (object == null)
126+
throw new NullPointerException();
127+
128+
CProver.atomicBegin();
129+
// this assume blocks this execution path in JBMC and simulates
130+
// the thread having to wait because the monitor is not available
131+
CProver.assume(object.monitorCount == 0);
132+
object.monitorCount++;
133+
CProver.atomicEnd();
134+
}
135+
136+
/**
137+
* This method is not present in the original Objecct class.
138+
* It will be called by JBMC when the monitor in this instance
139+
* is being released as a result of either the execution of a
140+
* monitorexit bytecode instruction or the return (normal or exceptional)
141+
* of a synchronized method. It decrements the monitorCounter that had
142+
* been incremented in monitorenter().
143+
*/
144+
public static void monitorexit(Object object)
145+
{
146+
//FIXME: we shoud remove the call to this method from the call
147+
// stack appended to the thrown exception
148+
if (object == null)
149+
throw new NullPointerException();
150+
151+
if (object.monitorCount == 0)
152+
throw new IllegalMonitorStateException();
153+
CProver.atomicBegin();
154+
object.monitorCount--;
155+
CProver.atomicEnd();
156+
}
157+
90158
}

src/main/java/java/lang/Runnable.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
package java.lang;
27+
28+
/**
29+
* The <code>Runnable</code> interface should be implemented by any
30+
* class whose instances are intended to be executed by a thread. The
31+
* class must define a method of no arguments called <code>run</code>.
32+
* <p>
33+
* This interface is designed to provide a common protocol for objects that
34+
* wish to execute code while they are active. For example,
35+
* <code>Runnable</code> is implemented by class <code>Thread</code>.
36+
* Being active simply means that a thread has been started and has not
37+
* yet been stopped.
38+
* <p>
39+
* In addition, <code>Runnable</code> provides the means for a class to be
40+
* active while not subclassing <code>Thread</code>. A class that implements
41+
* <code>Runnable</code> can run without subclassing <code>Thread</code>
42+
* by instantiating a <code>Thread</code> instance and passing itself in
43+
* as the target. In most cases, the <code>Runnable</code> interface should
44+
* be used if you are only planning to override the <code>run()</code>
45+
* method and no other <code>Thread</code> methods.
46+
* This is important because classes should not be subclassed
47+
* unless the programmer intends on modifying or enhancing the fundamental
48+
* behavior of the class.
49+
*
50+
* @author Arthur van Hoff
51+
* @see java.lang.Thread
52+
* @see java.util.concurrent.Callable
53+
* @since JDK1.0
54+
*/
55+
@FunctionalInterface
56+
public interface Runnable {
57+
/**
58+
* When an object implementing interface <code>Runnable</code> is used
59+
* to create a thread, starting the thread causes the object's
60+
* <code>run</code> method to be called in that separately executing
61+
* thread.
62+
* <p>
63+
* The general contract of the method <code>run</code> is that it may
64+
* take any action whatsoever.
65+
*
66+
* @see java.lang.Thread#run()
67+
*/
68+
public abstract void run();
69+
}

0 commit comments

Comments
 (0)