Skip to content
Snippets Groups Projects
Commit 963e88e7 authored by Utz-Uwe Haus's avatar Utz-Uwe Haus
Browse files

[MAMBA] Protect allocator factory by lock

parent d29539ae
Branches
Tags
No related merge requests found
...@@ -59,6 +59,15 @@ dnl Keep a backup of LDFLAGS and CFLAGS so it is not modified except for lib che ...@@ -59,6 +59,15 @@ dnl Keep a backup of LDFLAGS and CFLAGS so it is not modified except for lib che
CFLAGS_backup="${CFLAGS}" CFLAGS_backup="${CFLAGS}"
LDFLAGS_backup="${LDFLAGS}" LDFLAGS_backup="${LDFLAGS}"
dnl pthread checks on Cray CC need this to have _REENTRANT defined
CPPFLAGS+=" -D_REENTRANT"
AX_PTHREAD([
LIBS="$PTHREAD_LIBS $LIBS"
CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
CC="$PTHREAD_CC"
],[AC_MSG_FAILURE([Need pthread library])])
dnl LANL/SICM support dnl LANL/SICM support
AC_ARG_WITH([sicm], AC_ARG_WITH([sicm],
[AS_HELP_STRING([--with-sicm@<:@=/path/to/sicm/install@:>@], [AS_HELP_STRING([--with-sicm@<:@=/path/to/sicm/install@:>@],
......
This diff is collapsed.
...@@ -33,6 +33,8 @@ ...@@ -33,6 +33,8 @@
#include <stddef.h> #include <stddef.h>
#include <stdbool.h> #include <stdbool.h>
#include <string.h> #include <string.h>
#include <pthread.h>
#include <assert.h>
#include "mmb_error.h" #include "mmb_error.h"
#include "mmb_logging.h" #include "mmb_logging.h"
...@@ -50,7 +52,21 @@ typedef struct mmbAllocationFactory { ...@@ -50,7 +52,21 @@ typedef struct mmbAllocationFactory {
} mmbAllocationFactory; } mmbAllocationFactory;
static mmbAllocationFactory *global_factory = NULL; static mmbAllocationFactory *global_factory = NULL;
/* protecting global_factory */
pthread_mutex_t global_factory_mtx = PTHREAD_MUTEX_INITIALIZER;
#define LOCK_GLOBAL_FACTORY do { \
DEBUG("Locking global factory\n"); \
int _gflstat = pthread_mutex_lock(&global_factory_mtx); \
assert(_gflstat==0); \
} while(0);
#define UNLOCK_GLOBAL_FACTORY do { \
DEBUG("Unlocking global factory\n"); \
int _gflstat = pthread_mutex_unlock(&global_factory_mtx); \
assert(_gflstat==0); \
} while(0);
/* must be called under lock */
static mmbError mmb_alloc_factory_new(mmbAllocationFactory **out_factory) static mmbError mmb_alloc_factory_new(mmbAllocationFactory **out_factory)
{ {
/* Check parameter */ /* Check parameter */
...@@ -69,6 +85,7 @@ static mmbError mmb_alloc_factory_new(mmbAllocationFactory **out_factory) ...@@ -69,6 +85,7 @@ static mmbError mmb_alloc_factory_new(mmbAllocationFactory **out_factory)
return MMB_OK; return MMB_OK;
} }
/* must be called under lock */
static mmbError mmb_alloc_factory_delete(mmbAllocationFactory *in_factory) static mmbError mmb_alloc_factory_delete(mmbAllocationFactory *in_factory)
{ {
/* Check parameter */ /* Check parameter */
...@@ -115,9 +132,11 @@ mmbError mmb_alloc_check_params(const mmbAllocation *alloc) ...@@ -115,9 +132,11 @@ mmbError mmb_alloc_check_params(const mmbAllocation *alloc)
mmbError mmb_alloc_initialize(void) mmbError mmb_alloc_initialize(void)
{ {
mmbError stat = MMB_OK; mmbError stat = MMB_OK;
LOCK_GLOBAL_FACTORY;
if (NULL == global_factory) { if (NULL == global_factory) {
stat = mmb_alloc_factory_new(&global_factory); stat = mmb_alloc_factory_new(&global_factory);
} }
UNLOCK_GLOBAL_FACTORY;
return stat; return stat;
} }
...@@ -125,15 +144,21 @@ mmbError mmb_alloc_finalize(void) ...@@ -125,15 +144,21 @@ mmbError mmb_alloc_finalize(void)
{ {
mmbError stat; mmbError stat;
mmbAllocationFactory *fact = NULL; mmbAllocationFactory *fact = NULL;
LOCK_GLOBAL_FACTORY;
for (; NULL != global_factory; global_factory = fact) { for (; NULL != global_factory; global_factory = fact) {
fact = global_factory->next; fact = global_factory->next;
stat = mmb_alloc_factory_delete(global_factory); stat = mmb_alloc_factory_delete(global_factory);
if (MMB_OK != stat) { if (MMB_OK != stat) {
WARN("Error when deallocating the mmbAllocation factory.\n"); WARN("Error when deallocating the mmbAllocation factory.\n");
return stat; goto BAILOUT_UNLOCK;
} }
} }
return MMB_OK; BAILOUT_UNLOCK:
UNLOCK_GLOBAL_FACTORY;
return stat;
} }
mmbError mmb_alloc_allocate(void *ptr, const size_t n_bytes, mmbError mmb_alloc_allocate(void *ptr, const size_t n_bytes,
...@@ -141,14 +166,20 @@ mmbError mmb_alloc_allocate(void *ptr, const size_t n_bytes, ...@@ -141,14 +166,20 @@ mmbError mmb_alloc_allocate(void *ptr, const size_t n_bytes,
const bool owned, mmbAllocation **alloc) const bool owned, mmbAllocation **alloc)
{ {
/* Check parameter */ /* Check parameter */
if (NULL == global_factory) { mmbError stat;
ERR("Factory not initialized.\n");
return MMB_INVALID_ARG;
}
if (NULL == alloc) { if (NULL == alloc) {
ERR("Out parameter \"alloc\" cannot be NULL.\n"); ERR("Out parameter \"alloc\" cannot be NULL.\n");
return MMB_INVALID_ARG; return MMB_INVALID_ARG;
} }
LOCK_GLOBAL_FACTORY;
if (NULL == global_factory) {
ERR("Factory not initialized.\n");
stat = MMB_INVALID_ARG;
goto BAILOUT_UNLOCK;
}
/* Look for an available slot */ /* Look for an available slot */
mmbAllocationFactory *last = NULL, *factory = global_factory; mmbAllocationFactory *last = NULL, *factory = global_factory;
mmbAllocation *new_alloc = NULL; mmbAllocation *new_alloc = NULL;
...@@ -169,10 +200,10 @@ mmbError mmb_alloc_allocate(void *ptr, const size_t n_bytes, ...@@ -169,10 +200,10 @@ mmbError mmb_alloc_allocate(void *ptr, const size_t n_bytes,
} }
/* if this point is reached no suitable available entry has been found. /* if this point is reached no suitable available entry has been found.
* Allocate a new set of mmbAllocation's. */ * Allocate a new set of mmbAllocation's. */
mmbError stat = mmb_alloc_factory_new(&last->next); stat = mmb_alloc_factory_new(&last->next);
if (MMB_OK != stat) { if (MMB_OK != stat) {
WARN("Unable to allocate new set of mmbAllocation. Allocation fails.\n"); WARN("Unable to allocate new set of mmbAllocation. Allocation fails.\n");
return stat; goto BAILOUT_UNLOCK;
} }
new_alloc = &last->next->ptrs[0][0]; new_alloc = &last->next->ptrs[0][0];
last->next->avail[0] &= ~1; last->next->avail[0] &= ~1;
...@@ -183,7 +214,12 @@ SET_AND_OUT: ...@@ -183,7 +214,12 @@ SET_AND_OUT:
new_alloc->interface = interface; new_alloc->interface = interface;
new_alloc->owned = owned; new_alloc->owned = owned;
*alloc = new_alloc; *alloc = new_alloc;
return MMB_OK; stat = MMB_OK;
BAILOUT_UNLOCK:
UNLOCK_GLOBAL_FACTORY;
return stat;
} }
mmbError mmb_alloc_free(mmbAllocation *alloc) mmbError mmb_alloc_free(mmbAllocation *alloc)
...@@ -195,9 +231,12 @@ mmbError mmb_alloc_free(mmbAllocation *alloc) ...@@ -195,9 +231,12 @@ mmbError mmb_alloc_free(mmbAllocation *alloc)
WARN("Invalid \"alloc\" handle.\n"); WARN("Invalid \"alloc\" handle.\n");
return MMB_INVALID_ALLOCATION; return MMB_INVALID_ALLOCATION;
} }
LOCK_GLOBAL_FACTORY;
if (NULL == global_factory) { if (NULL == global_factory) {
ERR("Factory not initialized.\n"); ERR("Factory not initialized.\n");
return MMB_INVALID_ARG; stat = MMB_INVALID_ARG;
goto BAILOUT_UNLOCK;
} }
/* Find proper set that contains the allocated alloc. */ /* Find proper set that contains the allocated alloc. */
for (mmbAllocationFactory *factory = global_factory; for (mmbAllocationFactory *factory = global_factory;
...@@ -208,10 +247,15 @@ mmbError mmb_alloc_free(mmbAllocation *alloc) ...@@ -208,10 +247,15 @@ mmbError mmb_alloc_free(mmbAllocation *alloc)
const size_t block = offset / (8 * sizeof(unsigned int)); const size_t block = offset / (8 * sizeof(unsigned int));
const size_t index = offset % (8 * sizeof(unsigned int)); const size_t index = offset % (8 * sizeof(unsigned int));
factory->avail[block] |= 1<<index; factory->avail[block] |= 1<<index;
return MMB_OK; stat = MMB_OK;
goto BAILOUT_UNLOCK;
} }
} }
/* Could not find the proper set : incoherent state that should have happen */ /* Could not find the proper set : incoherent state that should have happen */
ERR("Impossible to find the corresponding mmbAllocation record.\n"); ERR("Impossible to find the corresponding mmbAllocation record.\n");
return MMB_ERROR; stat = MMB_ERROR;
BAILOUT_UNLOCK:
UNLOCK_GLOBAL_FACTORY;
return stat;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment