hsync

package
v1.0.8 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2023 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package hsync provides synchronized access to system handles.

Example
package main

import (
	"sync"
	"syscall"

	"github.com/GLMONTER/volmgmt/hsync"
)

// acquireHandle acquires a system handle from some source, probably by
// calling a function in syscall.
func acquireHandle() syscall.Handle {
	return 0 // System handles are actually uintptr values
}

// work performs some work that requires access to a system handle.
func work(wg *sync.WaitGroup, h *hsync.Handle) {
	defer wg.Done()
	defer h.Close()
	_ = h.Handle() // Do something with the actual system handle
}

const workers = 5

func main() {
	var wg sync.WaitGroup
	wg.Add(workers)

	h := hsync.New(acquireHandle())

	for i := 0; i < workers; i++ {
		go work(&wg, h.Clone())
	}

	// We can close our instance as soon as we're done with it, even though
	// the workers may still be relying on their cloned copies.
	h.Close()

	wg.Wait()
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrClosed = errors.New("handle already closed")

ErrClosed is returned when a handle is already closed.

Functions

This section is empty.

Types

type Handle

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

Handle provides shared access to a system handle by one or more instances without fear of the handle being closed prematurely. It should be created initially by calling New(). Additional instances are created by calling Clone().

Handle maintains a reference counter internally that tracks the number of instances. The system handle will be closed when all of the instances relying on it have been closed.

It is very important that each instance be closed. Failure to do so may result in leaked system handles.

func New

func New(handle syscall.Handle) *Handle

New returns a new Handle that guards access to the given system handle. It should only be called once for a particular system handle. Additional copies can be created by calling Clone().

When finished with the returned Handle, it is the caller's responsibility to release its resources by calling Close().

func (*Handle) Clone

func (h *Handle) Clone() *Handle

Clone returns an independent copy of h. When finished with a clone, it is the caller's responsibility to close it.

If h has already been closed calling Clone() will result in a panic.

func (*Handle) Close

func (h *Handle) Close() (err error)

Close prevents further use of h and indicates that h no longer requires access to its system handle. If h is the last instance referring to a particular system handle the system handle will be closed.

If h has already been closed ErrClosed will be returned.

Once h has been closed it cannot be cloned.

func (*Handle) Handle

func (h *Handle) Handle() syscall.Handle

Handle returns the system handle protected by h.

If h has already been closed calling Handle() will result in a panic.

Jump to

Keyboard shortcuts

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