Coding: Timed Event (Slideshow)#
Per-Event Timestamp#
Realtime vs. monotonic
jiffiesis current monotonic timeModify
my_eventhave a timestamp in
jiffiesformdetermined in
my_event_list_add()
Discussion: Deferred Events#
ioctl()to add a deferred eventCan we have multiple deferred events pending?
⟶ lets have only one for now. Otherwise we’d have to maintain a list somehow.
Who’s responsible for maintaining the timer/work?
⟶
struct my_device
Add Timer#
New
ioctl:MY_INJECT_DEFERRED_EVENT, a milliseconds parametermy_device_add_deferred_event()Userspace:
test-inject-deferred-event.cTest
Gotcha: Cleanup Running Timer#
Inject deferred event: 10s
rmmod my_driverWait until panic
Fix
my_device_destroy():del_timer_sync()
More Gotchas#
Questions
What if somebody injects an event while a deferred event is pending?
Try it out ⟶ panic
What if no event is pending altogether? Can I delete a timer that is not running? (Documentation is not at all clear ⟶ paranoia)
Try it out ⟶ work (appears to work)
Answer
Naive: flag and no locking
Bad ⟶ Spinlock (timers run in interrupt context)
Refactoring#
Symmetry: my_device_add_event(), and call that from
MY_INJECT_EVENT