Ruslan Osmanov avatar Ruslan Osmanov committed 789c216

Add: ev_backend, ev_resume, ev_suspend,ev_now_update, EvLoop::backend()
Add: examples with EvEmbed

Comments (0)

Files changed (6)

 	Connection: close
 
 
+EMBEDDING ONE LOOP INTO ANOTHER
+-------------------------------
+
+*Example 1*
+
+	<?php
+	/*
+ 	 * Try to get an embeddable event loop and embed it into the default event loop. 
+ 	 * If it is impossible, use the default
+ 	 * loop. The default loop is stored in $loop_hi, while the embeddable loop is 
+ 	 * stored in $loop_lo(which is $loop_hi in the case no embeddable loop can be 
+ 	 * used).
+ 	 * 
+ 	 * Sample translated to PHP
+ 	 * http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod#Examples_CONTENT-9
+ 	 */
+	$loop_hi = EvLoop::defaultLoop();
+	$loop_lo = NULL;
+	$embed   = NULL;
+
+	/*
+	* See if there is a chance of getting one that works
+	* (flags' value of 0 means autodetection)
+	*/
+	$loop_lo = ev_embeddable_backends() & ev_recommended_backends()
+    	? new EvLoop(ev_embeddable_backends() & ev_recommended_backends())
+    	: 0;
+
+	if ($loop_lo) {
+		$embed = new EvEmbed($loop_lo, function () {});
+	} else {
+		$loop_lo = $loop_hi;
+	}
+	?>
+
+*Example 2*
+
+	<?php
+	/*
+ 	 * Check if kqueue is available but not recommended and create a kqueue backend 
+ 	 * for use with sockets (which usually work with any kqueue implementation). 
+ 	 * Store the kqueue/socket-only event loop in loop_socket. (One might optionally 
+ 	 * use EVFLAG_NOENV, too)
+ 	 *
+ 	 * Example borrowed from
+ 	 * http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod#Examples_CONTENT-9
+ 	 */
+	$loop        = EvLoop::defaultLoop();
+	$socket_loop = NULL;
+	$embed       = NULL;
+
+	if (ev_supported_backends() & ~ev_recommended_backends() & EVBACKEND_KQUEUE) {
+		if (($socket_loop = new EvLoop(EVBACKEND_KQUEUE))) {
+			$embed = new EvEmbed($loop);
+		}
+	}
+
+	if (!$socket_loop) {
+		$socket_loop = $loop; 
+	}
+
+	// Now use $socket_loop for all sockets, and $loop for anything else
+	?>
+
 SIGNALS
 -------
 
 	}
 	?>
 
+
 AUTHORS
 =======
 
 	loop_other_ptr = PHP_EV_LOOP_FETCH_FROM_OBJECT(o_loop_other);
 
 	if (!(ev_backend(loop_other_ptr) & ev_embeddable_backends())) {
-        php_error_docref(NULL TSRMLS_CC, E_ERROR,
+        php_error_docref(NULL TSRMLS_CC, E_WARNING,
         		"Passed loop is not embeddable. Check out your backends.");
         return;
 	}
 	PHP_EV_REGISTER_LONG_CONSTANT(EV_CUSTOM);
 	PHP_EV_REGISTER_LONG_CONSTANT(EV_ERROR);
 
+	PHP_EV_REGISTER_LONG_CONSTANT(EVBACKEND_SELECT);
+	PHP_EV_REGISTER_LONG_CONSTANT(EVBACKEND_POLL);
+	PHP_EV_REGISTER_LONG_CONSTANT(EVBACKEND_EPOLL);
+	PHP_EV_REGISTER_LONG_CONSTANT(EVBACKEND_KQUEUE);
+	PHP_EV_REGISTER_LONG_CONSTANT(EVBACKEND_DEVPOLL);
+	PHP_EV_REGISTER_LONG_CONSTANT(EVBACKEND_PORT);
+	PHP_EV_REGISTER_LONG_CONSTANT(EVBACKEND_ALL);
+	PHP_EV_REGISTER_LONG_CONSTANT(EVBACKEND_MASK);
+
 #if !defined(_WIN32) && !defined(_MINIX)
 	pthread_atfork(0, 0, php_ev_default_fork);
 #endif
                                                                                   \
         RETURN_LONG(ev_ ## name(PHP_EV_LOOP_FETCH_FROM_OBJECT(ev_obj)));          \
     }
+
+#define PHP_EV_LOOP_FUNC_VOID_VOID(name)                                          \
+    PHP_FUNCTION(ev_ ## name)                                                     \
+    {                                                                             \
+        zval          *zloop;                                                     \
+        php_ev_object *ev_obj;                                                    \
+                                                                                  \
+        if (zend_parse_parameters_none() == FAILURE) {                            \
+            return;                                                               \
+        }                                                                         \
+                                                                                  \
+        zloop  = php_ev_default_loop(TSRMLS_C);                                   \
+        ev_obj = (php_ev_object *) zend_object_store_get_object(zloop TSRMLS_CC); \
+                                                                                  \
+        PHP_EV_CONSTRUCT_CHECK(ev_obj);                                           \
+                                                                                  \
+        ev_ ## name(PHP_EV_LOOP_FETCH_FROM_OBJECT(ev_obj));                       \
+    }
 /* }}} */
 
 PHP_EV_FUNC_INT_VOID(supported_backends)
 }
 /* }}} */
 
+/* {{{ proto int ev_backend(void) */
+PHP_EV_LOOP_FUNC_INT_VOID(backend)
+/* }}} */
+
+/* {{{ proto void ev_now_update(void) */
+PHP_EV_LOOP_FUNC_VOID_VOID(now_update)
+/* }}} */
+
+/* {{{ proto void ev_suspend(void) */
+PHP_EV_LOOP_FUNC_VOID_VOID(suspend)
+/* }}} */
+
+/* {{{ proto void ev_resume(void) */
+PHP_EV_LOOP_FUNC_VOID_VOID(resume)
+/* }}} */
+
+
 /* }}} */
 
 #endif /* HAVE_EV */
 	PHP_FE(ev_iteration,            arginfo_ev__void)
 	PHP_FE(ev_depth,                arginfo_ev__void)
 	PHP_FE(ev_verify,               arginfo_ev__void)
+	PHP_FE(ev_backend,              arginfo_ev__void)
 
 	{NULL, NULL, NULL}
 };
 	PHP_ME(EvLoop, nowUpdate,         arginfo_ev__void,             ZEND_ACC_PUBLIC)
 	PHP_ME(EvLoop, suspend,           arginfo_ev__void,             ZEND_ACC_PUBLIC)
 	PHP_ME(EvLoop, resume,            arginfo_ev__void,             ZEND_ACC_PUBLIC)
+	PHP_ME(EvLoop, backend,           arginfo_ev__void,             ZEND_ACC_PUBLIC)
 	PHP_ME(EvLoop, now,               arginfo_ev__void,             ZEND_ACC_PUBLIC)
 	PHP_ME(EvLoop, run,               arginfo_ev_run,               ZEND_ACC_PUBLIC)
 	PHP_ME(EvLoop, break,             arginfo_ev_break,             ZEND_ACC_PUBLIC)
 PHP_FUNCTION(ev_iteration);
 PHP_FUNCTION(ev_depth);
 PHP_FUNCTION(ev_verify);
+PHP_FUNCTION(ev_backend);
+PHP_FUNCTION(ev_now_update);
+PHP_FUNCTION(ev_suspend);
+PHP_FUNCTION(ev_resume);
 
 /* {{{ EvLoop */
 PHP_METHOD(EvLoop, __construct);
 PHP_METHOD(EvLoop, nowUpdate);
 PHP_METHOD(EvLoop, suspend);
 PHP_METHOD(EvLoop, resume);
+PHP_METHOD(EvLoop, backend);
 PHP_METHOD(EvLoop, now);
 PHP_METHOD(EvLoop, run);
 PHP_METHOD(EvLoop, break);
         ev_ ## evname(EV_A);                           \
     }
 
+#define PHP_EV_LOOP_METHOD_INT_VOID(name, evname)      \
+    PHP_METHOD(EvLoop, name)                           \
+    {                                                  \
+        PHP_EV_LOOP_FETCH_FROM_THIS;                   \
+                                                       \
+        if (zend_parse_parameters_none() == FAILURE) { \
+            return;                                    \
+        }                                              \
+                                                       \
+        RETURN_LONG(ev_ ## evname(EV_A));              \
+    }
+
 PHP_EV_LOOP_METHOD_VOID(loopFork, loop_fork)
 PHP_EV_LOOP_METHOD_VOID(verify, verify)
 PHP_EV_LOOP_METHOD_VOID(invokePending, invoke_pending)
 PHP_EV_LOOP_METHOD_VOID(nowUpdate, now_update)
 PHP_EV_LOOP_METHOD_VOID(suspend, suspend)
 PHP_EV_LOOP_METHOD_VOID(resume, resume)
+PHP_EV_LOOP_METHOD_INT_VOID(backend, backend)
 
 /* {{{ proto double EvLoop::now(void) */
 PHP_METHOD(EvLoop, now)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.