php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Return to Bug #55846
Patch shmop-cas revision 2011-10-04 16:11 UTC by brohreit at gmx dot net

Patch shmop-cas for *Extensibility Functions Bug #55846

Patch version 2011-10-04 16:11 UTC

Return to Bug #55846 | Download this patch
Patch Revisions:

Developer: brohreit@gmx.net

diff -Naur php-5.3.8/ext/shmop/php_shmop.h php-5.3.8-bj1/ext/shmop/php_shmop.h
--- php-5.3.8/ext/shmop/php_shmop.h	2011-01-01 03:19:59.000000000 +0100
+++ php-5.3.8-bj1/ext/shmop/php_shmop.h	2011-10-04 17:09:32.052844186 +0200
@@ -33,6 +33,7 @@
 PHP_FUNCTION(shmop_size);
 PHP_FUNCTION(shmop_write);
 PHP_FUNCTION(shmop_delete);
+PHP_FUNCTION(shmop_cas);
 
 #ifdef PHP_WIN32
 typedef int key_t;
diff -Naur php-5.3.8/ext/shmop/shmop.c php-5.3.8-bj1/ext/shmop/shmop.c
--- php-5.3.8/ext/shmop/shmop.c	2011-07-25 13:42:53.000000000 +0200
+++ php-5.3.8-bj1/ext/shmop/shmop.c	2011-10-04 17:09:32.052844186 +0200
@@ -76,6 +76,13 @@
 ZEND_BEGIN_ARG_INFO_EX(arginfo_shmop_delete, 0, 0, 1)
 	ZEND_ARG_INFO(0, shmid)
 ZEND_END_ARG_INFO()
+
+ZEND_BEGIN_ARG_INFO_EX(arginfo_shmop_cas, 0, 0, 4)
+	ZEND_ARG_INFO(0, shmid)
+	ZEND_ARG_INFO(0, offset)
+	ZEND_ARG_INFO(0, oldval)
+	ZEND_ARG_INFO(0, newval)
+ZEND_END_ARG_INFO()
 /* }}} */
 
 /* {{{ shmop_functions[] 
@@ -87,6 +94,7 @@
 	PHP_FE(shmop_size, 		arginfo_shmop_size)
 	PHP_FE(shmop_write, 	arginfo_shmop_write)
 	PHP_FE(shmop_delete, 	arginfo_shmop_delete)
+	PHP_FE(shmop_cas,		arginfo_shmop_cas)
 	PHP_FE_END
 };
 /* }}} */
@@ -365,6 +373,37 @@
 }
 /* }}} */
 
+/* {{{ proto int shmop_cas (int shmid, int offset, int oldval, int newval)
+   compare and swap */
+PHP_FUNCTION(shmop_cas)
+{
+	struct php_shmop *shmop;
+	int type;
+	long shmid, offset, oldval, newval;
+
+	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "llll", &shmid, &offset, &oldval, &newval) == FAILURE) {
+		return;
+	}
+
+	PHP_SHMOP_GET_RES
+
+	if ((shmop->shmatflg & SHM_RDONLY) == SHM_RDONLY) {
+		php_error_docref(NULL TSRMLS_CC, E_WARNING, "trying to write to a read only segment");
+		RETURN_FALSE;
+	}
+
+	if (offset < 0 || offset > shmop->size - sizeof(newval)) {
+		php_error_docref(NULL TSRMLS_CC, E_WARNING, "offset out of range");
+		RETURN_FALSE;
+	}
+
+	long *ptr = (long*)(shmop->addr + offset);
+	long retval = __sync_val_compare_and_swap(ptr, oldval, newval);
+
+	RETURN_LONG(retval);
+}
+/* }}} */
+
 #endif	/* HAVE_SHMOP */
 
 /*
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 06:01:30 2024 UTC