diff options
author | Luo Jinghua <sunmoon1997@gmail.com> | 2009-10-31 14:55:17 +0800 |
---|---|---|
committer | Luo Jinghua <sunmoon1997@gmail.com> | 2009-10-31 14:55:17 +0800 |
commit | 79022be97f49e5a9a6049d88a1ac4c539f47a199 (patch) | |
tree | 82b81abdc40c582ff18a2650cf687ce102e36899 | |
parent | 1cc5b3b0ae5f6b5bf3d7bb1f2c81611e7dd278ea (diff) |
milkway: remember all fds
-rw-r--r-- | milkway/mw-poll-sys-private.h | 4 | ||||
-rw-r--r-- | milkway/mw-poll-sys.c | 4 | ||||
-rw-r--r-- | milkway/mw-poll.c | 65 | ||||
-rw-r--r-- | milkway/mw-poll.h | 6 |
4 files changed, 72 insertions, 7 deletions
diff --git a/milkway/mw-poll-sys-private.h b/milkway/mw-poll-sys-private.h index f190030..6b57e09 100644 --- a/milkway/mw-poll-sys-private.h +++ b/milkway/mw-poll-sys-private.h @@ -37,10 +37,6 @@ struct mw_poll_sys_type { mw_poll_type_t base; }; -struct mw_poll_sys { - mw_poll_t base; -}; - mw_private mw_poll_t* mw_poll_sys_new(void); diff --git a/milkway/mw-poll-sys.c b/milkway/mw-poll-sys.c index a97b867..83c168b 100644 --- a/milkway/mw-poll-sys.c +++ b/milkway/mw-poll-sys.c @@ -44,6 +44,10 @@ extern int poll (struct pollfd *fds, unsigned int nfsd, int timeout); # endif /* !sun */ +struct mw_poll_sys { + mw_poll_t base; +}; + static mw_poll_sys_t* mw_poll_sys_init(mw_poll_sys_t *self, mw_poll_sys_type_t *type) diff --git a/milkway/mw-poll.c b/milkway/mw-poll.c index 2197e76..80bb198 100644 --- a/milkway/mw-poll.c +++ b/milkway/mw-poll.c @@ -19,6 +19,7 @@ */ #include "milkwayint.h" #include "milkway/mw-poll.h" +#include "milkway/mw-ptr-array.h" mw_poll_t* mw_poll_init(mw_poll_t *self, @@ -26,21 +27,79 @@ mw_poll_init(mw_poll_t *self, { if (!mw_object_init(&self->base, &type->base)) return NULL; + self->fds = mw_hash_new(mw_pointer_hash, NULL, NULL, NULL); + if (!self->fds) { + MW_SUPER_FINALIZE(self, MW_POLL_TYPE)(&self->base); + return NULL; + } return self; } +static mw_bool_t +mw_poll_default_add_fd(mw_poll_t *super, + mw_pollfd_t *fd, + mw_error_t **reterr) +{ + mw_poll_t *self = (mw_poll_t*)super; + + if (!mw_hash_insert(self->fds, (mw_pointer_t)fd->fd, NULL)) { + mw_error_set(reterr, "poll", MW_OUT_OF_MEM, + "failed to record the fd"); + return MW_FALSE; + } + return MW_TRUE; +} + +static mw_bool_t +mw_poll_default_remove_fd(mw_poll_t *super, + mw_pollfd_t *fd, + mw_error_t **reterr) +{ + mw_poll_t *self = (mw_poll_t*)super; + + if (!mw_hash_remove(self->fds, (mw_pointer_t)fd->fd)) { + mw_error_set(reterr, "poll", MW_OUT_OF_MEM, + "failed to remove the fd"); + return MW_FALSE; + } + return MW_TRUE; +} + +void +mw_poll_clear(mw_poll_t *self) +{ + mw_ptr_array_t *array; + mw_hash_iter_t iter, *it = &iter; + mw_uint_t i; + + if (!mw_hash_get_size(self->fds)) + return; + + array = mw_ptr_array_sized_new(mw_hash_get_size(self->fds)); + for (it = mw_hash_first(self->fds, it); it; + it = mw_hash_next(self->fds, it)) + mw_ptr_array_add(array, it->key); + + for (i = 0; i < array->len; i++) + ; + mw_object_unref(array); +} + static void -mw_poll_finalize(mw_object_t *self) +mw_poll_finalize(mw_object_t *super) { - mw_object_type_t *stype = MW_SUPER_TYPE_BASE(self, MW_POLL_TYPE); + mw_poll_t *self = (mw_poll_t*)super; - stype->finalize(self); + mw_object_unref(self->fds); + MW_SUPER_FINALIZE(self, MW_POLL_TYPE)(super); } static void mw_poll_type_init(mw_poll_type_t *self) { self->base.finalize = mw_poll_finalize; + self->add_fd = mw_poll_default_add_fd; + self->remove_fd = mw_poll_default_remove_fd; } MW_DEFINE_GET_TYPE(mw_poll, mw_poll_type_t, diff --git a/milkway/mw-poll.h b/milkway/mw-poll.h index 101dc1e..92caf41 100644 --- a/milkway/mw-poll.h +++ b/milkway/mw-poll.h @@ -22,6 +22,7 @@ #include <milkway/mw-object.h> #include <milkway/mw-error.h> +#include <milkway/mw-hash.h> typedef struct mw_poll_type mw_poll_type_t; typedef struct mw_poll mw_poll_t; @@ -49,6 +50,8 @@ struct mw_poll_type { struct mw_poll { mw_object_t base; + + mw_hash_t *fds; }; typedef enum @@ -104,4 +107,7 @@ mw_poll_poll(mw_poll_t *self, return type->poll(self, timeout_); } +mw_public void +mw_poll_clear(mw_poll_t *self); + #endif |