Documentation ¶
Overview ¶
Package hsync provides synchronized access to system handles.
Example ¶
package main import ( "sync" "syscall" "github.com/gentlemanautomaton/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 ¶
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 ¶
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 ¶
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 ¶
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.