@@ -73,10 +73,8 @@ pub fn client() -> Client {
73
73
}
74
74
75
75
struct ProxyData {
76
- /// The number of tokens assigned to this process.
77
- total : u16 ,
78
-
79
76
/// The number of tokens assigned to threads.
77
+ /// If this is 0, a single token is still assigned to this process, but is unused.
80
78
used : u16 ,
81
79
82
80
/// The number of threads requesting a token
@@ -98,7 +96,7 @@ impl Proxy {
98
96
pub fn new ( ) -> Arc < Self > {
99
97
let proxy = Arc :: new ( Proxy {
100
98
client : client ( ) ,
101
- data : Mutex :: new ( ProxyData { total : 1 , used : 1 , pending : 0 } ) ,
99
+ data : Mutex :: new ( ProxyData { used : 1 , pending : 0 } ) ,
102
100
wake_pending : Condvar :: new ( ) ,
103
101
helper : OnceLock :: new ( ) ,
104
102
} ) ;
@@ -112,7 +110,7 @@ impl Proxy {
112
110
if data. pending > 0 {
113
111
// Give the token to a waiting thread
114
112
token. drop_without_releasing ( ) ;
115
- data. total += 1 ;
113
+ assert ! ( data. used > 0 ) ;
116
114
data. used += 1 ;
117
115
data. pending -= 1 ;
118
116
proxy_. wake_pending . notify_one ( ) ;
@@ -131,10 +129,9 @@ impl Proxy {
131
129
pub fn acquire_thread ( & self ) {
132
130
let mut data = self . data . lock ( ) ;
133
131
134
- if data. total > data . used {
132
+ if data. used == 0 {
135
133
// There was a free token around. This can
136
134
// happen when all threads release their token.
137
- assert_eq ! ( data. total, 1 ) ;
138
135
assert_eq ! ( data. pending, 0 ) ;
139
136
data. used += 1 ;
140
137
} else {
@@ -158,8 +155,7 @@ impl Proxy {
158
155
data. used -= 1 ;
159
156
160
157
// Release the token unless it's the last one in the process
161
- if data. total > 1 {
162
- data. total -= 1 ;
158
+ if data. used > 0 {
163
159
drop ( data) ;
164
160
self . client . release_raw ( ) . ok ( ) ;
165
161
}
0 commit comments