Skip to content

Commit

Permalink
Try to use flock first to lock file.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengshuxin committed Apr 24, 2024
1 parent 0e96092 commit 687250d
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions lib_acl_cpp/src/stdlib/locker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,12 @@ bool locker::lock(void)
return true;
}

#ifdef ACL_HAS_FLOCK_LOCK
if (acl_myflock(fHandle_, ACL_FLOCK_STYLE_FLOCK, LOCK_EX) == 0) {
#else
int operation = ACL_FLOCK_OP_EXCLUSIVE;
if (acl_myflock(fHandle_, ACL_FLOCK_STYLE_FCNTL, operation) == 0) {
#endif
return true;
}

Expand All @@ -155,8 +159,12 @@ bool locker::try_lock(void)
return true;
}

#ifdef ACL_HAS_FLOCK_LOCK
if (acl_myflock(fHandle_, ACL_FLOCK_STYLE_FLOCK, (LOCK_EX | LOCK_NB)) == 0) {
#else
int operation = ACL_FLOCK_OP_EXCLUSIVE | ACL_FLOCK_OP_NOWAIT;
if (acl_myflock(fHandle_, ACL_FLOCK_STYLE_FCNTL, operation) == 0) {
#endif
return true;
}

Expand Down Expand Up @@ -193,8 +201,11 @@ bool locker::unlock(void)
return ret;
}

int operation = ACL_FLOCK_STYLE_FCNTL;
if (acl_myflock(fHandle_, operation, ACL_FLOCK_OP_NONE) == -1) {
#ifdef ACL_HAS_FLOCK_LOCK
if (acl_myflock(fHandle_, ACL_FLOCK_STYLE_FLOCK, ACL_FLOCK_OP_NONE) == -1) {
#else
if (acl_myflock(fHandle_, ACL_FLOCK_STYLE_FCNTL, ACL_FLOCK_OP_NONE) == -1) {
#endif
return false;
}
return ret;
Expand Down

0 comments on commit 687250d

Please sign in to comment.