osync

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2024 License: MIT Imports: 6 Imported by: 0

README

osync

osync is a Go library designed to provide thread-safe data structures and utilities for concurrent programming. Built with simplicity and performance in mind, osync leverages Mutex, RWMutex and Atomic to ensure safe access to shared resources.

Features

  • Thread-safe collections: Protects against race conditions with minimal overhead.
  • Observable values: Allows observing changes to a value.
  • Event handling: Provides synchronization primitives for coordinating tasks.
  • Simple API: Focuses on ease of use while offering powerful concurrency control.
  • Generic support: Utilizes Go generics to create versatile and reusable data structures.

Installation

To install osync, use go get:

go get github.com/eos175/osync

Usage

Set Example

Here's an example of how to use the Set provided by osync:

package main

import (
	"fmt"

	"github.com/eos175/osync"
)

func main() {
	set := osync.NewSet[int]()

	set.Add(1)
	set.Add(2)
	set.Add(3)

	fmt.Println("Set has 2:", set.Has(2)) // Output: Set has 2: true
	fmt.Println("Set length:", set.Len()) // Output: Set length: 3

	set.Delete(2)

	fmt.Println("Set has 2:", set.Has(2)) // Output: Set has 2: false

	// go1.23
	fmt.Println("Set contents:")
	for key := range set.Iterator() {
		fmt.Println(key)
	}
}
Observable Example

Here's an example of how to use the Observable provided by osync:

package main

import (
	"context"
	"fmt"
	"time"

	"github.com/eos175/osync"
)

func main() {
	obs := osync.NewObservable[int](0)

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	// Subscribe to changes
	ch := obs.Subscribe(ctx)

	go func() {
		// Update the observable value
		for i := 0; i < 10; i++ {
			obs.Set(i*i)
			time.Sleep(1 * time.Second)
		}
	}()

	// Print updates received from the observable
	for value := range ch {
		fmt.Println("Received value:", value)
	}
}
Event Example

Here's an example of how to use the Event provided by osync:

package main

import (
	"fmt"
	"time"

	"github.com/eos175/osync"
)

func main() {
	event := osync.NewEvent()

	go func() {
		// Wait for the event to be set
		fmt.Println("Waiting for event to be set...")
		event.Wait()
		fmt.Println("Event is set!")
	}()

	go func() {
		// Simulate some work before setting the event
		time.Sleep(2 * time.Second)
		fmt.Println("Setting event...")
		event.Set()
	}()

	// Wait for the event to be set
	time.Sleep(3 * time.Second)
}

Documentation

The full documentation is available on pkg.go.dev.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Event

type Event struct {
	// contains filtered or unexported fields
}

Event struct containing a state and a pointer to a channel

func NewEvent

func NewEvent() *Event

NewEvent initializes and returns a new Event instance

func (*Event) Clear

func (e *Event) Clear()

Clear the state of the event

func (*Event) IsSet

func (e *Event) IsSet() bool

IsSet checks if the event is set

func (*Event) Set

func (e *Event) Set()

Set the event and broadcast if it's newly set

func (*Event) Wait

func (e *Event) Wait()

Wait until the event is set

func (*Event) WaitTimeout

func (e *Event) WaitTimeout(timeout time.Duration) bool

WaitTimeout waits for the event to be set until the timeout

type Map

type Map[K comparable, T any] struct {
	// contains filtered or unexported fields
}

func NewMap

func NewMap[K comparable, T any]() *Map[K, T]

func (*Map[K, T]) ChangeKey

func (s *Map[K, T]) ChangeKey(key, new_key K) (T, bool)

func (*Map[K, T]) Clear

func (s *Map[K, T]) Clear()

func (*Map[K, T]) Clone

func (s *Map[K, T]) Clone() *Map[K, T]

func (*Map[K, T]) Delete

func (s *Map[K, T]) Delete(key K)

func (*Map[K, T]) ForEach

func (s *Map[K, T]) ForEach(fn func(key K, value T) bool)

func (*Map[K, T]) Get

func (s *Map[K, T]) Get(key K) (T, bool)

func (*Map[K, T]) GetOrSet

func (s *Map[K, T]) GetOrSet(key K, value T) (actual T, loaded bool)

func (*Map[K, T]) Len

func (s *Map[K, T]) Len() int

func (*Map[K, T]) Pop

func (s *Map[K, T]) Pop(key K) (T, bool)

func (*Map[K, T]) Set

func (s *Map[K, T]) Set(key K, value T)

type Observable

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

Observable is a generic structure that represents a value that can be observed.

func NewObservable

func NewObservable[T any](initialValue T) *Observable[T]

NewObservable creates a new Observable with an initial value.

func (*Observable[T]) Get

func (o *Observable[T]) Get() T

Get returns the current value of the observable.

func (*Observable[T]) Len

func (o *Observable[T]) Len() int

Len returns the number of observers currently subscribed.

func (*Observable[T]) Set

func (o *Observable[T]) Set(value T)

Set updates the value of the observable and notifies all observers.

func (*Observable[T]) Subscribe

func (o *Observable[T]) Subscribe(ctx context.Context) <-chan T

Subscribe allows an observer to receive notifications when the value changes.

type Set

type Set[T comparable] struct {
	// contains filtered or unexported fields
}

func NewSet

func NewSet[T comparable]() *Set[T]

func (*Set[T]) Add

func (s *Set[T]) Add(key T) bool

Adds a key to the set. Returns `true` if the key was added, or `false` if it already existed.

func (*Set[T]) Clear

func (s *Set[T]) Clear()

Removes all keys from the set.

func (*Set[T]) Clone

func (s *Set[T]) Clone() *Set[T]

Returns a shallow copy of the set.

func (*Set[T]) Delete

func (s *Set[T]) Delete(key T)

Removes a key from the set.

func (*Set[T]) ForEach

func (s *Set[T]) ForEach(fn func(key T) bool)

Iterates over all keys in the set, applying the provided function.

func (*Set[T]) Has

func (s *Set[T]) Has(key T) bool

Checks if a key exists in the set.

func (*Set[T]) Keys

func (s *Set[T]) Keys() []T

Returns a slice of all keys in the set.

func (*Set[T]) Len

func (s *Set[T]) Len() int

Returns the number of keys in the set.

func (*Set[T]) Pop

func (s *Set[T]) Pop(key T) bool

Removes a key from the set and returns `true` if the key existed.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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