A high-efficiency synchronization primitive that combines FIFO fairness with priority scheduling.
- Key Features
- Algorithm Overview
- Build Instructions
- API Reference
- Performance Characteristics
- Contributing
- License
✔ Strict Priority Scheduling - Highest priority wins when timestamps collide
✔ Deadlock-free - Guaranteed progress for all threads
✔ Condition Variable Based - Efficient thread wakeup
✔ Lightweight - Pure POSIX implementation
Three-phase selection process:
- Queue Insertion - Timestamped requests with priorities
- Winner Selection:
// 1. Find earliest timestamp
// 2. Select max priority among earliest
- Atomic Handoff - Condition variables ensure clean transitions
git clone https://github.com/RaziKouas/priority-mutual-exclusion-algorithm.git
cd priority-mutual-exclusion-algorithm
make
./pmutex_test
Function | Parameters | Description |
---|---|---|
pmutex_init() |
PMutex* m, int capacity |
Initialize mutex with queue capacity |
pmutex_free() |
PMutex* m |
Release all resources |
pmutex_request() |
PMutex* m, int pid, int priority |
Enqueue access request |
pmutex_lock() |
PMutex* m, int pid |
Block until granted access |
pmutex_unlock() |
PMutex* m, int pid |
Release critical section |
typedef struct {
pthread_mutex_t lock;
pthread_cond_t cond;
Process* queue; // Priority queue
int capacity; // Max queue size
int size; // Current queue size
int current_holder; // PID in critical section (-1 if empty)
} PMutex;
typedef struct {
int pid;
int priority;
struct timeval timestamp;
} Process;
Operation | Complexity | Notes |
---|---|---|
Request | O(1) | Lock + append |
Lock | O(n) | Queue scan + cond wait |
Unlock | O(1) | Atomic state update |
We welcome contributions! Please follow these guidelines:
- Fork the repository
- Create a feature branch (
git checkout -b feature/your-feature
) - Commit your changes
- Push to the branch
- Open a pull request
MIT © [Razi Kouas] - See LICENSE for details.