github.com/Andyfoo/golang/x/sys@v0.0.0-20190901054642-57c1bf301704/windows/svc/event.go (about)

     1  // Copyright 2012 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // +build windows
     6  
     7  package svc
     8  
     9  import (
    10  	"errors"
    11  
    12  	"github.com/Andyfoo/golang/x/sys/windows"
    13  )
    14  
    15  // event represents auto-reset, initially non-signaled Windows event.
    16  // It is used to communicate between go and asm parts of this package.
    17  type event struct {
    18  	h windows.Handle
    19  }
    20  
    21  func newEvent() (*event, error) {
    22  	h, err := windows.CreateEvent(nil, 0, 0, nil)
    23  	if err != nil {
    24  		return nil, err
    25  	}
    26  	return &event{h: h}, nil
    27  }
    28  
    29  func (e *event) Close() error {
    30  	return windows.CloseHandle(e.h)
    31  }
    32  
    33  func (e *event) Set() error {
    34  	return windows.SetEvent(e.h)
    35  }
    36  
    37  func (e *event) Wait() error {
    38  	s, err := windows.WaitForSingleObject(e.h, windows.INFINITE)
    39  	switch s {
    40  	case windows.WAIT_OBJECT_0:
    41  		break
    42  	case windows.WAIT_FAILED:
    43  		return err
    44  	default:
    45  		return errors.New("unexpected result from WaitForSingleObject")
    46  	}
    47  	return nil
    48  }