Fix up usb to pass reliably and to work cross-platform

This commit is contained in:
Kyle Lemons
2013-08-14 19:38:57 -07:00
parent ca13741ba1
commit b9f2aa720b
3 changed files with 7 additions and 5 deletions

View File

@@ -27,7 +27,7 @@ import (
type Context struct { type Context struct {
ctx *C.libusb_context ctx *C.libusb_context
done chan bool done chan struct{}
} }
func (c *Context) Debug(level int) { func (c *Context) Debug(level int) {
@@ -36,7 +36,7 @@ func (c *Context) Debug(level int) {
func NewContext() *Context { func NewContext() *Context {
c := &Context{ c := &Context{
done: make(chan bool), done: make(chan struct{}),
} }
if errno := C.libusb_init(&c.ctx); errno != 0 { if errno := C.libusb_init(&c.ctx); errno != 0 {
@@ -44,7 +44,10 @@ func NewContext() *Context {
} }
go func() { go func() {
tv := C.struct_timeval{0, 100000} tv := C.struct_timeval{
tv_sec: 0,
tv_usec: 100000,
}
for { for {
select { select {
case <-c.done: case <-c.done:
@@ -104,7 +107,6 @@ func (c *Context) ListDevices(each func(desc *Descriptor) bool) ([]*Device, erro
} }
func (c *Context) Close() error { func (c *Context) Close() error {
c.done <- true
close(c.done) close(c.done)
if c.ctx != nil { if c.ctx != nil {
C.libusb_exit(c.ctx) C.libusb_exit(c.ctx)

BIN
usb/usb.test Executable file

Binary file not shown.

View File

@@ -84,7 +84,7 @@ func TestEnum(t *testing.T) {
func TestMultipleContexts(t *testing.T) { func TestMultipleContexts(t *testing.T) {
var buf bytes.Buffer var buf bytes.Buffer
log.SetOutput(&buf) log.SetOutput(&buf)
for i := 0; i < 10; i++ { for i := 0; i < 2; i++ {
ctx := NewContext() ctx := NewContext()
_, err := ctx.ListDevices(func(desc *Descriptor) bool { _, err := ctx.ListDevices(func(desc *Descriptor) bool {
return false return false