Changeset: 59c6180d2d0b for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=59c6180d2d0b
Modified Files:
        clients/Tests/exports.stable.out
        gdk/gdk_system.c
        gdk/gdk_system.h
Branch: default
Log Message:

Avoid using __sync_lock_test_and_set/_InterlockedExchangePointer.
The GCC documentation warns that __sync_lock_test_and_set is only
minimally supported on many platforms and often only supports writing
the value 1 (a value that is useless in our use case).


diffs (131 lines):

diff --git a/clients/Tests/exports.stable.out b/clients/Tests/exports.stable.out
--- a/clients/Tests/exports.stable.out
+++ b/clients/Tests/exports.stable.out
@@ -268,6 +268,7 @@ BAT *GDKkey;
 ATOMIC_TYPE volatile GDKlockcnt;
 ATOMIC_TYPE volatile GDKlockcontentioncnt;
 MT_Lock *volatile GDKlocklist;
+int volatile GDKlocklistlock;
 ATOMIC_TYPE volatile GDKlocksleepcnt;
 void GDKlockstatistics(int);
 void *GDKmalloc(size_t size);
diff --git a/gdk/gdk_system.c b/gdk/gdk_system.c
--- a/gdk/gdk_system.c
+++ b/gdk/gdk_system.c
@@ -71,10 +71,10 @@ MT_Lock MT_system_lock MT_LOCK_INITIALIZ
 ATOMIC_TYPE volatile GDKlockcnt;
 ATOMIC_TYPE volatile GDKlockcontentioncnt;
 ATOMIC_TYPE volatile GDKlocksleepcnt;
-MT_Lock * volatile GDKlocklist;
+MT_Lock * volatile GDKlocklist = 0;
+int volatile GDKlocklistlock;
 
-/* merge sort of linked list
- * these two functions are nearly identical */
+/* merge sort of linked list */
 static MT_Lock *
 sortlocklist(MT_Lock *l)
 {
@@ -137,6 +137,10 @@ GDKlockstatistics(int what)
 {
        MT_Lock *l;
 
+       if (ATOMIC_CAS_int(GDKlocklistlock, 0, 1, dummy, "") != 0) {
+               fprintf(stderr, "#WARNING: GDKlocklistlock is set, so cannot 
access lock list\n");
+               return;
+       }
        GDKlocklist = sortlocklist(GDKlocklist);
        for (l = GDKlocklist; l; l = l->next)
                if (what == 0 ||
@@ -150,6 +154,7 @@ GDKlockstatistics(int what)
        fprintf(stderr, "#total lock count " SZFMT "\n", (size_t) GDKlockcnt);
        fprintf(stderr, "#lock contention  " SZFMT "\n", (size_t) 
GDKlockcontentioncnt);
        fprintf(stderr, "#lock sleep count " SZFMT "\n", (size_t) 
GDKlocksleepcnt);
+       GDKlocklistlock = 0;
 }
 #endif
 
diff --git a/gdk/gdk_system.h b/gdk/gdk_system.h
--- a/gdk/gdk_system.h
+++ b/gdk/gdk_system.h
@@ -214,21 +214,10 @@ typedef struct MT_Lock {
 
 gdk_export void GDKlockstatistics(int);
 gdk_export MT_Lock * volatile GDKlocklist;
+gdk_export int volatile GDKlocklistlock;
 gdk_export ATOMIC_TYPE volatile GDKlockcnt;
 gdk_export ATOMIC_TYPE volatile GDKlockcontentioncnt;
 gdk_export ATOMIC_TYPE volatile GDKlocksleepcnt;
-#ifdef _MSC_VER
-#if SIZEOF_SIZE_T == 8
-#define ATOMIC_XCG_ptr(var, val)       _InterlockedExchangePointer(&(var), 
(val))
-#define ATOMIC_CAS_ptr(var, old, new)  
_InterlockedCompareExchangePointer(&(var), (new), (old))
-#else
-#define ATOMIC_XCG_ptr(var, val)       ((void *) 
_InterlockedExchange((volatile long *) &(var), (long) (val)))
-#define ATOMIC_CAS_ptr(var, old, new)  ((void *) 
_InterlockedCompareExchange((volatile long *) &(var), (long) (new), (long) 
(old)))
-#endif
-#else
-#define ATOMIC_XCG_ptr(var, val)       __sync_lock_test_and_set(&(var), (val))
-#define ATOMIC_CAS_ptr(var, old, new)  __sync_val_compare_and_swap(&(var), 
(old), (new))
-#endif
 #define _DBG_LOCK_COUNT_0(l, n)                ATOMIC_INC(GDKlockcnt, dummy, n)
 #define _DBG_LOCK_CONTENTION(l, n)                                     \
        do {                                                            \
@@ -248,32 +237,40 @@ gdk_export ATOMIC_TYPE volatile GDKlocks
 #define _DBG_LOCK_COUNT_2(l)                                           \
        do {                                                            \
                (l)->count++;                                           \
-               if ((l)->next == (struct MT_Lock *) -1)                 \
-                       (l)->next = ATOMIC_XCG_ptr(GDKlocklist, (l));   \
+               if ((l)->next == (struct MT_Lock *) -1) {               \
+                       while (ATOMIC_CAS_int(GDKlocklistlock, 0, 1, dummy, "") 
!= 0) \
+                               ;                                       \
+                       (l)->next = GDKlocklist;                        \
+                       GDKlocklist = (l);                              \
+                       GDKlocklistlock = 0;                            \
+               }                                                       \
        } while (0)
-#define _DBG_LOCK_INIT(l, n)                                   \
-       do {                                                    \
-               (l)->count = (l)->contention = (l)->sleep = 0;  \
-               (l)->name = n;                                  \
-               (l)->next = ATOMIC_XCG_ptr(GDKlocklist, (l));   \
+#define _DBG_LOCK_INIT(l, n)                                           \
+       do {                                                            \
+               (l)->count = (l)->contention = (l)->sleep = 0;          \
+               (l)->name = n;                                          \
+               while (ATOMIC_CAS_int(GDKlocklistlock, 0, 1, dummy, "") != 0) \
+                       ;                                               \
+               (l)->next = GDKlocklist;                                \
+               GDKlocklist = (l);                                      \
+               GDKlocklistlock = 0;                                    \
        } while (0)
 #define _DBG_LOCK_DESTROY(l)                                           \
        do {                                                            \
                MT_Lock * volatile _p;                                  \
-               int _done = 0;                                          \
                /* save a copy for statistical purposes */              \
                _p = GDKmalloc(sizeof(MT_Lock));                        \
                memcpy(_p, l, sizeof(MT_Lock));                         \
-               _p->next = ATOMIC_XCG_ptr(GDKlocklist, _p);             \
-               do {                                                    \
-                       if (ATOMIC_CAS_ptr(GDKlocklist, (l), (l)->next) == (l)) 
\
+               while (ATOMIC_CAS_int(GDKlocklistlock, 0, 1, dummy, "") != 0) \
+                       ;                                               \
+               _p->next = GDKlocklist;                                 \
+               GDKlocklist = _p;                                       \
+               for (_p = GDKlocklist; _p; _p = _p->next)               \
+                       if (_p->next == (l)) {                          \
+                               _p->next = (l)->next;                   \
                                break;                                  \
-                       for (_p = GDKlocklist; _p; _p = _p->next)       \
-                               if (ATOMIC_CAS_ptr(_p->next, (l), (l)->next) == 
(l)) { \
-                                       _done = 1;                      \
-                                       break;                          \
-                               }                                       \
-               } while (!_done);                                       \
+                       }                                               \
+               GDKlocklistlock = 0;                                    \
        } while (0)
 
 #else
_______________________________________________
checkin-list mailing list
[email protected]
http://mail.monetdb.org/mailman/listinfo/checkin-list

Reply via email to