Commits

Ruslan Osmanov committed 562dc15

Add: evtimer_pending, event_pending, event_base_got_break, event_base_got_exit, event_base_gettimeofday_cached, event_base_update_cache_time,
Add: event_free, event_base_free (complete 'nop's! for bw compat.)

Comments (0)

Files changed (4)

             tv.tv_usec = (long) ((t - tv.tv_sec) * 1e6); \
         } while (0)
 
+#define PHP_EVENT_TIMEVAL_TO_DOUBLE(tv) (tv.tv_sec + tv.tv_usec * 1e-6)
+
+
 /* {{{ Private functions */
 
 /* {{{ fatal_error_cb
 /* }}} */
 
 /* {{{ proto bool evtimer_set(resource event, resource base, callable cb[, zval arg = NULL]);
- * Re-configures timer event */
+ * Re-configures timer event.
+ * Note, this function doesn't invoke obsolete libevent's event_set. It calls event_assign instead. */
 PHP_FUNCTION(evtimer_set)
 {
 	zval                  *zbase;
 }
 /* }}} */
 
+/* {{{ proto bool evtimer_pending(resource event);
+ * Detect whether timer event is pending or scheduled. */
+PHP_FUNCTION(evtimer_pending)
+{
+	zval        *zevent;
+	php_event_t *e;
+
+	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r",
+				&zevent) == FAILURE) {
+		return;
+	}
+
+	PHP_EVENT_FETCH_EVENT(e, zevent);
+
+	if (timer_is_pending(e->event)) {
+		RETURN_TRUE;
+	}
+	RETVAL_FALSE;
+	
+}
+/* }}} */
+
 
 /* {{{ proto bool event_set(resource event, resource base, mixed fd,[ int what = NULL[, callable cb = NULL[, zval arg = NULL]]]);
- * Re-configures event */
+ * Re-configures event.
+ * Note, this function doesn't invoke obsolete libevent's event_set. It calls event_assign instead.  */
 PHP_FUNCTION(event_set)
 {
 	zval                   *zbase;
 }
 /* }}} */
 
+/* {{{ proto bool event_pending(resource event, int flags);
+ *  Detect whether event is pending or scheduled. */
+PHP_FUNCTION(event_pending)
+{
+	zval        *zevent;
+	php_event_t *e;
+	long         flags;
+
+	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl",
+				&zevent, &flags) == FAILURE) {
+		return;
+	}
+
+	PHP_EVENT_FETCH_EVENT(e, zevent);
+
+	if (event_pending(e->event, flags, NULL)) {
+		RETURN_TRUE;
+	}
+	RETVAL_FALSE;
+}
+/* }}} */
+
+/* {{{ proto event_free(resource event);
+ * Does nothing! Exists for compatibility with scripts that used libevent ext. */
+PHP_FUNCTION(event_free)
+{
+}
+/* }}} */
 
 
 /* {{{ proto resource event_base_new(void);
 }
 /* }}} */
 
+/* {{{ proto bool event_base_got_break(resource base);
+ * Checks if the event loop was told to abort immediately by <function>event_loopbreak</function> */
+PHP_FUNCTION(event_base_got_break)
+{
+	zval             *zbase;
+	php_event_base_t *base;
+
+	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r",
+				&zbase) == FAILURE) {
+		return;
+	}
+
+	PHP_EVENT_FETCH_BASE(base, zbase);
+
+	if (event_base_got_break(base)) {
+		RETURN_TRUE;
+	}
+	RETVAL_FALSE;
+}
+/* }}} */
+
+/* {{{ proto bool event_base_got_exit(resource base);
+ * Checks if the event loop was told to exit by <function>event_loopexit</function> */
+PHP_FUNCTION(event_base_got_exit)
+{
+	zval             *zbase;
+	php_event_base_t *base;
+
+	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r",
+				&zbase) == FAILURE) {
+		return;
+	}
+
+	PHP_EVENT_FETCH_BASE(base, zbase);
+
+	if (event_base_got_exit(base)) {
+		RETURN_TRUE;
+	}
+	RETVAL_FALSE;
+}
+/* }}} */
+
+/* {{{ proto double event_base_gettimeofday_cached(resource base);
+ * On success returns the current time(as returned by gettimeofday()), looking
+ * at the cached value in 'base' if possible, and calling gettimeofday() or
+ * clock_gettime() as appropriate if there is no cached time. On failure
+ * returns NULL. */
+PHP_FUNCTION(event_base_gettimeofday_cached)
+{
+	zval                  *zbase;
+	php_event_base_t      *base;
+	struct timeval         tv;
+
+	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r",
+				&zbase) == FAILURE) {
+		return;
+	}
+
+	PHP_EVENT_FETCH_BASE(base, zbase);
+
+	if (event_base_gettimeofday_cached(base, &tv)) {
+		RETURN_NULL();
+	}
+	
+	RETVAL_DOUBLE(PHP_EVENT_TIMEVAL_TO_DOUBLE(tv));
+}
+/* }}} */
+
+#if LIBEVENT_VERSION_NUMBER >= 0x02010100
+/* {{{ proto bool event_base_update_cache_time(resource base);
+ * Updates cache time. Available since libevent 2.1.1-alpha */
+PHP_FUNCTION(event_base_update_cache_time)
+{
+	zval             *zbase;
+	php_event_base_t *base;
+
+	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r",
+				&zbase) == FAILURE) {
+		return;
+	}
+
+	PHP_EVENT_FETCH_BASE(base, zbase);
+
+	if (event_base_update_cache_time(base)) {
+		RETURN_FALSE;
+	}
+	RETVAL_TRUE;
+}
+/* }}} */
+#endif
+
+/* {{{ proto event_base_free(resource base);
+ * Does nothing! Exists for compatibility with scripts that used libevent ext. */
+PHP_FUNCTION(event_base_free)
+{
+}
+/* }}} */
+
+
 
 /* {{{ proto resource event_config_new(void);
  * On success returns a valid resource representing an event configuration
 	ZEND_ARG_INFO(0, priority)
 ZEND_END_ARG_INFO();
 
+ZEND_BEGIN_ARG_INFO_EX(arginfo_event_pending, 0, 0, 2)
+	ZEND_ARG_INFO(0, event)
+	ZEND_ARG_INFO(0, flags)
+ZEND_END_ARG_INFO();
+
 ZEND_BEGIN_ARG_INFO_EX(arginfo_evtimer_new, 0, 0, 2)
 	ZEND_ARG_INFO(0, base)
 	ZEND_ARG_INFO(0, cb)
 	PHP_FE(event_remove_timer, arginfo_event_1)
 #endif
 	PHP_FE(event_priority_set, arginfo_event_priority_set)
+	PHP_FE(event_pending, arginfo_event_pending)
 	PHP_FE(event_get_supported_methods, arginfo_event__void)
+	PHP_FE(event_free, arginfo_event_1)
 
 	PHP_FE(evtimer_new, arginfo_evtimer_new)
 	PHP_FE(evtimer_set, arginfo_evtimer_set)
+	PHP_FE(evtimer_pending, arginfo_event_1)
 
 	PHP_FE(event_base_new, arginfo_event__void)
 	PHP_FE(event_base_new_with_config, arginfo_event_base_config_1)
 	PHP_FE(event_base_dispatch, arginfo_event_base_1)
 	PHP_FE(event_base_loopexit, arginfo_event_base_loopexit)
 	PHP_FE(event_base_loopbreak, arginfo_event_base_1)
+	PHP_FE(event_base_got_break, arginfo_event_base_1)
+	PHP_FE(event_base_got_exit, arginfo_event_base_1)
+	PHP_FE(event_base_gettimeofday_cached, arginfo_event_base_1)
+#if LIBEVENT_VERSION_NUMBER >= 0x02010100
+	PHP_FE(event_base_update_cache_time, arginfo_event_base_1)
+#endif
+	PHP_FE(event_base_free, arginfo_event_base_1)
 
 	PHP_FE(event_config_new, arginfo_event__void)
 	PHP_FE(event_config_avoid_method, arginfo_event_config_avoid_method)
 PHP_FUNCTION(event_del);
 PHP_FUNCTION(event_remove_timer);
 PHP_FUNCTION(event_priority_set);
+PHP_FUNCTION(event_pending);
+PHP_FUNCTION(event_free);
 
 PHP_FUNCTION(evtimer_new);
 PHP_FUNCTION(evtimer_set);
+PHP_FUNCTION(evtimer_pending);
 
 PHP_FUNCTION(event_base_new);
 PHP_FUNCTION(event_base_new_with_config);
 PHP_FUNCTION(event_base_loopexit);
 PHP_FUNCTION(event_base_loopbreak);
 PHP_FUNCTION(event_base_dispatch);
+PHP_FUNCTION(event_base_got_break);
+PHP_FUNCTION(event_base_got_exit);
+PHP_FUNCTION(event_base_gettimeofday_cached);
+#if LIBEVENT_VERSION_NUMBER >= 0x02010100
+PHP_FUNCTION(event_base_update_cache_time);
+#endif
+PHP_FUNCTION(event_base_free);
 
 PHP_FUNCTION(event_get_supported_methods);
 

tests/03-event-del.phpt

 <?php 
 $base = event_base_new();
 
-$e1 = evtimer_new($base, function () { echo "not ok 1\n"; });
+$e1 = evtimer_new($base, function () { echo "not ok 3\n"; });
 event_add($e1, 0.1);
 
-$e2 = evtimer_new($base, function () { echo "ok 1\n"; });
+$e2 = evtimer_new($base, function () { echo "ok 3\n"; });
 event_add($e2, 0.2);
 
+evtimer_pending($e1) and print("ok 1\n");
+evtimer_pending($e2) and print("ok 2\n");
+
 event_del($e1);
+evtimer_pending($e1) and print("not ok 4\n");
+
 event_base_loop($base, EVENT_LOOP_ONCE);
 
-evtimer_set($e1, $base, function() { echo "ok 2\n"; });
+evtimer_set($e1, $base, function() { echo "ok 4\n"; });
 event_add($e1, 0.3);
 event_base_loop($base, EVENT_LOOP_ONCE);
 
 --EXPECT--
 ok 1
 ok 2
+ok 3
+ok 4