1
0
mirror of https://github.com/samba-team/samba.git synced 2024-12-23 17:34:34 +03:00

tevent: add tevent_fd_set_close_fn()

Let callers specify a close function
as an alternative to TEVENT_FD_AUTOCLOSE.

metze
This commit is contained in:
Stefan Metzmacher 2009-01-03 11:27:00 +01:00
parent e928d863e2
commit e45ed828d0
8 changed files with 48 additions and 5 deletions

View File

@ -240,6 +240,16 @@ struct tevent_aio *_tevent_add_aio(struct tevent_context *ev,
handler_name, location);
}
/*
set a close function on the fd event
*/
void tevent_fd_set_close_fn(struct tevent_fd *fde,
tevent_fd_close_fn_t close_fn)
{
if (!fde) return;
fde->event_ctx->ops->set_fd_close_fn(fde, close_fn);
}
/*
return the fd event flags
*/

View File

@ -37,6 +37,10 @@ typedef void (*tevent_fd_handler_t)(struct tevent_context *ev,
struct tevent_fd *fde,
uint16_t flags,
void *private_data);
typedef void (*tevent_fd_close_fn_t)(struct tevent_context *ev,
struct tevent_fd *fde,
int fd,
void *private_data);
typedef void (*tevent_timer_handler_t)(struct tevent_context *ev,
struct tevent_timer *te,
struct timeval current_time,
@ -107,6 +111,8 @@ struct tevent_aio *_tevent_add_aio(struct tevent_context *ev,
int tevent_loop_once(struct tevent_context *ev);
int tevent_loop_wait(struct tevent_context *ev);
void tevent_fd_set_close_fn(struct tevent_fd *fde,
tevent_fd_close_fn_t close_fn);
uint16_t tevent_fd_get_flags(struct tevent_fd *fde);
void tevent_fd_set_flags(struct tevent_fd *fde, uint16_t flags);

View File

@ -400,7 +400,10 @@ static int aio_event_fd_destructor(struct tevent_fd *fde)
epoll_del_event(aio_ev, fde);
if (fde->flags & TEVENT_FD_AUTOCLOSE) {
if (fde->close_fn) {
fde->close_fn(ev, fde, fde->fd, fde->private_data);
fde->fd = -1;
} else if (fde->flags & TEVENT_FD_AUTOCLOSE) {
close(fde->fd);
fde->fd = -1;
}
@ -551,6 +554,7 @@ static const struct tevent_ops aio_event_ops = {
.context_init = aio_event_context_init,
.add_fd = aio_event_add_fd,
.add_aio = aio_event_add_aio,
.set_fd_close_fn= tevent_common_fd_set_close_fn,
.get_fd_flags = tevent_common_fd_get_flags,
.set_fd_flags = aio_event_set_fd_flags,
.add_timer = tevent_common_add_timer,

View File

@ -358,7 +358,10 @@ static int epoll_event_fd_destructor(struct tevent_fd *fde)
epoll_del_event(epoll_ev, fde);
if (fde->flags & TEVENT_FD_AUTOCLOSE) {
if (fde->close_fn) {
fde->close_fn(ev, fde, fde->fd, fde->private_data);
fde->fd = -1;
} else if (fde->flags & TEVENT_FD_AUTOCLOSE) {
close(fde->fd);
fde->fd = -1;
}
@ -463,6 +466,7 @@ static int epoll_event_loop_wait(struct tevent_context *ev)
static const struct tevent_ops epoll_event_ops = {
.context_init = epoll_event_context_init,
.add_fd = epoll_event_add_fd,
.set_fd_close_fn= tevent_common_fd_set_close_fn,
.get_fd_flags = tevent_common_fd_get_flags,
.set_fd_flags = epoll_event_set_fd_flags,
.add_timer = tevent_common_add_timer,

View File

@ -34,3 +34,9 @@ void tevent_common_fd_set_flags(struct tevent_fd *fde, uint16_t flags)
if (fde->flags == flags) return;
fde->flags = flags;
}
void tevent_common_fd_set_close_fn(struct tevent_fd *fde,
tevent_fd_close_fn_t close_fn)
{
fde->close_fn = close_fn;
}

View File

@ -33,6 +33,8 @@ struct tevent_ops {
void *private_data,
const char *handler_name,
const char *location);
void (*set_fd_close_fn)(struct tevent_fd *fde,
tevent_fd_close_fn_t close_fn);
uint16_t (*get_fd_flags)(struct tevent_fd *fde);
void (*set_fd_flags)(struct tevent_fd *fde, uint16_t flags);
@ -70,8 +72,9 @@ struct tevent_fd {
struct tevent_fd *prev, *next;
struct tevent_context *event_ctx;
int fd;
uint16_t flags; /* see EVENT_FD_* flags */
uint16_t flags; /* see TEVENT_FD_* flags */
tevent_fd_handler_t handler;
tevent_fd_close_fn_t close_fn;
/* this is private for the specific handler */
void *private_data;
/* this is for debugging only! */
@ -146,6 +149,8 @@ struct tevent_context {
bool tevent_register_backend(const char *name, const struct tevent_ops *ops);
void tevent_common_fd_set_close_fn(struct tevent_fd *fde,
tevent_fd_close_fn_t close_fn);
uint16_t tevent_common_fd_get_flags(struct tevent_fd *fde);
void tevent_common_fd_set_flags(struct tevent_fd *fde, uint16_t flags);

View File

@ -103,7 +103,10 @@ static int select_event_fd_destructor(struct tevent_fd *fde)
DLIST_REMOVE(select_ev->fd_events, fde);
select_ev->destruction_count++;
if (fde->flags & TEVENT_FD_AUTOCLOSE) {
if (fde->close_fn) {
fde->close_fn(ev, fde, fde->fd, fde->private_data);
fde->fd = -1;
} else if (fde->flags & TEVENT_FD_AUTOCLOSE) {
close(fde->fd);
fde->fd = -1;
}
@ -266,6 +269,7 @@ static int select_event_loop_wait(struct tevent_context *ev)
static const struct tevent_ops select_event_ops = {
.context_init = select_event_context_init,
.add_fd = select_event_add_fd,
.set_fd_close_fn= tevent_common_fd_set_close_fn,
.get_fd_flags = tevent_common_fd_get_flags,
.set_fd_flags = tevent_common_fd_set_flags,
.add_timer = tevent_common_add_timer,

View File

@ -392,7 +392,10 @@ static int std_event_fd_destructor(struct tevent_fd *fde)
epoll_del_event(std_ev, fde);
if (fde->flags & TEVENT_FD_AUTOCLOSE) {
if (fde->close_fn) {
fde->close_fn(ev, fde, fde->fd, fde->private_data);
fde->fd = -1;
} else if (fde->flags & TEVENT_FD_AUTOCLOSE) {
close(fde->fd);
fde->fd = -1;
}
@ -584,6 +587,7 @@ static int std_event_loop_wait(struct tevent_context *ev)
static const struct tevent_ops std_event_ops = {
.context_init = std_event_context_init,
.add_fd = std_event_add_fd,
.set_fd_close_fn= tevent_common_fd_set_close_fn,
.get_fd_flags = tevent_common_fd_get_flags,
.set_fd_flags = std_event_set_fd_flags,
.add_timer = tevent_common_add_timer,