TimerLib: A High-level POSIX Timer Library in C
Links and Resources 🔗
GitHub Repository: TimerLib GitHub Link 🌐
Introduction
Hello and welcome to another technical blog post! Today, I want to showcase a project that I am particularly proud of: TimerLib, a high-level POSIX timer library written in C. If you’ve ever found yourself grappling with Linux timers and wishing for a more user-friendly way to handle them, this post and project are for you!
Objective
The goal of this project is to simplify timer management tasks, such as starting, pausing, resuming, and deleting timers. With TimerLib, developers can manage timers with a clean, straightforward API, abstracting away the complexity associated with lower-level timer functionalities.
Why TimerLib?
Here’s why TimerLib stands out:
- Simplified API: No more cumbersome setup or state management.
- State Management: Easily track your timer’s current state (INIT, RUNNING, PAUSED, etc.).
- Callback Support: Trigger customized callback functions upon timer expiration.
- Pause and Resume: Pause timers and resume right from where you left off.
- Time Remaining: Check how much time is left on your timer.
- Exponential Backoff: An optional feature that allows timers to back off exponentially.
Behind the Scenes: How TimerLib Works
Before diving into the implementation, let’s take a quick look at the structure that makes TimerLib so powerful:
typedef struct Timer_{
timer_t posix_timer;
void *user_arg;
unsigned long exp_timer;
unsigned long sec_exp_timer;
uint32_t thresdhold;
void (*cb)(struct Timer_ *, void *);
bool exponential_backoff;
unsigned long time_remaining;
uint32_t invocation_counter;
struct itimerspec ts;
unsigned long exp_back_off_time;
TIMER_STATE_T timer_state;
} Timer_t;
Here’s what each field in the Timer_t structure accomplishes:
- posix_timer: The native POSIX timer object.
- user_arg: Custom arguments for the callback function.
- exp_timer & sec_exp_timer: Control over first and subsequent timer expirations.
- thresdhold: Sets a limit on how many times the timer callback will be invoked.
- cb: A callback function to be invoked when the timer expires.
- exponential_backoff: Support for exponential backoff timers.
- time_remaining: Keeps track of remaining time when the timer is paused.
- invocation_counter: Counts how many times the timer has been invoked.
- ts: POSIX interval timer specification.
- exp_back_off_time: Time for exponential back-off.
- timer_state: Current state of the timer (INIT, RUNNING, PAUSED, etc.).
Sample Functions and Their Logic 🔍
Let’s get into the nitty-gritty details of some core functions:
start_timer 🟢
void start_timer(Timer_t *timer)
Logic
- Validates the timer object and initializes the POSIX timer. ✅
- Changes the timer’s internal state to RUNNING. 🏃♂️
- Starts the timer using POSIX’s timer_settime. ⏲️
pause_timer ⏸️
void pause_timer(Timer_t *timer);
Logic
- Checks the timer object’s validity and state. 👀
- Changes the internal state to PAUSED. ⏸️
- Stops the timer using lower-level POSIX functions. 🛑
resume_timer ▶️
void resume_timer(Timer_t *timer);
Logic
- Ensures the timer object and its state are valid. 👌
- Resumes the timer exactly where it was paused using saved state information. ⏯️
You can find further explanations and examples in the official README of the TimerLib GitHub repository. 📘
Conclusion and Future Work 🌟
TimerLib aims to take the pain out of working with Linux timers by providing a high-level, user-friendly API. It’s the perfect tool for anyone looking to manage timers without delving into the complexities of native timer systems. 🎈
In the future, I plan to extend TimerLib to support more advanced features and perhaps a GUI interface for easier timer management. 💡
Happy Coding! 🎉💻