summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuo Jinghua <sunmoon1997@gmail.com>2009-10-31 00:32:48 +0800
committerLuo Jinghua <sunmoon1997@gmail.com>2009-10-31 00:32:48 +0800
commit196ff05966841a6ffde5e9142fdc87af9e59b57a (patch)
treee1560fbc3af7f00723112ca15696d3b2510750d0
parent78996aaca666e68d8024b34fb242ae577e56212b (diff)
milkway: add a poll abstract layer
-rw-r--r--milkway/Makefile.am10
-rw-r--r--milkway/mw-poll.c47
-rw-r--r--milkway/mw-poll.h107
3 files changed, 162 insertions, 2 deletions
diff --git a/milkway/Makefile.am b/milkway/Makefile.am
index e53b986..622a4e5 100644
--- a/milkway/Makefile.am
+++ b/milkway/Makefile.am
@@ -15,14 +15,17 @@ milkway_headers = \
mw-compiler.h \
mw-pointer.h \
mw-time.h \
+ mw-strutil.h \
mw-crypt.h \
mw-hexlify.h \
mw-thread.h \
mw-object.h \
+ mw-error.h \
mw-connection-mgr.h \
mw-list.h \
mw-slist.h \
- mw-source.h
+ mw-source.h \
+ mw-poll.h
libmilkway_la_LDFLAGS = -version-info $(LT_VERSION_INFO) -no-undefined
libmilkway_la_LIBADD = @DEP_LIBS@ $(PTHREAD_LIBS)
@@ -45,14 +48,17 @@ libmilkway_la_SOURCES = \
mw-crypt.c \
mw-checksum.c \
mw-hexlify.c \
+ mw-strutil.c \
mw-thread.c \
mw-thread-private.h \
mw-thread-private.c \
mw-object.c \
+ mw-error.c \
mw-connection-mgr.c \
mw-list.c \
mw-slist.c \
- mw-source.c
+ mw-source.c \
+ mw-poll.c
milkwayincludedir = $(includedir)/milkway/milkway
milkwayinclude_HEADERS = $(milkway_headers)
diff --git a/milkway/mw-poll.c b/milkway/mw-poll.c
new file mode 100644
index 0000000..2197e76
--- /dev/null
+++ b/milkway/mw-poll.c
@@ -0,0 +1,47 @@
+/* Milkway
+ *
+ * Copyright (C) 2008- Luo Jinghua <sunmoon1997@gmail.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+#include "milkwayint.h"
+#include "milkway/mw-poll.h"
+
+mw_poll_t*
+mw_poll_init(mw_poll_t *self,
+ mw_poll_type_t *type)
+{
+ if (!mw_object_init(&self->base, &type->base))
+ return NULL;
+ return self;
+}
+
+static void
+mw_poll_finalize(mw_object_t *self)
+{
+ mw_object_type_t *stype = MW_SUPER_TYPE_BASE(self, MW_POLL_TYPE);
+
+ stype->finalize(self);
+}
+
+static void
+mw_poll_type_init(mw_poll_type_t *self)
+{
+ self->base.finalize = mw_poll_finalize;
+}
+
+MW_DEFINE_GET_TYPE(mw_poll, mw_poll_type_t,
+ MW_OBJECT_TYPE, "MWPoll", 0);
diff --git a/milkway/mw-poll.h b/milkway/mw-poll.h
new file mode 100644
index 0000000..101dc1e
--- /dev/null
+++ b/milkway/mw-poll.h
@@ -0,0 +1,107 @@
+/* Milkway
+ *
+ * Copyright (C) 2008- Luo Jinghua <sunmoon1997@gmail.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+#ifndef MW_POLL_H
+#define MW_POLL_H
+
+#include <milkway/mw-object.h>
+#include <milkway/mw-error.h>
+
+typedef struct mw_poll_type mw_poll_type_t;
+typedef struct mw_poll mw_poll_t;
+typedef struct mw_pollfd mw_pollfd_t;
+
+#define MW_POLL_TYPE mw_poll_get_type()
+
+struct mw_poll_type {
+ mw_object_type_t base;
+
+ mw_bool_t
+ (*add_fd) (mw_poll_t *self,
+ mw_pollfd_t *fd,
+ mw_error_t **reterr);
+
+ mw_bool_t
+ (*remove_fd) (mw_poll_t *self,
+ mw_pollfd_t *fd,
+ mw_error_t **reterr);
+
+ mw_bool_t
+ (*poll) (mw_poll_t *self,
+ int timeout_);
+};
+
+struct mw_poll {
+ mw_object_t base;
+};
+
+typedef enum
+{
+ MW_IO_IN = 1 << 0,
+ MW_IO_OUT = 1 << 1,
+ MW_IO_PRI = 1 << 2,
+ MW_IO_ERR = 1 << 3,
+ MW_IO_HUP = 1 << 4,
+ MW_IO_NVAL = 1 << 5
+} mw_poll_event_t;
+
+struct mw_pollfd {
+ mw_uintptr_t fd;
+
+ unsigned short events;
+ unsigned short revents;
+};
+
+mw_public mw_poll_type_t*
+mw_poll_get_type(void);
+
+mw_public mw_poll_t*
+mw_poll_init(mw_poll_t *self,
+ mw_poll_type_t *type);
+
+static mw_inline mw_bool_t
+mw_poll_add_fd(mw_poll_t *self,
+ mw_pollfd_t *fd,
+ mw_error_t **reterr)
+{
+ mw_poll_type_t *type = (mw_poll_type_t *)MW_TYPE(self);
+
+ return type->add_fd(self, fd, reterr);
+}
+
+static mw_inline mw_bool_t
+mw_poll_remove_fd(mw_poll_t *self,
+ mw_pollfd_t *fd,
+ mw_error_t **reterr)
+{
+ mw_poll_type_t *type = (mw_poll_type_t *)MW_TYPE(self);
+
+ return type->remove_fd(self, fd, reterr);
+}
+
+static mw_inline mw_bool_t
+mw_poll_poll(mw_poll_t *self,
+ int timeout_)
+{
+ mw_poll_type_t *type = (mw_poll_type_t *)MW_TYPE(self);
+
+ return type->poll(self, timeout_);
+}
+
+#endif