Changeset: 67c6695a41df for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=67c6695a41df
Modified Files:
        gdk/gdk_utils.c
Branch: Jul2012
Log Message:

Fix deadlock condition: don't use same lock around counters and thread join.
The deadlock occurred when vmtrim wanted to update counters while
GDKexit was holding the lock when it was joining threads.


diffs (189 lines):

diff --git a/gdk/gdk_utils.c b/gdk/gdk_utils.c
--- a/gdk/gdk_utils.c
+++ b/gdk/gdk_utils.c
@@ -316,16 +316,10 @@ int GDK_vm_trim = 1;
 static volatile size_t GDK_mallocedbytes_estimate = 0;
 static volatile size_t GDK_vm_cursize = 0;
 
-#ifndef NDEBUG
 static MT_Lock mbyteslock;
-#define malloc_lock() gdk_set_lock(mbyteslock, "malloc")
-#define        malloc_unlock() gdk_unset_lock(mbyteslock, "malloc");
-#define malloc_lock_init() MT_lock_init(&mbyteslock, "mbyteslock")
-#else
-#define malloc_lock()
-#define        malloc_unlock()
-#define malloc_lock_init()
-#endif
+#define malloc_lock(func)      gdk_set_lock(mbyteslock, func)
+#define        malloc_unlock(func)     gdk_unset_lock(mbyteslock, func);
+#define malloc_lock_init()     MT_lock_init(&mbyteslock, "mbyteslock")
 
 size_t _MT_pagesize = 0;       /* variable holding memory size */
 size_t _MT_npages = 0;         /* variable holding page size */
@@ -441,9 +435,9 @@ GDKmem_inuse(void)
        /* RAM/swapmem that Monet is really using now */
        size_t mem_mallocedbytes_estimate;
 
-       malloc_lock();
+       malloc_lock("GDKmem_inuse");
        mem_mallocedbytes_estimate = GDK_mallocedbytes_estimate;
-       malloc_unlock();
+       malloc_unlock("GDKmem_inuse");
 
        return mem_mallocedbytes_estimate;
 }
@@ -454,9 +448,9 @@ GDKvm_cursize(void)
        /* current Monet VM address space usage */
        size_t vm_cursize;
 
-       gdk_set_lock(GDKthreadLock, "GDKvm_cursize");
+       malloc_lock("GDKvm_cursize");
        vm_cursize = GDK_vm_cursize;
-       gdk_unset_lock(GDKthreadLock, "GDKvm_cursize");
+       malloc_unlock("GDKvm_cursize");
 
        return vm_cursize + GDKmem_inuse();
 }
@@ -485,18 +479,18 @@ GDKmem_heapcheck(int t)
        do {                                                    \
                int _idx;                                       \
                                                                \
-               malloc_lock();                                  \
+               malloc_lock("heapinc");                         \
                GDK_mallocedbytes_estimate += (_memdelta);      \
                GDKmallidx(_idx, _memdelta);                    \
                GDK_nmallocs[_idx]++;                           \
-               malloc_unlock();                                \
+               malloc_unlock("heapinc");                       \
        } while (0)
 #define heapdec(memdelta)                                              \
        do {                                                            \
                size_t _memdelta = (size_t) (memdelta);                 \
                int _idx;                                               \
                                                                        \
-               malloc_lock();                                          \
+               malloc_lock("heapdec");                                 \
                if (_memdelta > GDK_mallocedbytes_estimate) {           \
                        /* clearly, the stats are off: it should never  \
                         * become less-than-zero */                     \
@@ -506,19 +500,19 @@ GDKmem_heapcheck(int t)
                }                                                       \
                GDKmallidx(_idx, _memdelta);                            \
                GDK_nmallocs[_idx]--;                                   \
-               malloc_unlock();                                        \
+               malloc_unlock("heapdec");                               \
        } while (0)
 #else
 #define heapinc(_memdelta)                                     \
        do {                                                    \
-               malloc_lock();                                  \
+               malloc_lock("heapinc");                         \
                GDK_mallocedbytes_estimate += (_memdelta);      \
-               malloc_unlock();                                \
+               malloc_unlock("heapinc");                       \
        } while (0)
 #define heapdec(memdelta)                                              \
        do {                                                            \
                size_t _memdelta = (size_t) (memdelta);                 \
-               malloc_lock();                                          \
+               malloc_lock("heapdec");                                 \
                if (_memdelta > GDK_mallocedbytes_estimate) {           \
                        /* clearly, the stats are off: it should never  \
                         * become less-than-zero */                     \
@@ -526,7 +520,7 @@ GDKmem_heapcheck(int t)
                } else {                                                \
                        GDK_mallocedbytes_estimate -= _memdelta;        \
                }                                                       \
-               malloc_unlock();                                        \
+               malloc_unlock("heapdec");                               \
        } while (0)
 #endif
 
@@ -536,45 +530,45 @@ GDKmem_heapcheck(int t)
                size_t _vmdelta = (size_t) SEG_SIZE((vmdelta),MT_VMUNITLOG); \
                int _idx;                                               \
                                                                        \
-               gdk_set_lock(GDKthreadLock, fcn);                       \
+               malloc_lock(fcn);                                       \
                GDKmallidx(_idx, _vmdelta);                             \
                GDK_vm_nallocs[_idx]++;                                 \
                GDK_vm_cursize += _vmdelta;                             \
-               gdk_unset_lock(GDKthreadLock, fcn);                     \
+               malloc_unlock(fcn);                                     \
        } while (0)
 #define memdec(vmdelta, fcn)                                           \
        do {                                                            \
                size_t _vmdelta = (size_t) SEG_SIZE((vmdelta),MT_VMUNITLOG); \
                int _idx;                                               \
                                                                        \
-               gdk_set_lock(GDKthreadLock, fcn);                       \
+               malloc_lock(fcn);                                       \
                GDKmallidx(_idx, _vmdelta);                             \
                GDK_vm_nallocs[_idx]--;                                 \
                if (_vmdelta > GDK_vm_cursize)                          \
                        GDK_vm_cursize = 0;                             \
                else                                                    \
                        GDK_vm_cursize -= _vmdelta;                     \
-               gdk_unset_lock(GDKthreadLock, fcn);                     \
+               malloc_unlock(fcn);                                     \
        } while (0)
 #else
 #define meminc(vmdelta, fcn)                                           \
        do {                                                            \
                size_t _vmdelta = (size_t) SEG_SIZE((vmdelta),MT_VMUNITLOG); \
                                                                        \
-               gdk_set_lock(GDKthreadLock, fcn);                       \
+               malloc_lock(fcn);                                       \
                GDK_vm_cursize += _vmdelta;                             \
-               gdk_unset_lock(GDKthreadLock, fcn);                     \
+               malloc_unlock(fcn);                                     \
        } while (0)
 #define memdec(vmdelta, fcn)                                           \
        do {                                                            \
                size_t _vmdelta = (size_t) SEG_SIZE((vmdelta),MT_VMUNITLOG); \
                                                                        \
-               gdk_set_lock(GDKthreadLock, fcn);                       \
+               malloc_lock(fcn);                                       \
                if (_vmdelta > GDK_vm_cursize)                          \
                        GDK_vm_cursize = 0;                             \
                else                                                    \
                        GDK_vm_cursize -= _vmdelta;                     \
-               gdk_unset_lock(GDKthreadLock, fcn);                     \
+               malloc_unlock(fcn);                                     \
        } while (0)
 #endif
 
@@ -598,28 +592,28 @@ GDKmemdump(void)
        {
                int i;
 
-               malloc_lock();
+               malloc_lock("GDKmemdump");
                THRprintf(GDKstdout, "#memory histogram\n");
                for (i = 3; i < GDK_HISTO_MAX_BIT - 1; i++) {
                        size_t j = 1 << i;
 
                        THRprintf(GDKstdout, "# " SZFMT " " SZFMT "\n", j, 
GDK_nmallocs[i]);
                }
-               malloc_unlock();
+               malloc_unlock("GDKmemdump");
        }
 #endif
 #ifdef GDK_VM_KEEPHISTO
        {
                int i;
 
-               gdk_set_lock(GDKthreadLock, "GDKmemdump");
+               malloc_lock("GDKmemdump");
                THRprintf(GDKstdout, "\n#virtual memory histogram\n");
                for (i = 12; i < GDK_HISTO_MAX_BIT - 1; i++) {
                        size_t j = 1 << i;
 
                        THRprintf(GDKstdout, "# " SZFMT " " SZFMT "\n", j, 
GDK_vm_nallocs[i]);
                }
-               gdk_unset_lock(GDKthreadLock, "GDKmemdump");
+               malloc_unlock("GDKmemdump");
        }
 #endif
 }
_______________________________________________
Checkin-list mailing list
[email protected]
http://mail.monetdb.org/mailman/listinfo/checkin-list

Reply via email to