You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucy.apache.org by ma...@apache.org on 2009/12/30 22:24:47 UTC

svn commit: r894712 - in /lucene/lucy/trunk: charmonizer/ charmonizer/src/Charmonizer/Probe/ core/Lucy/Test/Util/ core/Lucy/Util/ perl/lib/Lucy/

Author: marvin
Date: Wed Dec 30 21:24:46 2009
New Revision: 894712

URL: http://svn.apache.org/viewvc?rev=894712&view=rev
Log:
Add support for atomic compare-and-swap-pointer.  (LUCY-91)

Added:
    lucene/lucy/trunk/charmonizer/src/Charmonizer/Probe/AtomicOps.c   (with props)
    lucene/lucy/trunk/charmonizer/src/Charmonizer/Probe/AtomicOps.h   (with props)
    lucene/lucy/trunk/core/Lucy/Test/Util/TestAtomic.bp   (with props)
    lucene/lucy/trunk/core/Lucy/Test/Util/TestAtomic.c   (with props)
    lucene/lucy/trunk/core/Lucy/Util/Atomic.bp   (with props)
    lucene/lucy/trunk/core/Lucy/Util/Atomic.c   (with props)
Modified:
    lucene/lucy/trunk/charmonizer/charmonize.c
    lucene/lucy/trunk/charmonizer/src/Charmonizer/Probe/Headers.c
    lucene/lucy/trunk/charmonizer/src/Charmonizer/Probe/Headers.h
    lucene/lucy/trunk/perl/lib/Lucy/Test.pm

Modified: lucene/lucy/trunk/charmonizer/charmonize.c
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/charmonizer/charmonize.c?rev=894712&r1=894711&r2=894712&view=diff
==============================================================================
--- lucene/lucy/trunk/charmonizer/charmonize.c (original)
+++ lucene/lucy/trunk/charmonizer/charmonize.c Wed Dec 30 21:24:46 2009
@@ -5,6 +5,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include "Charmonizer/Probe.h"
+#include "Charmonizer/Probe/AtomicOps.h"
 #include "Charmonizer/Probe/DirManip.h"
 #include "Charmonizer/Probe/Floats.h"
 #include "Charmonizer/Probe/FuncMacro.h"
@@ -37,6 +38,7 @@
     /* Run probe modules. */
     chaz_DirManip_run();
     chaz_Headers_run();
+    chaz_AtomicOps_run();
     chaz_FuncMacro_run();
     chaz_Integers_run();
     chaz_Floats_run();

Added: lucene/lucy/trunk/charmonizer/src/Charmonizer/Probe/AtomicOps.c
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/charmonizer/src/Charmonizer/Probe/AtomicOps.c?rev=894712&view=auto
==============================================================================
--- lucene/lucy/trunk/charmonizer/src/Charmonizer/Probe/AtomicOps.c (added)
+++ lucene/lucy/trunk/charmonizer/src/Charmonizer/Probe/AtomicOps.c Wed Dec 30 21:24:46 2009
@@ -0,0 +1,67 @@
+#define CHAZ_USE_SHORT_NAMES
+
+#include "Charmonizer/Core/HeadCheck.h"
+#include "Charmonizer/Core/ModHandler.h"
+#include "Charmonizer/Core/Util.h"
+#include "Charmonizer/Probe/AtomicOps.h"
+#include <string.h>
+#include <stdio.h>
+
+
+void
+AtomicOps_run(void) 
+{
+    chaz_bool_t has_libkern_osatomic_h = false;
+    chaz_bool_t has_sys_atomic_h       = false;
+    chaz_bool_t has_intrin_h           = false;
+
+    START_RUN("AtomicOps");
+
+    if (HeadCheck_check_header("libkern/OSAtomic.h")) {
+        has_libkern_osatomic_h = true;
+        ModHand_append_conf("#define CHY_HAS_LIBKERN_OSATOMIC_H\n");
+    }
+    if (HeadCheck_check_header("sys/atomic.h")) {
+        has_sys_atomic_h = true;
+        ModHand_append_conf("#define CHY_HAS_SYS_ATOMIC_H\n");
+    }
+    if (   HeadCheck_check_header("windows.h")
+        && HeadCheck_check_header("intrin.h")
+    ) {
+        has_intrin_h = true;
+        ModHand_append_conf("#define CHY_HAS_INTRIN_H\n");
+    }
+    
+    /* Shorten */
+    START_SHORT_NAMES;
+    if (has_libkern_osatomic_h) {
+        ModHand_shorten_macro("HAS_LIBKERN_OSATOMIC_H");
+    }
+    if (has_sys_atomic_h) {
+        ModHand_shorten_macro("HAS_SYS_ATOMIC_H");
+    }
+    if (has_intrin_h) {
+        ModHand_shorten_macro("HAS_INTRIN_H");
+    }
+    END_SHORT_NAMES;
+
+    END_RUN;
+}
+
+
+/**
+ * Copyright 2009 The Apache Software Foundation
+ *
+ * Licensed 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.
+ */
+

Propchange: lucene/lucy/trunk/charmonizer/src/Charmonizer/Probe/AtomicOps.c
------------------------------------------------------------------------------
    svn:eol-style = native

Added: lucene/lucy/trunk/charmonizer/src/Charmonizer/Probe/AtomicOps.h
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/charmonizer/src/Charmonizer/Probe/AtomicOps.h?rev=894712&view=auto
==============================================================================
--- lucene/lucy/trunk/charmonizer/src/Charmonizer/Probe/AtomicOps.h (added)
+++ lucene/lucy/trunk/charmonizer/src/Charmonizer/Probe/AtomicOps.h Wed Dec 30 21:24:46 2009
@@ -0,0 +1,50 @@
+/* Charmonizer/Probe/AtomicOps.h
+ */
+
+#ifndef H_CHAZ_ATOMICOPS
+#define H_CHAZ_ATOMICOPS 
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <stdio.h>
+
+/* Run the AtomicOps module.
+ *
+ * These following symbols will be defined if the associated headers are
+ * available:
+ * 
+ * HAS_LIBKERN_OSATOMIC_H  <libkern/OSAtomic.h> (Mac OS X)
+ * HAS_SYS_ATOMIC_H        <sys/atomic.h>       (Solaris)
+ * HAS_INTRIN_H            <intrin.h>           (Windows)
+ */
+void chaz_AtomicOps_run(void);
+
+#ifdef CHAZ_USE_SHORT_NAMES
+  #define AtomicOps_run    chaz_AtomicOps_run
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* H_CHAZ_ATOMICOPS */
+
+
+/**
+ * Copyright 2006 The Apache Software Foundation
+ *
+ * Licensed 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.
+ */
+

Propchange: lucene/lucy/trunk/charmonizer/src/Charmonizer/Probe/AtomicOps.h
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: lucene/lucy/trunk/charmonizer/src/Charmonizer/Probe/Headers.c
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/charmonizer/src/Charmonizer/Probe/Headers.c?rev=894712&r1=894711&r2=894712&view=diff
==============================================================================
--- lucene/lucy/trunk/charmonizer/src/Charmonizer/Probe/Headers.c (original)
+++ lucene/lucy/trunk/charmonizer/src/Charmonizer/Probe/Headers.c Wed Dec 30 21:24:46 2009
@@ -143,6 +143,11 @@
         }
     }
 
+    /* One-offs. */
+    if (HeadCheck_check_header("pthread.h")) {
+        S_keep("pthread.h");
+    }
+
     /* append the config with every header detected so far */
     for (i = 0; keepers[i] != NULL; i++) {
         S_encode_affirmation(keepers[i]);

Modified: lucene/lucy/trunk/charmonizer/src/Charmonizer/Probe/Headers.h
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/charmonizer/src/Charmonizer/Probe/Headers.h?rev=894712&r1=894711&r2=894712&view=diff
==============================================================================
--- lucene/lucy/trunk/charmonizer/src/Charmonizer/Probe/Headers.h (original)
+++ lucene/lucy/trunk/charmonizer/src/Charmonizer/Probe/Headers.h Wed Dec 30 21:24:46 2009
@@ -64,6 +64,10 @@
  * HAS_TERMIOS_H
  * HAS_UNISTD_H
  * HAS_UTIME_H
+ *
+ * If pthread.h is available, this will be exported:
+ * 
+ * HAS_PTHREAD_H
  */
 void 
 chaz_Headers_run(void);

Added: lucene/lucy/trunk/core/Lucy/Test/Util/TestAtomic.bp
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/core/Lucy/Test/Util/TestAtomic.bp?rev=894712&view=auto
==============================================================================
--- lucene/lucy/trunk/core/Lucy/Test/Util/TestAtomic.bp (added)
+++ lucene/lucy/trunk/core/Lucy/Test/Util/TestAtomic.bp Wed Dec 30 21:24:46 2009
@@ -0,0 +1,22 @@
+parcel Lucy;
+
+inert class Lucy::Test::Util::TestAtomic {
+    inert void
+    run_tests();
+}
+
+/* Copyright 2009 The Apache Software Foundation
+ *
+ * Licensed 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.
+ */
+

Propchange: lucene/lucy/trunk/core/Lucy/Test/Util/TestAtomic.bp
------------------------------------------------------------------------------
    svn:eol-style = native

Added: lucene/lucy/trunk/core/Lucy/Test/Util/TestAtomic.c
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/core/Lucy/Test/Util/TestAtomic.c?rev=894712&view=auto
==============================================================================
--- lucene/lucy/trunk/core/Lucy/Test/Util/TestAtomic.c (added)
+++ lucene/lucy/trunk/core/Lucy/Test/Util/TestAtomic.c Wed Dec 30 21:24:46 2009
@@ -0,0 +1,61 @@
+#include "Lucy/Util/ToolSet.h"
+
+#include "Lucy/Test.h"
+#include "Lucy/Test/Util/TestAtomic.h"
+#include "Lucy/Util/Atomic.h"
+
+static void
+test_cas_ptr(TestBatch *batch)
+{
+    int    foo = 1;
+    int    bar = 2;
+    int   *foo_pointer = &foo;
+    int   *bar_pointer = &bar;
+    int   *target      = NULL;
+
+    ASSERT_TRUE(batch, 
+        Atomic_cas_ptr((void**)&target, NULL, foo_pointer), 
+        "cas_ptr returns true on success");
+    ASSERT_TRUE(batch, target == foo_pointer, "cas_ptr sets target");
+
+    target = NULL;
+    ASSERT_FALSE(batch, 
+        Atomic_cas_ptr((void**)&target, bar_pointer, foo_pointer), 
+        "cas_ptr returns false when it old_value doesn't match");
+    ASSERT_TRUE(batch, target == NULL, 
+        "cas_ptr doesn't do anything to target when old_value doesn't match");
+
+    target = foo_pointer;
+    ASSERT_TRUE(batch, 
+        Atomic_cas_ptr((void**)&target, foo_pointer, bar_pointer), 
+        "cas_ptr from one value to another");
+    ASSERT_TRUE(batch, target == bar_pointer, "cas_ptr sets target");
+}
+
+void
+TestAtomic_run_tests()
+{
+    TestBatch *batch = Test_new_batch("TestAtomic", 6, NULL);
+
+    PLAN(batch);
+
+    test_cas_ptr(batch);
+
+    batch->destroy(batch);
+}
+
+/* Copyright 2009 The Apache Software Foundation
+ *
+ * Licensed 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.
+ */
+

Propchange: lucene/lucy/trunk/core/Lucy/Test/Util/TestAtomic.c
------------------------------------------------------------------------------
    svn:eol-style = native

Added: lucene/lucy/trunk/core/Lucy/Util/Atomic.bp
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/core/Lucy/Util/Atomic.bp?rev=894712&view=auto
==============================================================================
--- lucene/lucy/trunk/core/Lucy/Util/Atomic.bp (added)
+++ lucene/lucy/trunk/core/Lucy/Util/Atomic.bp Wed Dec 30 21:24:46 2009
@@ -0,0 +1,107 @@
+parcel Lucy;
+
+/** Provide atomic memory operations.
+ */
+inert class Lucy::Util::Atomic { }
+
+__C__
+
+/** Compare and swap a pointer.  Test whether the value at <code>target</code>
+ * matches <code>old_value</code>.  If it does, set <code>target</code> to
+ * <code>new_value</code> and return true.  Otherwise, return false.
+ */
+static CHY_INLINE chy_bool_t
+lucy_Atomic_cas_ptr(void *volatile *target, void *old_value, void *new_value);
+
+/************************** Mac OS X 10.4 and later ***********************/
+#ifdef CHY_HAS_LIBKERN_OSATOMIC_H
+#include <libkern/OSAtomic.h>
+
+static CHY_INLINE chy_bool_t
+lucy_Atomic_cas_ptr(void *volatile *target, void *old_value, void *new_value)
+{
+    return OSAtomicCompareAndSwapPtr(old_value, new_value, target);
+}
+
+/********************************** Windows *******************************/
+#elif defined(CHY_HAS_INTRIN_H)
+
+chy_bool_t
+lucy_Atomic_wrapped_cas_ptr(void *volatile *target, void *old_value, 
+                            void *new_value);
+
+static CHY_INLINE chy_bool_t
+lucy_Atomic_cas_ptr(void *volatile *target, void *old_value, void *new_value)
+{
+    return lucy_Atomic_wrapped_cas_ptr(target, old_value, new_value);
+}
+
+/**************************** Solaris 10 and later ************************/
+#elif defined(CHY_HAS_SYS_ATOMIC_H)
+#include <sys/atomic.h>
+
+static CHY_INLINE chy_bool_t
+lucy_Atomic_cas_ptr(void *volatile *target, void *old_value, void *new_value)
+{
+    return atomic_cas_ptr(target, old_value, new_value) == old_value;
+}
+
+/**************************** GCC 4.1.0 and later *************************/
+#elif (defined(__GNUC__) \
+    && (__GNUC__ * 100000 + __GNUC_MINOR__ * 1000 >= 401000))
+
+static CHY_INLINE chy_bool_t
+lucy_Atomic_cas_ptr(void *volatile *target, void *old_value, void *new_value)
+{
+    return __sync_bool_compare_and_swap(target, old_value, new_value);
+}
+
+/************************ Fall back to pthread.h. **************************/
+#elif defined(CHY_HAS_PTHREAD_H)
+#include <pthread.h>
+
+extern pthread_mutex_t lucy_Atomic_mutex;
+
+static CHY_INLINE chy_bool_t
+lucy_Atomic_cas_ptr(void *volatile *target, void *old_value, void *new_value)
+{
+    pthread_mutex_lock(&lucy_Atomic_mutex);
+    if (*target == old_value) {
+        *target = new_value;
+        pthread_mutex_unlock(&lucy_Atomic_mutex);
+        return true;
+    }
+    else {
+        pthread_mutex_unlock(&lucy_Atomic_mutex);
+        return false;
+    }
+}
+
+/******************** No support for atomics at all. ***********************/
+#else
+
+#error No support for atomic operations.
+
+#endif /* Big platform if-else chain. */
+
+#ifdef LUCY_USE_SHORT_NAMES
+  #define Atomic_cas_ptr lucy_Atomic_cas_ptr
+#endif
+
+__END_C__
+
+/* Copyright 2009 The Apache Software Foundation
+ *
+ * Licensed 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.
+ */
+

Propchange: lucene/lucy/trunk/core/Lucy/Util/Atomic.bp
------------------------------------------------------------------------------
    svn:eol-style = native

Added: lucene/lucy/trunk/core/Lucy/Util/Atomic.c
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/core/Lucy/Util/Atomic.c?rev=894712&view=auto
==============================================================================
--- lucene/lucy/trunk/core/Lucy/Util/Atomic.c (added)
+++ lucene/lucy/trunk/core/Lucy/Util/Atomic.c Wed Dec 30 21:24:46 2009
@@ -0,0 +1,39 @@
+#define C_LUCY_ATOMIC
+#define LUCY_USE_SHORT_NAMES
+#include "Lucy/Util/Atomic.h"
+
+/********************************** Windows ********************************/
+#ifdef CHY_HAS_WINDOWS_H
+#include <windows.h>
+
+chy_bool_t
+lucy_Atomic_wrapped_cas_ptr(void *volatile *target, void *old_value, 
+                            void *new_value)
+{
+    return InterlockedCompareExchangePointer(target, new_value, old_value) 
+        == old_value;
+}
+
+/************************** Fall back to ptheads ***************************/
+#elif defined(CHY_HAS_PTHREAD_H)
+
+#include <pthread.h>
+pthread_mutex_t lucy_Atomic_mutex = PTHREAD_MUTEX_INITIALIZER;
+
+#endif
+
+/* Copyright 2009 The Apache Software Foundation
+ *
+ * Licensed 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.
+ */
+

Propchange: lucene/lucy/trunk/core/Lucy/Util/Atomic.c
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: lucene/lucy/trunk/perl/lib/Lucy/Test.pm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/perl/lib/Lucy/Test.pm?rev=894712&r1=894711&r2=894712&view=diff
==============================================================================
--- lucene/lucy/trunk/perl/lib/Lucy/Test.pm (original)
+++ lucene/lucy/trunk/perl/lib/Lucy/Test.pm Wed Dec 30 21:24:46 2009
@@ -84,6 +84,9 @@
         lucy_TestRAMFolder_run_tests();
     }
     /* Lucy::Util */
+    else if (strEQ(package, "TestAtomic")) {
+        lucy_TestAtomic_run_tests();
+    }
     else if (strEQ(package, "TestIndexFileNames")) {
         lucy_TestIxFileNames_run_tests();
     }