-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMutex.js
141 lines (122 loc) · 3.77 KB
/
Mutex.js
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
'use strict';
/**
* @file
* @author Andrey Galkin <[email protected]>
*
*
* Copyright 2014-2017 FutoIn Project (https://futoin.org)
* Copyright 2014-2017 Andrey Galkin <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const ISync = require( './ISync' );
const { DefenseRejected } = require( './Errors' );
const mtx_sync = ( asp, mtx, step, on_error ) => {
const root = asp._root;
const release_step = ( asi ) => {
mtx._release( root );
root._handle_success( asi._call_args );
};
asp._on_cancel = mtx._release_handler;
asp._queue = [
[
( asi ) => {
if ( mtx._lock( asp, root ) ) {
root._handle_success( asp._call_args );
} else {
asi.waitExternal();
asi._call_args = asp._call_args;
}
},
undefined,
],
[ step, on_error ],
[ release_step, undefined ],
];
};
/**
* Mutual exclusion mechanism for AsyncSteps
*/
class Mutex extends ISync {
/**
* C-tor
* @param {integer} [max=1] - maximum number of simultaneous critical section entries
* @param {integer} [max_queue=null] - limit queue length, if set
*/
constructor( max = 1, max_queue = null ) {
super();
this._max = max;
this._locked = 0;
this._owners = new WeakMap();
this._queue = [];
this._max_queue = max_queue;
this._release_handler = ( asi ) => {
this._release( asi._root );
};
}
_lock( asi, root ) {
const owners = this._owners;
const owned = owners.get( root );
if ( owned ) {
owners.set( root, owned + 1 );
return true;
} else if ( this._locked >= this._max ) {
const queue = this._queue;
const max_queue = this._max_queue;
if ( ( max_queue !== null ) && ( queue.length >= max_queue ) ) {
root.error( DefenseRejected, 'Mutex queue limit' );
}
queue.push( asi );
return false;
} else {
this._locked += 1;
owners.set( root, 1 );
return true;
}
}
_release( root ) {
const owners = this._owners;
const owned = owners.get( root );
if ( owned ) {
if ( owned > 1 ) {
owners.set( root, owned - 1 );
return;
}
owners.delete( root );
this._locked -= 1;
const queue = this._queue;
while ( queue.length ) {
const other_as = queue.shift();
if ( other_as.state ) {
const other_root = other_as._root;
this._lock( other_as, other_root );
other_root._handle_success( other_as._call_args );
break;
}
}
} else {
const idx = this._queue.indexOf( root );
if ( idx >= 0 ) {
this._queue.splice( idx, 1 );
}
}
}
sync( as, step, onerror ) {
as.add(
( as ) => {
mtx_sync( as, this, step, onerror );
}
);
}
}
module.exports = Mutex;