mirror of
https://codeberg.org/redict/redict.git
synced 2025-01-23 16:48:27 -05:00
8ac7af1c5d
While this feature is not used by Redis, ae.c implements the ability for a timer to call a finalizer callback when an timer event is deleted. This feature was bugged since the start, and because it was never used we never noticed a problem. However Anthony LaTorre was using the same library in order to implement a different system: he found a bug that he describes as follows, and which he fixed with the patch in this commit, sent me by private email: --- Anthony email --- 've found one bug in the current implementation of the timed events. It's possible to lose track of a timed event if an event is added in the finalizerProc of another event. For example, suppose you start off with three timed events 1, 2, and 3. Then the linked list looks like: 3 -> 2 -> 1 Then, you run processTimeEvents and events 2 and 3 finish, so now the list looks like: -1 -> -1 -> 2 Now, on the next iteration of processTimeEvents it starts by deleting the first event, and suppose this finalizerProc creates a new event, so that the list looks like this: 4 -> -1 -> 2 On the next iteration of the while loop, when it gets to the second event, the variable prev is still set to NULL, so that the head of the event loop after the next event will be set to 2, i.e. after deleting the next event the event loop will look like: 2 and the event with id 4 will be lost. I've attached an example program to illustrate the issue. If you run it you will see that it prints: ``` foo id = 0 spam! ``` But if you uncomment line 29 and run it again it won't print "spam!". --- End of email --- Test.c source code is as follows: #include "ae.h" #include <stdio.h> aeEventLoop *el; int foo(struct aeEventLoop *el, long long id, void *data) { printf("foo id = %lld\n", id); return AE_NOMORE; } int spam(struct aeEventLoop *el, long long id, void *data) { printf("spam!\n"); return AE_NOMORE; } void bar(struct aeEventLoop *el, void *data) { aeCreateTimeEvent(el, 0, spam, NULL, NULL); } int main(int argc, char **argv) { el = aeCreateEventLoop(100); //aeCreateTimeEvent(el, 0, foo, NULL, NULL); aeCreateTimeEvent(el, 0, foo, NULL, bar); aeMain(el); return 0; } Anthony fixed the problem by using a linked list for the list of timers, and sent me back this patch after he tested the code in production for some time. The code looks sane to me, so committing it to Redis.
133 lines
5.2 KiB
C
133 lines
5.2 KiB
C
/* A simple event-driven programming library. Originally I wrote this code
|
|
* for the Jim's event-loop (Jim is a Tcl interpreter) but later translated
|
|
* it in form of a library for easy reuse.
|
|
*
|
|
* Copyright (c) 2006-2012, Salvatore Sanfilippo <antirez at gmail dot com>
|
|
* All rights reserved.
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without
|
|
* modification, are permitted provided that the following conditions are met:
|
|
*
|
|
* * Redistributions of source code must retain the above copyright notice,
|
|
* this list of conditions and the following disclaimer.
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
* documentation and/or other materials provided with the distribution.
|
|
* * Neither the name of Redis nor the names of its contributors may be used
|
|
* to endorse or promote products derived from this software without
|
|
* specific prior written permission.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
*/
|
|
|
|
#ifndef __AE_H__
|
|
#define __AE_H__
|
|
|
|
#include <time.h>
|
|
|
|
#define AE_OK 0
|
|
#define AE_ERR -1
|
|
|
|
#define AE_NONE 0 /* No events registered. */
|
|
#define AE_READABLE 1 /* Fire when descriptor is readable. */
|
|
#define AE_WRITABLE 2 /* Fire when descriptor is writable. */
|
|
#define AE_BARRIER 4 /* With WRITABLE, never fire the event if the
|
|
READABLE event already fired in the same event
|
|
loop iteration. Useful when you want to persist
|
|
things to disk before sending replies, and want
|
|
to do that in a group fashion. */
|
|
|
|
#define AE_FILE_EVENTS 1
|
|
#define AE_TIME_EVENTS 2
|
|
#define AE_ALL_EVENTS (AE_FILE_EVENTS|AE_TIME_EVENTS)
|
|
#define AE_DONT_WAIT 4
|
|
#define AE_CALL_AFTER_SLEEP 8
|
|
|
|
#define AE_NOMORE -1
|
|
#define AE_DELETED_EVENT_ID -1
|
|
|
|
/* Macros */
|
|
#define AE_NOTUSED(V) ((void) V)
|
|
|
|
struct aeEventLoop;
|
|
|
|
/* Types and data structures */
|
|
typedef void aeFileProc(struct aeEventLoop *eventLoop, int fd, void *clientData, int mask);
|
|
typedef int aeTimeProc(struct aeEventLoop *eventLoop, long long id, void *clientData);
|
|
typedef void aeEventFinalizerProc(struct aeEventLoop *eventLoop, void *clientData);
|
|
typedef void aeBeforeSleepProc(struct aeEventLoop *eventLoop);
|
|
|
|
/* File event structure */
|
|
typedef struct aeFileEvent {
|
|
int mask; /* one of AE_(READABLE|WRITABLE|BARRIER) */
|
|
aeFileProc *rfileProc;
|
|
aeFileProc *wfileProc;
|
|
void *clientData;
|
|
} aeFileEvent;
|
|
|
|
/* Time event structure */
|
|
typedef struct aeTimeEvent {
|
|
long long id; /* time event identifier. */
|
|
long when_sec; /* seconds */
|
|
long when_ms; /* milliseconds */
|
|
aeTimeProc *timeProc;
|
|
aeEventFinalizerProc *finalizerProc;
|
|
void *clientData;
|
|
struct aeTimeEvent *prev;
|
|
struct aeTimeEvent *next;
|
|
} aeTimeEvent;
|
|
|
|
/* A fired event */
|
|
typedef struct aeFiredEvent {
|
|
int fd;
|
|
int mask;
|
|
} aeFiredEvent;
|
|
|
|
/* State of an event based program */
|
|
typedef struct aeEventLoop {
|
|
int maxfd; /* highest file descriptor currently registered */
|
|
int setsize; /* max number of file descriptors tracked */
|
|
long long timeEventNextId;
|
|
time_t lastTime; /* Used to detect system clock skew */
|
|
aeFileEvent *events; /* Registered events */
|
|
aeFiredEvent *fired; /* Fired events */
|
|
aeTimeEvent *timeEventHead;
|
|
int stop;
|
|
void *apidata; /* This is used for polling API specific data */
|
|
aeBeforeSleepProc *beforesleep;
|
|
aeBeforeSleepProc *aftersleep;
|
|
} aeEventLoop;
|
|
|
|
/* Prototypes */
|
|
aeEventLoop *aeCreateEventLoop(int setsize);
|
|
void aeDeleteEventLoop(aeEventLoop *eventLoop);
|
|
void aeStop(aeEventLoop *eventLoop);
|
|
int aeCreateFileEvent(aeEventLoop *eventLoop, int fd, int mask,
|
|
aeFileProc *proc, void *clientData);
|
|
void aeDeleteFileEvent(aeEventLoop *eventLoop, int fd, int mask);
|
|
int aeGetFileEvents(aeEventLoop *eventLoop, int fd);
|
|
long long aeCreateTimeEvent(aeEventLoop *eventLoop, long long milliseconds,
|
|
aeTimeProc *proc, void *clientData,
|
|
aeEventFinalizerProc *finalizerProc);
|
|
int aeDeleteTimeEvent(aeEventLoop *eventLoop, long long id);
|
|
int aeProcessEvents(aeEventLoop *eventLoop, int flags);
|
|
int aeWait(int fd, int mask, long long milliseconds);
|
|
void aeMain(aeEventLoop *eventLoop);
|
|
char *aeGetApiName(void);
|
|
void aeSetBeforeSleepProc(aeEventLoop *eventLoop, aeBeforeSleepProc *beforesleep);
|
|
void aeSetAfterSleepProc(aeEventLoop *eventLoop, aeBeforeSleepProc *aftersleep);
|
|
int aeGetSetSize(aeEventLoop *eventLoop);
|
|
int aeResizeSetSize(aeEventLoop *eventLoop, int setsize);
|
|
|
|
#endif
|