[PATCH 1.1] mama.c: Reference counting prep


John Gray <jgray@...>
 

This is in preparation for adding improved reference counting and locking around

mama_open(), mama_start(), mama_stop(), and mama_close() to enable them to be

called safely from multi-threaded applications.

This is in preparation for adding improved reference counting and locking

around mama_open() and mama_close() to enable them to be called safely from

multi-threaded applications.

 

Index: c_cpp/src/c/mama.c

===================================================================

RCS file: /cvsroot/products/mama/c_cpp/src/c/mama.c,v

retrieving revision 1.128.4.7.2.2.4.19.4.2

diff -u -r1.128.4.7.2.2.4.19.4.2 mama.c

--- c_cpp/src/c/mama.c                7 Jan 2012 01:17:50 -0000              1.128.4.7.2.2.4.19.4.2

+++ c_cpp/src/c/mama.c             7 Jan 2012 01:34:20 -0000

@@ -1,4 +1,4 @@

-/* $Id: mama.c,v 1.128.4.7.2.2.4.19.4.2 2012/01/07 01:17:50 mikeschonberg Exp $

+/* $Id$

  *

  * OpenMAMA: The open middleware agnostic messaging API

  * Copyright (C) 2011 NYSE Inc.

@@ -128,7 +128,6 @@

 static void lookupIPAddress (void);

-static int gRefCount = 0;

 wproperty_t             gProperties      = 0;

static mamaStatsLogger  gStatsPublisher  = NULL;

@@ -148,11 +147,9 @@

 #define MAMA_PAYLOAD_MAX             CHAR_MAX

-static mamaBridge         gMamaBridges    [MAMA_MIDDLEWARE_MAX];

-static mamaPayloadBridge    gMamaPayloads   [MAMA_PAYLOAD_MAX];

static mamaPayloadBridge    gDefaultPayload = NULL;

-pthread_key_t last_err_key;

+static pthread_key_t last_err_key;

 /**

  * struct mamaApplicationGroup

@@ -164,16 +161,39 @@

     const char* myApplicationClass;

} mamaApplicationContext;

+/**

+ * This structure contains data needed to control starting and stopping of

+ * mama.

+ *

+ * TODO: Access to this structure will ultimately be protected by a reference

+ * count and a lock.

+ */

+typedef struct mamaImpl_

+{

+    mamaBridge           myBridges[MAMA_MIDDLEWARE_MAX];

+    mamaPayloadBridge    myPayloads[MAMA_PAYLOAD_MAX];

+    LIB_HANDLE           myBridgeLibraries[MAMA_MIDDLEWARE_MAX];

+    LIB_HANDLE           myPayloadLibraries[MAMA_PAYLOAD_MAX];

+    unsigned int         myRefCount;

+    pthread_mutex_t      myLock;

+} mamaImpl;

 static mamaApplicationContext  appContext;

static char mama_ver_string[256];

+static mamaImpl gImpl = {{0}, {0}, {0}, {0}, 0, PTHREAD_MUTEX_INITIALIZER};

+

+/* ************************************************************************* */

+/* Private Function Prototypes. */

+/* ************************************************************************* */

+

+

/*  Description :   This function will free any memory associated with a

  *                  mamaApplicationContext object but will not free the

  *                  object itself.

  *  Arguments   :   context [I] The context object to free.

  */

-void

+static void

mama_freeAppContext(mamaApplicationContext *context)

{

     /* Only continue if the object is valid. */

@@ -348,7 +368,7 @@

         statsLogMiddlewareName = "wmw";

     }

-    bridge = gMamaBridges[mamaMiddleware_convertFromString (statsLogMiddlewareName)];

+    bridge = gImpl.myBridges[mamaMiddleware_convertFromString (statsLogMiddlewareName)];

     if (MAMA_STATUS_OK != (result = mamaBridgeImpl_getInternalEventQueue (bridge,

                                                                           &queue)))

@@ -596,7 +616,7 @@

     for (middleware = 0; middleware < MAMA_MIDDLEWARE_MAX; middleware++)

     {

-        bridge = gMamaBridges [middleware];

+        bridge = gImpl.myBridges [middleware];

         if (bridge != NULL)

         {

@@ -610,9 +630,10 @@

mamaPayloadBridge

mamaInternal_findPayload (char id)

{

-    if (('\0' == id) || (MAMA_PAYLOAD_MAX < id)) return NULL;

+    if ('\0' == id)

+        return NULL;

-    return gMamaPayloads[(uint8_t)id];

+    return gImpl.myPayloads[(uint8_t)id];

}

 mamaPayloadBridge

@@ -668,7 +689,7 @@

             "********************************************************");

#endif

-   if (gRefCount++)  return result;

+   if (gImpl.myRefCount++)  return result;

 #ifdef WITH_INACTIVE_CHECK

     mama_log (MAMA_LOG_LEVEL_WARN,

@@ -831,10 +852,10 @@

     /* Look for a bridge for each of the middlewares and open them */

     for (middleware = 0; middleware != MAMA_MIDDLEWARE_MAX; ++middleware)

     {

-        mamaBridgeImpl* impl = (mamaBridgeImpl*) gMamaBridges [middleware];

+        mamaBridgeImpl* impl = (mamaBridgeImpl*) gImpl.myBridges [middleware];

         if (impl)

         {

-            mama_log (MAMA_LOG_LEVEL_FINE, mama_getVersion (gMamaBridges[middleware]));

+            mama_log (MAMA_LOG_LEVEL_FINE, mama_getVersion (gImpl.myBridges[middleware]));

             mamaQueue_enableStats(impl->mDefaultEventQueue);

             ++numBridges;

         }

@@ -903,7 +924,7 @@

                 statsMiddleware = "wmw";

             }

-            bridge = gMamaBridges[mamaMiddleware_convertFromString (statsMiddleware)];

+            bridge = gImpl.myBridges[mamaMiddleware_convertFromString (statsMiddleware)];

             if (MAMA_STATUS_OK != (result = mamaBridgeImpl_getInternalEventQueue (bridge,

                                                                &statsGenQueue)))

@@ -1050,7 +1071,7 @@

     mamaMiddleware middleware = 0;

     int payload = 0;

-    if( !--gRefCount )

+    if( !--gImpl.myRefCount )

     {

#ifdef WITH_ENTITLEMENTS

         if( gEntitlementClient != 0 )

@@ -1064,19 +1085,19 @@

         for (middleware = 0; middleware != MAMA_MIDDLEWARE_MAX; ++middleware)

         {

-            mamaBridge bridge = gMamaBridges[middleware];

+            mamaBridge bridge = gImpl.myBridges[middleware];

             if (bridge)

                mamaBridgeImpl_stopInternalEventQueue (bridge);

         }

         /* Look for a bridge for each of the payloads and close them */

         for (payload = 0; payload != MAMA_PAYLOAD_MAX; ++payload)

         {

-              mamaPayloadBridgeImpl* impl = (mamaPayloadBridgeImpl*) gMamaPayloads [(uint8_t)payload];

+             mamaPayloadBridgeImpl* impl = (mamaPayloadBridgeImpl*) gImpl.myPayloads [(uint8_t)payload];

             if (impl)

             {

             }

-            gMamaPayloads[(uint8_t)payload] = NULL;

+            gImpl.myPayloads[(uint8_t)payload] = NULL;

         }

         gDefaultPayload = NULL;

@@ -1166,11 +1187,11 @@

         /* Look for a bridge for each of the middlewares and close them */

         for (middleware = 0; middleware != MAMA_MIDDLEWARE_MAX; ++middleware)

         {

-            mamaBridgeImpl* impl = (mamaBridgeImpl*) gMamaBridges [middleware];

+            mamaBridgeImpl* impl = (mamaBridgeImpl*) gImpl.myBridges [middleware];

             if (impl)

             {

                 if (MAMA_STATUS_OK != (

-                   (result = impl->bridgeClose (gMamaBridges[middleware]))))

+                   (result = impl->bridgeClose (gImpl.myBridges[middleware]))))

                 {

                     mama_log (MAMA_LOG_LEVEL_ERROR,

                               "mama_close(): Error closing %s bridge.",

@@ -1178,7 +1199,7 @@

                 }

             }

-            gMamaBridges[middleware] = NULL;

+            gImpl.myBridges[middleware] = NULL;

         }

         /* The properties must not be closed down until after the bridges have been destroyed. */

@@ -1203,9 +1224,9 @@

         mama_freeAppContext(&appContext);

     }

-    if (gRefCount < 0)

+    if (gImpl.myRefCount < 0)

     {

-        gRefCount = 0;

+        gImpl.myRefCount = 0;

     }

     return result;

}

@@ -1339,10 +1360,10 @@

     /* Look for a bridge for each of the middlewares and open them */

     for (middleware = 0; middleware != MAMA_MIDDLEWARE_MAX; ++middleware)

     {

-        mamaBridgeImpl* impl = (mamaBridgeImpl*) gMamaBridges [middleware];

+        mamaBridgeImpl* impl = (mamaBridgeImpl*) gImpl.myBridges [middleware];

         if (impl)

         {

-            status = mama_stop (gMamaBridges[middleware]);

+            status = mama_stop (gImpl.myBridges[middleware]);

             if (MAMA_STATUS_OK != status)

             {

                 mama_log (MAMA_LOG_LEVEL_ERROR,

@@ -1676,8 +1697,10 @@

mamaInternal_registerBridge (mamaBridge     bridge,

                              const char*    middlewareName)

{

-    mamaMiddleware middleware =

-                    mamaMiddleware_convertFromString (middlewareName);

+    mamaMiddleware middleware;

+   

+    middleware = mamaMiddleware_convertFromString (middlewareName);

+

     if (middleware >= MAMA_MIDDLEWARE_MAX)

     {

         mama_log (MAMA_LOG_LEVEL_SEVERE,

@@ -1686,15 +1709,16 @@

         return;

     }

-    gMamaBridges [middleware] = bridge;

+    gImpl.myBridges[middleware] = bridge;

}

 mama_status

mama_setDefaultPayload (char id)

{

-    if (('\0' == id) || (MAMA_PAYLOAD_MAX < id) || gMamaPayloads[(uint8_t)id] == NULL) return MAMA_STATUS_NULL_ARG;

+    if ('\0' == id || gImpl.myPayloads[(uint8_t)id] == NULL)

+        return MAMA_STATUS_NULL_ARG;

-    gDefaultPayload = gMamaPayloads[(uint8_t)id];

+    gDefaultPayload = gImpl.myPayloads[(uint8_t)id];

     return MAMA_STATUS_OK;

}

@@ -1756,7 +1780,7 @@

         return MAMA_STATUS_NO_BRIDGE_IMPL;

     }

-    if (gMamaPayloads [payloadChar])

+    if (gImpl.myPayloads [(int)payloadChar])

     {

         mama_log (MAMA_LOG_LEVEL_NORMAL,

              "mama_loadPayloadBridge(): "

@@ -1765,7 +1789,8 @@

             return MAMA_STATUS_OK;

     }

-    gMamaPayloads [payloadChar] = *impl;

+    gImpl.myPayloads [(int)payloadChar] = *impl;

+    gImpl.myPayloadLibraries [(int)payloadChar] = bridgeLib;

     if (!gDefaultPayload)

     {

@@ -1804,14 +1829,15 @@

     LIB_HANDLE          bridgeLib       = NULL;

     bridge_createImpl   initFunc        = NULL;

     char*                                                                payloadName                    = NULL;

-    char                                                  payloadId                            = NULL;

+    char                                                 payloadId                            = '\0';

     mama_status                               result                                    = MAMA_STATUS_OK;

-    mamaMiddleware      middleware      =

-                    mamaMiddleware_convertFromString (middlewareName);

+    mamaMiddleware      middleware      = 0;

     if (!impl)

         return MAMA_STATUS_NULL_ARG;

+    middleware = mamaMiddleware_convertFromString (middlewareName);

+

     if (middleware >= MAMA_MIDDLEWARE_MAX)

     {

         mama_log (MAMA_LOG_LEVEL_ERROR,

@@ -1820,9 +1846,9 @@

     }

     /* Check if a bridge has already been initialized for the middleware */

-    if (gMamaBridges [middleware])

+    if (gImpl.myBridges [middleware])

     {

-        *impl = gMamaBridges [middleware];

+        *impl = gImpl.myBridges [middleware];

         return MAMA_STATUS_OK;

     }

@@ -1844,7 +1870,6 @@

                 bridgeImplName ? bridgeImplName : "",

                 getLibError());

         }

-

         else

         {

                 mama_log (MAMA_LOG_LEVEL_ERROR,

@@ -1889,18 +1914,19 @@

     result = ((mamaBridgeImpl*)(*impl))->bridgeOpen (*impl);

-    if (MAMA_STATUS_OK != result) return result;

+    if (MAMA_STATUS_OK != result)

+        return result;

     if (((mamaBridgeImpl*)(*impl))->bridgeGetDefaultPayloadId(&payloadName, &payloadId) == MAMA_STATUS_OK)

     {

-                              if (!gMamaPayloads [(uint8_t)payloadId])

+                             if (!gImpl.myPayloads [(uint8_t)payloadId])

                               {

                                               mamaPayloadBridge payloadImpl;

                                               mama_loadPayloadBridge (&payloadImpl,payloadName);

                               }

     }

-    gMamaBridges [middleware] = *impl;

+    gImpl.myBridges [middleware] = *impl;

     return MAMA_STATUS_OK;

}

 

Signed-off-by: John Gray <jgray@...>