Skip to content

Commit 990d652

Browse files
committed
introduce locking mechanism for SeqStorage
1 parent d86c071 commit 990d652

File tree

5 files changed

+57
-16
lines changed

5 files changed

+57
-16
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/sequence/storage/BasicSequenceStorage.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,20 @@ public final int length() {
4040

4141
@Override
4242
public void setNewLength(int length) {
43+
checkLock();
4344
this.length = length;
4445
}
4546

47+
protected final void incLength() {
48+
checkLock();
49+
this.length++;
50+
}
51+
52+
protected final void decLength() {
53+
checkLock();
54+
this.length--;
55+
}
56+
4657
public final int capacity() {
4758
return capacity;
4859
}
@@ -76,4 +87,5 @@ public void ensureCapacity(int newCapacity) throws ArithmeticException {
7687
public void minimizeCapacity() {
7788
capacity = length;
7889
}
90+
7991
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/sequence/storage/IntSequenceStorage.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public void insertIntItem(int idx, int value) {
136136
}
137137

138138
values[idx] = value;
139-
length++;
139+
incLength();
140140
}
141141

142142
@Override
@@ -166,7 +166,7 @@ public void setIntSliceInBound(int start, int stop, int step, IntSequenceStorage
166166
// range is the whole sequence?
167167
if (sameLengthProfile.profile(start == 0 && stop == length && step == 1)) {
168168
values = Arrays.copyOf(sequence.values, otherLength);
169-
length = otherLength;
169+
setNewLength(length);
170170
minimizeCapacity();
171171
return;
172172
}
@@ -177,12 +177,12 @@ public void setIntSliceInBound(int start, int stop, int step, IntSequenceStorage
177177
values[i] = sequence.values[j];
178178
}
179179

180-
length = length > stop ? length : stop;
180+
setNewLength(length > stop ? length : stop);
181181
}
182182

183183
public int popInt() {
184184
int pop = values[length - 1];
185-
length--;
185+
decLength();
186186
return pop;
187187
}
188188

@@ -199,7 +199,7 @@ public int indexOfInt(int value) {
199199
public void appendInt(int value) {
200200
ensureCapacity(length + 1);
201201
values[length] = value;
202-
length++;
202+
incLength();
203203
}
204204

205205
public void extendWithIntStorage(IntSequenceStorage other) {
@@ -211,7 +211,7 @@ public void extendWithIntStorage(IntSequenceStorage other) {
211211
values[i] = otherValues[j];
212212
}
213213

214-
length = extendedLength;
214+
setNewLength(extendedLength);
215215
}
216216

217217
@Override

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/sequence/storage/LongSequenceStorage.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public void insertLongItem(int idx, long value) {
146146
}
147147

148148
values[idx] = value;
149-
length++;
149+
incLength();
150150
}
151151

152152
@Override
@@ -176,7 +176,7 @@ public void setLongSliceInBound(int start, int stop, int step, LongSequenceStora
176176
// range is the whole sequence?
177177
if (sameLengthProfile.profile(start == 0 && stop == length)) {
178178
values = Arrays.copyOf(sequence.values, otherLength);
179-
length = otherLength;
179+
setNewLength(otherLength);
180180
minimizeCapacity();
181181
return;
182182
}
@@ -187,12 +187,12 @@ public void setLongSliceInBound(int start, int stop, int step, LongSequenceStora
187187
values[i] = sequence.values[j];
188188
}
189189

190-
length = length > stop ? length : stop;
190+
setNewLength(length > stop ? length : stop);
191191
}
192192

193193
public long popLong() {
194194
long pop = values[length - 1];
195-
length--;
195+
decLength();
196196
return pop;
197197
}
198198

@@ -209,7 +209,7 @@ public int indexOfLong(long value) {
209209
public void appendLong(long value) {
210210
ensureCapacity(length + 1);
211211
values[length] = value;
212-
length++;
212+
incLength();
213213
}
214214

215215
public void extendWithLongStorage(LongSequenceStorage other) {
@@ -221,7 +221,7 @@ public void extendWithLongStorage(LongSequenceStorage other) {
221221
values[i] = otherValues[j];
222222
}
223223

224-
length = extendedLength;
224+
setNewLength(extendedLength);
225225
}
226226

227227
@Override

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/sequence/storage/ObjectSequenceStorage.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public void insertItem(int idx, Object value) {
7272
}
7373

7474
values[idx] = value;
75-
length++;
75+
incLength();
7676
}
7777

7878
@Override
@@ -102,7 +102,7 @@ public void setObjectSliceInBound(int start, int stop, int step, ObjectSequenceS
102102
// range is the whole sequence?
103103
if (sameLengthProfile.profile(start == 0 && stop == length && step == 1)) {
104104
values = Arrays.copyOf(sequence.values, otherLength);
105-
length = otherLength;
105+
setNewLength(otherLength);
106106
minimizeCapacity();
107107
return;
108108
}
@@ -113,7 +113,7 @@ public void setObjectSliceInBound(int start, int stop, int step, ObjectSequenceS
113113
values[i] = sequence.values[j];
114114
}
115115

116-
length = length > stop ? length : stop;
116+
setNewLength(length > stop ? length : stop);
117117
}
118118

119119
@Override
@@ -150,7 +150,7 @@ public void increaseCapacityExact(int newCapacity) {
150150

151151
public Object popObject() {
152152
Object pop = values[length - 1];
153-
length--;
153+
decLength();
154154
return pop;
155155
}
156156

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/sequence/storage/SequenceStorage.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,17 @@
2525
*/
2626
package com.oracle.graal.python.runtime.sequence.storage;
2727

28+
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
29+
import com.oracle.graal.python.nodes.PRaiseNode;
30+
import com.oracle.truffle.api.CompilerDirectives;
31+
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
32+
2833
public abstract class SequenceStorage {
2934

35+
// Mutations lock
36+
protected boolean lock;
37+
@CompilationFinal private boolean lockingEnabled = false;
38+
3039
public enum ListStorageType {
3140
Uninitialized,
3241
Empty,
@@ -62,6 +71,26 @@ public boolean generalizesFrom(ListStorageType other) {
6271
}
6372
}
6473

74+
public final void setLock() {
75+
if (!lockingEnabled) {
76+
CompilerDirectives.transferToInterpreterAndInvalidate();
77+
lockingEnabled = true;
78+
}
79+
this.lock = true;
80+
}
81+
82+
public final void releaseLock() {
83+
this.lock = false;
84+
}
85+
86+
protected final void checkLock() {
87+
if (lockingEnabled) {
88+
if (lock) {
89+
PRaiseNode.getUncached().raise(PythonBuiltinClassType.ValueError);
90+
}
91+
}
92+
}
93+
6594
public abstract int length();
6695

6796
public abstract void setNewLength(int length);

0 commit comments

Comments
 (0)