[PATCH 2/4] Add mama_openWithPropertiesCount() and mama_closeCount()


Michael Schonberg <mschonberg@...>
 

From: Mike Schonberg <mschonberg@...>

These methods take an additonal unsigned int* parameter that upon return
contains the the reference count for mama_openXXX() and mama_closeXXX(). This
allows applications to performa one-time global initialization when
the int is 1 and mama_openWithPropertiesCount() returns MAMA_STATUS_OK. Likewise
applications can clean up global data when the count is zero and
mama_closeCount() returns MAMA_STATUS_OK.

Signed-off-by: John Gray <jgray@...>
---
mama/c_cpp/src/c/mama.c | 58 +++++++++++++++++++++++++++++++++++------
mama/c_cpp/src/c/mama/mama.h | 45 ++++++++++++++++++++++++++++++++
2 files changed, 94 insertions(+), 9 deletions(-)

diff --git a/mama/c_cpp/src/c/mama.c b/mama/c_cpp/src/c/mama.c
index cecfa4a..7b6add9 100644
--- a/mama/c_cpp/src/c/mama.c
+++ b/mama/c_cpp/src/c/mama.c
@@ -643,8 +643,9 @@ mamaInternal_getDefaultPayload (void)
}

mama_status
-mama_openWithProperties (const char* path,
- const char* filename)
+mama_openWithPropertiesCount (const char* path,
+ const char* filename,
+ unsigned int* count)
{
mama_status result = MAMA_STATUS_OK;
mama_size_t numBridges = 0;
@@ -695,6 +696,10 @@ mama_openWithProperties (const char* path,
{
if (MAMA_STATUS_OK == result)
gImpl.myRefCount++;
+
+ if (count)
+ *count = gImpl.myRefCount;
+
pthread_mutex_unlock (&gImpl.myLock);
return result;
}
@@ -757,6 +762,9 @@ mama_openWithProperties (const char* path,
mama_log (MAMA_LOG_LEVEL_ERROR,
"mama_openWithProperties(): "
"Could not create stats generator.");
+ if (count)
+ *count = gImpl.myRefCount;
+
pthread_mutex_unlock (&gImpl.myLock);
return result;
}
@@ -874,6 +882,9 @@ mama_openWithProperties (const char* path,
mama_log (MAMA_LOG_LEVEL_SEVERE,
"mama_openWithProperties(): "
"At least one bridge must be specified");
+ if (count)
+ *count = gImpl.myRefCount;
+
pthread_mutex_unlock (&gImpl.myLock);
return MAMA_STATUS_NO_BRIDGE_IMPL;
}
@@ -883,6 +894,9 @@ mama_openWithProperties (const char* path,
mama_log (MAMA_LOG_LEVEL_SEVERE,
"mama_openWithProperties(): "
"At least one payload must be specified");
+ if (count)
+ *count = gImpl.myRefCount;
+
pthread_mutex_unlock (&gImpl.myLock);
return MAMA_STATUS_NO_BRIDGE_IMPL;
}
@@ -901,6 +915,10 @@ mama_openWithProperties (const char* path,
"mama_openWithProperties(): "
"Error connecting to Entitlements Server");
mama_close();
+
+ if (count)
+ *count = gImpl.myRefCount;
+
pthread_mutex_unlock (&gImpl.myLock);
return result;
}
@@ -938,6 +956,8 @@ mama_openWithProperties (const char* path,
if (MAMA_STATUS_OK != (result = mamaBridgeImpl_getInternalEventQueue (bridge,
&statsGenQueue)))
{
+ if (count)
+ *count = gImpl.myRefCount;
pthread_mutex_unlock (&gImpl.myLock);
return result;
}
@@ -948,6 +968,8 @@ mama_openWithProperties (const char* path,
mama_log (MAMA_LOG_LEVEL_ERROR,
"mama_openWithProperties(): "
"Could not set queue for stats generator.");
+ if (count)
+ *count = gImpl.myRefCount;
pthread_mutex_unlock (&gImpl.myLock);
return result;
}
@@ -957,12 +979,16 @@ mama_openWithProperties (const char* path,
mama_log (MAMA_LOG_LEVEL_ERROR,
"mama_openWithProperties(): "
"Failed to enable stats logging");
+ if (count)
+ *count = gImpl.myRefCount;
pthread_mutex_unlock (&gImpl.myLock);
return result;
}
}

gImpl.myRefCount++;
+ if (count)
+ *count = gImpl.myRefCount;
pthread_mutex_unlock (&gImpl.myLock);
return result;
}
@@ -972,7 +998,14 @@ mama_open ()
{
/*Passing NULL as path and filename will result in the
default behaviour - mama.properties on $WOMBAT_PATH*/
- return mama_openWithProperties (NULL, NULL);
+ return mama_openWithPropertiesCount (NULL, NULL, NULL);
+}
+
+mama_status
+mama_openWithProperties (const char* path,
+ const char* filename)
+{
+ return mama_openWithPropertiesCount (path, filename, NULL);
}

mama_status
@@ -1079,7 +1112,7 @@ mama_getVersion (mamaBridge bridgeImpl)
}

mama_status
-mama_close ()
+mama_closeCount (unsigned int* count)
{
mama_status result = MAMA_STATUS_OK;
mamaMiddleware middleware = 0;
@@ -1088,6 +1121,8 @@ mama_close ()
pthread_mutex_lock (&gImpl.myLock);
if (gImpl.myRefCount == 0)
{
+ if (count)
+ *count = gImpl.myRefCount;
pthread_mutex_unlock (&gImpl.myLock);
return MAMA_STATUS_OK;
}
@@ -1113,11 +1148,8 @@ mama_close ()
/* Look for a bridge for each of the payloads and close them */
for (payload = 0; payload != MAMA_PAYLOAD_MAX; ++payload)
{
- mamaPayloadBridgeImpl* impl = (mamaPayloadBridgeImpl*) gImpl.myPayloads [(uint8_t)payload];
- if (impl)
- {
-
- }
+ /* mamaPayloadBridgeImpl* impl = (mamaPayloadBridgeImpl*)
+ * gImpl.myPayloads [(uint8_t)payload];*/
gImpl.myPayloads[(uint8_t)payload] = NULL;
}

@@ -1244,10 +1276,18 @@ mama_close ()
mama_freeAppContext(&appContext);

}
+ if (count)
+ *count = gImpl.myRefCount;
pthread_mutex_unlock (&gImpl.myLock);
return result;
}

+mama_status
+mama_close (void)
+{
+ return mama_closeCount (NULL);
+}
+
/**
* Start processing messages.
*/
diff --git a/mama/c_cpp/src/c/mama/mama.h b/mama/c_cpp/src/c/mama/mama.h
index 658bce6..557ac2c 100644
--- a/mama/c_cpp/src/c/mama/mama.h
+++ b/mama/c_cpp/src/c/mama/mama.h
@@ -220,6 +220,40 @@ extern "C"
extern mama_status
mama_openWithProperties (const char* path,
const char* filename);
+ /**
+ * Initialize MAMA.
+ *
+ * Allows users of the API to override the default behavior of mama_open()
+ * where a file mama.properties is required to be located in the directory
+ * specified by \$WOMBAT_PATH.
+ *
+ * The properties file must have the same structure as a standard
+ * mama.properties file.
+ *
+ * If null is passed as the path the API will look for the properties file on
+ * the \$WOMBAT_PATH.
+ *
+ * If null is passed as the filename the API will look for the default
+ * filename of mama.properties.
+ *
+ * The count value on return will be the number of times that mama_openXxx()
+ * has ben invoked successfully. Applicatiins can use this to perform
+ * one-time initialization when the value is 1 and the return is
+ * MAMA_STATUS_OK
+ *
+ * @param path Fully qualified path to the directory containing the properties
+ * file
+ * @param filename The name of the file containing MAMA properties.
+ * @param count The number of times mama_OpenXXX() has been called
+ * successfully.
+ *
+ * @return mama_status Whether the call was successful or not.
+ */
+ MAMAExpDLL
+ extern mama_status
+ mama_openWithPropertiesCount (const char* path,
+ const char* filename,
+ unsigned int* count);

/**
* Set a specific property for the API.
@@ -291,6 +325,17 @@ extern "C"
MAMAExpDLL
extern mama_status
mama_close (void);
+
+ /**
+ * Close MAMA and free all associated resource.
+ *
+ * @param count Filled with the number of times mama has been opened
+ * successfully. Applications can perform global one-time cleanup when this
+ * value is 0 and the return value is MAMA_STATUS_OK.
+ */
+ MAMAExpDLL
+ extern mama_status
+ mama_closeCount (unsigned int* count);

/**
* Return the version information for the library.
--
1.7.5.4