Beispiel #1
0
// LockExclusive exclusively locks an object. At the moment, lock duration cannot be specified so this locks the object
// indefinitely.
func (o *Object) LockExclusive(name, cookie, description string, flags ...LibradosLock) error {
	oid := C.CString(o.name)
	defer freeString(oid)

	n := C.CString(name)
	defer freeString(n)

	c := C.CString(cookie)
	defer freeString(c)

	d := C.CString(description)
	defer freeString(d)

	f := 0
	for _, flag := range flags {
		f |= int(flag)
	}
	ret := C.rados_lock_exclusive(o.ioContext, oid, n, c, d, nil, C.uint8_t(f))
	switch int(ret) {
	case -int(syscall.EBUSY):
		err := toRadosError(ret)
		err.Message = fmt.Sprintf("%s is already locked by another client", o.name)
		return err
	case -int(syscall.EEXIST):
		err := toRadosError(ret)
		err.Message = fmt.Sprintf("%s is already locked by current client", o.name)
		return err
	}
	return nil
}
Beispiel #2
0
// Take an exclusive lock on an object.
func (ioctx *IOContext) LockExclusive(oid, name, cookie, desc string, duration time.Duration, flags *byte) (int, error) {
	c_oid := C.CString(oid)
	c_name := C.CString(name)
	c_cookie := C.CString(cookie)
	c_desc := C.CString(desc)

	var c_duration C.struct_timeval
	if duration != 0 {
		tv := syscall.NsecToTimeval(time.Now().Add(duration).UnixNano())
		c_duration = C.struct_timeval{tv_sec: C.__time_t(tv.Sec), tv_usec: C.__suseconds_t(tv.Usec)}
	}

	var c_flags C.uint8_t
	if flags != nil {
		c_flags = C.uint8_t(*flags)
	}

	defer C.free(unsafe.Pointer(c_oid))
	defer C.free(unsafe.Pointer(c_name))
	defer C.free(unsafe.Pointer(c_cookie))
	defer C.free(unsafe.Pointer(c_desc))

	ret := C.rados_lock_exclusive(
		ioctx.ioctx,
		c_oid,
		c_name,
		c_cookie,
		c_desc,
		&c_duration,
		c_flags)

	// 0 on success, negative error code on failure
	// -EBUSY if the lock is already held by another (client, cookie) pair
	// -EEXIST if the lock is already held by the same (client, cookie) pair

	switch ret {
	case 0:
		return int(ret), nil
	case -16: // EBUSY
		return int(ret), nil
	case -17: // EEXIST
		return int(ret), nil
	default:
		return int(ret), RadosError(int(ret))
	}
}