Skip to content

Instantly share code, notes, and snippets.

@krakjoe
Created September 21, 2015 16:21
Show Gist options
  • Save krakjoe/10e1d6eceb170b7d00b0 to your computer and use it in GitHub Desktop.
Save krakjoe/10e1d6eceb170b7d00b0 to your computer and use it in GitHub Desktop.
forcing correctness ?
diff --git a/php_pthreads.c b/php_pthreads.c
index e0f3e40..51d84ca 100644
--- a/php_pthreads.c
+++ b/php_pthreads.c
@@ -104,6 +104,32 @@ zend_class_entry *spl_ce_RuntimeException;
ZEND_DECLARE_MODULE_GLOBALS(pthreads)
+typedef struct _pthreads_supported_sapi_t {
+ const char *name;
+ size_t nlen;
+} pthreads_supported_sapi_t;
+
+const static pthreads_supported_sapi_t supported[] = {
+ {ZEND_STRL("cli")},
+ {ZEND_STRL("phpdbg")}, /* not really supported, needs work */
+ {NULL, 0}
+};
+
+static inline zend_bool pthreads_is_supported_sapi(char *name) {
+ zend_long nlen = strlen(name);
+ const pthreads_supported_sapi_t *sapi = supported;
+
+ while (sapi->name) {
+ if (nlen == sapi->nlen &&
+ memcmp(sapi->name, name, nlen) == SUCCESS) {
+ return 1;
+ }
+ sapi++;
+ }
+
+ return 0;
+}
+
static inline void pthreads_globals_ctor(zend_pthreads_globals *pg TSRMLS_DC) {
ZVAL_UNDEF(&pg->this);
pg->pid = 0L;
@@ -165,6 +191,12 @@ PHP_MINIT_FUNCTION(pthreads)
{
zend_class_entry ce;
+ if (!pthreads_is_supported_sapi(sapi_module.name)) {
+ zend_error(E_ERROR, "The %s SAPI is not supported by pthreads",
+ sapi_module.name);
+ return FAILURE;
+ }
+
REGISTER_LONG_CONSTANT("PTHREADS_INHERIT_ALL", PTHREADS_INHERIT_ALL, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("PTHREADS_INHERIT_NONE", PTHREADS_INHERIT_NONE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("PTHREADS_INHERIT_INI", PTHREADS_INHERIT_INI, CONST_CS | CONST_PERSISTENT);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment