Skip to content

Commit

Permalink
incr.comp.: Move lock files out of directory being locked
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelwoerister committed Aug 22, 2016
1 parent d1d8258 commit 95d3cf3
Show file tree
Hide file tree
Showing 5 changed files with 301 additions and 186 deletions.
24 changes: 16 additions & 8 deletions src/librustc/session/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,25 +366,31 @@ impl Session {
pub fn mark_incr_comp_session_as_invalid(&self) {
let mut incr_comp_session = self.incr_comp_session.borrow_mut();

if let IncrCompSession::Active { .. } = *incr_comp_session { } else {
bug!("Trying to invalidate IncrCompSession `{:?}`", *incr_comp_session)
}
let session_directory = match *incr_comp_session {
IncrCompSession::Active { ref session_directory, .. } => {
session_directory.clone()
}
_ => bug!("Trying to invalidate IncrCompSession `{:?}`",
*incr_comp_session),
};

// Note: This will also drop the lock file, thus unlocking the directory
*incr_comp_session = IncrCompSession::InvalidBecauseOfErrors;
*incr_comp_session = IncrCompSession::InvalidBecauseOfErrors {
session_directory: session_directory
};
}

pub fn incr_comp_session_dir(&self) -> cell::Ref<PathBuf> {
let incr_comp_session = self.incr_comp_session.borrow();
cell::Ref::map(incr_comp_session, |incr_comp_session| {
match *incr_comp_session {
IncrCompSession::NotInitialized |
IncrCompSession::InvalidBecauseOfErrors => {
IncrCompSession::NotInitialized => {
bug!("Trying to get session directory from IncrCompSession `{:?}`",
*incr_comp_session)
}
IncrCompSession::Active { ref session_directory, .. } |
IncrCompSession::Finalized { ref session_directory } => {
IncrCompSession::Finalized { ref session_directory } |
IncrCompSession::InvalidBecauseOfErrors { ref session_directory } => {
session_directory
}
}
Expand Down Expand Up @@ -541,7 +547,9 @@ pub enum IncrCompSession {
// This is an error state that is reached when some compilation error has
// occurred. It indicates that the contents of the session directory must
// not be used, since they might be invalid.
InvalidBecauseOfErrors,
InvalidBecauseOfErrors {
session_directory: PathBuf,
}
}

fn init_llvm(sess: &Session) {
Expand Down
53 changes: 34 additions & 19 deletions src/librustc_data_structures/flock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,19 +220,18 @@ mod imp {
use std::path::Path;
use std::fs::{File, OpenOptions};
use std::os::raw::{c_ulong, c_ulonglong, c_int};
use std::os::windows::fs::OpenOptionsExt;

pub type DWORD = c_ulong;
pub type BOOL = c_int;
pub type ULONG_PTR = c_ulonglong;
type DWORD = c_ulong;
type BOOL = c_int;
type ULONG_PTR = c_ulonglong;

type LPOVERLAPPED = *mut OVERLAPPED;
const LOCKFILE_EXCLUSIVE_LOCK: DWORD = 0x00000002;
const LOCKFILE_FAIL_IMMEDIATELY: DWORD = 0x00000001;

pub const FILE_SHARE_DELETE: DWORD = 0x4;
pub const FILE_SHARE_READ: DWORD = 0x1;
pub const FILE_SHARE_WRITE: DWORD = 0x2;
const FILE_SHARE_DELETE: DWORD = 0x4;
const FILE_SHARE_READ: DWORD = 0x1;
const FILE_SHARE_WRITE: DWORD = 0x2;

#[repr(C)]
struct OVERLAPPED {
Expand Down Expand Up @@ -263,19 +262,30 @@ mod imp {
create: bool,
exclusive: bool)
-> io::Result<Lock> {
assert!(p.parent().unwrap().exists(),
"Parent directory of lock-file must exist: {}",
p.display());

let share_mode = FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE;

let f = {
let mut open_options = OpenOptions::new().read(true)
.share_mode(share_mode);
if create {
open_options.create(true);
}
let mut open_options = OpenOptions::new();
open_options.read(true)
.share_mode(share_mode);

if create {
open_options.create(true)
.write(true);
}

match open_options.open(p) {
Ok(file) => file,
Err(err) => return Err(err),
debug!("Attempting to open lock file `{}`", p.display());
let file = match open_options.open(p) {
Ok(file) => {
debug!("Lock file opened successfully");
file
}
Err(err) => {
debug!("Error opening lock file: {}", err);
return Err(err)
}
};

Expand All @@ -291,17 +301,22 @@ mod imp {
dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
}

LockFileEx(f.as_raw_handle(),
debug!("Attempting to acquire lock on lock file `{}`",
p.display());
LockFileEx(file.as_raw_handle(),
dwFlags,
0,
0xFFFF_FFFF,
0xFFFF_FFFF,
&mut overlapped)
};
if ret == 0 {
Err(io::Error::last_os_error())
let err = io::Error::last_os_error();
debug!("Failed acquiring file lock: {}", err);
Err(err)
} else {
Ok(Lock { _file: f })
debug!("Successfully acquired lock.");
Ok(Lock { _file: file })
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_data_structures/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
#![feature(staged_api)]
#![feature(unboxed_closures)]
#![feature(fn_traits)]
#![feature(libc)]

#![cfg_attr(unix, feature(libc))]
#![cfg_attr(test, feature(test))]

extern crate core;
Expand Down
Loading

0 comments on commit 95d3cf3

Please sign in to comment.