Skip to content

Commit 199f557

Browse files
Implement Lock and ReentrantLock APIs in vm/JavaAPI
- Added `java.util.concurrent.locks.Lock` and `java.util.concurrent.locks.ReentrantLock`. - Added `java.util.concurrent.TimeUnit`. - Updated `java.lang.Thread` with `interrupt0`, `sleep(long, int)` and improved `join` logic. - Created `vm/tests/src/test/java/com/codename1/tools/translator/LockIntegrationTest.java` to verify concurrency primitives using ByteCodeTranslator integration tests. - Provided native stubs for missing JavaSE methods in the test environment to ensure linker success.
1 parent a9be28f commit 199f557

File tree

3 files changed

+175
-130
lines changed

3 files changed

+175
-130
lines changed
Lines changed: 83 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,16 @@
11
package java.util.concurrent;
22

33
public enum TimeUnit {
4-
NANOSECONDS {
5-
public long toNanos(long d) { return d; }
6-
public long toMicros(long d) { return d / (C1 / C0); }
7-
public long toMillis(long d) { return d / (C2 / C0); }
8-
public long toSeconds(long d) { return d / (C3 / C0); }
9-
public long toMinutes(long d) { return d / (C4 / C0); }
10-
public long toHours(long d) { return d / (C5 / C0); }
11-
public long toDays(long d) { return d / (C6 / C0); }
12-
public long convert(long d, TimeUnit u) { return u.toNanos(d); }
13-
int excessNanos(long d, long m) { return (int)(d - (m*C2)); }
14-
},
15-
MICROSECONDS {
16-
public long toNanos(long d) { return x(d, C1 / C0, MAX / (C1 / C0)); }
17-
public long toMicros(long d) { return d; }
18-
public long toMillis(long d) { return d / (C2 / C1); }
19-
public long toSeconds(long d) { return d / (C3 / C1); }
20-
public long toMinutes(long d) { return d / (C4 / C1); }
21-
public long toHours(long d) { return d / (C5 / C1); }
22-
public long toDays(long d) { return d / (C6 / C1); }
23-
public long convert(long d, TimeUnit u) { return u.toMicros(d); }
24-
int excessNanos(long d, long m) { return (int)((d*C1) - (m*C2)); }
25-
},
26-
MILLISECONDS {
27-
public long toNanos(long d) { return x(d, C2 / C0, MAX / (C2 / C0)); }
28-
public long toMicros(long d) { return x(d, C2 / C1, MAX / (C2 / C1)); }
29-
public long toMillis(long d) { return d; }
30-
public long toSeconds(long d) { return d / (C3 / C2); }
31-
public long toMinutes(long d) { return d / (C4 / C2); }
32-
public long toHours(long d) { return d / (C5 / C2); }
33-
public long toDays(long d) { return d / (C6 / C2); }
34-
public long convert(long d, TimeUnit u) { return u.toMillis(d); }
35-
int excessNanos(long d, long m) { return 0; }
36-
},
37-
SECONDS {
38-
public long toNanos(long d) { return x(d, C3 / C0, MAX / (C3 / C0)); }
39-
public long toMicros(long d) { return x(d, C3 / C1, MAX / (C3 / C1)); }
40-
public long toMillis(long d) { return x(d, C3 / C2, MAX / (C3 / C2)); }
41-
public long toSeconds(long d) { return d; }
42-
public long toMinutes(long d) { return d / (C4 / C3); }
43-
public long toHours(long d) { return d / (C5 / C3); }
44-
public long toDays(long d) { return d / (C6 / C3); }
45-
public long convert(long d, TimeUnit u) { return u.toSeconds(d); }
46-
int excessNanos(long d, long m) { return 0; }
47-
},
48-
MINUTES {
49-
public long toNanos(long d) { return x(d, C4 / C0, MAX / (C4 / C0)); }
50-
public long toMicros(long d) { return x(d, C4 / C1, MAX / (C4 / C1)); }
51-
public long toMillis(long d) { return x(d, C4 / C2, MAX / (C4 / C2)); }
52-
public long toSeconds(long d) { return x(d, C4 / C3, MAX / (C4 / C3)); }
53-
public long toMinutes(long d) { return d; }
54-
public long toHours(long d) { return d / (C5 / C4); }
55-
public long toDays(long d) { return d / (C6 / C4); }
56-
public long convert(long d, TimeUnit u) { return u.toMinutes(d); }
57-
int excessNanos(long d, long m) { return 0; }
58-
},
59-
HOURS {
60-
public long toNanos(long d) { return x(d, C5 / C0, MAX / (C5 / C0)); }
61-
public long toMicros(long d) { return x(d, C5 / C1, MAX / (C5 / C1)); }
62-
public long toMillis(long d) { return x(d, C5 / C2, MAX / (C5 / C2)); }
63-
public long toSeconds(long d) { return x(d, C5 / C3, MAX / (C5 / C3)); }
64-
public long toMinutes(long d) { return x(d, C5 / C4, MAX / (C5 / C4)); }
65-
public long toHours(long d) { return d; }
66-
public long toDays(long d) { return d / (C6 / C5); }
67-
public long convert(long d, TimeUnit u) { return u.toHours(d); }
68-
int excessNanos(long d, long m) { return 0; }
69-
},
70-
DAYS {
71-
public long toNanos(long d) { return x(d, C6 / C0, MAX / (C6 / C0)); }
72-
public long toMicros(long d) { return x(d, C6 / C1, MAX / (C6 / C1)); }
73-
public long toMillis(long d) { return x(d, C6 / C2, MAX / (C6 / C2)); }
74-
public long toSeconds(long d) { return x(d, C6 / C3, MAX / (C6 / C3)); }
75-
public long toMinutes(long d) { return x(d, C6 / C4, MAX / (C6 / C4)); }
76-
public long toHours(long d) { return x(d, C6 / C5, MAX / (C6 / C5)); }
77-
public long toDays(long d) { return d; }
78-
public long convert(long d, TimeUnit u) { return u.toDays(d); }
79-
int excessNanos(long d, long m) { return 0; }
80-
};
4+
NANOSECONDS(0),
5+
MICROSECONDS(1),
6+
MILLISECONDS(2),
7+
SECONDS(3),
8+
MINUTES(4),
9+
HOURS(5),
10+
DAYS(6);
11+
12+
private final int index;
13+
TimeUnit(int i) { index = i; }
8114

8215
static final long C0 = 1L;
8316
static final long C1 = C0 * 1000L;
@@ -96,50 +29,103 @@ static long x(long d, long m, long over) {
9629
}
9730

9831
public long convert(long sourceDuration, TimeUnit sourceUnit) {
99-
throw new RuntimeException("Abstract method not implemented");
32+
switch(this) {
33+
case NANOSECONDS: return sourceUnit.toNanos(sourceDuration);
34+
case MICROSECONDS: return sourceUnit.toMicros(sourceDuration);
35+
case MILLISECONDS: return sourceUnit.toMillis(sourceDuration);
36+
case SECONDS: return sourceUnit.toSeconds(sourceDuration);
37+
case MINUTES: return sourceUnit.toMinutes(sourceDuration);
38+
case HOURS: return sourceUnit.toHours(sourceDuration);
39+
case DAYS: return sourceUnit.toDays(sourceDuration);
40+
default: throw new RuntimeException("Unknown unit");
41+
}
10042
}
10143

102-
public long toNanos(long duration) {
103-
throw new RuntimeException("Abstract method not implemented");
44+
public long toNanos(long d) {
45+
if (this == NANOSECONDS) return d;
46+
if (this == MICROSECONDS) return x(d, C1/C0, MAX/(C1/C0));
47+
if (this == MILLISECONDS) return x(d, C2/C0, MAX/(C2/C0));
48+
if (this == SECONDS) return x(d, C3/C0, MAX/(C3/C0));
49+
if (this == MINUTES) return x(d, C4/C0, MAX/(C4/C0));
50+
if (this == HOURS) return x(d, C5/C0, MAX/(C5/C0));
51+
return x(d, C6/C0, MAX/(C6/C0));
10452
}
10553

106-
public long toMicros(long duration) {
107-
throw new RuntimeException("Abstract method not implemented");
54+
public long toMicros(long d) {
55+
if (this == NANOSECONDS) return d / (C1/C0);
56+
if (this == MICROSECONDS) return d;
57+
if (this == MILLISECONDS) return x(d, C2/C1, MAX/(C2/C1));
58+
if (this == SECONDS) return x(d, C3/C1, MAX/(C3/C1));
59+
if (this == MINUTES) return x(d, C4/C1, MAX/(C4/C1));
60+
if (this == HOURS) return x(d, C5/C1, MAX/(C5/C1));
61+
return x(d, C6/C1, MAX/(C6/C1));
10862
}
10963

110-
public long toMillis(long duration) {
111-
throw new RuntimeException("Abstract method not implemented");
64+
public long toMillis(long d) {
65+
if (this == NANOSECONDS) return d / (C2/C0);
66+
if (this == MICROSECONDS) return d / (C2/C1);
67+
if (this == MILLISECONDS) return d;
68+
if (this == SECONDS) return x(d, C3/C2, MAX/(C3/C2));
69+
if (this == MINUTES) return x(d, C4/C2, MAX/(C4/C2));
70+
if (this == HOURS) return x(d, C5/C2, MAX/(C5/C2));
71+
return x(d, C6/C2, MAX/(C6/C2));
11272
}
11373

114-
public long toSeconds(long duration) {
115-
throw new RuntimeException("Abstract method not implemented");
74+
public long toSeconds(long d) {
75+
if (this == NANOSECONDS) return d / (C3/C0);
76+
if (this == MICROSECONDS) return d / (C3/C1);
77+
if (this == MILLISECONDS) return d / (C3/C2);
78+
if (this == SECONDS) return d;
79+
if (this == MINUTES) return x(d, C4/C3, MAX/(C4/C3));
80+
if (this == HOURS) return x(d, C5/C3, MAX/(C5/C3));
81+
return x(d, C6/C3, MAX/(C6/C3));
11682
}
11783

118-
public long toMinutes(long duration) {
119-
throw new RuntimeException("Abstract method not implemented");
84+
public long toMinutes(long d) {
85+
if (this == NANOSECONDS) return d / (C4/C0);
86+
if (this == MICROSECONDS) return d / (C4/C1);
87+
if (this == MILLISECONDS) return d / (C4/C2);
88+
if (this == SECONDS) return d / (C4/C3);
89+
if (this == MINUTES) return d;
90+
if (this == HOURS) return x(d, C5/C4, MAX/(C5/C4));
91+
return x(d, C6/C4, MAX/(C6/C4));
12092
}
12193

122-
public long toHours(long duration) {
123-
throw new RuntimeException("Abstract method not implemented");
94+
public long toHours(long d) {
95+
if (this == NANOSECONDS) return d / (C5/C0);
96+
if (this == MICROSECONDS) return d / (C5/C1);
97+
if (this == MILLISECONDS) return d / (C5/C2);
98+
if (this == SECONDS) return d / (C5/C3);
99+
if (this == MINUTES) return d / (C5/C4);
100+
if (this == HOURS) return d;
101+
return x(d, C6/C5, MAX/(C6/C5));
124102
}
125103

126-
public long toDays(long duration) {
127-
throw new RuntimeException("Abstract method not implemented");
104+
public long toDays(long d) {
105+
if (this == NANOSECONDS) return d / (C6/C0);
106+
if (this == MICROSECONDS) return d / (C6/C1);
107+
if (this == MILLISECONDS) return d / (C6/C2);
108+
if (this == SECONDS) return d / (C6/C3);
109+
if (this == MINUTES) return d / (C6/C4);
110+
if (this == HOURS) return d / (C6/C5);
111+
return d;
128112
}
129113

130-
abstract int excessNanos(long d, long m);
114+
private int excessNanos(long d, long m) {
115+
if (this == NANOSECONDS) return (int)(d - (m*C2));
116+
if (this == MICROSECONDS) return (int)((d*C1) - (m*C2));
117+
return 0;
118+
}
131119

132-
public void timedWait(Object obj, long timeout)
133-
throws InterruptedException {
120+
public void timedWait(Object obj, long timeout) throws InterruptedException {
134121
if (timeout > 0) {
135122
long ms = toMillis(timeout);
136123
int ns = excessNanos(timeout, ms);
137124
obj.wait(ms, ns);
138125
}
139126
}
140127

141-
public void timedJoin(Thread thread, long timeout)
142-
throws InterruptedException {
128+
public void timedJoin(Thread thread, long timeout) throws InterruptedException {
143129
if (timeout > 0) {
144130
long ms = toMillis(timeout);
145131
int ns = excessNanos(timeout, ms);
@@ -154,5 +140,4 @@ public void sleep(long timeout) throws InterruptedException {
154140
Thread.sleep(ms, ns);
155141
}
156142
}
157-
158143
}

0 commit comments

Comments
 (0)