Skip to content

Commit

Permalink
feat timer: added initial functions
Browse files Browse the repository at this point in the history
Signed-off-by: John Sanpe <[email protected]>
  • Loading branch information
sanpeqf committed Feb 29, 2024
1 parent ecf31e1 commit 71804f2
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 7 deletions.
13 changes: 7 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@ set(HEADERS

set(SRCS
${HEADERS}
${PROJECT_SOURCE_DIR}/src/xdbd.c
${PROJECT_SOURCE_DIR}/src/adb/adb.c
${PROJECT_SOURCE_DIR}/src/adb/xdbd_adb.c
${PROJECT_SOURCE_DIR}/src/adb/xdbd_adb_request.c
${PROJECT_SOURCE_DIR}/src/adb/command.c
${PROJECT_SOURCE_DIR}/src/adb/packet.c
${PROJECT_SOURCE_DIR}/src/adb/xdbd_adb.c
${PROJECT_SOURCE_DIR}/src/adb/xdbd_adb_request.c
${PROJECT_SOURCE_DIR}/src/connection/connection.c
${PROJECT_SOURCE_DIR}/src/event/xdbd_event.c
${PROJECT_SOURCE_DIR}/src/event/select/xdbd_select.c
${PROJECT_SOURCE_DIR}/src/core/xdbd_pool.c
${PROJECT_SOURCE_DIR}/src/core/xdbd_buf.c
${PROJECT_SOURCE_DIR}/src/core/xdbd_pool.c
${PROJECT_SOURCE_DIR}/src/core/xdbd_timer.c
${PROJECT_SOURCE_DIR}/src/event/select/xdbd_select.c
${PROJECT_SOURCE_DIR}/src/event/xdbd_event.c
${PROJECT_SOURCE_DIR}/src/xdbd.c
)

set(
Expand Down
2 changes: 1 addition & 1 deletion include/xdbd.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ typedef struct xdbd_connection_s xdbd_connection_t;
typedef struct xdbd_listening_s xdbd_listening_t;
typedef struct xdbd_event_s xdbd_event_t;
typedef struct xdbd_buf_s xdbd_buf_t;

typedef struct xdbd_timer_s xdbd_timer_t;
typedef struct xdbd_s xdbd_t;

struct xdbd_s {
Expand Down
38 changes: 38 additions & 0 deletions src/core/xdbd_timer.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* Copyright(c) 2024 John Sanpe <[email protected]>
*/

#include <xdbd_timer.h>

BFDEV_RB_ROOT_CACHED(xdbd_timers);

static inline long
timer_cmp(const bfdev_rb_node_t *key1, const bfdev_rb_node_t *key2, void *pdata)
{
xdbd_timer_t *node1, *node2;

node1 = bfdev_container_of(key1, xdbd_timer_t, rb);
node2 = bfdev_container_of(key2, xdbd_timer_t, rb);

/* Ignoring conflicts */
return node1->time < node2->time ? BFDEV_LT : BFDEV_BT;
}

xdbd_timer_t *
xdbd_first_timer(void)
{
return bfdev_rb_cached_first_entry(&xdbd_timers, xdbd_timer_t, rb);
}

void
xdbd_add_timer(xdbd_timer_t *timer)
{
bfdev_rb_cached_insert(&xdbd_timers, &timer->rb, timer_cmp, NULL);
}

void
xdbd_remove_timer(xdbd_timer_t *timer)
{
bfdev_rb_cached_remove(&xdbd_timers, &timer->rb);
}
28 changes: 28 additions & 0 deletions src/core/xdbd_timer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* Copyright(c) 2024 John Sanpe <[email protected]>
*/

#ifndef __XDBD_TIMER__H__
#define __XDBD_TIMER__H__

#include <xdbd.h>
#include <bfdev.h>

struct xdbd_timer_s {
bfdev_rb_node_t rb;
xdbd_msec_t time;
};

extern bfdev_rb_root_cached_t xdbd_timers;

extern xdbd_timer_t *
xdbd_first_timer(void);

extern void
xdbd_add_timer(xdbd_timer_t *timer);

extern void
xdbd_remove_timer(xdbd_timer_t *timer);

#endif /* __XDBD_TIMER__H__ */

0 comments on commit 71804f2

Please sign in to comment.