atomic

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Example
package main

import (
	"fmt"

	"github.com/wazazaby/gs/atomic"
)

func main() {
	ch := atomic.MakeCloseSafeChan[string](8)

	fmt.Println("Cap:", ch.Cap())
	fmt.Println("Len:", ch.Len())

	sent := ch.Send("foo")
	fmt.Println("Sent foo ->", sent)

	sent = ch.Send("bar")
	fmt.Println("Sent bar ->", sent)

	fmt.Println("Len:", ch.Len())

	foo, ok := ch.Receive()
	fmt.Printf("Got %s - chan open -> %t\n", foo, ok)

	bar, ok := ch.Receive()
	fmt.Printf("Got %s - chan open -> %t\n", bar, ok)

	sent = ch.Send("john")
	fmt.Println("Sent john ->", sent)

	sent = ch.Send("doe")
	fmt.Println("Sent doe ->", sent)

	fmt.Println(ch.Close())

	fmt.Println("Len:", ch.Len())

	for value := range ch.Iter() {
		fmt.Printf("Got %s in range\n", value)
	}

	sent = ch.Send("baz")
	fmt.Println("Sent baz ->", sent)

	baz, ok := ch.Receive()
	fmt.Printf("Is empty baz -> %t - chan open -> %t\n", baz == "", ok)

}
Output:

Cap: 8
Len: 0
Sent foo -> true
Sent bar -> true
Len: 2
Got foo - chan open -> true
Got bar - chan open -> true
Sent john -> true
Sent doe -> true
<nil>
Len: 2
Got john in range
Got doe in range
Sent baz -> false
Is empty baz -> true - chan open -> false

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CloseSafeChan added in v0.5.0

type CloseSafeChan[T any] struct {
	// contains filtered or unexported fields
}

CloseSafeChan is a wrapper around a channel, bringing thread-safety on concurrently executing send and close operations.

Any goroutine can safely close a CloseSafeChan instance without worrying if other goroutines are currently sending values.

This is guaranteed by the implementation's inner-state, using atomic compare-and-swap operations to transition from the channel open state to the closing, and then closed state.

During this transition, sending goroutines won't be able to send data on the channel, and will be notified by CloseSafeChan.Send returning false.

func MakeCloseSafeChan added in v0.5.0

func MakeCloseSafeChan[T any](size ...int) *CloseSafeChan[T]

MakeCloseSafeChan initializes a new CloseSafeChan instance.

The size argument works identically to the size argument when creating a standard channel using [make].

func (*CloseSafeChan[T]) Cap added in v0.5.0

func (c *CloseSafeChan[T]) Cap() int

Cap returns the CloseSafeChan instance's buffer capacity, in units of elements.

It simply calls [cap] on the underlying channel.

func (*CloseSafeChan[T]) Close added in v0.5.0

func (c *CloseSafeChan[T]) Close() error

Close closes the CloseSafeChan instance.

It is safe to call it from concurrently running goroutines, as only the first caller will be able to close the underlying channel.

It never returns an error.

func (*CloseSafeChan[T]) Iter added in v0.5.0

func (c *CloseSafeChan[T]) Iter() iter.Seq[T]

Iter returns an iterator ranging on all the values buffered or sent in the underlying channel.

It works exactly like ranging over a channel :

  • The iteration will stop once the CloseSafeChan instance is closed and there are no more values to be received
  • You can skip values using the continue statement
  • You can break out of the iteration using the break statement

func (*CloseSafeChan[T]) Len added in v0.5.0

func (c *CloseSafeChan[T]) Len() int

Len returns the number of elements queued (unread) in the CloseSafeChan instance's buffer.

It simply calls [len] on the underlying channel.

func (*CloseSafeChan[T]) Receive added in v0.5.0

func (c *CloseSafeChan[T]) Receive() (T, bool)

Receive receives a value from the CloseSafeChan instance.

It returns the value, and a bool indicating if the CloseSafeChan instance is open (true) or closed (false).

It works similarly to pulling from a goroutine using the arrow operator.

ch := make(chan struct{})
v, ok := <-ch

func (*CloseSafeChan[T]) Send added in v0.5.0

func (c *CloseSafeChan[T]) Send(value T) bool

Send sends a value to the CloseSafeChan instance.

It returns true if the CloseSafeChan instance is still open and the send operation succeeded, false if the CloseSafeChan instance is closing or is closed.

type Value

type Value[T any] struct {
	// contains filtered or unexported fields
}

func (*Value[T]) CompareAndSwap

func (v *Value[T]) CompareAndSwap(old T, new T) (swapped bool)

func (*Value[T]) Load

func (v *Value[T]) Load() (val T, loaded bool)

func (*Value[T]) Store

func (v *Value[T]) Store(val T)

func (*Value[T]) Swap

func (v *Value[T]) Swap(new T) (old T, swapped bool)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL