Skip to content

Commit

Permalink
convert current write lock into read lock
Browse files Browse the repository at this point in the history
  • Loading branch information
davies committed Nov 20, 2023
1 parent b7be0cf commit 269e900
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
6 changes: 6 additions & 0 deletions pkg/meta/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,12 @@ func testLocks(t *testing.T, m Meta) {
if st := m.Flock(ctx, inode, o1, syscall.F_WRLCK, false); st != 0 {
t.Fatalf("flock wlock: %s", st)
}
if st := m.Flock(ctx, inode, o1, syscall.F_RDLCK, false); st != 0 {
t.Fatalf("flock rlock: %s", st)
}
if st := m.Flock(ctx, inode, o1, syscall.F_WRLCK, false); st != 0 {
t.Fatalf("flock wlock: %s", st)
}
if st := m.Flock(ctx, inode, o1, syscall.F_UNLCK, false); st != 0 {
t.Fatalf("flock unlock: %s", st)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/meta/redis_lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func (r *redisMeta) Flock(ctx Context, inode Ino, owner uint64, ltype uint32, bl
if err != nil {
return err
}
delete(owners, lkey)
if ltype == F_RDLCK {
for _, v := range owners {
if v == "W" {
Expand All @@ -67,7 +68,6 @@ func (r *redisMeta) Flock(ctx Context, inode Ino, owner uint64, ltype uint32, bl
})
return err
}
delete(owners, lkey)
if len(owners) > 0 {
return syscall.EAGAIN
}
Expand Down
18 changes: 9 additions & 9 deletions pkg/meta/sql_lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,29 +60,29 @@ func (m *dbMeta) Flock(ctx Context, inode Ino, owner_ uint64, ltype uint32, bloc
locks[key{l.Sid, l.Owner}] = l
}

me := key{m.sid, owner}
flk, ok := locks[me]
delete(locks, me)
var typec byte = 'W'
if ltype == F_RDLCK {
for _, l := range locks {
if l.Ltype == 'W' {
return syscall.EAGAIN
}
}
return mustInsert(s, flock{Inode: inode, Owner: owner, Ltype: 'R', Sid: m.sid})
}
me := key{m.sid, owner}
flk, ok := locks[me]
delete(locks, me)
if len(locks) > 0 {
typec = 'R'
} else if len(locks) > 0 {
return syscall.EAGAIN
}
var n int64
if ok {
if flk.Ltype != 'W' {
n, err = s.Cols("Ltype").Update(&flock{Ltype: 'W'}, &flock{Inode: inode, Owner: owner, Sid: m.sid})
if flk.Ltype != typec {
n, err = s.Cols("Ltype").Update(&flock{Ltype: typec}, &flock{Inode: inode, Owner: owner, Sid: m.sid})
} else {
n = 1
}
} else {
n, err = s.InsertOne(&flock{Inode: inode, Owner: owner, Ltype: 'W', Sid: m.sid})
n, err = s.InsertOne(&flock{Inode: inode, Owner: owner, Ltype: typec, Sid: m.sid})
}
if err == nil && n == 0 {
err = fmt.Errorf("insert/update failed")
Expand Down
4 changes: 2 additions & 2 deletions pkg/meta/tkv_lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ func (m *kvMeta) Flock(ctx Context, inode Ino, owner uint64, ltype uint32, block
case F_UNLCK:
delete(ls, lkey)
case F_RDLCK:
for _, l := range ls {
if l == 'W' {
for o, l := range ls {
if l == 'W' && o != lkey {
return syscall.EAGAIN
}
}
Expand Down

0 comments on commit 269e900

Please sign in to comment.