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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
//! Thread-safe Snowflake Generator
//!
//! This module provides [`Generator`] which can safely be shared between threads. Its constructor
//! is also const allowing to use it in a `static` context.
//!
//! # Example
//! ```
//! use snowflaked::sync::Generator;
//!
//! static GENERATOR: Generator = Generator::new(0);
//!
//! fn generate_id() -> u64 {
//!     GENERATOR.generate()
//! }
//! ```

use std::time::SystemTime;

use crate::builder::Builder;
use crate::loom::{AtomicU64, Ordering};
use crate::time::{DefaultTime, Time};
use crate::{const_panic_new, Components, Snowflake, INSTANCE_MAX};

/// A generator for unique snowflake ids. Since [`generate`] accepts a `&self` reference this can
/// be used in a `static` context.
///
/// # Cloning
///
/// Cloning a `Generator` will create a second one with the same state as the original one. The
/// internal state is copied and not shared. If you need to share a single `Generator` you need to
/// manually wrap it in an [`Arc`] (or similar).
///
/// # Example
/// ```
/// use snowflaked::sync::Generator;
///
/// static GENERATOR: Generator = Generator::new(0);
///
/// fn generate_id() -> u64 {
///     GENERATOR.generate()
/// }
/// ```
///
/// [`generate`]: Self::generate
/// [`Arc`]: std::sync::Arc
#[derive(Debug)]
pub struct Generator {
    internal: InternalGenerator<SystemTime>,
}

impl Generator {
    /// Creates a new `Generator` using the given `instance`.
    ///
    /// # Panics
    ///
    /// Panics if `instance` exceeds the maximum value (2^10 - 1).
    #[cfg(not(loom))]
    #[inline]
    pub const fn new(instance: u16) -> Self {
        match Self::new_checked(instance) {
            Some(this) => this,
            None => const_panic_new(),
        }
    }

    /// Creates a new `Generator` using the given `instance`. Returns `None` if the instance
    /// exceeds the maximum instance value (2^10 - 1).
    #[cfg(not(loom))]
    #[inline]
    pub const fn new_checked(instance: u16) -> Option<Self> {
        if instance > INSTANCE_MAX {
            None
        } else {
            Some(Self::new_unchecked(instance))
        }
    }

    /// Creates a new `Generator` using the given `instance` without checking if it exceeds the
    /// maximum value (2^10 - 1).
    ///
    /// Note: If `instance` exceeds the maximum value the `Generator` will create incorrect
    /// snowflakes.
    #[cfg(not(loom))]
    #[inline]
    pub const fn new_unchecked(instance: u16) -> Self {
        Self {
            internal: InternalGenerator::new_unchecked(instance),
        }
    }

    /// Creates a new `Builder` used to configure a `Generator`.
    ///
    /// # Examples
    ///
    /// ```
    /// # use snowflaked::sync::Generator;
    /// use std::time::SystemTime;
    ///
    /// let epoch = SystemTime::now();
    /// let generator: Generator = Generator::builder().instance(123).epoch(epoch).build();
    ///
    /// assert_eq!(generator.instance(), 123);
    /// assert_eq!(generator.epoch(), epoch);
    /// ```
    #[inline]
    pub const fn builder() -> Builder {
        Builder::new()
    }

    /// Returns the configured instance component of this `Generator`.
    ///
    /// # Examples
    ///
    /// ```
    /// # use snowflaked::sync::Generator;
    /// #
    /// let mut generator = Generator::new(123);
    ///
    /// assert_eq!(generator.instance(), 123);
    /// ```
    #[inline]
    pub fn instance(&self) -> u16 {
        self.internal.instance()
    }

    /// Returns the configured epoch of this `Generator`. By default this is [`UNIX_EPOCH`].
    ///
    /// # Examples
    ///
    /// ```
    /// # use snowflaked::sync::Generator;
    /// use std::time::UNIX_EPOCH;
    ///
    /// let generator = Generator::new(123);
    /// assert_eq!(generator.epoch(), UNIX_EPOCH);
    /// ```
    ///
    /// [`UNIX_EPOCH`]: std::time::UNIX_EPOCH
    #[inline]
    pub fn epoch(&self) -> SystemTime {
        self.internal.epoch()
    }

    /// Generate a new unique snowflake id.
    pub fn generate<T>(&self) -> T
    where
        T: Snowflake,
    {
        self.internal.generate(std::hint::spin_loop)
    }
}

impl From<Builder> for Generator {
    fn from(builder: Builder) -> Self {
        let internal = InternalGenerator {
            components: AtomicU64::new(Components::new(builder.instance as u64).to_bits()),
            epoch: builder.epoch,
        };

        Self { internal }
    }
}

#[derive(Debug)]
struct InternalGenerator<T>
where
    T: Time,
{
    components: AtomicU64,
    epoch: T,
}

impl<T> InternalGenerator<T>
where
    T: Time,
{
    #[cfg(not(loom))]
    #[inline]
    const fn new_unchecked(instance: u16) -> Self
    where
        T: DefaultTime,
    {
        Self {
            components: AtomicU64::new(Components::new(instance as u64).to_bits()),
            epoch: T::DEFAULT,
        }
    }

    // AtomicU64 is not const, we have to choose a different code path
    // than the regular `new_unchecked`.
    #[cfg(loom)]
    #[inline]
    fn new_unchecked(instance: u16) -> Self
    where
        T: DefaultTime,
    {
        Self {
            components: AtomicU64::new(Components::new(instance as u64).to_bits()),
            epoch: T::DEFAULT,
        }
    }

    #[cfg(loom)]
    #[inline]
    fn new_unchecked_with_epoch(instance: u16, epoch: T) -> Self {
        Self {
            components: AtomicU64::new(Components::new(instance as u64).to_bits()),
            epoch,
        }
    }

    #[inline]
    fn instance(&self) -> u16 {
        let bits = self.components.load(Ordering::Relaxed);
        Components::from_bits(bits).instance() as u16
    }

    #[inline]
    fn epoch(&self) -> T
    where
        T: Copy,
    {
        self.epoch
    }

    fn generate<S, F>(&self, tick_wait: F) -> S
    where
        S: Snowflake,
        F: Fn(),
    {
        use std::cmp;

        // Since `fetch_update` doesn't return a result,
        // we store the result in this mutable variable.
        // Note that using MaybeUninit is not necessary
        // as the compiler is smart enough to elide the Option for us.
        let mut id = None;

        let _ = self
            .components
            .fetch_update(Ordering::Relaxed, Ordering::Relaxed, |bits| {
                let mut components = Components::from_bits(bits);
                let mut now = self.epoch.elapsed().as_millis() as u64;
                let instance = components.instance();
                match now.cmp(&components.timestamp()) {
                    cmp::Ordering::Less => {
                        panic!("Clock has moved backwards! This is not supported");
                    }
                    cmp::Ordering::Greater => {
                        components.reset_sequence();
                        components.set_timestamp(now);
                        id = Some(S::from_parts(now, instance, 0));
                        Some(components.to_bits())
                    }
                    cmp::Ordering::Equal => {
                        let sequence = components.take_sequence();
                        if sequence == 0 {
                            now = Self::wait_until_next_millisecond(&self.epoch, now, &tick_wait);
                        }
                        components.set_timestamp(now);
                        id = Some(S::from_parts(now, instance, sequence));
                        Some(components.to_bits())
                    }
                }
            });
        id.expect("ID should have been set within the fetch_update closure.")
    }

    fn wait_until_next_millisecond<F>(epoch: &T, last_millisecond: u64, tick_wait: F) -> u64
    where
        F: Fn(),
    {
        loop {
            let now = epoch.elapsed().as_millis() as u64;
            if now > last_millisecond {
                return now;
            }
            tick_wait();
        }
    }
}

#[cfg(all(test, not(loom)))]
mod tests {
    use std::sync::mpsc;
    use std::thread;

    use super::Generator;
    use crate::Snowflake;

    #[test]
    fn test_generate() {
        const INSTANCE: u64 = 0;

        let mut last_id = None;
        let generator = Generator::new_unchecked(INSTANCE as u16);

        for _ in 0..10_000 {
            let id: u64 = generator.generate();
            assert_eq!(id.instance(), INSTANCE);
            assert!(
                last_id < Some(id),
                "expected {:?} to be less than {:?}",
                last_id,
                Some(id)
            );
            last_id = Some(id);
        }
    }

    #[test]
    fn test_generate_threads() {
        const INSTANCE: u64 = 0;
        const THREADS: usize = 4;

        static GENERATOR: Generator = Generator::new_unchecked(INSTANCE as u16);

        let (tx, rx) = mpsc::sync_channel::<Vec<u64>>(THREADS);

        for _ in 0..THREADS {
            let tx = tx.clone();
            thread::spawn(move || {
                let mut ids = Vec::with_capacity(10_000);

                for _ in 0..10_000 {
                    ids.push(GENERATOR.generate());
                }

                tx.send(ids).unwrap();
            });
        }

        let mut ids = Vec::with_capacity(10_000 * THREADS);
        for _ in 0..THREADS {
            ids.extend(rx.recv().unwrap());
        }

        for (index, id) in ids.iter().enumerate() {
            for (index2, id2) in ids.iter().enumerate() {
                if index != index2 && id == id2 {
                    panic!(
                        "Found duplicate id {} (SEQ {}, INS {}, TS {}) at index {} and {}",
                        id,
                        id.sequence(),
                        id.instance(),
                        id.timestamp(),
                        index,
                        index2
                    );
                }
            }
        }
    }

    #[test]
    fn test_generate_no_duplicates() {
        let generator = Generator::new_unchecked(0);
        let mut ids: Vec<u64> = Vec::with_capacity(10_000);

        for _ in 0..ids.capacity() {
            ids.push(generator.generate());
        }

        for (index, id) in ids.iter().enumerate() {
            for (index2, id2) in ids.iter().enumerate() {
                if index != index2 && id == id2 {
                    panic!(
                        "Found duplicate id {} (SEQ {}, INS {}, TS {}) at index {} and {}",
                        id,
                        id.sequence(),
                        id.instance(),
                        id.timestamp(),
                        index,
                        index2
                    );
                }
            }
        }
    }

    // #[test]
    // fn test_generator_clone() {
    //     let orig = Generator::new_unchecked(0);

    //     let cloned = orig.clone();

    //     let orig_bits = Components::from_bits(orig.components.load(Ordering::Relaxed));
    //     let cloned_bits = Components::from_bits(cloned.components.load(Ordering::Relaxed));

    //     assert_eq!(orig_bits, cloned_bits);
    // }
}

#[cfg(all(test, loom))]
mod loom_tests {
    use std::sync::{mpsc, Arc, Mutex};
    use std::time::Duration;

    use loom::thread;

    use super::InternalGenerator;
    use crate::loom::Ordering;
    use crate::time::{DefaultTime, Time};
    use crate::Components;

    #[derive(Copy, Clone, Debug)]
    pub struct TestTime(u64);

    impl Time for TestTime {
        fn elapsed(&self) -> Duration {
            Duration::from_millis(self.0)
        }
    }

    impl DefaultTime for TestTime {
        const DEFAULT: Self = Self(0);
    }

    fn panic_on_wait() {
        panic!("unexpected wait");
    }

    const THREADS: usize = 2;

    #[test]
    fn no_duplicates_no_wrap() {
        loom::model(|| {
            let generator = Arc::new(InternalGenerator::<TestTime>::new_unchecked(0));
            let (tx, rx) = mpsc::channel();

            let threads: Vec<_> = (0..THREADS)
                .map(|_| {
                    let generator = generator.clone();
                    let tx = tx.clone();

                    thread::spawn(move || {
                        let id: u64 = generator.generate(panic_on_wait);
                        tx.send(id).unwrap();
                    })
                })
                .collect();

            for th in threads {
                th.join().unwrap();
            }

            let id1 = rx.recv().unwrap();
            let id2 = rx.recv().unwrap();
            assert_ne!(id1, id2);
        });
    }

    #[test]
    fn no_duplicates_wrap() {
        static DEFAULT_TIME: Mutex<u64> = Mutex::new(0);

        // FIXME: Using raw pointers here is not optimal, but
        // required to get DEFAULT working. Maybe
        #[derive(Clone, Debug)]
        struct TestTimeWrap(Arc<Mutex<u64>>);

        impl Time for TestTimeWrap {
            fn elapsed(&self) -> Duration {
                let ms = self.0.lock().unwrap();
                Duration::from_millis(*ms)
            }
        }

        loom::model(|| {
            let ticked = Arc::new(Mutex::new(false));
            let time = Arc::new(Mutex::new(0));

            let mut generator =
                InternalGenerator::new_unchecked_with_epoch(0, TestTimeWrap(time.clone()));

            // Move the generator into a wrapping state.
            generator.components.with_mut(|bits| {
                let mut components = Components::from_bits(*bits);
                components.set_sequence(4095);
                *bits = components.to_bits();
            });

            let generator = Arc::new(generator);
            let (tx, rx) = mpsc::channel();

            let threads: Vec<_> = (0..THREADS)
                .map(|_| {
                    let ticked = ticked.clone();
                    let time = time.clone();

                    let generator = generator.clone();
                    let tx = tx.clone();

                    thread::spawn(move || {
                        let id: u64 = generator.generate(move || {
                            let mut ticked = ticked.lock().unwrap();

                            if !*ticked {
                                *ticked = true;

                                let mut ms = time.lock().unwrap();
                                *ms += 1;
                            }
                        });

                        tx.send(id).unwrap();
                    })
                })
                .collect();

            for th in threads {
                th.join().unwrap();
            }

            let id1 = rx.recv().unwrap();
            let id2 = rx.recv().unwrap();
            assert_ne!(id1, id2);
        });
    }
}