github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/src/runtime/interrupt/interrupt_tinygoriscv.go (about)

     1  //go:build tinygo.riscv
     2  
     3  package interrupt
     4  
     5  import "device/riscv"
     6  
     7  // State represents the previous global interrupt state.
     8  type State uintptr
     9  
    10  // Disable disables all interrupts and returns the previous interrupt state. It
    11  // can be used in a critical section like this:
    12  //
    13  //	state := interrupt.Disable()
    14  //	// critical section
    15  //	interrupt.Restore(state)
    16  //
    17  // Critical sections can be nested. Make sure to call Restore in the same order
    18  // as you called Disable (this happens naturally with the pattern above).
    19  func Disable() (state State) {
    20  	return State(riscv.DisableInterrupts())
    21  }
    22  
    23  // Restore restores interrupts to what they were before. Give the previous state
    24  // returned by Disable as a parameter. If interrupts were disabled before
    25  // calling Disable, this will not re-enable interrupts, allowing for nested
    26  // cricital sections.
    27  func Restore(state State) {
    28  	riscv.EnableInterrupts(uintptr(state))
    29  }
    30  
    31  // In returns whether the system is currently in an interrupt.
    32  func In() bool {
    33  	// There is one exception that has the value 0 (instruction address
    34  	// misaligned), but it's not very likely and even if it happens, it's not
    35  	// really something that can be recovered from. Therefore I think it's safe
    36  	// to ignore it. It's handled specially (in handleException).
    37  	return riscv.MCAUSE.Get() != 0
    38  }