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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
//! # Tokio-lk
//!
//! ** A lock-by-id future for tokio **
//!
//! `Lock` future will return `Guard` once it gets the mutex.
//! to hold the lock in subsequent futures, move the `Guard` inside the future output.
//! to release the mutex, simply drops the `Guard` from your future chain.
//!
//! Each `Lock` object is assigned an **unique** id.
//! The uniqueness is promised until USIZE_MAX of id gets generated.
//! Make sure old Locks are dropped before you generate new Locks above this amount.
//!
//! ## Changelog
//! - 0.2.1 - add features for using either hashbrown or dashmap. add `KeyPool` for hashmap abstraction.
//! - 0.2.0 - bump to futures 0.3 and tokio 0.2
//! - 0.1.3 - now depends on [dashmap](https://crates.io/crates/dashmap) to replace `RwLock<HashMap>`
//! - 0.1.2 - first stable version
//!
//! ## Example:
//! ```rust,no_run
//! use std::time::{Duration, Instant};
//! use tokio_lk::*;
//! use futures::prelude::*;
//! use tokio::runtime::Runtime;
//! use tokio::time::delay_for;
//!
//! let mut rt = Runtime::new().unwrap();
//! let map = KeyPool::<MapType>::new();
//! let now = Instant::now();
//! // this task will compete with task2 for lock at id 1
//! let task1 = async {
//!     let _guard = Lock::fnew(1, map.clone()).await.await;
//!     delay_for(Duration::from_millis(100)).await;
//! };
//! // this task will compete with task1 for lock at id 1
//! let task2 = async {
//!     let _guard = Lock::fnew(1, map.clone()).await.await;
//!     delay_for(Duration::from_millis(100)).await;
//! };
//! // no other task compete for lock at id 2
//! let task3 = async {
//!     let _guard = Lock::fnew(2, map.clone()).await.await;
//!     delay_for(Duration::from_millis(100)).await;
//! };
//! rt.block_on(async { tokio::join!(task1, task2, task3) });
//! ```
//!
//! ## Features
//! - hashbrown
//!     * provides `MapType` as a type alias of `hashbrown::HashMap` for KeyPool initialization
//! - dashmap
//!     * provides `DashMapType` as a type alias of `dashmap::DashMap` for KeyPool initialization
//! - default: hashbrown
//! - all: both `hashbrown` and `dashmap`
//!
//! ## Benchmark
//! to run the benchmark, execute the following command in the prompt:
//! ```bash
//! cargo bench -- --nocapture
//! ```
//! The `lock1000_parallel` benchmark is to run 1000 futures locked by a single lock to update the
//! counter.
//! The `lock1000_serial` benchmark is to run run similar operations in a single thread.
//! Currently our implementation is about 4~5 times slower than the single threaded version.
#![feature(test, specialization)]

#[cfg(feature = "dashmap")]
use dashmap::DashMap;
use futures::prelude::*;
#[cfg(feature = "hashbrown")]
use hashbrown::HashMap;
use lazy_static::lazy_static;
use std::ops::{Deref, DerefMut};
use std::pin::Pin;
use std::sync::atomic::{AtomicUsize, Ordering::*};
use std::sync::{Arc, Mutex};
use std::task::{Context, Poll};
mod atomic_serial_waker;
use atomic_serial_waker::AtomicSerialWaker;
use std::fmt;
#[cfg(test)]
mod test;

/// the map type used to store lock keys
#[cfg(feature = "hashbrown")]
pub type MapType = Arc<Mutex<HashMap<usize, Arc<(AtomicUsize, AtomicUsize, AtomicSerialWaker)>>>>;
#[cfg(feature = "dashmap")]
pub type DashMapType = Arc<DashMap<usize, Arc<(AtomicUsize, AtomicUsize, AtomicSerialWaker)>>>;

lazy_static! {
    static ref ID: AtomicUsize = AtomicUsize::new(1);
    static ref TASK: AtomicSerialWaker = AtomicSerialWaker::new();
}

#[inline]
fn get_id() -> usize {
    let result = ID.fetch_add(1, Relaxed);
    if result == 0 {
        ID.fetch_add(1, Relaxed)
    } else {
        result
    }
}

pub struct KeyPool<T> {
    table: T,
}

pub trait NewKeyPool<T> {
    fn new() -> Self;
}

#[cfg(feature = "hashbrown")]
impl NewKeyPool<MapType> for KeyPool<MapType> {
    #[inline]
    fn new() -> Self {
        Self {
            table: Arc::new(Mutex::new(HashMap::new())),
        }
    }
}

#[cfg(feature = "dashmap")]
impl NewKeyPool<DashMapType> for KeyPool<DashMapType> {
    #[inline]
    fn new() -> Self {
        Self {
            table: Arc::new(DashMap::new()),
        }
    }
}

impl<T: Clone> Clone for KeyPool<T> {
    #[inline]
    fn clone(&self) -> Self {
        Self {
            table: self.table.clone(),
        }
    }
}

impl<T> Deref for KeyPool<T> {
    type Target = T;
    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.table
    }
}

impl<T: fmt::Debug> fmt::Debug for KeyPool<T> {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.table.fmt(f)
    }
}

impl<T> DerefMut for KeyPool<T> {
    #[inline]
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.table
    }
}

/// ### Lock future struct
/// The lock future refers to shared map to support lock-by-id functionality
#[derive(Debug)]
pub struct Lock<T>
where
    Lock<T>: Destruct<T>,
{
    target: usize,
    value: Arc<(AtomicUsize, AtomicUsize, AtomicSerialWaker)>,
    map: KeyPool<T>,
    id: usize,
    has_guard: bool,
}

pub struct AsyncInsert<T> {
    pub(crate) target: usize,
    pub(crate) map: KeyPool<T>,
}

#[cfg(feature = "hashbrown")]
impl Future for AsyncInsert<MapType> {
    type Output = Arc<(AtomicUsize, AtomicUsize, AtomicSerialWaker)>;
    #[inline]
    fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
        let pinned = Pin::get_mut(self);
        match pinned.map.try_lock() {
            Ok(mut wmap) => {
                let value = wmap
                    .entry(pinned.target)
                    .and_modify(|e| {
                        e.1.fetch_add(1, Relaxed);
                    })
                    .or_insert_with(|| {
                        Arc::new((
                            AtomicUsize::new(0),
                            AtomicUsize::new(0),
                            AtomicSerialWaker::new(),
                        ))
                    })
                    .clone();
                Poll::Ready(value)
            }
            Err(_) => {
                TASK.register(cx.waker());
                Poll::Pending
            }
        }
    }
}

#[cfg(feature = "dashmap")]
impl Future for AsyncInsert<DashMapType> {
    type Output = Arc<(AtomicUsize, AtomicUsize, AtomicSerialWaker)>;
    #[inline]
    fn poll(self: Pin<&mut Self>, _cx: &mut Context) -> Poll<Self::Output> {
        let pinned = Pin::get_mut(self);
        let value = pinned
            .map
            .entry(pinned.target)
            .and_modify(|e| {
                e.1.fetch_add(1, Relaxed);
            })
            .or_insert_with(|| {
                Arc::new((
                    AtomicUsize::new(0),
                    AtomicUsize::new(0),
                    AtomicSerialWaker::new(),
                ))
            })
            .clone();
        Poll::Ready(value)
    }
}

impl<T: Clone> Lock<T>
where
    Lock<T>: Destruct<T>,
    AsyncInsert<T>: Future<Output = Arc<(AtomicUsize, AtomicUsize, AtomicSerialWaker)>>,
{
    /// Create a Lock instance on the future's result
    #[inline]
    pub async fn fnew(target: usize, map: KeyPool<T>) -> Self {
        let map2 = map.clone();
        let value = AsyncInsert { target, map }.await;
        TASK.wake();
        Self {
            target,
            value,
            map: map2,
            id: get_id(),
            has_guard: false,
        }
    }
}

pub trait New<T: Clone>: Destruct<T> {
    fn new(target: usize, map: KeyPool<T>) -> Self;
}

#[cfg(feature = "hashbrown")]
impl New<MapType> for Lock<MapType> {
    /// Create a Lock instance.
    /// This operation might block threads from parking for a while
    /// Don't use this function inside tokio context
    /// please refer to `fnew` function to provide asynchrons `Lock` instance generation
    #[inline]
    fn new(target: usize, map: KeyPool<MapType>) -> Self {
        let id = get_id();
        let value = map
            .lock()
            .unwrap()
            .entry(target)
            .and_modify(|e| {
                e.1.fetch_add(1, Relaxed);
            })
            .or_insert_with(|| {
                Arc::new((
                    AtomicUsize::new(0),
                    AtomicUsize::new(1),
                    AtomicSerialWaker::new(),
                ))
            })
            .clone();
        TASK.wake();
        Self {
            target,
            value,
            map,
            id,
            has_guard: false,
        }
    }
}

#[cfg(feature = "dashmap")]
impl New<DashMapType> for Lock<DashMapType> {
    /// Create a Lock instance.
    /// This operation might block threads from parking for a while
    /// Don't use this function inside tokio context
    /// please refer to `fnew` function to provide asynchrons `Lock` instance generation
    #[inline]
    fn new(target: usize, map: KeyPool<DashMapType>) -> Self {
        let id = get_id();
        let value = map
            .entry(target)
            .and_modify(|e| {
                e.1.fetch_add(1, Relaxed);
            })
            .or_insert_with(|| {
                Arc::new((
                    AtomicUsize::new(0),
                    AtomicUsize::new(1),
                    AtomicSerialWaker::new(),
                ))
            })
            .clone();
        TASK.wake();
        Self {
            target,
            value,
            map,
            id,
            has_guard: false,
        }
    }
}

pub trait Destruct<T> {
    fn destruct(&mut self);
}

default impl<T> Destruct<T> for Lock<T> {
    #[inline]
    fn destruct(&mut self) {}
}

#[cfg(feature = "hashbrown")]
impl Destruct<MapType> for Lock<MapType> {
    #[inline]
    fn destruct(&mut self) {
        if !self.has_guard && self.value.1.fetch_sub(1, Relaxed) == 1 {
            self.map.lock().unwrap().remove(&self.target);
        }
    }
}

#[cfg(feature = "dashmap")]
impl Destruct<DashMapType> for Lock<DashMapType> {
    #[inline]
    fn destruct(&mut self) {
        if !self.has_guard && self.value.1.fetch_sub(1, Relaxed) == 1 {
            self.map.remove(&self.target);
        }
    }
}

default impl<T> Destruct<T> for Guard<T> {
    #[inline]
    fn destruct(&mut self) {}
}

#[cfg(feature = "hashbrown")]
impl Destruct<MapType> for Guard<MapType> {
    #[inline]
    fn destruct(&mut self) {
        self.value.0.swap(0, Relaxed);
        self.value.2.wake();
        if self.value.1.fetch_sub(1, AcqRel) == 1 {
            self.map.lock().unwrap().remove(&self.target);
        }
    }
}

#[cfg(feature = "dashmap")]
impl Destruct<DashMapType> for Guard<DashMapType> {
    #[inline]
    fn destruct(&mut self) {
        self.value.0.swap(0, Relaxed);
        self.value.2.wake();
        if self.value.1.fetch_sub(1, AcqRel) == 1 {
            self.map.remove(&self.target);
        }
    }
}

impl<T: Clone> Clone for Lock<T>
where
    Lock<T>: Destruct<T>,
{
    #[inline]
    fn clone(&self) -> Self {
        self.value.1.fetch_add(1, Relaxed);
        Self {
            target: self.target,
            value: self.value.clone(),
            map: self.map.clone(),
            id: get_id(),
            has_guard: false,
        }
    }
}

impl<T: Clone + Unpin> Future for Lock<T>
where
    Lock<T>: Destruct<T>,
    Guard<T>: Destruct<T>,
{
    type Output = Guard<T>;
    #[inline]
    fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
        let pinned = Pin::get_mut(self);
        pinned.value.2.register(cx.waker());
        if let Err(prev) = pinned
            .value
            .0
            .compare_exchange_weak(0, pinned.id, SeqCst, SeqCst)
        {
            if prev == pinned.id {
                pinned.value.2.wake();
                return Poll::Ready(Guard::new(
                    pinned.target,
                    pinned.value.clone(),
                    pinned.map.clone(),
                ));
            }
            return Poll::Pending;
        }
        pinned.has_guard = true;
        pinned.value.2.wake();
        Poll::Ready(Guard::new(
            pinned.target,
            pinned.value.clone(),
            pinned.map.clone(),
        ))
    }
}

/// the `Guard` functions like `MutexGuard` in the standard library.
/// hold it, and then you hold the mutex.
/// drop it, and you release the lock.
#[derive(Debug)]
pub struct Guard<T>
where
    Guard<T>: Destruct<T>,
{
    target: usize,
    value: Arc<(AtomicUsize, AtomicUsize, AtomicSerialWaker)>,
    map: KeyPool<T>,
}

impl<T> Guard<T>
where
    Guard<T>: Destruct<T>,
{
    #[inline]
    pub fn new(
        target: usize,
        value: Arc<(AtomicUsize, AtomicUsize, AtomicSerialWaker)>,
        map: KeyPool<T>,
    ) -> Self {
        Self { target, value, map }
    }
}

unsafe impl<T> Send for Guard<T> where Guard<T>: Destruct<T> {}
unsafe impl<T> Sync for Guard<T> where Guard<T>: Destruct<T> {}

impl<T> Drop for Guard<T>
where
    Guard<T>: Destruct<T>,
{
    #[inline]
    fn drop(&mut self) {
        self.destruct();
    }
}

impl<T> Drop for Lock<T>
where
    Lock<T>: Destruct<T>,
{
    #[inline]
    fn drop(&mut self) {
        self.destruct();
    }
}