github.com/lirm/aeron-go@v0.0.0-20230415210743-920325491dc4/aeron/imagelist.go (about)

     1  /*
     2  Copyright 2016 Stanislav Liberman
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package aeron
    18  
    19  import (
    20  	"sync/atomic"
    21  	"unsafe"
    22  )
    23  
    24  // ImageList is a helper class to manage list of images atomically without locks
    25  type ImageList struct {
    26  	img unsafe.Pointer
    27  }
    28  
    29  // NewImageList is a factory method for ImageList
    30  func NewImageList() *ImageList {
    31  	list := new(ImageList)
    32  
    33  	var images []Image
    34  	list.img = unsafe.Pointer(&images)
    35  
    36  	return list
    37  }
    38  
    39  // Get returns a pointer to the underlying image array loaded atomically
    40  func (l *ImageList) Get() []Image {
    41  	return *(*[]Image)(atomic.LoadPointer(&l.img))
    42  }
    43  
    44  // Set atomically sets the reference to the underlying array
    45  func (l *ImageList) Set(imgs []Image) {
    46  	atomic.StorePointer(&l.img, unsafe.Pointer(&imgs))
    47  }
    48  
    49  // Empty is a convenience method to reset the contents of the list
    50  func (l *ImageList) Empty() (oldList []Image) {
    51  	oldList = l.Get()
    52  	l.Set(make([]Image, 0))
    53  	return
    54  }