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
//! Hardware transactional memory primitives

#![feature(core_intrinsics)]
#![feature(link_llvm_intrinsics)]
#![feature(test)]
#![warn(missing_docs)]

extern crate test;

#[cfg_attr(
    all(target_arch = "powerpc64", feature = "htm"),
    path = "./powerpc64.rs"
)]
#[cfg_attr(all(target_arch = "x86_64", feature = "rtm"), path = "./x86_64.rs")]
#[cfg_attr(
    not(any(
        all(target_arch = "x86_64", feature = "rtm"),
        all(target_arch = "powerpc64", feature = "htm")
    )),
    path = "./unsupported.rs"
)]
pub mod back;

use std::{
    cell::UnsafeCell,
    marker::PhantomData,
    ops::{Deref, DerefMut},
    sync::atomic::AtomicUsize,
};

/// Returns true if the platform may support hardware transactional memory.
#[inline]
pub const fn htm_supported() -> bool {
    back::htm_supported()
}

/// Returns true if the platform definitely supports hardware transactional memory.
#[inline]
pub fn htm_supported_runtime() -> bool {
    back::htm_supported_runtime()
}

/// Attempts to begin a hardware transaction.
///
/// Control is returned to the point where begin was called on a failed transaction, only the
/// [`BeginCode`] now contains the reason for the failure.
///
/// # Safety
///
/// It is unsafe to always retry the transaction after a failure. It is also unsafe to
/// never subsequently call `end`.
#[inline]
pub unsafe fn begin() -> BeginCode {
    BeginCode(back::begin())
}

/// Aborts an in progress hardware transaction.
///
/// # Safety
///
/// There must be an in progress hardware transaction.
#[inline]
pub unsafe fn abort() -> ! {
    back::abort()
}

/// Tests the current transactional state of the thread.
#[inline]
pub unsafe fn test() -> TestCode {
    TestCode(back::test())
}

/// Ends and commits an in progress hardware transaction.
///
/// # Safety
///
/// There must be an in progress hardware transaction.
#[inline]
pub unsafe fn end() {
    back::end()
}

/// The result of calling `begin`.
#[repr(transparent)]
#[derive(PartialEq, Eq, Ord, PartialOrd, Copy, Clone, Debug, Hash)]
pub struct BeginCode(back::BeginCode);

impl BeginCode {
    /// Returns true if the `BeginCode` represents a successfully started transaction.
    #[inline]
    pub fn is_started(&self) -> bool {
        self.0.is_started()
    }

    /// Returns true if the `BeginCode` represents a transaction that was explicitly [`abort`]ed.
    #[inline]
    pub fn is_explicit_abort(&self) -> bool {
        self.0.is_explicit_abort()
    }

    /// Returns true if retrying the hardware transaction is suggested.
    #[inline]
    pub fn is_retry(&self) -> bool {
        self.0.is_retry()
    }

    /// Returns true if the transaction aborted due to a memory conflict.
    #[inline]
    pub fn is_conflict(&self) -> bool {
        self.0.is_conflict()
    }

    /// Returns true if the transaction aborted due to running out of capacity.
    ///
    /// Hardware transactions are typically bounded by L1 cache sizes.
    #[inline]
    pub fn is_capacity(&self) -> bool {
        self.0.is_capacity()
    }
}

/// The result of calling `test`.
#[repr(transparent)]
#[derive(PartialEq, Eq, Ord, PartialOrd, Copy, Clone, Debug, Hash)]
pub struct TestCode(back::TestCode);

impl TestCode {
    /// Returns true if the current thread is in a hardware transaction.
    #[inline]
    pub fn in_transaction(&self) -> bool {
        self.0.in_transaction()
    }

    /// Returns true if the current thread is in a suspended hardware transaction.
    #[inline]
    pub fn is_suspended(&self) -> bool {
        self.0.is_suspended()
    }
}

/// A hardware memory transaction.
///
/// On drop, the transaction is committed.
#[derive(Debug)]
pub struct HardwareTx {
    _private: PhantomData<*mut ()>,
}

impl Drop for HardwareTx {
    #[inline]
    fn drop(&mut self) {
        unsafe { end() }
    }
}

impl HardwareTx {
    /// Starts a new hardware transaction.
    ///
    /// Takes a retry handler which is called on transaction abort. If the retry handler returns
    /// `Ok(())`, the transaction is retried. Any `Err` is passed back to the location where `new`
    /// was called.
    ///
    /// The retry handler is never called with [`BeginCode`]s where `code.is_started() == true`.
    ///
    /// # Safety
    ///
    /// It is unsafe to pass in a retry handler that never returns `Err`. It is also unsafe to leak
    /// the transaction, unless [`end`] is manually called sometime after.
    #[inline]
    pub unsafe fn new<F, E>(mut retry_handler: F) -> Result<Self, E>
    where
        F: FnMut(BeginCode) -> Result<(), E>,
    {
        loop {
            let b = begin();
            if std::intrinsics::likely(b.is_started()) {
                return Ok(HardwareTx {
                    _private: PhantomData,
                });
            } else {
                retry_handler(b)?
            }
        }
    }

    /// Aborts the current transaction.
    ///
    /// Aborting a hardware transaction will effectively reset the thread/call stack to the location
    /// where new was called, passing control to the retry handler.
    ///
    /// Even though this never returns, it does **not** panic.
    #[inline(always)]
    pub fn abort(&self) -> ! {
        unsafe { abort() }
    }
}

/// An atomic and hardware transactional usize.
#[derive(Debug)]
#[repr(transparent)]
pub struct HtmUsize {
    inner: UnsafeCell<AtomicUsize>,
}

unsafe impl Send for HtmUsize {}
unsafe impl Sync for HtmUsize {}

impl HtmUsize {
    /// Creates a new hardware transactional cell.
    #[inline]
    pub const fn new(value: usize) -> Self {
        HtmUsize {
            inner: UnsafeCell::new(AtomicUsize::new(value)),
        }
    }

    /// # Safety
    ///
    /// This is unsafe because AtomicUsize already allows mutation through immutable reference.
    /// Therefore, the returned mutable reference cannot escape this module.
    #[inline(always)]
    unsafe fn as_raw(&self, _: &HardwareTx) -> &mut AtomicUsize {
        &mut *self.inner.get()
    }

    /// Get the contained value transactionally.
    #[inline(always)]
    pub fn get(&self, htx: &HardwareTx) -> usize {
        unsafe { *self.as_raw(htx).get_mut() }
    }

    /// Set the contained value transactionally.
    #[inline(always)]
    pub fn set(&self, htx: &HardwareTx, value: usize) {
        unsafe { *self.as_raw(htx).get_mut() = value }
    }
}

impl Deref for HtmUsize {
    type Target = AtomicUsize;

    #[inline(always)]
    fn deref(&self) -> &Self::Target {
        unsafe { &*self.inner.get() }
    }
}

impl DerefMut for HtmUsize {
    #[inline(always)]
    fn deref_mut(&mut self) -> &mut Self::Target {
        unsafe { &mut *self.inner.get() }
    }
}

macro_rules! bench_tx {
    ($name:ident, $count:expr) => {
        #[bench]
        fn $name(bench: &mut test::Bencher) {
            const ITER_COUNT: usize = 1_000_000;
            const WORDS_WRITTEN: usize = $count;

            #[repr(align(4096))]
            struct AlignedArr([usize; WORDS_WRITTEN]);

            let mut arr = AlignedArr([0usize; WORDS_WRITTEN]);

            for (i, elem) in arr.0.iter_mut().enumerate() {
                unsafe { std::ptr::write_volatile(elem, test::black_box(elem.wrapping_add(i))) };
                test::black_box(elem);
            }

            bench.iter(move || {
                for _ in 0..ITER_COUNT {
                    unsafe {
                        let tx = HardwareTx::new(|_| -> Result<(), ()> { Err(()) });
                        for i in 0..arr.0.len() {
                            *arr.0.get_unchecked_mut(i) =
                                arr.0.get_unchecked_mut(i).wrapping_add(1);
                        }
                        drop(tx);
                    }
                }
            });
        }
    };
}

bench_tx! {bench_tx0000, 0}
bench_tx! {bench_tx0001, 1}
bench_tx! {bench_tx0002, 2}
bench_tx! {bench_tx0004, 4}
bench_tx! {bench_tx0008, 8}
bench_tx! {bench_tx0016, 16}
bench_tx! {bench_tx0024, 24}
bench_tx! {bench_tx0032, 32}
bench_tx! {bench_tx0040, 40}
bench_tx! {bench_tx0048, 48}
bench_tx! {bench_tx0056, 56}
bench_tx! {bench_tx0064, 64}
bench_tx! {bench_tx0072, 72}
bench_tx! {bench_tx0080, 80}
bench_tx! {bench_tx0112, 112}
bench_tx! {bench_tx0120, 120}
bench_tx! {bench_tx0128, 128}
bench_tx! {bench_tx0256, 256}

#[bench]
fn bench_abort(bench: &mut test::Bencher) {
    const ITER_COUNT: usize = 1_000_000;

    bench.iter(|| {
        for _ in 0..ITER_COUNT {
            unsafe {
                let tx = HardwareTx::new(|code| -> Result<(), ()> {
                    if code.is_explicit_abort() {
                        Err(())
                    } else {
                        Ok(())
                    }
                });
                drop(tx.map(|tx| tx.abort()));
            }
        }
    });
}

#[test]
fn begin_end() {
    const ITER_COUNT: usize = 1_000_000;

    let mut fails = 0;
    for _ in 0..ITER_COUNT {
        unsafe {
            let _tx = HardwareTx::new(|_| -> Result<(), ()> {
                fails += 1;
                Ok(())
            })
            .unwrap();
        }
    }
    println!(
        "fail rate {:.4}%",
        fails as f64 * 100.0 / (ITER_COUNT + fails) as f64
    );
}

#[test]
fn test_in_transaction() {
    for _ in 0..1000000 {
        unsafe {
            assert!(!test().in_transaction());
            let _tx = HardwareTx::new(|_| -> Result<(), ()> { Ok(()) }).unwrap();
            assert!(test().in_transaction());
        }
    }
}

#[test]
fn begin_abort() {
    let mut i = 0i32;
    let mut abort_count = 0;
    loop {
        let i = &mut i;
        *i += 1;
        unsafe {
            let tx = HardwareTx::new(|code| -> Result<(), ()> {
                if code.is_explicit_abort() {
                    abort_count += 1;
                    *i += 1;
                }
                Ok(())
            })
            .unwrap();

            if *i % 128 != 0 && *i != 1_000_000 {
                tx.abort();
            }
        }
        if *i == 1_000_000 {
            break;
        }
    }
    assert_eq!(abort_count, 992187);
}

#[test]
fn capacity_check() {
    use std::mem;

    const CACHE_LINE_SIZE: usize = 64 / mem::size_of::<usize>();

    let mut data = vec![0usize; 1000000];
    let mut capacity = 0;
    let end = data.len() / CACHE_LINE_SIZE;
    for i in (0..end).rev() {
        data[i * CACHE_LINE_SIZE] = data[i * CACHE_LINE_SIZE].wrapping_add(1);
        test::black_box(&mut data[i * CACHE_LINE_SIZE]);
    }
    for max in 0..end {
        let mut fail_count = 0;
        unsafe {
            let tx = HardwareTx::new(|code| {
                let cap = code.is_capacity();
                if cap {
                    fail_count += 1;
                }
                if !cap || fail_count < 1000 {
                    Ok(())
                } else {
                    Err(())
                }
            });
            let tx = match tx {
                Ok(tx) => tx,
                Err(()) => break,
            };
            for i in 0..max {
                let elem = data.get_unchecked_mut(i * CACHE_LINE_SIZE);
                *elem = elem.wrapping_add(1);
            }
            drop(tx);
        }
        capacity = max;
    }
    test::black_box(&mut data);
    println!("sum: {}", data.iter().sum::<usize>());
    // println!("Data: {:?}", data);
    println!(
        "Capacity: {}",
        capacity * mem::size_of::<usize>() * CACHE_LINE_SIZE
    );
}

#[test]
fn supported() {
    let compile = htm_supported();
    let runtime = htm_supported_runtime();
    if compile {
        assert!(runtime);
    }

    println!("");
    println!("compile time support check: {}", compile);
    println!("     runtime support check: {}", runtime);
}