Skip to content

Commit

Permalink
Added PancakeSchedulerGetNextExecutionTimeOffset()
Browse files Browse the repository at this point in the history
Prevents race conditions caused by several calls to time() (might result
in calculated offset -1)
  • Loading branch information
pp3345 committed Apr 11, 2014
1 parent fc240d4 commit a30300c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion LinuxPoll/PancakeLinuxPoll.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ static void PancakeLinuxPollWait() {
struct epoll_event events[32];
Int32 numEvents, i;

numEvents = epoll_wait(PancakeLinuxPollFD, events, 32, (PancakeSchedulerGetNextExecutionTime() - time(NULL)) * 1000);
numEvents = epoll_wait(PancakeLinuxPollFD, events, 32, PancakeSchedulerGetNextExecutionTimeOffset() * 1000);

if(UNEXPECTED(numEvents == -1)) {
if(PancakeDoShutdown) {
Expand Down
12 changes: 12 additions & 0 deletions PancakeScheduler.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,18 @@ PANCAKE_API inline UNative PancakeSchedulerGetNextExecutionTime() {
return events->time < time(NULL) ? time(NULL) : events->time;
}

PANCAKE_API inline UNative PancakeSchedulerGetNextExecutionTimeOffset() {
UNative now;

if(!events) {
return 86400;
}

now = time(NULL);

return events->time < now ? 0 : events->time - now;
}

PANCAKE_API inline UNative PancakeSchedulerGetNextScheduledTime() {
if(!events) {
return 0;
Expand Down
1 change: 1 addition & 0 deletions PancakeScheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ typedef struct _PancakeSchedulerEvent {
PANCAKE_API PancakeSchedulerEvent *PancakeSchedule(UNative time, PancakeSchedulerEventCallback callback, void *arg);
PANCAKE_API inline void PancakeUnschedule(PancakeSchedulerEvent *event);
PANCAKE_API inline UNative PancakeSchedulerGetNextExecutionTime(); /* returns >= time() */
PANCAKE_API inline UNative PancakeSchedulerGetNextExecutionTimeOffset(); /* return >= 0 */
PANCAKE_API inline UNative PancakeSchedulerGetNextScheduledTime(); /* returns actual scheduled time (can be < time()) */
PANCAKE_API void PancakeSchedulerRun();

Expand Down

0 comments on commit a30300c

Please sign in to comment.