commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject svn commit: r1127065 - /commons/sandbox/runtime/trunk/src/main/native/os/unix/poll.c
Date Tue, 24 May 2011 14:12:26 GMT
Author: mturk
Date: Tue May 24 14:12:25 2011
New Revision: 1127065

URL: http://svn.apache.org/viewvc?rev=1127065&view=rev
Log:
OS localize pollset implementations

Modified:
    commons/sandbox/runtime/trunk/src/main/native/os/unix/poll.c

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/poll.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/poll.c?rev=1127065&r1=1127064&r2=1127065&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/poll.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/poll.c Tue May 24 14:12:25 2011
@@ -49,6 +49,20 @@ typedef struct acr_pollset_t {
     pthread_cond_t    wakeup;
 } acr_pollset_t;
 
+J_DECLARE_CLAZZ = {
+    INVALID_FIELD_OFFSET,
+    0,
+    0,
+    0,
+    ACR_UNX_CP "Pollset"
+};
+
+J_DECLARE_M_ID(0000) = {
+    0,
+    "<init>",
+    "()V"
+};
+
 static short ieventt(int event)
 {
     short rv = 0;
@@ -87,13 +101,6 @@ static short reventt(short event)
     return rv;
 }
 
-ACR_NET_EXPORT(jboolean, Pollset, init0)(JNI_STDARGS)
-{
-    /* Always supported
-     */
-    return JNI_TRUE;
-}
-
 ACR_NET_EXPORT(jint, Pollset, nmax0)(JNI_STDARGS)
 {
     int nm = 0;
@@ -110,7 +117,18 @@ ACR_NET_EXPORT(jint, Pollset, nmax0)(JNI
     return nm;
 }
 
-ACR_NET_EXPORT(jlong, Pollset, create0)(JNI_STDARGS, jint size)
+ACR_NET_EXPORT(jobject, Pollset, new0)(JNI_STDARGS)
+{
+    if (_clazzn.u == 1)
+        return (*env)->NewObject(env, _clazzn.i, J4MID(0000));
+    if (AcrLoadClass(env, &_clazzn, 0) == JNI_FALSE)
+        return 0;
+    R_LOAD_METHOD(0000, 0);
+    _clazzn.u = 1;
+    return (*env)->NewObject(env, _clazzn.i, J4MID(0000));
+}
+
+ACR_UNX_EXPORT(jlong, PollsetImpl, create0)(JNI_STDARGS, jint size)
 {
     int rc;
     acr_pollset_t *ps;
@@ -162,7 +180,7 @@ cleanup:
     return 0;
 }
 
-ACR_NET_EXPORT(void, Pollset, destroy0)(JNI_STDARGS, jlong pollset)
+ACR_UNX_EXPORT(void, PollsetImpl, destroy0)(JNI_STDARGS, jlong pollset)
 {
     int i;
     acr_pollset_t *ps = J2P(pollset, acr_pollset_t *);
@@ -201,8 +219,8 @@ ACR_NET_EXPORT(void, Pollset, destroy0)(
     AcrFree(ps);
 }
 
-ACR_NET_EXPORT(jint, Pollset, clear0)(JNI_STDARGS, jlong pollset,
-                                      jobjectArray rs)
+ACR_UNX_EXPORT(jint, PollsetImpl, clear0)(JNI_STDARGS, jlong pollset,
+                                          jobjectArray rs)
 {
     int i;
     int cnt = 0;
@@ -239,7 +257,7 @@ ACR_NET_EXPORT(jint, Pollset, clear0)(JN
     return cnt;
 }
 
-ACR_NET_EXPORT(void, Pollset, wakeup0)(JNI_STDARGS, jlong pollset)
+ACR_UNX_EXPORT(void, PollsetImpl, wakeup0)(JNI_STDARGS, jlong pollset)
 {
     acr_pollset_t *ps = J2P(pollset, acr_pollset_t *);
 
@@ -252,9 +270,9 @@ ACR_NET_EXPORT(void, Pollset, wakeup0)(J
     pthread_mutex_unlock(&ps->mutex);
 }
 
-ACR_NET_EXPORT(jint, Pollset, wait0)(JNI_STDARGS, jlong pollset, jobjectArray rs,
-                                     jshortArray revents, jint timeout,
-                                     jboolean rmsignaled)
+ACR_UNX_EXPORT(jint, PollsetImpl, wait0)(JNI_STDARGS, jlong pollset, jobjectArray rs,
+                                         jshortArray revents, jint timeout,
+                                         jboolean rmsignaled)
 {
     int i, ns, rc = 0;
     int rv = 0;
@@ -304,7 +322,7 @@ ACR_NET_EXPORT(jint, Pollset, wait0)(JNI
         rc = ACR_GET_OS_ERROR();
     pthread_mutex_lock(&ps->mutex);
     if (ps->state == PSS_DESTROY) {
-        /* Interrupted by free0 or wakeup0 */
+        /* Interrupted by free0 */
         pthread_cond_broadcast(&ps->wakeup);
         pthread_mutex_unlock(&ps->mutex);
         return 0;
@@ -324,6 +342,19 @@ ACR_NET_EXPORT(jint, Pollset, wait0)(JNI
         pthread_mutex_unlock(&ps->mutex);
         return 0;
     }
+    if (ps->state == PSS_WAKEUP) {
+        /* Interrupted by wakeup0 */
+        if (ps->fdset[0].revents != 0) {
+            /* Drain the wakeup pipe.
+             * Wakeup pipe is always at index zero.
+             */
+            AcrDrainPipe(ps->wpipe[0]);
+        }
+        ps->state = 0;
+        pthread_cond_broadcast(&ps->wakeup);
+        pthread_mutex_unlock(&ps->mutex);
+        return 0;
+    }
     ps->state = PSS_WAIT;
     pevents   = JARRAY_CRITICAL(jshort, revents);
     /* Cycle trough the descriptors */
@@ -350,13 +381,17 @@ ACR_NET_EXPORT(jint, Pollset, wait0)(JNI
             }
         }
         else {
-            if (now == 0)
-                now = AcrTimeNow();                
-            if (ps->ooset[i].ttl > 0 && ps->ooset[i].exp > now) {
-                /* Expired descriptor */
-                ps->fdset[i].revents = POLLHUP;
-                pevents[rv] = ACR_POLLTTL;
-                (*env)->SetObjectArrayElement(env, rs, rv++, ps->ooset[i].obj);
+            /* Check for the expired descriptors.
+             */
+            if (ps->ooset[i].ttl > 0) {
+                if (now == 0)
+                    now = AcrTimeNow();                
+                if (ps->ooset[i].exp > now) {
+                    /* Expired descriptor */
+                    ps->fdset[i].revents = POLLHUP;
+                    pevents[rv] = ACR_POLLTTL;
+                    (*env)->SetObjectArrayElement(env, rs, rv++, ps->ooset[i].obj);
+                }
             }
         }
     }
@@ -393,10 +428,10 @@ ACR_NET_EXPORT(jint, Pollset, wait0)(JNI
     return rv;
 }
 
-ACR_NET_EXPORT(jint, Pollset, add0)(JNI_STDARGS, jlong pollset, jobject fo,
-                                    jint f, jint events, jint ttlms)
+ACR_UNX_EXPORT(jint, PollsetImpl, add0)(JNI_STDARGS, jlong pollset, jobject fo,
+                                        jint f, jint events, jint ttlms)
 {
-    int rc = 0;
+    int i, rc = 0;
     acr_pollset_t *ps = J2P(pollset, acr_pollset_t *);
 
     pthread_mutex_lock(&ps->mutex);
@@ -419,6 +454,14 @@ ACR_NET_EXPORT(jint, Pollset, add0)(JNI_
         rc = ACR_EOVERFLOW;
         goto cleanup;
     }
+    for (i = 1; i < ps->used; i++) {
+        if (ps->fdset[i].fd == f) {
+            /* Duplicate descriptor
+             */
+            rc = ACR_EALREADY;
+            goto cleanup;
+        }
+    }
     ps->fdset[ps->used].fd      = f;
     ps->fdset[ps->used].events  = ieventt(events);
     ps->fdset[ps->used].revents = 0;
@@ -444,6 +487,48 @@ cleanup:
     return rc;
 }
 
+ACR_UNX_EXPORT(jint, PollsetImpl, del0)(JNI_STDARGS, jlong pollset, jobject fo,
+                                        jint f)
+{
+    int i, rc = ACR_EOF;
+    acr_pollset_t *ps = J2P(pollset, acr_pollset_t *);
+
+    pthread_mutex_lock(&ps->mutex);
+    while (ps->state != 0) {
+        if (ps->state == PSS_DESTROY) {
+            rc = 0;
+            goto cleanup;
+        }
+        if (ps->state == PSS_POLL) {
+            char ch   = 1;
+            ps->state = PSS_WAKEUP;
+            r_write(ps->wpipe[1], &ch, 1);
+        }
+        if ((rc = pthread_cond_wait(&ps->wakeup, &ps->mutex)) != 0)
+            goto cleanup;
+    }
+
+    for (i = 1; i < ps->used; i++) {
+        if (ps->ooset[i].obj == fo) {
+            int dest = i;
+            int used = ps->used;
+            ps->used--;
+            /* Unref descriptor */
+            (*env)->DeleteGlobalRef(env, ps->ooset[i].obj);
+            for (++i; i < used; i++) {
+                ps->fdset[dest] = ps->fdset[i];
+                ps->ooset[dest] = ps->ooset[i];
+                dest++;
+            }
+            rc = 0;
+        }
+    }
+
+cleanup:
+    pthread_mutex_unlock(&ps->mutex);
+    return rc;
+}
+
 ACR_NET_EXPORT(jint, Poll, wait0)(JNI_STDARGS, jintArray fdset,
                                   jshortArray events, jshortArray revents,
                                   jint nevents, jint timeout)
@@ -534,8 +619,7 @@ ACR_NET_EXPORT(jshort, Poll, wait1)(JNI_
             break;
     }
     if (ns == -1)
-        return -1;
-    else
-        return reventt(pfd.revents);
+        ACR_THROW_NET_ERRNO();
+    return reventt(pfd.revents);
 }
                               
\ No newline at end of file



Mime
View raw message