Skip to content

Commit

Permalink
make semaphore panic safer
Browse files Browse the repository at this point in the history
  • Loading branch information
corwinkuiper committed Aug 30, 2024
1 parent 35f70d7 commit 00b97e5
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,23 @@ impl Semaphore {
*inner -= 1;
}

let r = f();
struct SemaphoreDrop<'a> {
mutex: &'a Mutex<usize>,
cond: &'a Condvar,
}

*self.mutex.lock().unwrap() += 1;
impl Drop for SemaphoreDrop<'_> {
fn drop(&mut self) {
*self.mutex.lock().unwrap() += 1;
self.cond.notify_one();
}
}

let _d = SemaphoreDrop {
mutex: &self.mutex,
cond: &self.cond,
};

self.cond.notify_one();
r
f()
}
}

0 comments on commit 00b97e5

Please sign in to comment.