Skip to content

Commit 26fd727

Browse files
committed
Fixed isLocked(type) for RWLockWriter, removed functionality from RWLockReader because it is incorrect
1 parent a4055ed commit 26fd727

File tree

7 files changed

+23
-42
lines changed

7 files changed

+23
-42
lines changed

docs/assets/search.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/classes/RWLockReader.html

Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.

docs/classes/RWLockWriter.html

Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.

docs/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
<a href="#js-async-locks" id="js-async-locks" style="color: inherit; text-decoration: none;">
33
<h1>js-async-locks</h1>
44
</a>
5-
<p><a href="https://gitlab.com/MatrixAI/open-source/js-async-locks/commits/master"><img src="https://gitlab.com/MatrixAI/open-source/js-async-locks/badges/master/pipeline.svg" alt="pipeline status"></a></p>
5+
<p>staging:<a href="https://gitlab.com/MatrixAI/open-source/js-async-locks/commits/staging"><img src="https://gitlab.com/MatrixAI/open-source/js-async-locks/badges/staging/pipeline.svg" alt="pipeline status"></a>
6+
master:<a href="https://gitlab.com/MatrixAI/open-source/js-async-locks/commits/master"><img src="https://gitlab.com/MatrixAI/open-source/js-async-locks/badges/master/pipeline.svg" alt="pipeline status"></a></p>
67
<p>Asynchronous lock utilities.</p>
78

89
<a href="#installation" id="installation" style="color: inherit; text-decoration: none;">

src/RWLockReader.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ class RWLockReader implements Lockable {
1616
protected readerCountBlocked: number = 0;
1717
protected _readerCount: number = 0;
1818
protected _writerCount: number = 0;
19-
protected activeLock: 'read' | 'write' | null = null;
2019

2120
public lock(
2221
type: 'read' | 'write',
@@ -70,10 +69,8 @@ class RWLockReader implements Lockable {
7069
throw e;
7170
}
7271
readersRelease();
73-
this.activeLock = 'read';
7472
} else {
7573
readersRelease();
76-
this.activeLock = 'read';
7774
// Yield for the first reader to finish locking
7875
await yieldMicro();
7976
}
@@ -86,7 +83,6 @@ class RWLockReader implements Lockable {
8683
this.writersRelease();
8784
}
8885
readersRelease();
89-
this.activeLock = null;
9086
// Allow semaphore to settle https://github.com/DirtyHairy/async-mutex/issues/54
9187
await yieldMicro();
9288
},
@@ -113,12 +109,10 @@ class RWLockReader implements Lockable {
113109
--this._writerCount;
114110
throw e;
115111
}
116-
this.activeLock = 'write';
117112
return [
118113
async () => {
119114
release();
120115
--this._writerCount;
121-
this.activeLock = null;
122116
// Allow semaphore to settle https://github.com/DirtyHairy/async-mutex/issues/54
123117
await yieldMicro();
124118
},
@@ -143,15 +137,8 @@ class RWLockReader implements Lockable {
143137
* Check if locked
144138
* If passed `type`, it will also check that the active lock is of that type
145139
*/
146-
public isLocked(type?: 'read' | 'write'): boolean {
147-
if (type != null) {
148-
return (
149-
this.activeLock === type &&
150-
(this.readersLock.isLocked() || this.writersLock.isLocked())
151-
);
152-
} else {
153-
return this.readersLock.isLocked() || this.writersLock.isLocked();
154-
}
140+
public isLocked(): boolean {
141+
return this.readersLock.isLocked() || this.writersLock.isLocked();
155142
}
156143

157144
public async waitForUnlock(timeout?: number): Promise<void> {

src/RWLockWriter.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ class RWLockWriter implements Lockable {
1717
protected readerCountBlocked: number = 0;
1818
protected _readerCount: number = 0;
1919
protected _writerCount: number = 0;
20-
protected activeLock: 'read' | 'write' | null = null;
2120

2221
public lock(
2322
type: 'read' | 'write',
@@ -71,9 +70,7 @@ class RWLockWriter implements Lockable {
7170
--this._readerCount;
7271
throw e;
7372
}
74-
this.activeLock = 'read';
7573
} else {
76-
this.activeLock = 'read';
7774
// Yield for the first reader to finish locking
7875
await yieldMicro();
7976
}
@@ -83,7 +80,6 @@ class RWLockWriter implements Lockable {
8380
// The last reader unlocks
8481
if (readerCount === 0) {
8582
this.readersRelease();
86-
this.activeLock = null;
8783
// Allow semaphore to settle https://github.com/DirtyHairy/async-mutex/issues/54
8884
await yieldMicro();
8985
}
@@ -130,13 +126,11 @@ class RWLockWriter implements Lockable {
130126
await yieldMicro();
131127
throw e;
132128
}
133-
this.activeLock = 'write';
134129
return [
135130
async () => {
136131
this.readersRelease();
137132
writersRelease();
138133
--this._writerCount;
139-
this.activeLock = null;
140134
// Allow semaphore to settle https://github.com/DirtyHairy/async-mutex/issues/54
141135
await yieldMicro();
142136
},
@@ -162,11 +156,10 @@ class RWLockWriter implements Lockable {
162156
* If passed `type`, it will also check that the active lock is of that type
163157
*/
164158
public isLocked(type?: 'read' | 'write'): boolean {
165-
if (type != null) {
166-
return (
167-
this.activeLock === type &&
168-
(this.readersLock.isLocked() || this.writersLock.isLocked())
169-
);
159+
if (type === 'read') {
160+
return this.readersLock.isLocked();
161+
} else if (type === 'write') {
162+
return this.writersLock.isLocked();
170163
} else {
171164
return this.readersLock.isLocked() || this.writersLock.isLocked();
172165
}

tests/RWLockReader.test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ describe(RWLockReader.name, () => {
77
test('withF', async () => {
88
const lock = new RWLockReader();
99
const p1 = withF([lock.read()], async ([lock]) => {
10-
expect(lock.isLocked('read')).toBe(true);
10+
expect(lock.isLocked()).toBe(true);
1111
expect(lock.readerCount).toBe(1);
1212
expect(lock.writerCount).toBe(0);
1313
});
@@ -19,7 +19,7 @@ describe(RWLockReader.name, () => {
1919
expect(lock.readerCount).toBe(0);
2020
expect(lock.writerCount).toBe(0);
2121
const p2 = withF([lock.write()], async ([lock]) => {
22-
expect(lock.isLocked('write')).toBe(true);
22+
expect(lock.isLocked()).toBe(true);
2323
expect(lock.readerCount).toBe(0);
2424
expect(lock.writerCount).toBe(1);
2525
});
@@ -38,22 +38,22 @@ describe(RWLockReader.name, () => {
3838
string,
3939
void
4040
> {
41-
expect(lock.isLocked('read')).toBe(true);
41+
expect(lock.isLocked()).toBe(true);
4242
expect(lock.readerCount).toBe(1);
4343
expect(lock.writerCount).toBe(0);
4444
yield 'first';
45-
expect(lock.isLocked('read')).toBe(true);
45+
expect(lock.isLocked()).toBe(true);
4646
expect(lock.readerCount).toBe(1);
4747
expect(lock.writerCount).toBe(0);
4848
yield 'second';
49-
expect(lock.isLocked('read')).toBe(true);
49+
expect(lock.isLocked()).toBe(true);
5050
expect(lock.readerCount).toBe(1);
5151
expect(lock.writerCount).toBe(0);
5252
return 'last';
5353
});
5454
for await (const _ of g1) {
5555
// It should be locked during iteration
56-
expect(lock.isLocked('read')).toBe(true);
56+
expect(lock.isLocked()).toBe(true);
5757
expect(lock.readerCount).toBe(1);
5858
expect(lock.writerCount).toBe(0);
5959
}
@@ -113,22 +113,22 @@ describe(RWLockReader.name, () => {
113113
string,
114114
void
115115
> {
116-
expect(lock.isLocked('write')).toBe(true);
116+
expect(lock.isLocked()).toBe(true);
117117
expect(lock.readerCount).toBe(0);
118118
expect(lock.writerCount).toBe(1);
119119
yield 'first';
120-
expect(lock.isLocked('write')).toBe(true);
120+
expect(lock.isLocked()).toBe(true);
121121
expect(lock.readerCount).toBe(0);
122122
expect(lock.writerCount).toBe(1);
123123
yield 'second';
124-
expect(lock.isLocked('write')).toBe(true);
124+
expect(lock.isLocked()).toBe(true);
125125
expect(lock.readerCount).toBe(0);
126126
expect(lock.writerCount).toBe(1);
127127
return 'last';
128128
});
129129
for await (const _ of g1) {
130130
// It should be locked during iteration
131-
expect(lock.isLocked('write')).toBe(true);
131+
expect(lock.isLocked()).toBe(true);
132132
expect(lock.readerCount).toBe(0);
133133
expect(lock.writerCount).toBe(1);
134134
}

0 commit comments

Comments
 (0)