Exemplo n.º 1
1
func newpollster() (p *pollster, err error) {
	p = new(pollster)
	var e int
	if p.kq, e = syscall.Kqueue(); e != 0 {
		return nil, os.NewSyscallError("kqueue", e)
	}
	p.events = p.eventbuf[0:0]
	return p, nil
}
Exemplo n.º 2
0
// kqueue creates a new kernel event queue and returns a descriptor.
func kqueue() (kq int, err error) {
	kq, err = syscall.Kqueue()
	if kq == -1 {
		return kq, err
	}
	return kq, nil
}
Exemplo n.º 3
0
func runKqueue() {
	fd, err := syscall.Kqueue()
	evTrackList := make([]syscall.Kevent_t, 1024)
	if err != nil {
		log.Fatal("error initializing Kqueue: ", err)
		return
	}

	addFilesToKqueue(evTrackList, path)

	// configure timeout
	timeout := syscall.Timespec{
		Sec:  0,
		Nsec: 0,
	}

	// wait for events
	for {
		// create kevent
		events := make([]syscall.Kevent_t, 10)
		_, err := syscall.Kevent(fd, evTrackList, events, &timeout)
		if err != nil {
			log.Println("Error creating kevent")
		}
		// check if there was an event and process it
		if len(events) > 0 && events[0].Ident > 0 {
			processEvent()
		}
	}
}
Exemplo n.º 4
0
func NewPoll() (p *pollster, err error) {
	p = new(pollster)
	if p.kq, err = syscall.Kqueue(); err != nil {
		return nil, os.NewSyscallError("kqueue", err)
	}
	syscall.CloseOnExec(p.kq)
	return p, nil
}
Exemplo n.º 5
0
func newpollster() (p *pollster, err error) {
	p = new(pollster)
	if p.kq, err = syscall.Kqueue(); err != nil {
		return nil, os.NewSyscallError("kqueue", err)
	}
	syscall.CloseOnExec(p.kq)
	p.events = p.eventbuf[0:0]
	return p, nil
}
Exemplo n.º 6
0
func NewKqueue() (*Kqueue, error) {
	fd, err := syscall.Kqueue()
	if err != nil {
		return nil, os.NewSyscallError("kqueue", err)
	}
	return &Kqueue{
		Fd: fd,
	}, nil
}
Exemplo n.º 7
0
func newkqueue() (*kqueue, error) {
	fd, err := syscall.Kqueue()
	if err != nil {
		return nil, os.NewSyscallError("kqueue", err)
	}
	return &kqueue{
		fd: fd,
	}, nil
}
Exemplo n.º 8
0
func NewWatcher() (IFSEventsWatcher, error) {
	if fd, err := syscall.Kqueue(); fd == sysCallFailed {
		return nil, os.NewSyscallError("kqueue", err)
	} else {
		w := &watcher{
			kq:            fd,
			watchedByFD:   make(map[int]*watchInfo),
			watchedByPath: make(map[string]*watchInfo),
		}

		return w, nil
	}
}
Exemplo n.º 9
0
// init initializes kqueue.
func (k *kqueue) init() (err error) {
	if k.fd, err = syscall.Kqueue(); err != nil {
		return
	}
	// Creates pipe used to stop `Kevent` call by registering it,
	// watching read end and writing to other end of it.
	if err = syscall.Pipe(k.pipefds[:]); err != nil {
		return
	}
	var kevn [1]syscall.Kevent_t
	syscall.SetKevent(&kevn[0], k.pipefds[0], syscall.EVFILT_READ, syscall.EV_ADD)
	_, err = syscall.Kevent(k.fd, kevn[:], nil, nil)
	return
}
Exemplo n.º 10
0
func New(fd int) (p *Poller, err error) {
	p = &Poller{}

	p.kqfd, err = syscall.Kqueue()
	if p.kqfd == -1 || err != nil {
		return nil, err
	}

	p.event = syscall.Kevent_t{
		Ident:  uint32(fd),
		Filter: syscall.EVFILT_WRITE,
		Flags:  syscall.EV_ADD | syscall.EV_ENABLE | syscall.EV_ONESHOT,
		Fflags: 0,
		Data:   0,
		Udata:  nil,
	}
	return p, nil
}
Exemplo n.º 11
0
// NewWatcher creates and returns a new kevent instance using kqueue(2)
func NewWatcher() (*Watcher, error) {
	fd, errno := syscall.Kqueue()
	if fd == -1 {
		return nil, os.NewSyscallError("kqueue", errno)
	}
	w := &Watcher{
		kq:      fd,
		watches: make(map[string]int),
		paths:   make(map[int]string),
		finfo:   make(map[int]*os.FileInfo),
		Event:   make(chan *FileEvent),
		Error:   make(chan error),
		done:    make(chan bool, 1),
	}

	go w.readEvents()
	return w, nil
}
Exemplo n.º 12
0
// NewWatcher creates and returns a new kevent instance using kqueue(2)
func NewWatcher() (*Watcher, error) {
	fd, errno := syscall.Kqueue()
	if fd == -1 {
		return nil, os.NewSyscallError("kqueue", errno)
	}
	w := &Watcher{
		kq:              fd,
		watches:         make(map[string]int),
		enFlags:         make(map[string]uint32),
		paths:           make(map[int]string),
		finfo:           make(map[int]os.FileInfo),
		fileExists:      make(map[string]bool),
		externalWatches: make(map[string]bool),
		Events:          make(chan Event),
		Errors:          make(chan error),
		done:            make(chan bool, 1),
	}

	go w.readEvents()
	return w, nil
}
Exemplo n.º 13
0
Arquivo: main.go Projeto: rsc/rsc
func main() {
	flag.Parse()
	args = flag.Args()

	var err error
	win, err = acme.New()
	if err != nil {
		log.Fatal(err)
	}
	pwd, _ := os.Getwd()
	win.Name(pwd + "/+watch")
	win.Ctl("clean")
	win.Fprintf("tag", "Get ")
	needrun <- true
	go events()
	go runner()

	kq.fd, err = syscall.Kqueue()
	if err != nil {
		log.Fatal(err)
	}
	kq.m = make(map[string]*os.File)
	kq.name = make(map[int]string)

	dir, err := os.Open(".")
	if err != nil {
		log.Fatal(err)
	}
	kq.dir = dir
	kadd(int(dir.Fd()))
	readdir := true

	for {
		if readdir {
			kq.dir.Seek(0, 0)
			names, err := kq.dir.Readdirnames(-1)
			if err != nil {
				log.Fatalf("readdir: %v", err)
			}
			for _, name := range names {
				if kq.m[name] != nil {
					continue
				}
				f, err := os.Open(name)
				if err != nil {
					continue
				}
				kq.m[name] = f
				fd := int(f.Fd())
				kq.name[fd] = name
				kadd(fd)
			}
		}

		kbuf := make([]syscall.Kevent_t, 1)
		var n int
		for {
			n, err = syscall.Kevent(kq.fd, nil, kbuf[:1], nil)
			if err == syscall.EINTR {
				continue
			}
			break
		}
		if err != nil {
			log.Fatalf("kevent wait: %v", err)
		}
		ev := &kbuf[0]
		if n != 1 || int(ev.Filter) != syscall.EVFILT_VNODE {
			log.Fatal("kqueue phase error")
		}

		select {
		case needrun <- true:
		default:
		}

		fd := int(ev.Ident)
		readdir = fd == int(kq.dir.Fd())
		time.Sleep(100 * time.Millisecond)
		kadd(fd)
	}
}
Exemplo n.º 14
0
// Initialize bsd implementation of the eventListener interface
func createListener() (eventListener, error) {
	listener := &kqueueListener{}
	kq, err := syscall.Kqueue()
	listener.kq = kq
	return listener, err
}