github.com/jingleWang/moby@v1.13.1/pkg/system/events_windows.go (about)

     1  package system
     2  
     3  // This file implements syscalls for Win32 events which are not implemented
     4  // in golang.
     5  
     6  import (
     7  	"syscall"
     8  	"unsafe"
     9  
    10  	"golang.org/x/sys/windows"
    11  )
    12  
    13  var (
    14  	procCreateEvent = modkernel32.NewProc("CreateEventW")
    15  	procOpenEvent   = modkernel32.NewProc("OpenEventW")
    16  	procSetEvent    = modkernel32.NewProc("SetEvent")
    17  	procResetEvent  = modkernel32.NewProc("ResetEvent")
    18  	procPulseEvent  = modkernel32.NewProc("PulseEvent")
    19  )
    20  
    21  // CreateEvent implements win32 CreateEventW func in golang. It will create an event object.
    22  func CreateEvent(eventAttributes *syscall.SecurityAttributes, manualReset bool, initialState bool, name string) (handle syscall.Handle, err error) {
    23  	namep, _ := syscall.UTF16PtrFromString(name)
    24  	var _p1 uint32
    25  	if manualReset {
    26  		_p1 = 1
    27  	}
    28  	var _p2 uint32
    29  	if initialState {
    30  		_p2 = 1
    31  	}
    32  	r0, _, e1 := procCreateEvent.Call(uintptr(unsafe.Pointer(eventAttributes)), uintptr(_p1), uintptr(_p2), uintptr(unsafe.Pointer(namep)))
    33  	use(unsafe.Pointer(namep))
    34  	handle = syscall.Handle(r0)
    35  	if handle == syscall.InvalidHandle {
    36  		err = e1
    37  	}
    38  	return
    39  }
    40  
    41  // OpenEvent implements win32 OpenEventW func in golang. It opens an event object.
    42  func OpenEvent(desiredAccess uint32, inheritHandle bool, name string) (handle syscall.Handle, err error) {
    43  	namep, _ := syscall.UTF16PtrFromString(name)
    44  	var _p1 uint32
    45  	if inheritHandle {
    46  		_p1 = 1
    47  	}
    48  	r0, _, e1 := procOpenEvent.Call(uintptr(desiredAccess), uintptr(_p1), uintptr(unsafe.Pointer(namep)))
    49  	use(unsafe.Pointer(namep))
    50  	handle = syscall.Handle(r0)
    51  	if handle == syscall.InvalidHandle {
    52  		err = e1
    53  	}
    54  	return
    55  }
    56  
    57  // SetEvent implements win32 SetEvent func in golang.
    58  func SetEvent(handle syscall.Handle) (err error) {
    59  	return setResetPulse(handle, procSetEvent)
    60  }
    61  
    62  // ResetEvent implements win32 ResetEvent func in golang.
    63  func ResetEvent(handle syscall.Handle) (err error) {
    64  	return setResetPulse(handle, procResetEvent)
    65  }
    66  
    67  // PulseEvent implements win32 PulseEvent func in golang.
    68  func PulseEvent(handle syscall.Handle) (err error) {
    69  	return setResetPulse(handle, procPulseEvent)
    70  }
    71  
    72  func setResetPulse(handle syscall.Handle, proc *windows.LazyProc) (err error) {
    73  	r0, _, _ := proc.Call(uintptr(handle))
    74  	if r0 != 0 {
    75  		err = syscall.Errno(r0)
    76  	}
    77  	return
    78  }
    79  
    80  var temp unsafe.Pointer
    81  
    82  // use ensures a variable is kept alive without the GC freeing while still needed
    83  func use(p unsafe.Pointer) {
    84  	temp = p
    85  }