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
//! # 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.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 dashmap::DashMap;
//! use std::sync::Arc;
//! use std::time::{Duration, Instant};
//! use tokio_lk::*;
//! use tokio::prelude::*;
//! use tokio::runtime::Runtime;
//! use tokio::timer::Delay;
//!
//! let mut rt = Runtime::new().unwrap();
//! let map = Arc::new(DashMap::new());
//! let now = Instant::now();
//! // this task will compete with task2 for lock at id 1
//! let task1 = Lock::fnew(1, map.clone())
//!     .and_then(|lock| Ok(lock))
//!     .and_then(|guard| {
//!         Delay::new(Instant::now() + Duration::from_millis(100))
//!             .map_err(|_| ())
//!             .map(move |_| guard)
//!     })
//!     .and_then(|_| Ok(()));
//! // this task will compete with task1 for lock at id 1
//! let task2 = Lock::fnew(1, map.clone())
//!     .and_then(|lock| Ok(lock))
//!     .and_then(|guard| {
//!         Delay::new(Instant::now() + Duration::from_millis(100))
//!             .map_err(|_| ())
//!             .map(move |_| guard)
//!     })
//!     .and_then(|_| Ok(()));
//! // no other task compete for lock at id 2
//! let task3 = Lock::fnew(2, map.clone())
//!     .and_then(|lock| Ok(lock))
//!     .and_then(|guard| {
//!         Delay::new(Instant::now() + Duration::from_millis(100))
//!             .map_err(|_| ())
//!             .map(move |_| guard)
//!     })
//!     .and_then(|_| Ok(()));
//! rt.block_on(task1.join3(task2, task3)).unwrap();
//! ```
//!
//! ## 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 6 times slower than the single threaded version.
#![feature(test)]

use dashmap::DashMap;
use lazy_static::lazy_static;
use std::sync::atomic::{AtomicUsize, Ordering::*};
use std::sync::Arc;
use tokio::prelude::*;
mod atomic_serial_waker;
use atomic_serial_waker::AtomicSerialWaker;
#[cfg(test)]
mod test;

/// the map type used to store lock keys
pub type MapType = 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
    }
}

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

struct AsyncInsert {
    pub(crate) target: usize,
    pub(crate) map: MapType,
}

impl Future for AsyncInsert {
    type Item = Arc<(AtomicUsize, AtomicUsize, AtomicSerialWaker)>;
    type Error = ();
    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        let value = self
            .map
            .entry(self.target)
            .and_modify(|e| {
                e.1.fetch_add(1, Relaxed);
            })
            .or_insert_with(|| {
                Arc::new((
                    AtomicUsize::new(0),
                    AtomicUsize::new(0),
                    AtomicSerialWaker::new(),
                ))
            })
            .clone();
        Ok(Async::Ready(value))
    }
}

impl Lock {
    /// 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
    pub fn new(target: usize, map: MapType) -> 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,
        }
    }
    /// Create a Lock instance on the future's result
    pub fn fnew(target: usize, map: MapType) -> impl Future<Item = Self, Error = ()> {
        let map2 = map.clone();
        let task = AsyncInsert { target, map };
        task.and_then(move |value| {
            TASK.wake();
            Ok(Self {
                target,
                value,
                map: map2,
                id: get_id(),
                has_guard: false,
            })
        })
    }
}

impl Clone for Lock {
    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 Future for Lock {
    type Item = Guard;
    type Error = ();
    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        self.value.2.register();
        if let Err(prev) = self
            .value
            .0
            .compare_exchange_weak(0, self.id, SeqCst, SeqCst)
        {
            if prev == self.id {
                self.value.2.wake();
                return Ok(Async::Ready(Guard::new(
                    self.target,
                    self.value.clone(),
                    self.map.clone(),
                )));
            }
            return Ok(Async::NotReady);
        }
        self.has_guard = true;
        self.value.2.wake();
        Ok(Async::Ready(Guard::new(
            self.target,
            self.value.clone(),
            self.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 {
    target: usize,
    value: Arc<(AtomicUsize, AtomicUsize, AtomicSerialWaker)>,
    map: MapType,
}

impl Guard {
    pub fn new(
        target: usize,
        value: Arc<(AtomicUsize, AtomicUsize, AtomicSerialWaker)>,
        map: MapType,
    ) -> Self {
        Self { target, value, map }
    }
}

unsafe impl Send for Guard {}
unsafe impl Sync for Guard {}

impl Drop for Guard {
    fn drop(&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 Drop for Lock {
    fn drop(&mut self) {
        if !self.has_guard && self.value.1.fetch_sub(1, Relaxed) == 1 {
            self.map.remove(&self.target);
        }
    }
}