RWSafe

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2024 License: MIT Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Buffer

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

Buffer is a thread-safe, generic data structure that allows multiple goroutines to produce and consume elements in a synchronized manner. It is implemented as a queue and uses channels to synchronize the goroutines. The Buffer should be initialized with the Init method before use.

func (*Buffer[T]) CleanBuffer

func (b *Buffer[T]) CleanBuffer()

CleanBuffer removes all elements from the Buffer, effectively resetting it to an empty state. Precalculated elements are kept as they are no longer in the buffer but in the channel. It locks the firstMutex to ensure thread-safety during the operation.

This method is safe for concurrent use by multiple goroutines.

Note: This method assumes that the trimFrom method of the safeElement type is thread-safe.

If the Buffer is already empty, this method does nothing.

func (*Buffer[T]) Cleanup

func (b *Buffer[T]) Cleanup()

Cleanup is a method of the Buffer type. It resets the Buffer by clearing its internal state, setting the first and last pointers, and the receiveChannel and sendChannel to nil. This effectively frees all resources used by the Buffer.

This method is not thread-safe and should only be called when no other goroutines are accessing the Buffer. It waits until all goroutines launched by the Buffer have finished executing before proceeding.

func (*Buffer[T]) Init

func (b *Buffer[T]) Init(bufferSize int) (chan<- T, <-chan T)

Init initializes a Buffer instance. It ensures the initialization is done only once, even if called multiple times. It creates two channels and a condition variable, and starts two goroutines: one that listens for incoming messages on the send channel and adds them to the Buffer, and another that sends messages from the Buffer to the receive channel. This method should be called before using the Buffer.

Parameters:

  • bufferSize: The size of the buffer for the send and receive channels. Must be a non-negative integer. If a negative integer is provided, the method will panic.

Returns:

  • A send-only channel of type T. Messages sent to this channel are added to the Buffer.
  • A receive-only channel of type T. Messages from the Buffer are sent to this channel.

Information: To close the buffer, just close the send-only channel. Once that is done, a cascade of events will happen:

  • The goroutine that listens for incoming messages will stop listening and exit.
  • The goroutine that sends messages from the Buffer to the receive channel will stop sending messages once the Buffer is empty, and then exit.
  • The Buffer will be cleaned up.

func (*Buffer[T]) Wait

func (b *Buffer[T]) Wait()

Wait is a method of the Buffer type. It blocks the calling goroutine until all goroutines launched by the Buffer have finished executing. This is achieved by waiting for the internal WaitGroup to be done.

This method is thread-safe and can be called from multiple goroutines.

type RWSafe

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

func NewRWSafe

func NewRWSafe[T any](value T) *RWSafe[T]

func (*RWSafe[T]) Get

func (rw *RWSafe[T]) Get() T

func (*RWSafe[T]) Set

func (rw *RWSafe[T]) Set(value T)

type RWSafeQueue

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

RWSafeQueue is a thread-safe queue that allows multiple goroutines to produce and consume elements in a synchronized manner. It is implemented as a linked list with pointers to the first and last elements.

func NewRWSafeQueue

func NewRWSafeQueue[T any](values ...T) *RWSafeQueue[T]

NewRWSafeQueue initializes a new RWSafeQueue with a given set of initial values.

Parameters:

  • values: Zero or more initial values to add to the queue. The values are of type T.

Returns:

  • A pointer to the newly created RWSafeQueue.

func (*RWSafeQueue[T]) Clear

func (b *RWSafeQueue[T]) Clear()

Clear removes all values from the RWSafeQueue.

func (*RWSafeQueue[T]) Dequeue

func (b *RWSafeQueue[T]) Dequeue() T

Dequeue removes the first value from the RWSafeQueue and returns it.

The method returns the value that was removed from the queue.

If the queue is empty, the method panics with an appropriate message.

func (*RWSafeQueue[T]) Enqueue

func (b *RWSafeQueue[T]) Enqueue(value T)

Enqueue adds a new value to the end of the RWSafeQueue.

Parameters:

  • value: The value to add to the queue. The value is of type T.

func (*RWSafeQueue[T]) IsEmpty

func (b *RWSafeQueue[T]) IsEmpty() bool

IsEmpty checks if the RWSafeQueue is empty.

The method returns a boolean indicating whether the queue is empty. It returns true if the queue is empty, and false otherwise.

Jump to

Keyboard shortcuts

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