source: src/libcfa/bits/locks.h @ ea8b2f7

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumwith_gc
Last change on this file since ea8b2f7 was ea8b2f7, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Immetidate fix for halting processors, drifting still an issue

  • Property mode set to 100644
File size: 4.0 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.h -- Fast internal locks.
8//
9// Author           : Thierry Delisle
10// Created On       : Tue Oct 31 15:14:38 2017
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Fri Mar 30 18:18:13 2018
13// Update Count     : 9
14//
15
16#pragma once
17
18#include "bits/debug.h"
19#include "bits/defs.h"
20#include <assert.h>
21
22#ifdef __cforall
23        extern "C" {
24                #include <pthread.h>
25        }
26#endif
27
28// pause to prevent excess processor bus usage
29#if defined( __sparc )
30        #define Pause() __asm__ __volatile__ ( "rd %ccr,%g0" )
31#elif defined( __i386 ) || defined( __x86_64 )
32        #define Pause() __asm__ __volatile__ ( "pause" : : : )
33#elif defined( __ARM_ARCH )
34        #define Pause() __asm__ __volatile__ ( "nop" : : : )
35#else
36        #error unsupported architecture
37#endif
38
39#if defined( __i386 ) || defined( __x86_64 ) || defined( __ARM_ARCH )
40        // Intel recommendation
41        #define __ALIGN__ __attribute__(( aligned (128) ))
42#elif defined( __sparc )
43        #define __ALIGN__ CALIGN
44#else
45        #error unsupported architecture
46#endif
47
48struct __spinlock_t {
49        // Wrap in struct to prevent false sharing with debug info
50        struct {
51                // Align lock on 128-bit boundary
52                __ALIGN__ volatile _Bool lock;
53        };
54        #ifdef __CFA_DEBUG__
55                // previous function to acquire the lock
56                const char * prev_name;
57                // previous thread to acquire the lock
58                void* prev_thrd;
59        #endif
60} __ALIGN__;
61
62#ifdef __cforall
63        extern "C" {
64                extern void disable_interrupts();
65                extern void enable_interrupts_noPoll();
66        }
67
68        extern void yield( unsigned int );
69
70        static inline void ?{}( __spinlock_t & this ) {
71                this.lock = 0;
72        }
73
74
75        #ifdef __CFA_DEBUG__
76                void __cfaabi_dbg_record(__spinlock_t & this, const char * prev_name);
77        #else
78                #define __cfaabi_dbg_record(x, y)
79        #endif
80
81        // Lock the spinlock, return false if already acquired
82        static inline _Bool try_lock  ( __spinlock_t & this __cfaabi_dbg_ctx_param2 ) {
83                _Bool result = (this.lock == 0) && (__atomic_test_and_set( &this.lock, __ATOMIC_ACQUIRE ) == 0);
84                if( result ) {
85                        disable_interrupts();
86                        __cfaabi_dbg_record( this, caller );
87                }
88                return result;
89        }
90
91        // Lock the spinlock, spin if already acquired
92        static inline void lock( __spinlock_t & this __cfaabi_dbg_ctx_param2 ) {
93                #ifndef NOEXPBACK
94                        enum { SPIN_START = 4, SPIN_END = 64 * 1024, };
95                        unsigned int spin = SPIN_START;
96                #endif
97
98                for ( unsigned int i = 1;; i += 1 ) {
99                        if ( (this.lock == 0) && (__atomic_test_and_set( &this.lock, __ATOMIC_ACQUIRE ) == 0) ) break;
100                        #ifndef NOEXPBACK
101                                // exponential spin
102                                for ( volatile unsigned int s = 0; s < spin; s += 1 ) Pause();
103
104                                // slowly increase by powers of 2
105                                if ( i % 64 == 0 ) spin += spin;
106
107                                // prevent overflow
108                                if ( spin > SPIN_END ) spin = SPIN_START;
109                        #else
110                                Pause();
111                        #endif
112                }
113                disable_interrupts();
114                __cfaabi_dbg_record( this, caller );
115        }
116
117        static inline void unlock( __spinlock_t & this ) {
118                enable_interrupts_noPoll();
119                __atomic_clear( &this.lock, __ATOMIC_RELEASE );
120        }
121
122
123        #ifdef __CFA_WITH_VERIFY__
124                extern bool __cfaabi_dbg_in_kernel();
125        #endif
126
127        struct __bin_sem_t {
128                int_fast8_t     counter;
129                pthread_mutex_t lock;
130                pthread_cond_t  cond;
131        };
132
133        static inline void ?{}(__bin_sem_t & this) with( this ) {
134                counter = 0;
135                pthread_mutex_init(&lock, NULL);
136                pthread_cond_init (&cond, NULL);
137        }
138
139        static inline void ^?{}(__bin_sem_t & this) with( this ) {
140                pthread_mutex_destroy(&lock);
141                pthread_cond_destroy (&cond);
142        }
143
144        static inline void wait(__bin_sem_t & this) with( this ) {
145                verify(__cfaabi_dbg_in_kernel());
146                pthread_mutex_lock(&lock);
147                if(counter != 0) {   // this must be a loop, not if!
148                        pthread_cond_wait(&cond, &lock);
149                }
150                counter = 1;
151                pthread_mutex_unlock(&lock);
152        }
153
154        static inline void post(__bin_sem_t & this) with( this ) {
155                verify(__cfaabi_dbg_in_kernel());
156                pthread_mutex_lock(&lock);
157                bool needs_signal = counter == 0;
158                counter = 1;
159                pthread_mutex_unlock(&lock);
160                if (!needs_signal)
161                        pthread_cond_signal(&cond);
162                }
163#endif
Note: See TracBrowser for help on using the repository browser.