github.com/aclements/go-misc@v0.0.0-20240129233631-2f6ede80790c/memmodel/hbtso.go (about)

     1  // Copyright 2016 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  // HBTSO is an HBGenerator that implements TSO.
     8  type HBTSO struct{}
     9  
    10  func (HBTSO) HappensBefore(p *Prog, i, j PC) HBType {
    11  	op1, op2 := p.OpAt(i), p.OpAt(j)
    12  	sameThread := i.TID == j.TID
    13  
    14  	switch {
    15  	case op1.Type == OpStore && op2.Type == OpStore:
    16  		// Stores are totally ordered.
    17  		return HBHappensBefore
    18  
    19  	case sameThread && op1.Type == OpLoad && op2.Type == OpLoad:
    20  		// Loads are program ordered.
    21  		return HBHappensBefore
    22  
    23  	case sameThread && op1.Type == OpLoad && op2.Type == OpStore:
    24  		// Loads are program ordered before stores. (But *not*
    25  		// the other way around.)
    26  		return HBHappensBefore
    27  
    28  	case op1.Type == OpStore && op2.Type == OpLoad && op1.Var == op2.Var:
    29  		// If the load observes the store, then the store
    30  		// happened before the load.
    31  		return HBConditional
    32  	}
    33  
    34  	return HBConcurrent
    35  }
    36  
    37  func (HBTSO) String() string {
    38  	return "TSO"
    39  }