Browse Source

option to use a trylock on malloc() instead of a lock

malloc() can now be more agreesive on multi-threaded applications by not waiting
on locked raw blocks.
Ioannis Koutras 11 years ago
parent
commit
52b135e316
7 changed files with 36 additions and 12 deletions
  1. 8 0
      CMakeLists.txt
  2. 1 0
      DefineOptions.cmake
  3. 2 0
      config.h.cmake
  4. 1 0
      linux.preset
  5. 1 0
      osx.preset
  6. 4 0
      private-include/locks.h
  7. 19 12
      src/malloc.c

+ 8 - 0
CMakeLists.txt

@@ -37,6 +37,11 @@ if(WITH_SYSTEM_CALLS STREQUAL "mmap")
   endif(NOT DEFINED SYS_ALLOC_SIZE)
 endif(WITH_SYSTEM_CALLS STREQUAL "mmap")
 
+# Checking locks
+if(NOT HAVE_LOCKS AND TRYLOCK_ON_MALLOC)
+  message(FATAL_ERROR "You have to set HAVE_LOCKS to use TRYLOCK_ON_MALLOC.")
+endif(NOT HAVE_LOCKS AND TRYLOCK_ON_MALLOC)
+
 # Checking free-list setups
 if(RAW_BLOCKS_TYPE STREQUAL "freelist")
   set(FL_RB_ONLY ON)
@@ -146,6 +151,9 @@ if(NOT WITH_SYSTEM_CALLS STREQUAL "none")
 endif(NOT WITH_SYSTEM_CALLS STREQUAL "none")
 
 message(STATUS "POSIX locking mechanisms: " ${HAVE_LOCKS})
+if(HAVE_LOCKS)
+  message(STATUS "Use a trylock on malloc(): " ${TRYLOCK_ON_MALLOC})
+endif(HAVE_LOCKS)
 
 message(STATUS "Raw blocks type: " ${RAW_BLOCKS_TYPE})
 

+ 1 - 0
DefineOptions.cmake

@@ -13,6 +13,7 @@ set(WITH_SYSTEM_CALLS "none" CACHE STRING "Choose what system calls can be used,
 set(SYS_ALLOC_SIZE 4096 CACHE INTEGER "Choose the default system allocation size")
 
 option(HAVE_LOCKS "Build with POSIX locking mechanisms" OFF)
+option(TRYLOCK_ON_MALLOC "Use a trylock on malloc()" OFF)
 
 set(RAW_BLOCKS_TYPE "freelist" CACHE STRING "Choose what raw blocks can be used, options are: freelist, bitmap")
 

+ 2 - 0
config.h.cmake

@@ -56,6 +56,8 @@
 #cmakedefine SYS_ALLOC_SIZE @SYS_ALLOC_SIZE@
 /** Support for locks */
 #cmakedefine HAVE_LOCKS
+/** Use a trylock on malloc() */
+#cmakedefine TRYLOCK_ON_MALLOC
 
 /* Free-list Settings */
 #ifdef FL_RB_ONLY

+ 1 - 0
linux.preset

@@ -1,6 +1,7 @@
 set(WITH_SYSTEM_CALLS "mmap" CACHE STRING "Choose what system calls can be used, options are: none, sbrk, mmap")
 set(SYS_ALLOC_SIZE 32768 CACHE INTEGER "Choose the default system allocation size")
 option(HAVE_LOCKS "Build with POSIX locking mechanisms" ON)
+option(TRYLOCK_ON_MALLOC "Use a trylock on malloc()" ON)
 set(FITTING_POLICY "good" CACHE STRING "Choose the fitting policy in freelist-organized raw blocks, options are: best, exact, first, good")
 set(GOOD_FIT_PERCENTAGE 0.8 CACHE DOUBLE "Choose the good-fit percentage")
 set(WITH_COALESCING "variable" CACHE STRING "Build with coalescing support")

+ 1 - 0
osx.preset

@@ -1,6 +1,7 @@
 set(WITH_SYSTEM_CALLS "mmap" CACHE STRING "Choose what system calls can be used, options are: none, sbrk, mmap")
 set(SYS_ALLOC_SIZE 32768 CACHE INTEGER "Choose the default system allocation size")
 option(HAVE_LOCKS "Build with POSIX locking mechanisms" ON)
+option(TRYLOCK_ON_MALLOC "Use a trylock on malloc()" ON)
 set(FITTING_POLICY "first" CACHE STRING "Choose the fitting policy in freelist-organized raw blocks, options are: best, exact, first, good")
 set(GOOD_FIT_PERCENTAGE 0.8 CACHE DOUBLE "Choose the good-fit percentage")
 set(WITH_COALESCING "none" CACHE STRING "Build with coalescing support")

+ 4 - 0
private-include/locks.h

@@ -37,6 +37,8 @@
 #define UNLOCK_GLOBAL() pthread_mutex_unlock(&systemallocator.creation_mutex)
 /** Locks a specific raw block */
 #define LOCK_RAW_BLOCK(rb) pthread_mutex_lock(&rb->mutex)
+/** Tries to lock a specific raw block */
+#define TRYLOCK_RAW_BLOCK(rb) pthread_mutex_trylock(&rb->mutex)
 /** Initializes a raw block lock */
 #define INIT_RAW_BLOCK_LOCK(rb) pthread_mutex_init(&rb->mutex, NULL);
 /** Destroys a raw block lock */
@@ -53,6 +55,8 @@
 /** Does nothing. */
 #define LOCK_RAW_BLOCK(RB)
 /** Does nothing. */
+#define TRYLOCK_RAW_BLOCK(RB)
+/** Does nothing. */
 #define INIT_RAW_BLOCK_LOCK(RB)
 /** Does nothing. */
 #define DESTROY_RAW_BLOCK_LOCK(RB)

+ 19 - 12
src/malloc.c

@@ -51,19 +51,26 @@ void * malloc(size_t size) {
     /* Try to find a raw block available for allocation */
 
     SLIST_FOREACH(raw_block, &systemallocator.rb_head, pointers) {
-        LOCK_RAW_BLOCK(raw_block);
-        encapsulated_rb = (DEFAULT_RB_T *)
-            ((uintptr_t) raw_block + sizeof(raw_block_header_t));
-        ptr = dmmlib_malloc(encapsulated_rb, size);
-        UNLOCK_RAW_BLOCK(raw_block);
-
-        if(ptr != NULL) {
-            /* Check that a valid pointer has been returned */
-            assert(((uintptr_t) raw_block < (uintptr_t) ptr) &&
-                    ((uintptr_t) ptr < (uintptr_t) raw_block +
-                     raw_block->size + sizeof(raw_block_header_t)));
-            break;
+#ifdef TRYLOCK_ON_MALLOC
+        if(TRYLOCK_RAW_BLOCK(raw_block) == 0) {
+#else /* TRYLOCK_ON_MALLOC */
+            LOCK_RAW_BLOCK(raw_block);
+#endif /* TRYLOCK_ON_MALLOC */
+            encapsulated_rb = (DEFAULT_RB_T *)
+                ((uintptr_t) raw_block + sizeof(raw_block_header_t));
+            ptr = dmmlib_malloc(encapsulated_rb, size);
+            UNLOCK_RAW_BLOCK(raw_block);
+
+            if(ptr != NULL) {
+                /* Check that a valid pointer has been returned */
+                assert(((uintptr_t) raw_block < (uintptr_t) ptr) &&
+                        ((uintptr_t) ptr < (uintptr_t) raw_block +
+                         raw_block->size + sizeof(raw_block_header_t)));
+                break;
+            }
+#ifdef TRYLOCK_ON_MALLOC
         }
+#endif /* TRYLOCK_ON_MALLOC */
     }
 
     if(ptr == NULL) {