Пример #1
0
func mq_open(name string, oflag int, mode int, attr mq_attr) (mqd_t, error) {
	cname := C.CString(name)
	defer C.free(unsafe.Pointer(cname))
	var cattr C.struct_mq_attr
	cattr.mq_flags = C.__syscall_slong_t(attr.mq_flags)
	cattr.mq_maxmsg = C.__syscall_slong_t(attr.mq_maxmsg)
	cattr.mq_msgsize = C.__syscall_slong_t(attr.mq_msgsize)
	cattr.mq_curmsgs = C.__syscall_slong_t(attr.mq_curmsgs)
	q, err := C._mq_open(cname, C.int(oflag), C.mode_t(mode), &cattr)
	if err != nil {
		log.Printf("%d\n", err)
		return mqd_t(q), err
	}
	return mqd_t(q), nil
}
Пример #2
0
func newInterval(ns uint64) (_ interval, err error) {
	fd, _, errno := syscall.Syscall(syscall.SYS_TIMERFD_CREATE, C.CLOCK_MONOTONIC, C.TFD_CLOEXEC, 0)
	if fd == ^uintptr(0) {
		err = errors.New("Couldn't create timerfd: " + errno.Error())
		return
	}
	defer func() {
		if err != nil {
			syscall.Close(int(fd))
		}
	}()

	tspec := C.struct_timespec{
		tv_sec:  C.__time_t(ns / 1000000000),
		tv_nsec: C.__syscall_slong_t(ns % 1000000000),
	}
	ispec := C.struct_itimerspec{
		it_interval: tspec,
		it_value:    tspec,
	}
	ret, _, errno := syscall.Syscall6(syscall.SYS_TIMERFD_SETTIME, fd, 0, reflect.ValueOf(&ispec).Pointer(), 0, 0, 0)
	if ret == ^uintptr(0) {
		err = errors.New("Couldn't set interval: " + errno.Error())
		return
	}

	return interval{os.NewFile(fd, "timerfd")}, nil
}
Пример #3
0
// Run applies a group of SemOps atomically.
func (ss *SemaphoreSet) Run(ops *SemOps, timeout time.Duration) error {
	var cto *C.struct_timespec
	if timeout >= 0 {
		cto = &C.struct_timespec{
			tv_sec:  C.__time_t(timeout / time.Second),
			tv_nsec: C.__syscall_slong_t(timeout % time.Second),
		}
	}

	var opptr *C.struct_sembuf
	if len(*ops) > 0 {
		opptr = &(*ops)[0]
	}

	rc, err := C.semtimedop(C.int(ss.id), opptr, C.size_t(len(*ops)), cto)
	if rc == -1 {
		return err
	}
	return nil
}