apr-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j..@apache.org
Subject svn commit: r1790155 - in /apr/apr/branches/1.6.x/locks/unix: misc.c proc_mutex.c
Date Tue, 04 Apr 2017 19:07:28 GMT
Author: jim
Date: Tue Apr  4 19:07:27 2017
New Revision: 1790155

URL: http://svn.apache.org/viewvc?rev=1790155&view=rev
Log:
and the rest of the timedacquires

Modified:
    apr/apr/branches/1.6.x/locks/unix/misc.c
    apr/apr/branches/1.6.x/locks/unix/proc_mutex.c

Modified: apr/apr/branches/1.6.x/locks/unix/misc.c
URL: http://svn.apache.org/viewvc/apr/apr/branches/1.6.x/locks/unix/misc.c?rev=1790155&r1=1790154&r2=1790155&view=diff
==============================================================================
--- apr/apr/branches/1.6.x/locks/unix/misc.c (original)
+++ apr/apr/branches/1.6.x/locks/unix/misc.c Tue Apr  4 19:07:27 2017
@@ -18,19 +18,25 @@
 #include "apr_arch_thread_mutex.h"
 #define APR_WANT_MEMFUNC
 #include "apr_want.h"
-#include <stdio.h>
+
 #if APR_HAS_THREADS
+#if APR_HAS_SYSVSEM_SERIALIZE
+#if !HAVE_SEMTIMEDOP
+#include <sys/sem.h>
+#endif
+#endif
+#define SLEEP_TIME_NS      10000000
+#define NANOSECS_PER_SEC 1000000000
+extern int errno;
 
 #ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK
 extern int pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abs_timeout);
-#define SLEEP_TIME_NS      10000000
-#define NANOSECS_PER_SEC 1000000000
 /*
-* A pthread_mutex_timedlock() impl for OSX/macOS, which lacks the
-* real thing.
-* NOTE: Unlike the real McCoy, won't return EOWNERDEAD, EDEADLK
-*       or EOWNERDEAD
-*/
+ * A pthread_mutex_timedlock() impl for OSX/macOS, which lacks the
+ * real thing.
+ * NOTE: Unlike the real McCoy, won't return EOWNERDEAD, EDEADLK
+ *       or EOWNERDEAD
+ */
 int pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abs_timeout)
 {
    int rv;
@@ -52,7 +58,7 @@ int pthread_mutex_timedlock(pthread_mute
    }
    /* If we had a REALLY small timeout ;) */
    if (remaining.tv_sec < 0) {
-       return pthread_mutex_trylock(mutex);
+       return ETIMEDOUT;
    }
    while ((rv = pthread_mutex_trylock(mutex)) == EBUSY) {
        ts.tv_sec = 0;
@@ -73,6 +79,118 @@ int pthread_mutex_timedlock(pthread_mute
 
    return rv;
 }
-
 #endif /* HAVE_PTHREAD_MUTEX_TIMEDLOCK */
+
+#if APR_HAS_POSIXSEM_SERIALIZE
+#if !HAVE_SEM_TIMEDWAIT
+extern int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout);
+/*
+ * A sem_timedwait() impl for OSX/macOS, which lacks the
+ * real thing.
+ */
+int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout)
+{
+   int rv;
+   struct timespec remaining, ts, tod;
+   apr_time_t now;
+
+   remaining = *abs_timeout;
+   now = apr_time_now();
+   tod.tv_sec = apr_time_sec(now);
+   tod.tv_nsec = apr_time_usec(now) * 1000; /* nanoseconds */
+
+   remaining.tv_sec -= tod.tv_sec;
+   if (tod.tv_nsec <= remaining.tv_nsec) {
+       remaining.tv_nsec -= tod.tv_nsec;
+   }
+   else {
+       remaining.tv_sec--;
+       remaining.tv_nsec = (NANOSECS_PER_SEC - (tod.tv_nsec - remaining.tv_nsec));
+   }
+   /* If we had a REALLY small timeout ;) */
+   if (remaining.tv_sec < 0) {
+       return ETIMEDOUT;
+   }
+   errno = 0;
+   while ( ((rv = sem_trywait(sem)) != 0) && (errno == EAGAIN)) {
+       ts.tv_sec = 0;
+       ts.tv_nsec = (remaining.tv_sec > 0 ? SLEEP_TIME_NS : 
+                    (remaining.tv_nsec < SLEEP_TIME_NS ? remaining.tv_nsec : SLEEP_TIME_NS));
+       nanosleep(&ts, &ts);
+       if (ts.tv_nsec <= remaining.tv_nsec) {
+           remaining.tv_nsec -= ts.tv_nsec;
+       }
+       else {
+           remaining.tv_sec--;
+           remaining.tv_nsec = (NANOSECS_PER_SEC - (ts.tv_nsec - remaining.tv_nsec));
+       }
+       if (remaining.tv_sec < 0 || (!remaining.tv_sec && remaining.tv_nsec <=
SLEEP_TIME_NS)) {
+           return ETIMEDOUT;
+       }
+   }
+   return rv;
+}
+#endif /* HAVE_SEM_TIMEDWAIT */
+#endif /* APR_HAS_POSIXSEM_SERIALIZE */
+
+#if APR_HAS_SYSVSEM_SERIALIZE
+#if !HAVE_SEMTIMEDOP
+extern int semtimedop(int semid, struct sembuf *sops, unsigned nsops,
+                      const struct timespec *abs_timeout);
+/*
+ * A semtimedop() impl for OSX/macOS, which lacks the
+ * real thing.
+ */
+int semtimedop(int semid, struct sembuf *sops, unsigned nsops,
+               const struct timespec *abs_timeout)
+{
+   int rv;
+   struct timespec remaining, ts, tod;
+   apr_time_t now;
+   struct sembuf proc_mutex_op_try;
+
+    proc_mutex_op_try.sem_num = 0;
+    proc_mutex_op_try.sem_op = -1;
+    proc_mutex_op_try.sem_flg = SEM_UNDO | IPC_NOWAIT;
+
+   remaining = *abs_timeout;
+   now = apr_time_now();
+   tod.tv_sec = apr_time_sec(now);
+   tod.tv_nsec = apr_time_usec(now) * 1000; /* nanoseconds */
+
+   remaining.tv_sec -= tod.tv_sec;
+   if (tod.tv_nsec <= remaining.tv_nsec) {
+       remaining.tv_nsec -= tod.tv_nsec;
+   }
+   else {
+       remaining.tv_sec--;
+       remaining.tv_nsec = (NANOSECS_PER_SEC - (tod.tv_nsec - remaining.tv_nsec));
+   }
+   /* If we had a REALLY small timeout ;) */
+   if (remaining.tv_sec < 0) {
+       return ETIMEDOUT;
+   }
+   errno = 0;
+   while ( ((rv = semop(semid, &proc_mutex_op_try, nsops)) != 0) && (errno ==
EAGAIN)) {
+       ts.tv_sec = 0;
+       ts.tv_nsec = (remaining.tv_sec > 0 ? SLEEP_TIME_NS : 
+                    (remaining.tv_nsec < SLEEP_TIME_NS ? remaining.tv_nsec : SLEEP_TIME_NS));
+       nanosleep(&ts, &ts);
+       if (ts.tv_nsec <= remaining.tv_nsec) {
+           remaining.tv_nsec -= ts.tv_nsec;
+       }
+       else {
+           remaining.tv_sec--;
+           remaining.tv_nsec = (NANOSECS_PER_SEC - (ts.tv_nsec - remaining.tv_nsec));
+       }
+       if (remaining.tv_sec < 0 || (!remaining.tv_sec && remaining.tv_nsec <=
SLEEP_TIME_NS)) {
+           return ETIMEDOUT;
+       }
+   }
+   return rv;
+}
+#endif /* HAVE_SEMTIMEDOP */
+#endif /* APR_HAS_SYSVSEM_SERIALIZE */
+
+
 #endif /* APR_HAS_THREADS */

Modified: apr/apr/branches/1.6.x/locks/unix/proc_mutex.c
URL: http://svn.apache.org/viewvc/apr/apr/branches/1.6.x/locks/unix/proc_mutex.c?rev=1790155&r1=1790154&r2=1790155&view=diff
==============================================================================
--- apr/apr/branches/1.6.x/locks/unix/proc_mutex.c (original)
+++ apr/apr/branches/1.6.x/locks/unix/proc_mutex.c Tue Apr  4 19:07:27 2017
@@ -187,7 +187,9 @@ static apr_status_t proc_mutex_posix_tim
                                                   apr_time_t timeout,
                                                   int absolute)
 {
-#if HAVE_SEM_TIMEDWAIT
+#if !HAVE_SEM_TIMEDWAIT
+extern int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout);
+#endif
     if (timeout < 0) {
         return proc_mutex_posix_acquire(mutex);
     }
@@ -213,9 +215,6 @@ static apr_status_t proc_mutex_posix_tim
     }
     mutex->curr_locked = 1;
     return APR_SUCCESS;
-#else
-    return APR_ENOTIMPL;
-#endif
 }
 
 static apr_status_t proc_mutex_posix_release(apr_proc_mutex_t *mutex)
@@ -342,7 +341,10 @@ static apr_status_t proc_mutex_sysv_time
                                                  apr_time_t timeout,
                                                  int absolute)
 {
-#if HAVE_SEMTIMEDOP
+#if !HAVE_SEMTIMEDOP
+extern int semtimedop(int semid, struct sembuf *sops, unsigned nsops,
+                      const struct timespec *abs_timeout);
+#endif
     if (timeout < 0) {
         return proc_mutex_sysv_acquire(mutex);
     }
@@ -367,9 +369,6 @@ static apr_status_t proc_mutex_sysv_time
     }
     mutex->curr_locked = 1;
     return APR_SUCCESS;
-#else
-    return APR_ENOTIMPL;
-#endif
 }
 
 static apr_status_t proc_mutex_sysv_release(apr_proc_mutex_t *mutex)
@@ -1221,11 +1220,9 @@ static apr_status_t proc_mutex_choose_me
 #if APR_HAS_PROC_PTHREAD_SERIALIZE \
             && defined(HAVE_PTHREAD_MUTEX_ROBUST)
         new_mutex->meth = &mutex_proc_pthread_methods;
-#elif APR_HAS_SYSVSEM_SERIALIZE \
-            && defined(HAVE_SEMTIMEDOP)
+#elif APR_HAS_SYSVSEM_SERIALIZE
         new_mutex->meth = &mutex_sysv_methods;
-#elif APR_HAS_POSIXSEM_SERIALIZE \
-            && defined(HAVE_SEM_TIMEDWAIT)
+#elif APR_HAS_POSIXSEM_SERIALIZE
         new_mutex->meth = &mutex_posixsem_methods;
 #else
         return APR_ENOTIMPL;



Mime
View raw message