2022-11-26 23:57:50 +00:00
|
|
|
// Copyright 2020 The Jujutsu Authors
|
2020-12-12 08:00:42 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2023-07-10 15:17:00 +00:00
|
|
|
#![allow(missing_docs)]
|
|
|
|
|
2023-05-06 01:54:33 +00:00
|
|
|
#[cfg_attr(unix, path = "lock/unix.rs")]
|
|
|
|
#[cfg_attr(not(unix), path = "lock/fallback.rs")]
|
|
|
|
mod platform;
|
2020-12-12 08:00:42 +00:00
|
|
|
|
2023-05-06 01:54:33 +00:00
|
|
|
pub use platform::FileLock;
|
2020-12-12 08:00:42 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2021-04-29 06:51:58 +00:00
|
|
|
use std::cmp::max;
|
2023-05-06 01:54:33 +00:00
|
|
|
use std::time::Duration;
|
2023-12-22 00:22:59 +00:00
|
|
|
use std::{fs, thread};
|
2020-12-12 08:00:42 +00:00
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn lock_basic() {
|
2022-09-07 04:16:35 +00:00
|
|
|
let temp_dir = testutils::new_temp_dir();
|
|
|
|
let lock_path = temp_dir.path().join("test.lock");
|
2020-12-12 08:00:42 +00:00
|
|
|
assert!(!lock_path.exists());
|
|
|
|
{
|
|
|
|
let _lock = FileLock::lock(lock_path.clone());
|
|
|
|
assert!(lock_path.exists());
|
|
|
|
}
|
|
|
|
assert!(!lock_path.exists());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn lock_concurrent() {
|
2022-09-07 04:16:35 +00:00
|
|
|
let temp_dir = testutils::new_temp_dir();
|
|
|
|
let data_path = temp_dir.path().join("test");
|
|
|
|
let lock_path = temp_dir.path().join("test.lock");
|
2023-12-22 00:22:59 +00:00
|
|
|
fs::write(&data_path, 0_u32.to_le_bytes()).unwrap();
|
2021-04-29 06:51:58 +00:00
|
|
|
let num_threads = max(num_cpus::get(), 4);
|
2023-05-21 10:49:43 +00:00
|
|
|
thread::scope(|s| {
|
|
|
|
for _ in 0..num_threads {
|
2023-12-22 00:22:59 +00:00
|
|
|
s.spawn(|| {
|
|
|
|
let _lock = FileLock::lock(lock_path.clone());
|
|
|
|
let data = fs::read(&data_path).unwrap();
|
|
|
|
let value = u32::from_le_bytes(data.try_into().unwrap());
|
2023-05-21 10:49:43 +00:00
|
|
|
thread::sleep(Duration::from_millis(1));
|
2023-12-22 00:22:59 +00:00
|
|
|
fs::write(&data_path, (value + 1).to_le_bytes()).unwrap();
|
2023-05-21 10:49:43 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2023-12-22 00:22:59 +00:00
|
|
|
let data = fs::read(&data_path).unwrap();
|
|
|
|
let value = u32::from_le_bytes(data.try_into().unwrap());
|
2021-04-29 06:51:58 +00:00
|
|
|
assert_eq!(value, num_threads as u32);
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
}
|