github.com/songzhibin97/go-baseutils@v0.0.2-0.20240302024150-487d8ce9c082/structure/sets/skipset/flag.go (about)

     1  package skipset
     2  
     3  import "sync/atomic"
     4  
     5  const (
     6  	fullyLinked = 1 << iota
     7  	marked
     8  )
     9  
    10  type bitflag struct {
    11  	data uint32
    12  }
    13  
    14  func (f *bitflag) SetTrue(flags uint32) {
    15  	for {
    16  		old := atomic.LoadUint32(&f.data)
    17  		if old&flags != flags {
    18  			// Flag is 0, need set it to 1.
    19  			n := old | flags
    20  			if !atomic.CompareAndSwapUint32(&f.data, old, n) {
    21  				continue
    22  			}
    23  		}
    24  		return
    25  	}
    26  }
    27  
    28  func (f *bitflag) SetFalse(flags uint32) {
    29  	for {
    30  		old := atomic.LoadUint32(&f.data)
    31  		check := old & flags
    32  		if check != 0 {
    33  			// Flag is 1, need set it to 0.
    34  			n := old ^ check
    35  			if !atomic.CompareAndSwapUint32(&f.data, old, n) {
    36  				continue
    37  			}
    38  		}
    39  		return
    40  	}
    41  }
    42  
    43  func (f *bitflag) Get(flag uint32) bool {
    44  	return (atomic.LoadUint32(&f.data) & flag) != 0
    45  }
    46  
    47  func (f *bitflag) MGet(check, expect uint32) bool {
    48  	return (atomic.LoadUint32(&f.data) & check) == expect
    49  }