keos_project4/sync/mod.rs
1//! # Synchronization Primitives.
2//!
3//! An operating system kernel must coordinate access to shared resources among
4//! multiple threads of execution. This coordination is vital to ensure data
5//! consistency, prevent race conditions, and maintain overall system stability
6//! and performance.
7//!
8//! In earlier projects, KeOS employed a single synchronization primitive: the
9//! [`SpinLock`]. While [`SpinLock`] provides correct mutual exclusion by
10//! repeatedly checking for lock availability (i.e., "spinning"), it is
11//! inefficient under high contention, as it wastes CPU cycles by actively
12//! polling instead of sleeping.
13//!
14//! In this project, you will address these limitations by implementing
15//! additional synchronization primitives that offer more efficient and
16//! expressive mechanisms for managing concurrency. These primitives are
17//! essential tools for building correct and performant multithreaded
18//! systems, particularly in contexts where threads may need to sleep,
19//! coordinate, or share limited resources.
20//!
21//! The synchronization primitives you will implement include:
22//!
23//! - [`Mutex`]: A mutual exclusion primitive that ensures only one thread can
24//! access a critical section at a time. Unlike a spinlock, a mutex puts the
25//! thread to sleep if the lock is unavailable, avoiding busy-waiting and
26//! reducing CPU usage.
27//!
28//! - [`ConditionVariable`]: A coordination mechanism that allows threads to
29//! sleep until a particular condition becomes true. It enables efficient
30//! producer-consumer style synchronization and is often used in conjunction
31//! with a mutex.
32//!
33//! - [`Semaphore`]: A counting synchronization primitive that controls access
34//! to a shared resource by maintaining a counter. It allows a fixed number of
35//! threads to access the resource concurrently and can also be used to
36//! implement other synchronization patterns.
37//!
38//! Together, these primitives provide a flexible foundation for managing
39//! concurrency in a robust and scalable kernel. They will enable you to
40//! implement more sophisticated system services, such as blocking system calls
41//! and multithreaded applications.
42//!
43//! Different synchronization primitives are suited to different concurrency
44//! patterns. The table below summarizes their key characteristics:
45//!
46//! | Primitive | Blocks Thread? | Fair? | Typical Use Case |
47//! |-----------------------|----------------|----------|--------------------------------------------------|
48//! | [`SpinLock`] | No (busy wait) | No | Short, uncontended critical sections in the kernel |
49//! | [`Mutex`] | Yes | Yes | Exclusive access to shared data |
50//! | [`ConditionVariable`] | Yes | Yes | Waiting for a condition to become true |
51//! | [`Semaphore`] | Yes | Depends | Limiting access to a bounded resource |
52//!
53//! - **SpinLock** spins in a loop until the lock becomes available. This is
54//! suitable for extremely short operations in low-contention paths.
55//! - **Mutex** puts the thread to sleep when the lock is unavailable, making it
56//! ideal for when exclusive access is needed and blocking is acceptable
57//! - **ConditionVariable** works in tandem with a mutex to block threads until
58//! a predicate becomes false, then wake them up. Meaning that it fits when a
59//! thread must wait for a condition while coordinating with a [`Mutex`].
60//! - **Semaphore** tracks a count of available permits and is often used to
61//! control access to a pool of resources or to implement thread joins. It can
62//! be used when multiple threads can proceed concurrently, up to a fixed
63//! limit.
64//!
65//! ## Implementation Orders
66//! 1. [`mutex`]
67//! 2. [`condition_variable`]
68//! 3. [`semaphore`]
69//!
70//! [`mutex`]: self::mutex
71//! [`condition_variable`]: self::condition_variable
72//! [`semaphore`]: self::semaphore
73//! [`SpinLock`]: keos::sync::SpinLock
74//! [`Mutex`]: crate::sync::mutex::Mutex
75//! [`ConditionVariable`]: crate::sync::condition_variable::ConditionVariable
76//! [`Semaphore`]: crate::sync::semaphore::Semaphore
77
78pub mod condition_variable;
79pub mod mutex;
80pub mod semaphore;
81
82pub use condition_variable::*;
83pub use mutex::*;
84pub use semaphore::*;