You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2011/06/24 09:51:01 UTC

svn commit: r1139190 - in /commons/sandbox/runtime/trunk/src/main/native: Makefile.unx.in os/linux/epoll.c

Author: mturk
Date: Fri Jun 24 07:51:01 2011
New Revision: 1139190

URL: http://svn.apache.org/viewvc?rev=1139190&view=rev
Log:
Add linux epoll skeleton

Added:
    commons/sandbox/runtime/trunk/src/main/native/os/linux/epoll.c   (with props)
Modified:
    commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in

Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in?rev=1139190&r1=1139189&r2=1139190&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in (original)
+++ commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in Fri Jun 24 07:51:01 2011
@@ -84,6 +84,7 @@ DARWIN_SOURCES=\
 HPUX_SOURCES=\
 	$(TOPDIR)/os/hpux/os.c
 LINUX_SOURCES=\
+	$(TOPDIR)/os/linux/epoll.c \
 	$(TOPDIR)/os/linux/misc.c \
 	$(TOPDIR)/os/linux/os.c
 SOLARIS_SOURCES=\

Added: commons/sandbox/runtime/trunk/src/main/native/os/linux/epoll.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/linux/epoll.c?rev=1139190&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/linux/epoll.c (added)
+++ commons/sandbox/runtime/trunk/src/main/native/os/linux/epoll.c Fri Jun 24 07:51:01 2011
@@ -0,0 +1,169 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "acr/clazz.h"
+#include "acr/memory.h"
+#include "acr/jniapi.h"
+#include "acr/port.h"
+#include "acr/time.h"
+#include "acr/iodefs.h"
+#include "acr/netapi.h"
+#include "arch_opts.h"
+#include <poll.h>
+#include <sys/epoll.h>
+
+/* pollset operation states */
+#define PSS_DESTROY     1
+#define PSS_POLL        2
+#define PSS_WAIT        3
+#define PSS_WAKEUP      4
+
+typedef struct acr_pollfd_t {
+    int            fd;
+    jobject        obj;
+    acr_time_t     ttl;
+    acr_time_t     exp;
+} acr_pollfd_t;
+
+typedef struct acr_pollset_t {
+    struct epoll_event *epset;
+    struct epoll_event *eeset;
+    acr_pollfd_t       *ooset;
+    int                 epfd;
+    int                 used;
+    int                 size;
+    volatile int        state;
+    int                 wpipe[2];
+    pthread_mutex_t     mutex;
+    pthread_cond_t      wakeup;
+} acr_pollset_t;
+
+static short ieventt(int event)
+{
+    short rv = 0;
+
+    if (event & ACR_OP_INP)
+        rv |= EPOLLIN;
+    if (event & ACR_OP_OUT)
+        rv |= EPOLLOUT;
+    if (event & ACR_OP_PRI)
+        rv |= EPOLLPRI;
+    /* EPOLLERR, EPOLLHUP, and EPOLLNVAL aren't valid as requested events
+     */
+    return rv;
+}
+
+static short reventt(short event)
+{
+    short rv = 0;
+
+    if (event & EPOLLIN)
+        rv |= ACR_OP_INP;
+    if (event & EPOLLOUT)
+        rv |= ACR_OP_OUT;
+    if (event & EPOLLPRI)
+        rv |= ACR_OP_PRI;
+    if (event & EPOLLERR)
+        rv |= ACR_OP_ERROR;
+    if (event & EPOLLHUP)
+        rv |= ACR_OP_HANGUP;
+#if defined(EPOLLRDHUP)
+    if (event & EPOLLRDHUP)
+        rv |= ACR_OP_RDHUP;
+#endif
+    return rv;
+}
+
+ACR_NET_EXPORT(jlong, LinuxSelector, create0)(JNI_STDARGS, jint size)
+{
+    int rc;
+    acr_pollset_t *ps;
+    struct epoll_event epipe;
+
+    ps = ACR_TALLOC(acr_pollset_t);
+    if (ps == 0)
+        return 0;
+    ps->wpipe[0] = -1;
+    ps->wpipe[1] = -1;
+    ps->epfd     = -1;
+    ps->size     = size + 1;
+    ps->used     = 1;
+    /* Create the result epoll set.
+     */
+    ps->epset    = ACR_MALLOC(struct epoll_event, ps->size);
+    if (ps->epset == 0)
+        return 0;
+    ps->ooset    = ACR_MALLOC(acr_pollfd_t,  ps->size);
+    if (ps->epset == 0) {
+        AcrFree(ps->epset);
+        return 0;
+    }
+    if ((rc = AcrPipePair(ps->wpipe, 0)) != 0) {
+        ACR_THROW_NET_ERROR(rc);
+        goto cleanup;
+    }
+#if defined(EPOLL_CLOEXEC)
+    ps->epfd = epoll_create1(EPOLL_CLOEXEC);
+    if (ps->epfd == -1) {
+        ACR_THROW_NET_ERRNO();
+        goto cleanup;
+    }
+#else
+    ps->epfd = epoll_create(ps->size);
+    if (ps->epfd == -1) {
+        ACR_THROW_NET_ERRNO();
+        goto cleanup;
+    }
+    if ((rc = AcrCloseOnExec(ps->epfd)) != 0) {
+        ACR_THROW_NET_ERROR(rc);
+        goto cleanup;
+    }
+#endif
+    /* Add the wakeup pipe to the pset
+     */
+    ps->ooset[0].fd  = ps->wpipe[0];
+    ps->ooset[0].obj = 0;
+    ps->ooset[0].ttl = ACR_INFINITE;
+    ps->ooset[0].exp = ACR_INFINITE;
+
+    epipe.data.ptr   = &ps->ooset[0];
+    epipe.events     = EPOLLIN;
+    if (epoll_ctl(ps->epfd, EPOLL_CTL_ADD, ps->ooset[0].fd, &epipe) == -1) {
+        /* Failed adding pipe to the pollset
+         */
+        ACR_THROW_NET_ERRNO();
+        goto cleanup;        
+    }
+    if (pthread_cond_init(&ps->wakeup, 0) != 0) {
+        ACR_THROW_NET_ERRNO();
+        goto cleanup;
+    }
+    if (pthread_mutex_init(&ps->mutex, 0) != 0) {
+        ACR_THROW_NET_ERRNO();
+        pthread_cond_destroy(&ps->wakeup);
+        goto cleanup;
+    }
+    return P2J(ps);
+
+cleanup:
+    r_close(ps->epfd);
+    r_close(ps->wpipe[0]);
+    r_close(ps->wpipe[1]);
+    AcrFree(ps->epset);
+    AcrFree(ps->ooset);
+    AcrFree(ps);
+    return 0;
+}

Propchange: commons/sandbox/runtime/trunk/src/main/native/os/linux/epoll.c
------------------------------------------------------------------------------
    svn:eol-style = native