Expand description
§Semaphore.
A semaphore is a fundamental synchronization primitive used to regulate concurrent access to a finite set of resources. It maintains an internal count representing the number of available “permits.” Each permit grants a thread the right to access a shared resource. Semaphores are particularly well-suited for controlling resource allocation and coordinating thread execution in systems with bounded capacity constraints.
Conceptually, a semaphore can be implemented using a combination of a mutex for mutual exclusion and a condition variable to support blocking and waking threads. This approach ensures thread-safe and efficient control over the internal permit count.
Semaphores are widely used in operating systems to solve classic concurrency problems. One common example is the producer-consumer pattern, in which multiple threads coordinate access to a bounded buffer. A semaphore ensures that producers do not overfill the buffer and consumers do not read from an empty buffer.
Another critical use case is event signaling. A semaphore initialized with zero permits can serve as a one-time or repeating signal, allowing one thread to notify another when a particular event has occurred.
§Semaphore in KeOS
In KeOS, the Semaphore abstraction provides a clean and safe interface
for controlling access to shared resources. Semaphore is combined with a
resource to protect. Threads can acquire a permit by calling
Semaphore::wait, and release it either explicitly via
Semaphore::signal or implicitly using the SemaphorePermits RAII
guard. This design encourages robust and leak-free resource management, even
in the presence of early returns or panics.
Key features of the Semaphore interface include:
-
Semaphore::wait(): Decrements the permit count if a permit is available. If no permits remain, the calling thread blocks until one becomes available. -
Semaphore::signal(): Increments the permit count and wakes one blocked thread, if any. This allows threads to proceed once a resource has been released or an event has been signaled. -
SemaphorePermits: An RAII-based wrapper that automatically callssignal()when dropped. This ensures that permits are always correctly released, even in the face of errors or control-flow disruptions.
By integrating semaphores into the KeOS kernel, you will gain a flexible and expressive tool for coordinating access to shared resources and implementing complex thread synchronization patterns.
§Usage Example
let sema = Semaphore::new(3, state); // Allows up to 3 concurrent threads to the state
// Acquire a permit (blocks if unavailable)
let permit = sema.wait();
// Critical section (up to 3 threads can enter concurrently)
permit.work(); // Call a method defined on the `state`.
// Permit is automatically released when `permit` goes out of scope.
// Otherwise, you can explicitly released it with `drop(permit)``§Implementation Requirements
You need to implement the followings:
By implementing the all synchorinzation primitives, your KeOS kernel now
ready to serve multi-threaded process in the next section.
Structs§
- Semaphore
- Counting semaphore.
- Semaphore
Permits - An RAII implementation of a “scoped semaphore”. When this structure is dropped (falls out of scope), the semaphore will be signaled.