[PATCH 12/30] Added wthread_global_mutex macros


Michael Schonberg <mschonberg@...>
 

In preparation for windows support, replace global phread_mutex_t and
wthread_mutex_t inititialized with PTHREAD_MUTEX_INITIALIZER with
wthread_gobal_mtuex() macros. This is required because Windows critical
sections and mutexes do not have static initializers. They need to be
protected with atomic access to a global state variable.

The macros are simple for Linux:
WTHREAD_GLOBAL_MUTEX(NAME) initializes the mutex
wthread_mutex_(un)lock(name) locks and unlocks.

For Windows, the lock macro increments a global reference count to ensure
that the mutex only gets initialized once, and spins on a another variable
to wait until initialization complets. See windows/port.h (future check
in) for details.

Signed-off-by: Mike Schonberg <mschonberg@nyx.com>
---
common/c_cpp/src/c/linux/port.h | 7 +++++++
common/c_cpp/src/c/properties.l | 16 ++++++----------
2 files changed, 13 insertions(+), 10 deletions(-)

diff --git a/common/c_cpp/src/c/linux/port.h b/common/c_cpp/src/c/linux/port.h
index 00a1405..8b4b873 100644
--- a/common/c_cpp/src/c/linux/port.h
+++ b/common/c_cpp/src/c/linux/port.h
@@ -58,6 +58,13 @@ extern "C"
#define MAMAExpBridgeDLL
#define MAMACALLTYPE

+/* PTHREAD static locks are easy */
+typedef pthread_mutex_t wthread_static_mutex_t;
+#define WSTATIC_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
+#define wthread_static_mutex_lock(x) pthread_mutex_lock((x))
+#define wthread_static_mutex_unlock(x) pthread_mutex_unlock((x))
+
+
#define WCOMMONINLINE inline static
#define WCOMMONFORCEINLINE inline static

diff --git a/common/c_cpp/src/c/properties.l b/common/c_cpp/src/c/properties.l
index 555483b..3c327e3 100644
--- a/common/c_cpp/src/c/properties.l
+++ b/common/c_cpp/src/c/properties.l
@@ -22,19 +22,15 @@
/** This file should be processed by FLEX or LEX **/

%{
+#include "port.h"
#include "propertyinternal.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "wombat/wincompat.h"

-#ifndef WIN32
-#include <pthread.h>
-#endif
-
extern int gPropertyDebug;
-
-wthread_mutex_t lexerLock = PTHREAD_MUTEX_INITIALIZER;
+wthread_static_mutex_t lexerLock = WSTATIC_MUTEX_INITIALIZER;

%}

@@ -200,12 +196,12 @@ propertiesImpl_ImportPropertiesFromFile( propertiesImpl this,
return 0;
}

- wthread_mutex_lock (&lexerLock);
+ wthread_static_mutex_lock (&lexerLock);
yyrestart(f);
properties = this;
yylex();
properties = NULL;
- wthread_mutex_unlock (&lexerLock);
+ wthread_static_mutex_unlock (&lexerLock);

free( fullPath );
fclose( f );
@@ -220,7 +216,7 @@ propertiesImpl_AddString( propertiesImpl this,

if( gPropertyDebug ) fprintf( stderr, "Scanning property string %s\n", string );

- wthread_mutex_lock (&lexerLock);
+ wthread_static_mutex_lock (&lexerLock);

properties = this;
/* yy_scan_string changes the global lex stack buffer there lock until this
@@ -229,7 +225,7 @@ propertiesImpl_AddString( propertiesImpl this,
yylex();
yy_delete_buffer(buf);
properties = NULL;
- wthread_mutex_unlock (&lexerLock);
+ wthread_static_mutex_unlock (&lexerLock);

return 1;
}
--
1.7.7.6