Permalink
Browse files

PHPEMBED: lock the initialization

Summary: there is a slim chance of a race condition, so add a mutex

Reviewed By: marc

Test Plan: I was able to repro this pretty consistently using a ton of threads
just shutting down and recreating the global php object. Since making this
change I haven't had a crash

Revert Plan: yup




git-svn-id: svn+ssh://tubbs/svnroot/projects/phpembed/trunk@68190 2c7ba8d8-a2f7-0310-a573-de162e16dcc7
  • Loading branch information...
1 parent e179fc1 commit 449d6f9fd2c01422434f9fb276873d86520a3592 boz committed Nov 6, 2007
Showing with 18 additions and 2 deletions.
  1. +6 −2 src/php_cxx.cpp
  2. +12 −0 src/php_cxx.h
View
@@ -932,19 +932,19 @@ php::~php()
POP_CTX();
#ifdef ZTS
tsrm_mutex_free(lock);
-#endif
p.clients--;
if(p.clients == 0 && p.initialized == true){
p.initialized = false;
+#endif
p.message_function = p.error_function = p.output_function = NULL;
TSRMLS_FETCH();
php_module_shutdown(TSRMLS_C);
sapi_shutdown();
#ifdef ZTS
tsrm_shutdown();
-#endif
}
+#endif
}
php::php(bool _type_warnings)
@@ -994,10 +994,14 @@ php::php(bool _type_warnings)
int php::init_global_php(){
// PHPE: lots of objects will use this one initialized php instance
+#ifdef ZTS
+ tsrm_mutex_lock(p.init_lock);
p.clients++;
if(p.initialized == true)
return SUCCESS;
p.initialized = true;
+ tsrm_mutex_unlock(p.init_lock);
+#endif
// set up the callbacks
php_embed_module.sapi_error = error_wrap;
View
@@ -34,15 +34,27 @@ class php_const
public:
php_const() {
message_function = error_function = output_function = NULL;
+#ifdef ZTS
initialized = false;
clients = 0;
+ init_lock = tsrm_mutex_alloc();
+#endif
+ }
+
+ ~php_const(){
+#ifdef ZTS
+ tsrm_mutex_free(init_lock);
+#endif
}
void (*message_function)(const char *);
void (*error_function)(const char *);
void (*output_function)(const char *);
+#ifdef ZTS
bool initialized;
int clients;
+ MUTEX_T init_lock;
+#endif
};
// This is the global constants variable we define for the reasons listed above

0 comments on commit 449d6f9

Please sign in to comment.