[PATCH 1/4] Added locking around mama_open() and mama_close()


Michael Schonberg <mschonberg@...>
 

From: Mike Schonberg <mschonberg@...>

Improved the reference counting logic and added locking around mama_open() and
mama_close(). The change ensures that all initialization (one-time) and cleanup
only occurs once. It also ensures that the reference count never goes belowe
zero.

Signed-off-by: John Gray <jgray@...>
---
mama/c_cpp/src/c/mama.c | 39 ++++++++++++++++++++++++++++-----------
1 files changed, 28 insertions(+), 11 deletions(-)

diff --git a/mama/c_cpp/src/c/mama.c b/mama/c_cpp/src/c/mama.c
index 4142c16..cecfa4a 100644
--- a/mama/c_cpp/src/c/mama.c
+++ b/mama/c_cpp/src/c/mama.c
@@ -654,6 +654,8 @@ mama_openWithProperties (const char* path,
const char* statsLogging = "false";
const char* catchCallbackExceptions = NULL;

+ pthread_mutex_lock (&gImpl.myLock);
+
if (pthread_key_create(&last_err_key, NULL) != 0)
{
mama_log (MAMA_LOG_LEVEL_NORMAL, "WARNING!!! - CANNOT ALLOCATE KEY FOR ERRORS");
@@ -689,7 +691,14 @@ mama_openWithProperties (const char* path,
"********************************************************");
#endif

- if (gImpl.myRefCount++) return result;
+ if (0 != gImpl.myRefCount)
+ {
+ if (MAMA_STATUS_OK == result)
+ gImpl.myRefCount++;
+ pthread_mutex_unlock (&gImpl.myLock);
+ return result;
+ }
+ /* Code after this point is one-time initialization */

#ifdef WITH_INACTIVE_CHECK
mama_log (MAMA_LOG_LEVEL_WARN,
@@ -748,10 +757,10 @@ mama_openWithProperties (const char* path,
mama_log (MAMA_LOG_LEVEL_ERROR,
"mama_openWithProperties(): "
"Could not create stats generator.");
+ pthread_mutex_unlock (&gImpl.myLock);
return result;
}

-
globalLogging = properties_Get (gProperties, "mama.statslogging.global.logging");
globalPublishing = properties_Get (gProperties, "mama.statslogging.global.publishing");
transportLogging = properties_Get (gProperties, "mama.statslogging.transport.logging");
@@ -842,7 +851,6 @@ mama_openWithProperties (const char* path,
}
}

-
if (mamaInternal_statsPublishingEnabled())
{
mamaInternal_loadStatsPublisher();
@@ -866,16 +874,16 @@ mama_openWithProperties (const char* path,
mama_log (MAMA_LOG_LEVEL_SEVERE,
"mama_openWithProperties(): "
"At least one bridge must be specified");
+ pthread_mutex_unlock (&gImpl.myLock);
return MAMA_STATUS_NO_BRIDGE_IMPL;
}

if (!gDefaultPayload)
{
-
-
mama_log (MAMA_LOG_LEVEL_SEVERE,
"mama_openWithProperties(): "
"At least one payload must be specified");
+ pthread_mutex_unlock (&gImpl.myLock);
return MAMA_STATUS_NO_BRIDGE_IMPL;
}

@@ -893,6 +901,7 @@ mama_openWithProperties (const char* path,
"mama_openWithProperties(): "
"Error connecting to Entitlements Server");
mama_close();
+ pthread_mutex_unlock (&gImpl.myLock);
return result;
}
#endif /* WITH_ENTITLEMENTS */
@@ -929,6 +938,7 @@ mama_openWithProperties (const char* path,
if (MAMA_STATUS_OK != (result = mamaBridgeImpl_getInternalEventQueue (bridge,
&statsGenQueue)))
{
+ pthread_mutex_unlock (&gImpl.myLock);
return result;
}
}
@@ -938,6 +948,7 @@ mama_openWithProperties (const char* path,
mama_log (MAMA_LOG_LEVEL_ERROR,
"mama_openWithProperties(): "
"Could not set queue for stats generator.");
+ pthread_mutex_unlock (&gImpl.myLock);
return result;
}

@@ -946,10 +957,13 @@ mama_openWithProperties (const char* path,
mama_log (MAMA_LOG_LEVEL_ERROR,
"mama_openWithProperties(): "
"Failed to enable stats logging");
+ pthread_mutex_unlock (&gImpl.myLock);
return result;
}
}

+ gImpl.myRefCount++;
+ pthread_mutex_unlock (&gImpl.myLock);
return result;
}

@@ -1071,7 +1085,14 @@ mama_close ()
mamaMiddleware middleware = 0;
int payload = 0;

- if( !--gImpl.myRefCount )
+ pthread_mutex_lock (&gImpl.myLock);
+ if (gImpl.myRefCount == 0)
+ {
+ pthread_mutex_unlock (&gImpl.myLock);
+ return MAMA_STATUS_OK;
+ }
+
+ if (!--gImpl.myRefCount)
{
#ifdef WITH_ENTITLEMENTS
if( gEntitlementClient != 0 )
@@ -1120,7 +1141,6 @@ mama_close ()
gUnknownMsgStat = NULL;
}

-
if (gMessageStat)
{
mamaStat_destroy (gMessageStat);
@@ -1224,10 +1244,7 @@ mama_close ()
mama_freeAppContext(&appContext);

}
- if (gImpl.myRefCount < 0)
- {
- gImpl.myRefCount = 0;
- }
+ pthread_mutex_unlock (&gImpl.myLock);
return result;
}

--
1.7.5.4

Join Openmama-dev@lists.openmama.org to automatically receive all group messages.