source: libcfa/src/bits/locks.hfa @ d0fcc82

ADTast-experimentalpthread-emulation
Last change on this file since d0fcc82 was ced5e2a, checked in by caparsons <caparson@…>, 2 years ago

removed debug fields from spinlock_t

  • Property mode set to 100644
File size: 2.3 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// bits/locks.hfa -- Basic spinlocks that are reused in the system.
8// Used for locks that aren't specific to cforall threads and can be used anywhere
9//
10//  *** Must not contain code specific to libcfathread ***
11//
12// Author           : Thierry Delisle
13// Created On       : Tue Oct 31 15:14:38 2017
14// Last Modified By : Peter A. Buhr
15// Last Modified On : Wed Aug 12 14:18:07 2020
16// Update Count     : 13
17//
18
19#pragma once
20
21#include "bits/debug.hfa"
22#include "bits/defs.hfa"
23#include <assert.h>
24
25struct __spinlock_t {
26        // Wrap in struct to prevent false sharing with debug info
27        volatile bool lock;
28};
29
30#ifdef __cforall
31        extern "C" {
32                extern void disable_interrupts() OPTIONAL_THREAD;
33                extern void enable_interrupts( bool poll = true ) OPTIONAL_THREAD;
34                #define __cfaabi_dbg_record_lock(x, y)
35        }
36
37        static inline void ?{}( __spinlock_t & this ) {
38                this.lock = 0;
39        }
40
41        // Lock the spinlock, return false if already acquired
42        static inline bool try_lock  ( __spinlock_t & this __cfaabi_dbg_ctx_param2 ) {
43                disable_interrupts();
44                bool result = (this.lock == 0) && (__atomic_test_and_set( &this.lock, __ATOMIC_ACQUIRE ) == 0);
45                if( result ) {
46                        __cfaabi_dbg_record_lock( this, caller );
47                } else {
48                        enable_interrupts( false );
49                }
50                return result;
51        }
52
53        // Lock the spinlock, spin if already acquired
54        static inline void lock( __spinlock_t & this __cfaabi_dbg_ctx_param2 ) {
55                #ifndef NOEXPBACK
56                        enum { SPIN_START = 4, SPIN_END = 64 * 1024, };
57                        unsigned int spin = SPIN_START;
58                #endif
59
60                disable_interrupts();
61                for ( unsigned int i = 1;; i += 1 ) {
62                        if ( (this.lock == 0) && (__atomic_test_and_set( &this.lock, __ATOMIC_ACQUIRE ) == 0) ) break;
63                        #ifndef NOEXPBACK
64                                // exponential spin
65                                for ( volatile unsigned int s = 0; s < spin; s += 1 ) Pause();
66
67                                // slowly increase by powers of 2
68                                if ( i % 64 == 0 ) spin += spin;
69
70                                // prevent overflow
71                                if ( spin > SPIN_END ) spin = SPIN_START;
72                        #else
73                                Pause();
74                        #endif
75                }
76                __cfaabi_dbg_record_lock( this, caller );
77        }
78
79        static inline void unlock( __spinlock_t & this ) {
80                __atomic_clear( &this.lock, __ATOMIC_RELEASE );
81                enable_interrupts( false );
82        }
83#endif
Note: See TracBrowser for help on using the repository browser.