Ruslan Osmanov avatar Ruslan Osmanov committed e15198b

Add: createStopped watchers' method
Change: start watchers automatically by default

Comments (0)

Files changed (24)

-Start watchers automatically by default
-=======================================
-In most cases watchers are started automatically.
-Getting rid of need to call ::start() method we skip the function call overhead!.
-
-
-Whether static class Ev, or \Ev namespace for functions/properties associated with
-default loop
-===================================================================================
-
-
 libevent buffer functionality?
 =============================
 
 #include "watcher.h"
 
 /* {{{ php_ev_check_object_ctor */
-void php_ev_check_object_ctor(INTERNAL_FUNCTION_PARAMETERS, zval *loop)
+void php_ev_check_object_ctor(INTERNAL_FUNCTION_PARAMETERS, zval *loop, zend_bool ctor, zend_bool start)
 {
 	zval                  *self;
 	php_ev_object         *o_self;
 	php_ev_object         *o_loop;
-	ev_check              *check_watcher;
+	ev_check              *w;
 
-	zval                  *data          = NULL;
-	zend_fcall_info        fci           = empty_fcall_info;
-	zend_fcall_info_cache  fcc           = empty_fcall_info_cache;
-	long                   priority      = 0;
+	zval                  *data     = NULL;
+	zend_fcall_info        fci      = empty_fcall_info;
+	zend_fcall_info_cache  fcc      = empty_fcall_info_cache;
+	long                   priority = 0;
 
 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "f|z!l",
 				&fci, &fcc, &data, &priority) == FAILURE) {
 		self = getThis();
 	}
 
-	o_self        = (php_ev_object *) zend_object_store_get_object(self TSRMLS_CC);
-	o_loop        = (php_ev_object *) zend_object_store_get_object(loop TSRMLS_CC);
-	check_watcher = (ev_check *) php_ev_new_watcher(sizeof(ev_check), self,
+	o_self = (php_ev_object *) zend_object_store_get_object(self TSRMLS_CC);
+	o_loop = (php_ev_object *) zend_object_store_get_object(loop TSRMLS_CC);
+	w      = (ev_check *) php_ev_new_watcher(sizeof(ev_check), self,
 			PHP_EV_LOOP_OBJECT_FETCH_FROM_OBJECT(o_loop),
 			&fci, &fcc, data, priority TSRMLS_CC);
 
-	check_watcher->type = EV_CHECK;
+	w->type = EV_CHECK;
 
-	o_self->ptr = (void *) check_watcher;
+	o_self->ptr = (void *) w;
+
+	if (start) {
+		PHP_EV_WATCHER_START(ev_check, w);
+	}
 }
 /* }}} */
 
 /* {{{ proto EvCheck::__construct(callable callback[, mixed data = NULL[, int priority = 0]]) */
 PHP_METHOD(EvCheck, __construct)
 {
-	php_ev_check_object_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, NULL);
+	PHP_EV_WATCHER_CTOR(check, NULL);
+}
+/* }}} */
+
+/* {{{ proto EvCheck::createStopped(callable callback[, mixed data = NULL[, int priority = 0]]) */
+PHP_METHOD(EvCheck, createStopped)
+{
+	PHP_EV_WATCHER_FACTORY_NS(check, NULL);
 }
 /* }}} */
 
 #include "watcher.h"
 
 /* {{{ php_ev_child_object_ctor */
-void php_ev_child_object_ctor(INTERNAL_FUNCTION_PARAMETERS, zval *loop)
+void php_ev_child_object_ctor(INTERNAL_FUNCTION_PARAMETERS, zval *loop, zend_bool ctor, zend_bool start)
 {
 	long                   pid;
 	zend_bool              trace;
 	zval                  *self;
 	php_ev_object         *o_self;
 	php_ev_object         *o_loop;
-	ev_child              *child_watcher;
+	ev_child              *w;
 
 	zval                  *data          = NULL;
 	zend_fcall_info        fci           = empty_fcall_info;
 		return;
 	}
 
-	/* If loop is NULL, then we're in __construct() */
-	if (loop) {
+	if (ctor) {
+		self = getThis();
+	} else {
 		PHP_EV_INIT_CLASS_OBJECT(return_value, ev_child_class_entry_ptr);
 		self = return_value; 
-	} else {
+	}
+
+	if (!loop) {
 		loop = php_ev_default_loop(TSRMLS_C);
-		self = getThis();
 	}
 
-	o_self        = (php_ev_object *) zend_object_store_get_object(self TSRMLS_CC);
-	o_loop        = (php_ev_object *) zend_object_store_get_object(loop TSRMLS_CC);
-	child_watcher = (ev_child *) php_ev_new_watcher(sizeof(ev_child), self,
+	o_self = (php_ev_object *) zend_object_store_get_object(self TSRMLS_CC);
+	o_loop = (php_ev_object *) zend_object_store_get_object(loop TSRMLS_CC);
+	w      = (ev_child *) php_ev_new_watcher(sizeof(ev_child), self,
 			PHP_EV_LOOP_OBJECT_FETCH_FROM_OBJECT(o_loop),
 			&fci, &fcc, data, priority TSRMLS_CC);
 
-	child_watcher->type = EV_CHILD;
+	w->type = EV_CHILD;
 	
-	ev_child_set(child_watcher, pid, trace);
+	ev_child_set(w, pid, trace);
 
-	o_self->ptr = (void *) child_watcher;
+	o_self->ptr = (void *) w;
 
+	if (start) {
+		PHP_EV_WATCHER_START(ev_child, w);
+	}
 }
 /* }}} */
 
 /* {{{ proto EvChild::__construct(int pid, bool trace, callable callback[, mixed data = NULL[, int priority = 0]]) */
 PHP_METHOD(EvChild, __construct)
 {
-	php_ev_child_object_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, NULL);
+	PHP_EV_WATCHER_CTOR(child, NULL);
+}
+/* }}} */
+
+/* {{{ proto EvChild::createStopped(int pid, bool trace, callable callback[, mixed data = NULL[, int priority = 0]]) */
+PHP_METHOD(EvChild, createStopped)
+{
+	PHP_EV_WATCHER_FACTORY_NS(child, NULL);
 }
 /* }}} */
 
 {
 	long       pid;
 	zend_bool  trace;
-	ev_child  *child_watcher;
+	ev_child  *w;
 
 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "bl",
 				&pid, &trace) == FAILURE) {
 		return;
 	}
 
-	child_watcher = (ev_child *) PHP_EV_WATCHER_FETCH_FROM_THIS();
+	w = (ev_child *) PHP_EV_WATCHER_FETCH_FROM_THIS();
 
-	PHP_EV_WATCHER_RESET(ev_child, child_watcher, (child_watcher, pid, trace));
+	PHP_EV_WATCHER_RESET(ev_child, w, (w, pid, trace));
 }
 /* }}} */
 
 #include "watcher.h"
 
 /* {{{ php_ev_embed_object_ctor */
-void php_ev_embed_object_ctor(INTERNAL_FUNCTION_PARAMETERS, zval *loop)
+void php_ev_embed_object_ctor(INTERNAL_FUNCTION_PARAMETERS, zval *loop, zend_bool ctor, zend_bool start)
 {
-	zval                  *self = NULL;
+	zval                  *self;
 	php_ev_object         *o_self;
 	php_ev_object         *o_loop;
 	php_ev_object         *o_loop_other;
-	ev_embed              *embed_watcher;
+	ev_embed              *w;
 	ev_loop               *loop_other_ptr;
 	php_ev_embed          *embed_ptr;
 
 
 	PHP_EV_ASSERT(loop_other_ptr);
 
-	/* If loop is NULL, then we're in __construct() */
-	if (loop) {
+	if (ctor) {
+		self = getThis();
+	} else {
 		PHP_EV_INIT_CLASS_OBJECT(return_value, ev_embed_class_entry_ptr);
-
-		PHP_EV_ASSERT((self == NULL));
 		self = return_value; 
-	} else {
+	}
+
+	if (!loop) {
 		loop = php_ev_default_loop(TSRMLS_C);
-		self = getThis();
 	}
 
 	embed_ptr = (php_ev_embed *) emalloc(sizeof(php_ev_embed));
 	memset(embed_ptr, 0, sizeof(php_ev_embed));
 
-	embed_watcher = &embed_ptr->embed;
+	w = &embed_ptr->embed;
 
-	o_self        = (php_ev_object *) zend_object_store_get_object(self TSRMLS_CC);
-	o_loop        = (php_ev_object *) zend_object_store_get_object(loop TSRMLS_CC);
+	o_self = (php_ev_object *) zend_object_store_get_object(self TSRMLS_CC);
+	o_loop = (php_ev_object *) zend_object_store_get_object(loop TSRMLS_CC);
 
-	php_ev_set_watcher((ev_watcher *)embed_watcher, sizeof(ev_embed), self,
+	php_ev_set_watcher((ev_watcher *)w, sizeof(ev_embed), self,
 			PHP_EV_LOOP_OBJECT_FETCH_FROM_OBJECT(o_loop),
 			&fci, &fcc, data, priority TSRMLS_CC);
 
-	embed_watcher->type = EV_EMBED;
+	w->type = EV_EMBED;
 
-	ev_embed_set(embed_watcher, PHP_EV_LOOP_FETCH_FROM_OBJECT(o_loop_other));
+	ev_embed_set(w, PHP_EV_LOOP_FETCH_FROM_OBJECT(o_loop_other));
 
 	o_self->ptr = (void *) embed_ptr;
+
+	if (start) {
+		PHP_EV_WATCHER_START(ev_embed, w);
+	}
 }
 /* }}} */
 
 /* {{{ proto EvEmbed::__construct(EvLoop other, callable callback[, mixed data = NULL[, int priority = 0]]) */
 PHP_METHOD(EvEmbed, __construct)
 {
-	php_ev_embed_object_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, NULL);
+	PHP_EV_WATCHER_CTOR(embed, NULL);
+}
+/* }}} */
+
+/* {{{ proto EvEmbed::createStopped(EvLoop other, callable callback[, mixed data = NULL[, int priority = 0]]) */
+PHP_METHOD(EvEmbed, createStopped)
+{
+	PHP_EV_WATCHER_FACTORY_NS(embed, NULL);
 }
 /* }}} */
 
 PHP_METHOD(EvEmbed, set)
 {
 	zval          *loop_other;
-	ev_embed      *embed_watcher;
+	ev_embed      *w;
 	php_ev_object *o_loop_other;
 	ev_loop       *loop_other_ptr;
 
         return;
 	}
 
-	embed_watcher  = (ev_embed *) PHP_EV_WATCHER_FETCH_FROM_THIS();
+	w  = (ev_embed *) PHP_EV_WATCHER_FETCH_FROM_THIS();
 
 	PHP_EV_ASSERT(PHP_EV_LOOP_FETCH_FROM_OBJECT(o_loop_other));
-	PHP_EV_WATCHER_RESET(ev_embed, embed_watcher, (embed_watcher, PHP_EV_LOOP_FETCH_FROM_OBJECT(o_loop_other)));
+	PHP_EV_WATCHER_RESET(ev_embed, w, (w, PHP_EV_LOOP_FETCH_FROM_OBJECT(o_loop_other)));
 }
 /* }}} */
 
 /* {{{ proto void EvEmbed::sweep(void) */
 PHP_METHOD(EvEmbed, sweep)
 {
-	ev_embed *embed_watcher;
+	ev_embed *w;
 
 	if (zend_parse_parameters_none() == FAILURE) {
 		return;
 	}
 
-	embed_watcher = (ev_embed *) PHP_EV_WATCHER_FETCH_FROM_THIS();
+	w = (ev_embed *) PHP_EV_WATCHER_FETCH_FROM_THIS();
 
-	ev_embed_sweep(php_ev_watcher_loop_ptr(embed_watcher), embed_watcher);
+	ev_embed_sweep(php_ev_watcher_loop_ptr(w), w);
 }
 /* }}} */
 
  * This is a helper for derived watcher class objects. */
 static void php_ev_watcher_free_storage(ev_watcher *ptr TSRMLS_DC)
 {
-	zval       *data;
-	ev_watcher *w_next  , *w_prev;
+	zval        *data;
+	ev_watcher  *w_next  , *w_prev;
+	php_ev_loop *o_loop;
 
 	/* What if we do it in php_ev_loop_free_storage()?*/
 	php_ev_stop_watcher(ptr TSRMLS_CC);
 		php_ev_watcher_prev(w_next) = w_prev;
 	}
 
+	o_loop = php_ev_watcher_loop(ptr);
+
+	if (o_loop && o_loop->w == ptr) {
+		o_loop->w = NULL;
+	}
+
 #if 0
-	php_ev_loop *o_loop = php_ev_watcher_loop(ptr);
 	if (o_loop) {
 		ev_watcher *pw = o_loop->w;
 
 
 /* {{{ Global functions */
 
-#define PHP_EV_FUNC_INT_VOID(name)                      \
-    PHP_FUNCTION(ev_ ## name)                           \
-    {                                                   \
-        if (zend_parse_parameters_none() == FAILURE) {  \
-            return;                                     \
-        }                                               \
-                                                        \
-        RETURN_LONG((long)ev_ ## name());               \
+#define PHP_EV_FUNC_INT_VOID(name)                                                \
+    PHP_FUNCTION(ev_ ## name)                                                     \
+    {                                                                             \
+        if (zend_parse_parameters_none() == FAILURE) {                            \
+            return;                                                               \
+        }                                                                         \
+                                                                                  \
+        RETURN_LONG((long) ev_ ## name());                                        \
+    }
+
+#define PHP_EV_LOOP_FUNC_INT_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);                                           \
+                                                                                  \
+        RETURN_LONG(ev_ ## name(PHP_EV_LOOP_FETCH_FROM_OBJECT(ev_obj)));          \
     }
+/* }}} */
 
 PHP_EV_FUNC_INT_VOID(supported_backends)
 PHP_EV_FUNC_INT_VOID(recommended_backends)
 {
 	long flags = 0;
 	zval *zloop;
+	php_ev_object *ev_obj;
 
 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &flags) == FAILURE) {
 		return;
 	}
 
-	zloop = php_ev_default_loop(TSRMLS_C);
+	zloop  = php_ev_default_loop(TSRMLS_C);
+	ev_obj = (php_ev_object *) zend_object_store_get_object(zloop TSRMLS_CC);
 
-	php_ev_object *ev_obj = (php_ev_object *) zend_object_store_get_object(zloop TSRMLS_CC);
 	PHP_EV_CONSTRUCT_CHECK(ev_obj);
 
 	ev_run(PHP_EV_LOOP_FETCH_FROM_OBJECT(ev_obj), flags);
 /* {{{ proto double ev_now(void) */
 PHP_FUNCTION(ev_now)
 {
-	zval           *zloop  = php_ev_default_loop(TSRMLS_C);
-	php_ev_object  *ev_obj;
+	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);
 
 	RETURN_DOUBLE((double) ev_now(PHP_EV_LOOP_FETCH_FROM_OBJECT(ev_obj)));
 }
 /* }}} */
 
+/* {{{ proto int ev_iteration(void) */
+PHP_EV_LOOP_FUNC_INT_VOID(iteration)
+/* }}} */
+
+/* {{{ proto int ev_depth(void) */
+PHP_EV_LOOP_FUNC_INT_VOID(depth)
+/* }}} */
+
+/* {{{ proto void ev_verify(void) */
+PHP_FUNCTION(ev_verify)
+{
+	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_verify(PHP_EV_LOOP_FETCH_FROM_OBJECT(ev_obj));
+}
+/* }}} */
+
 /* }}} */
 
 #endif /* HAVE_EV */
 	PHP_FE(ev_run,                  arginfo_ev_run)
 	PHP_FE(ev_now,                  arginfo_ev__void)
 	PHP_FE(ev_break,                arginfo_ev_break)
+	PHP_FE(ev_iteration,            arginfo_ev__void)
+	PHP_FE(ev_depth,                arginfo_ev__void)
+	PHP_FE(ev_verify,               arginfo_ev__void)
 
 	{NULL, NULL, NULL}
 };
 /* {{{ ev_loop_class_entry_functions */
 const zend_function_entry ev_loop_class_entry_functions[] = {
 	PHP_ME(EvLoop, __construct,       arginfo_ev_default_loop,      ZEND_ACC_PUBLIC  | ZEND_ACC_CTOR)
-	PHP_ME(EvLoop, default_loop,      arginfo_ev_default_loop,      ZEND_ACC_PUBLIC  | ZEND_ACC_STATIC)
+	PHP_ME(EvLoop, defaultLoop,       arginfo_ev_default_loop,      ZEND_ACC_PUBLIC  | ZEND_ACC_STATIC)
 	PHP_ME(EvLoop, loop_fork,         arginfo_ev__void,             ZEND_ACC_PUBLIC)
 	PHP_ME(EvLoop, verify,            arginfo_ev__void,             ZEND_ACC_PUBLIC)
 	PHP_ME(EvLoop, invoke_pending,    arginfo_ev__void,             ZEND_ACC_PUBLIC)
 
 /* {{{ ev_io_class_entry_functions */
 const zend_function_entry ev_io_class_entry_functions[] = {
-	PHP_ME(EvIo, __construct, arginfo_ev_io,     ZEND_ACC_PUBLIC  | ZEND_ACC_CTOR)
-	PHP_ME(EvIo, set,         arginfo_ev_io_set, ZEND_ACC_PUBLIC)
+	PHP_ME(EvIo, __construct,   arginfo_ev_io,     ZEND_ACC_PUBLIC  | ZEND_ACC_CTOR)
+	PHP_ME(EvIo, set,           arginfo_ev_io_set, ZEND_ACC_PUBLIC)
+	PHP_ME(EvIo, createStopped, arginfo_ev_io,     ZEND_ACC_PUBLIC  | ZEND_ACC_STATIC)
 	{ NULL, NULL, NULL }
 };
 /* }}} */
 
 /* {{{ ev_timer_class_entry_functions */
 const zend_function_entry ev_timer_class_entry_functions[] = {
-	PHP_ME(EvTimer, __construct, arginfo_ev_timer,     ZEND_ACC_PUBLIC  | ZEND_ACC_CTOR)
-	PHP_ME(EvTimer, set,         arginfo_ev_timer_set, ZEND_ACC_PUBLIC)
-	PHP_ME(EvTimer, again,       arginfo_ev__void,     ZEND_ACC_PUBLIC)
+	PHP_ME(EvTimer, __construct,   arginfo_ev_timer,     ZEND_ACC_PUBLIC  | ZEND_ACC_CTOR)
+	PHP_ME(EvTimer, set,           arginfo_ev_timer_set, ZEND_ACC_PUBLIC)
+	PHP_ME(EvTimer, again,         arginfo_ev__void,     ZEND_ACC_PUBLIC)
+	PHP_ME(EvTimer, createStopped, arginfo_ev_timer,     ZEND_ACC_PUBLIC  | ZEND_ACC_STATIC)
 
 	{ NULL, NULL, NULL }
 };
 #if EV_PERIODIC_ENABLE
 /* {{{ ev_periodic_class_entry_functions */
 const zend_function_entry ev_periodic_class_entry_functions[] = {
-	PHP_ME(EvPeriodic, __construct, arginfo_ev_periodic,     ZEND_ACC_PUBLIC  | ZEND_ACC_CTOR)
-	PHP_ME(EvPeriodic, set,         arginfo_ev_periodic_set, ZEND_ACC_PUBLIC)
-	PHP_ME(EvPeriodic, again,       arginfo_ev__void,        ZEND_ACC_PUBLIC)
-	PHP_ME(EvPeriodic, at,          arginfo_ev__void,        ZEND_ACC_PUBLIC)
+	PHP_ME(EvPeriodic, __construct,   arginfo_ev_periodic,     ZEND_ACC_PUBLIC  | ZEND_ACC_CTOR)
+	PHP_ME(EvPeriodic, set,           arginfo_ev_periodic_set, ZEND_ACC_PUBLIC)
+	PHP_ME(EvPeriodic, again,         arginfo_ev__void,        ZEND_ACC_PUBLIC)
+	PHP_ME(EvPeriodic, at,            arginfo_ev__void,        ZEND_ACC_PUBLIC)
+	PHP_ME(EvPeriodic, createStopped, arginfo_ev_periodic,     ZEND_ACC_PUBLIC  | ZEND_ACC_STATIC)
 
 	{ NULL, NULL, NULL }
 };
 #if EV_SIGNAL_ENABLE
 /* {{{ ev_signal_class_entry_functions */
 const zend_function_entry ev_signal_class_entry_functions[] = {
-	PHP_ME(EvSignal, __construct, arginfo_ev_signal,     ZEND_ACC_PUBLIC  | ZEND_ACC_CTOR)
-	PHP_ME(EvSignal, set,         arginfo_ev_signal_set, ZEND_ACC_PUBLIC)
+	PHP_ME(EvSignal, __construct,   arginfo_ev_signal,     ZEND_ACC_PUBLIC  | ZEND_ACC_CTOR)
+	PHP_ME(EvSignal, set,           arginfo_ev_signal_set, ZEND_ACC_PUBLIC)
+	PHP_ME(EvSignal, createStopped, arginfo_ev_signal,     ZEND_ACC_PUBLIC  | ZEND_ACC_STATIC)
 
 	{ NULL, NULL, NULL }
 };
 #if EV_CHILD_ENABLE
 /* {{{ ev_child_class_entry_functions */
 const zend_function_entry ev_child_class_entry_functions[] = {
-	PHP_ME(EvChild, __construct, arginfo_ev_child,     ZEND_ACC_PUBLIC  | ZEND_ACC_CTOR)
-	PHP_ME(EvChild, set,         arginfo_ev_child_set, ZEND_ACC_PUBLIC)
+	PHP_ME(EvChild, __construct,   arginfo_ev_child,     ZEND_ACC_PUBLIC  | ZEND_ACC_CTOR)
+	PHP_ME(EvChild, set,           arginfo_ev_child_set, ZEND_ACC_PUBLIC)
+	PHP_ME(EvChild, createStopped, arginfo_ev_child,     ZEND_ACC_PUBLIC  | ZEND_ACC_STATIC)
 
 	{ NULL, NULL, NULL }
 };
 #if EV_STAT_ENABLE
 /* {{{ ev_stat_class_entry_functions */
 const zend_function_entry ev_stat_class_entry_functions[] = {
-	PHP_ME(EvStat, __construct, arginfo_ev_stat,     ZEND_ACC_PUBLIC  | ZEND_ACC_CTOR)
-	PHP_ME(EvStat, set,         arginfo_ev_stat_set, ZEND_ACC_PUBLIC)
-	PHP_ME(EvStat, attr,        arginfo_ev__void,    ZEND_ACC_PUBLIC)
-	PHP_ME(EvStat, prev,        arginfo_ev__void,    ZEND_ACC_PUBLIC)
-	PHP_ME(EvStat, stat,        arginfo_ev__void,    ZEND_ACC_PUBLIC)
+	PHP_ME(EvStat, __construct,   arginfo_ev_stat,     ZEND_ACC_PUBLIC  | ZEND_ACC_CTOR)
+	PHP_ME(EvStat, set,           arginfo_ev_stat_set, ZEND_ACC_PUBLIC)
+	PHP_ME(EvStat, attr,          arginfo_ev__void,    ZEND_ACC_PUBLIC)
+	PHP_ME(EvStat, prev,          arginfo_ev__void,    ZEND_ACC_PUBLIC)
+	PHP_ME(EvStat, stat,          arginfo_ev__void,    ZEND_ACC_PUBLIC)
+	PHP_ME(EvStat, createStopped, arginfo_ev_stat,     ZEND_ACC_PUBLIC  | ZEND_ACC_STATIC)
 
 	{ NULL, NULL, NULL }
 };
 #if EV_IDLE_ENABLE
 /* {{{ ev_idle_class_entry_functions */
 const zend_function_entry ev_idle_class_entry_functions[] = {
-	PHP_ME(EvIdle, __construct, arginfo_ev_idle, ZEND_ACC_PUBLIC  | ZEND_ACC_CTOR)
+	PHP_ME(EvIdle, __construct,   arginfo_ev_idle, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
+	PHP_ME(EvIdle, createStopped, arginfo_ev_idle, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
 	{ NULL, NULL, NULL }
 };
 /* }}} */
 #if EV_CHECK_ENABLE
 /* {{{ ev_check_class_entry_functions */
 const zend_function_entry ev_check_class_entry_functions[] = {
-	PHP_ME(EvCheck, __construct, arginfo_ev_check, ZEND_ACC_PUBLIC  | ZEND_ACC_CTOR)
+	PHP_ME(EvCheck, __construct,   arginfo_ev_check, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
+	PHP_ME(EvCheck, createStopped, arginfo_ev_check, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
 	{ NULL, NULL, NULL }
 };
 /* }}} */
 #if EV_PREPARE_ENABLE
 /* {{{ ev_prepare_class_entry_functions */
 const zend_function_entry ev_prepare_class_entry_functions[] = {
-	PHP_ME(EvPrepare, __construct, arginfo_ev_prepare, ZEND_ACC_PUBLIC  | ZEND_ACC_CTOR)
+	PHP_ME(EvPrepare, __construct,   arginfo_ev_prepare, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
+	PHP_ME(EvPrepare, createStopped, arginfo_ev_prepare, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
 	{ NULL, NULL, NULL }
 };
 /* }}} */
 #if EV_EMBED_ENABLE
 /* {{{ ev_embed_class_entry_functions */
 const zend_function_entry ev_embed_class_entry_functions[] = {
-	PHP_ME(EvEmbed, __construct, arginfo_ev_embed,     ZEND_ACC_PUBLIC  | ZEND_ACC_CTOR)
-	PHP_ME(EvEmbed, set,         arginfo_ev_embed_set, ZEND_ACC_PUBLIC)
-	PHP_ME(EvEmbed, sweep,       arginfo_ev__void,     ZEND_ACC_PUBLIC)
+	PHP_ME(EvEmbed, __construct,   arginfo_ev_embed,     ZEND_ACC_PUBLIC  | ZEND_ACC_CTOR)
+	PHP_ME(EvEmbed, set,           arginfo_ev_embed_set, ZEND_ACC_PUBLIC)
+	PHP_ME(EvEmbed, sweep,         arginfo_ev__void,     ZEND_ACC_PUBLIC)
+	PHP_ME(EvEmbed, createStopped, arginfo_ev_embed,     ZEND_ACC_PUBLIC  | ZEND_ACC_STATIC)
 	{ NULL, NULL, NULL }
 };
 /* }}} */
 #if EV_FORK_ENABLE
 /* {{{ ev_fork_class_entry_functions */
 const zend_function_entry ev_fork_class_entry_functions[] = {
-	PHP_ME(EvFork, __construct, arginfo_ev_fork, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
+	PHP_ME(EvFork, __construct,   arginfo_ev_fork, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
+	PHP_ME(EvFork, createStopped, arginfo_ev_fork, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
 	{ NULL, NULL, NULL }
 };
 /* }}} */
 PHP_FUNCTION(ev_run);
 PHP_FUNCTION(ev_now);
 PHP_FUNCTION(ev_break);
+PHP_FUNCTION(ev_iteration);
+PHP_FUNCTION(ev_depth);
+PHP_FUNCTION(ev_verify);
 
 /* {{{ EvLoop */
 PHP_METHOD(EvLoop, __construct);
-PHP_METHOD(EvLoop, default_loop);
+PHP_METHOD(EvLoop, defaultLoop);
 PHP_METHOD(EvLoop, loop_fork);
 PHP_METHOD(EvLoop, verify);
 PHP_METHOD(EvLoop, invoke_pending);
 /* {{{ EvIo */
 PHP_METHOD(EvIo, __construct);
 PHP_METHOD(EvIo, set);
+PHP_METHOD(EvIo, createStopped);
 /* }}} */
 
 /* {{{ EvTimer */
 PHP_METHOD(EvTimer, __construct);
 PHP_METHOD(EvTimer, set);
 PHP_METHOD(EvTimer, again);
+PHP_METHOD(EvTimer, createStopped);
 /* }}} */
 
 #if EV_PERIODIC_ENABLE
 PHP_METHOD(EvPeriodic, set);
 PHP_METHOD(EvPeriodic, again);
 PHP_METHOD(EvPeriodic, at);
+PHP_METHOD(EvPeriodic, createStopped);
 /* }}} */
 #endif
 
 /* {{{ EvSignal */
 PHP_METHOD(EvSignal, __construct);
 PHP_METHOD(EvSignal, set);
+PHP_METHOD(EvSignal, createStopped);
 /* }}} */
 #endif
 
 /* {{{ EvChild */
 PHP_METHOD(EvChild, __construct);
 PHP_METHOD(EvChild, set);
+PHP_METHOD(EvChild, createStopped);
 /* }}} */
 #endif
 
 PHP_METHOD(EvStat, attr);
 PHP_METHOD(EvStat, prev);
 PHP_METHOD(EvStat, stat);
+PHP_METHOD(EvStat, createStopped);
 /* }}} */
 #endif
 
 #if EV_IDLE_ENABLE
 /* {{{ EvIdle */
 PHP_METHOD(EvIdle, __construct);
+PHP_METHOD(EvIdle, createStopped);
 /* }}} */
 #endif
 
 #if EV_CHECK_ENABLE
 /* {{{ EvCheck */
 PHP_METHOD(EvCheck, __construct);
+PHP_METHOD(EvCheck, createStopped);
 /* }}} */
 #endif
 
 #if EV_PREPARE_ENABLE
 /* {{{ EvPrepare */
 PHP_METHOD(EvPrepare, __construct);
+PHP_METHOD(EvPrepare, createStopped);
 /* }}} */
 #endif
 
 PHP_METHOD(EvEmbed, __construct);
 PHP_METHOD(EvEmbed, set);
 PHP_METHOD(EvEmbed, sweep);
+PHP_METHOD(EvEmbed, createStopped);
 /* }}} */
 #endif
 
 #if EV_FORK_ENABLE
 /* {{{ EvFork*/
 PHP_METHOD(EvFork, __construct);
+PHP_METHOD(EvFork, createStopped);
 /* }}} */
 #endif
 
 #include "watcher.h"
 
 /* {{{ php_ev_fork_object_ctor */
-void php_ev_fork_object_ctor(INTERNAL_FUNCTION_PARAMETERS, zval *loop)
+void php_ev_fork_object_ctor(INTERNAL_FUNCTION_PARAMETERS, zval *loop, zend_bool ctor, zend_bool start)
 {
 	zval                  *self;
 	php_ev_object         *o_self;
 	php_ev_object         *o_loop;
-	ev_fork               *fork_watcher;
+	ev_fork               *w;
 
-	zval                  *data         = NULL;
-	zend_fcall_info        fci          = empty_fcall_info;
-	zend_fcall_info_cache  fcc          = empty_fcall_info_cache;
-	long                   priority     = 0;
+	zval                  *data     = NULL;
+	zend_fcall_info        fci      = empty_fcall_info;
+	zend_fcall_info_cache  fcc      = empty_fcall_info_cache;
+	long                   priority = 0;
 
 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "f|z!l",
 				&fci, &fcc, &data, &priority) == FAILURE) {
 		return;
 	}
 
-	/* If loop is NULL, then we're in __construct() */
-	if (loop) {
-		PHP_EV_INIT_CLASS_OBJECT(return_value, ev_fork_class_entry_ptr);
-		self = return_value; 
+	if (ctor) {
+		self = getThis();
 	} else {
+		PHP_EV_INIT_CLASS_OBJECT(return_value, ev_io_class_entry_ptr);
+		self = return_value; 
+	}
+
+	if (!loop) {
 		loop = php_ev_default_loop(TSRMLS_C);
-		self = getThis();
 	}
 
-	o_self        = (php_ev_object *) zend_object_store_get_object(self TSRMLS_CC);
-	o_loop        = (php_ev_object *) zend_object_store_get_object(loop TSRMLS_CC);
-	fork_watcher = (ev_fork *) php_ev_new_watcher(sizeof(ev_fork), self,
+	o_self = (php_ev_object *) zend_object_store_get_object(self TSRMLS_CC);
+	o_loop = (php_ev_object *) zend_object_store_get_object(loop TSRMLS_CC);
+	w      = (ev_fork *) php_ev_new_watcher(sizeof(ev_fork), self,
 			PHP_EV_LOOP_OBJECT_FETCH_FROM_OBJECT(o_loop),
 			&fci, &fcc, data, priority TSRMLS_CC);
 
-	fork_watcher->type = EV_CHECK;
+	w->type = EV_FORK;
 
-	o_self->ptr = (void *) fork_watcher;
+	o_self->ptr = (void *) w;
+
+	if (start) {
+		PHP_EV_WATCHER_START(ev_fork, w);
+	}
 }
 /* }}} */
 
 /* {{{ proto EvFork::__construct(EvLoop loop, callable callback[, mixed data = NULL[, int priority = 0]]) */
 PHP_METHOD(EvFork, __construct)
 {
-	php_ev_fork_object_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, NULL);
+	PHP_EV_WATCHER_CTOR(fork, NULL);
+}
+/* }}} */
+
+/* {{{ proto EvFork::createStopped(EvLoop loop, callable callback[, mixed data = NULL[, int priority = 0]]) */
+PHP_METHOD(EvFork, createStopped)
+{
+	PHP_EV_WATCHER_FACTORY_NS(fork, NULL);
 }
 /* }}} */
 
 #include "watcher.h"
 
 /* {{{ php_ev_idle_object_ctor */
-void php_ev_idle_object_ctor(INTERNAL_FUNCTION_PARAMETERS, zval *loop)
+void php_ev_idle_object_ctor(INTERNAL_FUNCTION_PARAMETERS, zval *loop, zend_bool ctor, zend_bool start)
 {
 	zval                  *self;
 	php_ev_object         *o_self;
 	php_ev_object         *o_loop;
-	ev_idle               *idle_watcher;
+	ev_idle               *w;
 
 	zval                  *data         = NULL;
 	zend_fcall_info        fci          = empty_fcall_info;
 		return;
 	}
 
-	/* If loop is NULL, then we're in __construct() */
-	if (loop) {
+	if (ctor) {
+		self = getThis();
+	} else {
 		PHP_EV_INIT_CLASS_OBJECT(return_value, ev_idle_class_entry_ptr);
 		self = return_value; 
-	} else {
+	}
+
+	if (!loop) {
 		loop = php_ev_default_loop(TSRMLS_C);
-		self = getThis();
 	}
 
-	o_self       = (php_ev_object *) zend_object_store_get_object(self TSRMLS_CC);
-	o_loop       = (php_ev_object *) zend_object_store_get_object(loop TSRMLS_CC);
-	idle_watcher = (ev_idle *) php_ev_new_watcher(sizeof(ev_idle), self,
+	o_self = (php_ev_object *) zend_object_store_get_object(self TSRMLS_CC);
+	o_loop = (php_ev_object *) zend_object_store_get_object(loop TSRMLS_CC);
+	w      = (ev_idle *) php_ev_new_watcher(sizeof(ev_idle), self,
 			PHP_EV_LOOP_OBJECT_FETCH_FROM_OBJECT(o_loop),
 			&fci, &fcc, data, priority TSRMLS_CC);
 
-	idle_watcher->type = EV_IDLE;
+	w->type = EV_IDLE;
 
-	o_self->ptr = (void *) idle_watcher;
+	o_self->ptr = (void *) w;
+
+	if (start) {
+		PHP_EV_WATCHER_START(ev_idle, w);
+	}
 }
 /* }}} */
 
 /* {{{ proto EvIdle::__construct(callable callback[, mixed data = NULL[, int priority = 0]]) */
 PHP_METHOD(EvIdle, __construct)
 {
-	php_ev_idle_object_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, NULL);
+	PHP_EV_WATCHER_CTOR(idle, NULL);
+}
+/* }}} */
+
+/* {{{ proto EvIdle::createStopped(callable callback[, mixed data = NULL[, int priority = 0]]) */
+PHP_METHOD(EvIdle, createStopped)
+{
+	PHP_EV_WATCHER_FACTORY_NS(idle, NULL);
 }
 /* }}} */
 
 #include "watcher.h"
 
 /* {{{ php_ev_io_object_ctor */
-void php_ev_io_object_ctor(INTERNAL_FUNCTION_PARAMETERS, zval *loop)
+void php_ev_io_object_ctor(INTERNAL_FUNCTION_PARAMETERS, zval *loop, zend_bool ctor, zend_bool start)
 {
-	zval                  *self       = NULL;
+	zval                  *self;
 	zval                  *z_fd;
 	php_ev_object         *o_self;
 	php_ev_object         *o_loop;
-	ev_io                 *io_watcher;
+	ev_io                 *w;
 	php_socket_t           fd;
 
-	zval                  *data       = NULL;
-	zend_fcall_info        fci        = empty_fcall_info;
-	zend_fcall_info_cache  fcc        = empty_fcall_info_cache;
-	long                   priority   = 0;
+	zval                  *data     = NULL;
+	zend_fcall_info        fci      = empty_fcall_info;
+	zend_fcall_info_cache  fcc      = empty_fcall_info_cache;
+	long                   priority = 0;
 	long                   events;
 
 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zlf|z!l",
 		return;
 	}
 
-	/* If loop is NULL, then we're in __construct() */
-	if (loop) {
+	if (ctor) {
+		self = getThis();
+	} else {
 		PHP_EV_INIT_CLASS_OBJECT(return_value, ev_io_class_entry_ptr);
-
-		PHP_EV_ASSERT((self == NULL));
 		self = return_value; 
-	} else {
+	}
+
+	if (!loop) {
 		loop = php_ev_default_loop(TSRMLS_C);
-		self = getThis();
 	}
 
-	o_self     = (php_ev_object *) zend_object_store_get_object(self TSRMLS_CC);
-	o_loop     = (php_ev_object *) zend_object_store_get_object(loop TSRMLS_CC);
-	io_watcher = (ev_io *) php_ev_new_watcher(sizeof(ev_io), self,
+	o_self = (php_ev_object *) zend_object_store_get_object(self TSRMLS_CC);
+	o_loop = (php_ev_object *) zend_object_store_get_object(loop TSRMLS_CC);
+	w      = (ev_io *) php_ev_new_watcher(sizeof(ev_io), self,
 			PHP_EV_LOOP_OBJECT_FETCH_FROM_OBJECT(o_loop),
 			&fci, &fcc, data, priority TSRMLS_CC);
 
-	io_watcher->type = EV_IO;
+	w->type = EV_IO;
 	
-	ev_io_set(io_watcher, fd, events);
+	ev_io_set(w, fd, events);
 
-	o_self->ptr = (void *) io_watcher;
+	o_self->ptr = (void *) w;
+
+	if (start) {
+		PHP_EV_WATCHER_START(ev_io, w);
+	}
 }
 /* }}} */
 
 /* {{{ proto EvIo::__construct(mixed fd, int events, callable callback[, mixed data = NULL[, int priority = 0]]) */
 PHP_METHOD(EvIo, __construct)
 {
-	php_ev_io_object_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, NULL);
+	PHP_EV_WATCHER_CTOR(io, NULL);
+}
+/* }}} */
+
+/* {{{ proto EvIo::createStopped(mixed fd, int events, callable callback[, mixed data = NULL[, int priority = 0]]) */
+PHP_METHOD(EvIo, createStopped)
+{
+	PHP_EV_WATCHER_FACTORY_NS(io, NULL);
 }
 /* }}} */
 
 }
 /* }}} */
 
-/* {{{ proto EvLoop EvLoop::default_loop([int flags = EVLAG_AUTO[, callable callback = NULL[, mixed data = NULL[, double io_collect_interval = 0.[, double timeout_collect_interval = 0.]]]]])
+/* {{{ proto EvLoop EvLoop::defaultLoop([int flags = EVLAG_AUTO[, callable callback = NULL[, mixed data = NULL[, double io_collect_interval = 0.[, double timeout_collect_interval = 0.]]]]])
 */
-PHP_METHOD(EvLoop, default_loop)
+PHP_METHOD(EvLoop, defaultLoop)
 {
 	php_ev_loop_object_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU,
 			/* in ctor */FALSE, /* is_default_loop */TRUE);
 /* {{{ proto EvIo EvLoop::io(mixed fd, int events, callable callback[, mixed data = NULL[, int priority = 0]]) */
 PHP_METHOD(EvLoop, io)
 {
-	php_ev_io_object_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, getThis());
+	PHP_EV_WATCHER_FACTORY(io, getThis());
 }
 /* }}} */
 
 /* {{{ proto EvTimer EvLoop::timer(double after, double repeat, EvLoop loop, callable callback[, mixed data = NULL[, int priority = 0]]) */
 PHP_METHOD(EvLoop, timer)
 {
-	php_ev_timer_object_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, getThis());
+	PHP_EV_WATCHER_FACTORY(timer, getThis());
 }
 /* }}} */
 
 /* {{{ proto EvPeriodic EvLoop::periodic(double offset, double interval, callable reschedule_cb, callable callback[, mixed data = NULL[, int priority = 0]]) */
 PHP_METHOD(EvLoop, periodic)
 {
-	php_ev_periodic_object_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, getThis());
+	PHP_EV_WATCHER_FACTORY(periodic, getThis());
 }
 /* }}} */
 #endif
 /* {{{ proto EvSignal EvLoop::signal(int signum, callable callback[, mixed data = NULL[, int priority = 0]]) */
 PHP_METHOD(EvLoop, signal)
 {
-	php_ev_signal_object_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, getThis());
+	PHP_EV_WATCHER_FACTORY(signal, getThis());
 }
 /* }}} */
 #endif
 /* {{{ proto EvChild EvLoop::child(int pid, bool trace, callable callback[, mixed data = NULL[, int priority = 0]]) */
 PHP_METHOD(EvLoop, child)
 {
-	php_ev_child_object_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, getThis());
+	PHP_EV_WATCHER_FACTORY(child, getThis());
 }
 /* }}} */
 #endif
 /* {{{ proto EvStat EvLoop::stat(string path, double interval, callable callback[, mixed data = NULL[, int priority = 0]]) */
 PHP_METHOD(EvLoop, stat)
 {
-	php_ev_stat_object_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, getThis());
+	PHP_EV_WATCHER_FACTORY(stat, getThis());
 }
 /* }}} */
 #endif
 /* {{{ proto EvIdle EvLoop::idle(callable callback[, mixed data = NULL[, int priority = 0]]) */
 PHP_METHOD(EvLoop, idle)
 {
-	php_ev_idle_object_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, getThis());
+	PHP_EV_WATCHER_FACTORY(idle, getThis());
 }
 /* }}} */
 #endif
 /* {{{ proto EvCheck EvLoop::check() */
 PHP_METHOD(EvLoop, check)
 {
-	php_ev_check_object_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, getThis());
+	PHP_EV_WATCHER_FACTORY(check, getThis());
 }
 /* }}} */
 #endif
 /* {{{ proto EvPrepare EvLoop::prepare() */
 PHP_METHOD(EvLoop, prepare)
 {
-	php_ev_prepare_object_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, getThis());
+	PHP_EV_WATCHER_FACTORY(prepare, getThis());
 }
 /* }}} */
 #endif
 /* {{{ proto EvEmbed EvLoop::embed() */
 PHP_METHOD(EvLoop, embed)
 {
-	php_ev_embed_object_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, getThis());
+	PHP_EV_WATCHER_FACTORY(embed, getThis());
 }
 /* }}} */
 #endif
 /* {{{ proto EvFork EvLoop::fork() */
 PHP_METHOD(EvLoop, fork)
 {
-	php_ev_fork_object_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, getThis());
+	PHP_EV_WATCHER_FACTORY(fork, getThis());
 }
 /* }}} */
 #endif
 /* }}} */
 
 /* {{{ php_ev_periodic_object_ctor */
-void php_ev_periodic_object_ctor(INTERNAL_FUNCTION_PARAMETERS, zval *loop)
+void php_ev_periodic_object_ctor(INTERNAL_FUNCTION_PARAMETERS, zval *loop, zend_bool ctor, zend_bool start)
 {
 	double                 offset;
 	double                 interval;
 	zend_fcall_info        fci_reschedule;
 	zend_fcall_info_cache  fcc_reschedule;
 
-	zval                  *self             = NULL;
+	zval                  *self;
 	php_ev_object         *o_self;
 	php_ev_object         *o_loop;
-	ev_periodic           *periodic_watcher;
+	ev_periodic           *w;
 	php_ev_periodic       *periodic_ptr;
 
 	zval                  *data             = NULL;
 
 	PHP_EV_CHECK_REPEAT(interval);
 
-	/* If loop is NULL, then we're in __construct() */
-	if (loop) {
+	if (ctor) {
+		self = getThis();
+	} else {
 		PHP_EV_INIT_CLASS_OBJECT(return_value, ev_periodic_class_entry_ptr);
-
-		PHP_EV_ASSERT((self == NULL));
 		self = return_value; 
-	} else {
+	}
+
+	if (!loop) {
 		loop = php_ev_default_loop(TSRMLS_C);
-		self = getThis();
 	}
 
 	periodic_ptr = (php_ev_periodic *) emalloc(sizeof(php_ev_periodic));
 	memset(periodic_ptr, 0, sizeof(php_ev_periodic));
 
-	periodic_watcher = &periodic_ptr->periodic;
+	w = &periodic_ptr->periodic;
 
 	o_self = (php_ev_object *) zend_object_store_get_object(self TSRMLS_CC);
 	o_loop = (php_ev_object *) zend_object_store_get_object(loop TSRMLS_CC);
 
-	php_ev_set_watcher((ev_watcher *)periodic_watcher, sizeof(ev_periodic), self,
+	php_ev_set_watcher((ev_watcher *)w, sizeof(ev_periodic), self,
 			PHP_EV_LOOP_OBJECT_FETCH_FROM_OBJECT(o_loop),
 			&fci, &fcc, data, priority TSRMLS_CC);
 
-	periodic_watcher->type = EV_PERIODIC;
-
+	w->type = EV_PERIODIC;
 
 	if (ZEND_FCI_INITIALIZED(fci_reschedule)) { /* argument is not NULL */
 		PHP_EV_COPY_FCALL_INFO(periodic_ptr->fci, periodic_ptr->fcc,
 				&fci_reschedule, &fcc_reschedule);
 
-		ev_periodic_set(periodic_watcher, offset, interval, php_ev_periodic_rescheduler);
+		ev_periodic_set(w, offset, interval, php_ev_periodic_rescheduler);
 	} else {
-		ev_periodic_set(periodic_watcher, offset, interval, 0);
+		ev_periodic_set(w, offset, interval, 0);
 	}
 
 	o_self->ptr = (void *) periodic_ptr;
-}
 
+	if (start) {
+		PHP_EV_WATCHER_START(ev_periodic, w);
+	}
+}
 /* }}} */
 
 
  * NOTE: reschedule_cb could be NULL */
 PHP_METHOD(EvPeriodic, __construct)
 {
-	php_ev_periodic_object_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, NULL);
+	PHP_EV_WATCHER_CTOR(periodic, NULL);
+}
+/* }}} */
+
+/* {{{ proto EvPeriodic::createStopped(double offset, double interval, callable reschedule_cb, callable callback[, mixed data = NULL[, int priority = 0]]) 
+ * NOTE: reschedule_cb could be NULL */
+PHP_METHOD(EvPeriodic, createStopped)
+{
+	PHP_EV_WATCHER_FACTORY_NS(periodic, NULL);
 }
 /* }}} */
 
 /* {{{ proto void EvPeriodic::set(double offset, double interval[, callable reschedule_cb]) */
 PHP_METHOD(EvPeriodic, set)
 {
-	double                offset;
-	double                interval;
-	zend_fcall_info       fci      = empty_fcall_info;
-	zend_fcall_info_cache fcc      = empty_fcall_info_cache;
+	double                 offset;
+	double                 interval;
+	zend_fcall_info        fci          = empty_fcall_info;
+	zend_fcall_info_cache  fcc          = empty_fcall_info_cache;
 
-	ev_periodic     *periodic_watcher;
-	php_ev_object   *ev_obj;
-	php_ev_periodic *periodic_ptr;
+	ev_periodic           *w;
+	php_ev_object         *ev_obj;
+	php_ev_periodic       *periodic_ptr;
 
 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "dd|f",
 				&offset, &interval, &fci, &fcc) == FAILURE) {
 
 	PHP_EV_CHECK_REPEAT(interval);
 
-	ev_obj           = (php_ev_object *) zend_object_store_get_object(getThis() TSRMLS_CC);
-	periodic_ptr     = (php_ev_periodic *) PHP_EV_WATCHER_FETCH_FROM_OBJECT(ev_obj);
-	periodic_watcher = (ev_periodic *) periodic_ptr;
+	ev_obj       = (php_ev_object *) zend_object_store_get_object(getThis() TSRMLS_CC);
+	periodic_ptr = (php_ev_periodic *) PHP_EV_WATCHER_FETCH_FROM_OBJECT(ev_obj);
+	w            = (ev_periodic *) periodic_ptr;
 	
 	/* Free fci and fcc within periodic_ptr, since they will be overwritten anyways */
 
 	if (ZEND_FCI_INITIALIZED(fci)) {
 		PHP_EV_COPY_FCALL_INFO(periodic_ptr->fci, periodic_ptr->fcc, &fci, &fcc);
 
-		PHP_EV_WATCHER_RESET(ev_periodic, periodic_watcher,
-				(periodic_watcher, offset, interval, php_ev_periodic_rescheduler));
+		PHP_EV_WATCHER_RESET(ev_periodic, w,
+				(w, offset, interval, php_ev_periodic_rescheduler));
 	} else {
-		PHP_EV_WATCHER_RESET(ev_periodic, periodic_watcher,
-				(periodic_watcher, offset, interval, 0));
+		PHP_EV_WATCHER_RESET(ev_periodic, w,
+				(w, offset, interval, 0));
 	}
 }
 /* }}} */
 /* {{{ proto void EvPeriodic::again(void) */
 PHP_METHOD(EvPeriodic, again)
 {
-	ev_periodic *periodic_watcher;
+	ev_periodic *w;
 
 	if (zend_parse_parameters_none() == FAILURE) {
 		return;
 	}
 
-	periodic_watcher = (ev_periodic *) PHP_EV_WATCHER_FETCH_FROM_THIS();
+	w = (ev_periodic *) PHP_EV_WATCHER_FETCH_FROM_THIS();
 
-	ev_periodic_again(php_ev_watcher_loop_ptr(periodic_watcher), periodic_watcher);
-	PHP_EV_WATCHER_UNREF(periodic_watcher); 
+	ev_periodic_again(php_ev_watcher_loop_ptr(w), w);
+	PHP_EV_WATCHER_UNREF(w); 
 }
 /* }}} */
 
 #include "watcher.h"
 
 /* {{{ php_ev_prepare_object_ctor */
-void php_ev_prepare_object_ctor(INTERNAL_FUNCTION_PARAMETERS, zval *loop)
+void php_ev_prepare_object_ctor(INTERNAL_FUNCTION_PARAMETERS, zval *loop, zend_bool ctor, zend_bool start)
 {
 	zval                  *self;
 	php_ev_object         *o_self;
 	php_ev_object         *o_loop;
-	ev_prepare            *prepare_watcher;
+	ev_prepare            *w;
 
-	zval                  *data            = NULL;
-	zend_fcall_info        fci             = empty_fcall_info;
-	zend_fcall_info_cache  fcc             = empty_fcall_info_cache;
-	long                   priority        = 0;
+	zval                  *data     = NULL;
+	zend_fcall_info        fci      = empty_fcall_info;
+	zend_fcall_info_cache  fcc      = empty_fcall_info_cache;
+	long                   priority = 0;
 
 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "f|z!l",
 				&fci, &fcc, &data, &priority) == FAILURE) {
 		return;
 	}
 
-	/* If loop is NULL, then we're in __construct() */
-	if (loop) {
-		PHP_EV_INIT_CLASS_OBJECT(return_value, ev_prepare_class_entry_ptr);
-		self = return_value; 
+	if (ctor) {
+		self = getThis();
 	} else {
+		PHP_EV_INIT_CLASS_OBJECT(return_value, ev_io_class_entry_ptr);
+		self = return_value; 
+	}
+
+	if (!loop) {
 		loop = php_ev_default_loop(TSRMLS_C);
-		self = getThis();
 	}
 
-	o_self          = (php_ev_object *) zend_object_store_get_object(self TSRMLS_CC);
-	o_loop          = (php_ev_object *) zend_object_store_get_object(loop TSRMLS_CC);
-	prepare_watcher = (ev_prepare *) php_ev_new_watcher(sizeof(ev_prepare), self,
+	o_self = (php_ev_object *) zend_object_store_get_object(self TSRMLS_CC);
+	o_loop = (php_ev_object *) zend_object_store_get_object(loop TSRMLS_CC);
+	w      = (ev_prepare *) php_ev_new_watcher(sizeof(ev_prepare), self,
 			PHP_EV_LOOP_OBJECT_FETCH_FROM_OBJECT(o_loop),
 			&fci, &fcc, data, priority TSRMLS_CC);
 
-	prepare_watcher->type = EV_PREPARE;
+	w->type = EV_PREPARE;
 
-	o_self->ptr = (void *) prepare_watcher;
+	o_self->ptr = (void *) w;
+
+	if (start) {
+		PHP_EV_WATCHER_START(ev_prepare, w);
+	}
 }
 /* }}} */
 
 /* {{{ proto EvPrepare::__construct(callable callback[, mixed data = NULL[, int priority = 0]]) */
 PHP_METHOD(EvPrepare, __construct)
 {
-	php_ev_prepare_object_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, NULL);
+	PHP_EV_WATCHER_CTOR(prepare, NULL);
+}
+/* }}} */
+
+/* {{{ proto EvPrepare::createStopped(callable callback[, mixed data = NULL[, int priority = 0]]) */
+PHP_METHOD(EvPrepare, createStopped)
+{
+	PHP_EV_WATCHER_FACTORY_NS(prepare, NULL);
 }
 /* }}} */
 
 
 zval *php_ev_default_loop(TSRMLS_D);
 
-void php_ev_io_object_ctor(INTERNAL_FUNCTION_PARAMETERS, zval *loop);
-void php_ev_timer_object_ctor(INTERNAL_FUNCTION_PARAMETERS, zval *loop);
-void php_ev_periodic_object_ctor(INTERNAL_FUNCTION_PARAMETERS, zval *loop);
-void php_ev_child_object_ctor(INTERNAL_FUNCTION_PARAMETERS, zval *loop);
-void php_ev_stat_object_ctor(INTERNAL_FUNCTION_PARAMETERS, zval *loop);
-void php_ev_idle_object_ctor(INTERNAL_FUNCTION_PARAMETERS, zval *loop);
-void php_ev_check_object_ctor(INTERNAL_FUNCTION_PARAMETERS, zval *loop);
-void php_ev_prepare_object_ctor(INTERNAL_FUNCTION_PARAMETERS, zval *loop);
-void php_ev_embed_object_ctor(INTERNAL_FUNCTION_PARAMETERS, zval *loop);
-void php_ev_fork_object_ctor(INTERNAL_FUNCTION_PARAMETERS, zval *loop);
+void php_ev_io_object_ctor(INTERNAL_FUNCTION_PARAMETERS, zval *loop, zend_bool ctor, zend_bool start);
+void php_ev_timer_object_ctor(INTERNAL_FUNCTION_PARAMETERS, zval *loop, zend_bool ctor, zend_bool start);
+void php_ev_periodic_object_ctor(INTERNAL_FUNCTION_PARAMETERS, zval *loop, zend_bool ctor, zend_bool start);
+void php_ev_child_object_ctor(INTERNAL_FUNCTION_PARAMETERS, zval *loop, zend_bool ctor, zend_bool start);
+void php_ev_stat_object_ctor(INTERNAL_FUNCTION_PARAMETERS, zval *loop, zend_bool ctor, zend_bool start);
+void php_ev_idle_object_ctor(INTERNAL_FUNCTION_PARAMETERS, zval *loop, zend_bool ctor, zend_bool start);
+void php_ev_check_object_ctor(INTERNAL_FUNCTION_PARAMETERS, zval *loop, zend_bool ctor, zend_bool start);
+void php_ev_prepare_object_ctor(INTERNAL_FUNCTION_PARAMETERS, zval *loop, zend_bool ctor, zend_bool start);
+void php_ev_embed_object_ctor(INTERNAL_FUNCTION_PARAMETERS, zval *loop, zend_bool ctor, zend_bool start);
+void php_ev_fork_object_ctor(INTERNAL_FUNCTION_PARAMETERS, zval *loop, zend_bool ctor, zend_bool start);
 
 #endif /* PHP_EV_PRIV_H */
 
 #include "watcher.h"
 
 /* {{{ php_ev_signal_object_ctor */
-void php_ev_signal_object_ctor(INTERNAL_FUNCTION_PARAMETERS, zval *loop)
+void php_ev_signal_object_ctor(INTERNAL_FUNCTION_PARAMETERS, zval *loop, zend_bool ctor, zend_bool start)
 {
 	long           signum;
 	zval          *self;
 	php_ev_object *o_self;
 	php_ev_object *o_loop;
-	ev_signal     *signal_watcher;
+	ev_signal     *w;
 
 	zval                  *data       = NULL;
 	zend_fcall_info        fci        = empty_fcall_info;
 		return;
 	}
 
-	/* If loop is NULL, then we're in __construct() */
-	if (loop) {
+	if (ctor) {
+		self = getThis();
+	} else {
 		PHP_EV_INIT_CLASS_OBJECT(return_value, ev_signal_class_entry_ptr);
 		self = return_value; 
-	} else {
+	}
+
+	if (!loop) {
 		loop = php_ev_default_loop(TSRMLS_C);
-		self = getThis();
 	}
 
-	o_self        = (php_ev_object *) zend_object_store_get_object(self TSRMLS_CC);
-	o_loop        = (php_ev_object *) zend_object_store_get_object(loop TSRMLS_CC);
-	signal_watcher = (ev_signal *) php_ev_new_watcher(sizeof(ev_signal), self,
+	o_self = (php_ev_object *) zend_object_store_get_object(self TSRMLS_CC);
+	o_loop = (php_ev_object *) zend_object_store_get_object(loop TSRMLS_CC);
+	w      = (ev_signal *) php_ev_new_watcher(sizeof(ev_signal), self,
 			PHP_EV_LOOP_OBJECT_FETCH_FROM_OBJECT(o_loop),
 			&fci, &fcc, data, priority TSRMLS_CC);
 
-	signal_watcher->type = EV_SIGNAL;
+	w->type = EV_SIGNAL;
 	
-	ev_signal_set(signal_watcher, signum);
+	ev_signal_set(w, signum);
 
-	o_self->ptr = (void *) signal_watcher;
+	o_self->ptr = (void *) w;
+
+	if (start) {
+		PHP_EV_WATCHER_START(ev_signal, w);
+	}
 }
 /* }}} */
 
 /* {{{ proto EvSignal::__construct(int signum, callable callback[, mixed data = NULL[, int priority = 0]]) */
 PHP_METHOD(EvSignal, __construct)
 {
-	php_ev_signal_object_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, NULL);
+	PHP_EV_WATCHER_CTOR(signal, NULL);
+}
+/* }}} */
+
+/* {{{ proto EvSignal::createStopped(int signum, callable callback[, mixed data = NULL[, int priority = 0]]) */
+PHP_METHOD(EvSignal, createStopped)
+{
+	PHP_EV_WATCHER_FACTORY_NS(signal, NULL);
 }
 /* }}} */
 
 PHP_METHOD(EvSignal, set)
 {
 	long       signum;
-	ev_signal *signal_watcher;
+	ev_signal *w;
 
 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l",
 				&signum) == FAILURE) {
 
 	PHP_EV_CHECK_SIGNUM(signum);
 
-	signal_watcher = (ev_signal *) PHP_EV_WATCHER_FETCH_FROM_THIS();
+	w = (ev_signal *) PHP_EV_WATCHER_FETCH_FROM_THIS();
 
-	PHP_EV_SIGNAL_RESET(signal_watcher, (signal_watcher, signum));
+	PHP_EV_SIGNAL_RESET(w, (w, signum));
 }
 /* }}} */
 
 /* }}} */
 
 /* {{{ php_ev_stat_object_ctor */
-void php_ev_stat_object_ctor(INTERNAL_FUNCTION_PARAMETERS, zval *loop)
+void php_ev_stat_object_ctor(INTERNAL_FUNCTION_PARAMETERS, zval *loop, zend_bool ctor, zend_bool start)
 {
 	char                  *path;
 	int                    path_len;
 	zval                  *self         = NULL;
 	php_ev_object         *o_self;
 	php_ev_object         *o_loop;
-	ev_stat               *stat_watcher;
+	ev_stat               *w;
 	php_ev_stat           *stat_ptr;
 
 	zval                  *data         = NULL;
 	zend_fcall_info_cache  fcc          = empty_fcall_info_cache;
 	long                   priority     = 0;
 
-
 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "pdf|z!l",
 				&path, &path_len, &interval, &fci, &fcc,
 				&data, &priority) == FAILURE) {
 		return;
 	}
 
-	/* If loop is NULL, then we're in __construct() */
-	if (loop) {
+	if (ctor) {
+		self = getThis();
+	} else {
 		PHP_EV_INIT_CLASS_OBJECT(return_value, ev_stat_class_entry_ptr);
-
-		PHP_EV_ASSERT((self == NULL));
 		self = return_value; 
-	} else {
+	}
+
+	if (!loop) {
 		loop = php_ev_default_loop(TSRMLS_C);
-		self = getThis();
 	}
 
 	stat_ptr = (php_ev_stat *) emalloc(sizeof(php_ev_stat));
 	
 	stat_ptr->path = estrndup(path, path_len); 
 
-	stat_watcher = &stat_ptr->stat;
+	w = &stat_ptr->stat;
 
-	o_self       = (php_ev_object *) zend_object_store_get_object(self TSRMLS_CC);
-	o_loop       = (php_ev_object *) zend_object_store_get_object(loop TSRMLS_CC);
+	o_self = (php_ev_object *) zend_object_store_get_object(self TSRMLS_CC);
+	o_loop = (php_ev_object *) zend_object_store_get_object(loop TSRMLS_CC);
 
-	php_ev_set_watcher((ev_watcher *)stat_watcher, sizeof(ev_stat), self,
+	php_ev_set_watcher((ev_watcher *)w, sizeof(ev_stat), self,
 			PHP_EV_LOOP_OBJECT_FETCH_FROM_OBJECT(o_loop),
 			&fci, &fcc, data, priority TSRMLS_CC);
 
-	stat_watcher->type = EV_STAT;
+	w->type = EV_STAT;
 	
-	ev_stat_set(stat_watcher, stat_ptr->path, interval);
+	ev_stat_set(w, stat_ptr->path, interval);
 
 	o_self->ptr = (void *) stat_ptr;
+
+	if (start) {
+		PHP_EV_WATCHER_START(ev_stat, w);
+	}
 }
 /* }}} */
 
 /* {{{ proto EvStat::__construct(string path, double interval, callable callback[, mixed data = NULL[, int priority = 0]]) */
 PHP_METHOD(EvStat, __construct)
 {
-	php_ev_stat_object_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, NULL);
+	PHP_EV_WATCHER_CTOR(stat, NULL);
+}
+/* }}} */
+
+/* {{{ proto EvStat::createStopped(string path, double interval, callable callback[, mixed data = NULL[, int priority = 0]]) */
+PHP_METHOD(EvStat, createStopped)
+{
+	PHP_EV_WATCHER_FACTORY_NS(stat, NULL);
 }
 /* }}} */
 
 	char          *path;
 	int            path_len;
 	double         interval;
-	ev_stat       *stat_watcher;
+	ev_stat       *w;
 	php_ev_stat   *stat_ptr;
 	php_ev_object *ev_obj;
 
 		return;
 	}
 
-	stat_watcher = (ev_stat *) PHP_EV_WATCHER_FETCH_FROM_THIS();
+	w = (ev_stat *) PHP_EV_WATCHER_FETCH_FROM_THIS();
 
 	ev_obj       = (php_ev_object *) zend_object_store_get_object(getThis() TSRMLS_CC);
 	stat_ptr     = (php_ev_stat *) PHP_EV_WATCHER_FETCH_FROM_OBJECT(ev_obj);
-	stat_watcher = (ev_stat *) stat_ptr;
+	w = (ev_stat *) stat_ptr;
 
 	PHP_EV_ASSERT(stat_ptr->path);
 	efree(stat_ptr->path);
 	stat_ptr->path = estrndup(path, path_len);
 
-	PHP_EV_WATCHER_RESET(ev_stat, stat_watcher, (stat_watcher, stat_ptr->path, interval));
+	PHP_EV_WATCHER_RESET(ev_stat, w, (w, stat_ptr->path, interval));
 }
 /* }}} */
 
 /* {{{ proto mixed EvStat::attr(void) */
 PHP_METHOD(EvStat, attr)
 {
-	ev_stat     *stat_watcher;
+	ev_stat     *w;
 	ev_statdata *st;
 
 	if (zend_parse_parameters_none() == FAILURE) {
 		return;
 	}
 
-	stat_watcher = (ev_stat *) PHP_EV_WATCHER_FETCH_FROM_THIS();
-	st           = &stat_watcher->attr;
+	w  = (ev_stat *) PHP_EV_WATCHER_FETCH_FROM_THIS();
+	st = &w->attr;
 
 	if (!st->st_nlink) {
 		errno = ENOENT;
 /* {{{ proto mixed EvStat::prev(void) */
 PHP_METHOD(EvStat, prev)
 {
-	ev_stat     *stat_watcher;
+	ev_stat     *w;
 	ev_statdata *st;
 
 	if (zend_parse_parameters_none() == FAILURE) {
 		return;
 	}
 
-	stat_watcher = (ev_stat *) PHP_EV_WATCHER_FETCH_FROM_THIS();
-	st           = &stat_watcher->prev;
+	w  = (ev_stat *) PHP_EV_WATCHER_FETCH_FROM_THIS();
+	st = &w->prev;
 
 	if (!st->st_nlink) {
 		errno = ENOENT;
 PHP_METHOD(EvStat, stat)
 {
 	php_ev_object *ev_obj;
-	ev_stat       *stat_watcher;
+	ev_stat       *w;
 	ev_statdata   *st;
 
 	if (zend_parse_parameters_none() == FAILURE) {
 		return;
 	}
 
-	ev_obj       = (php_ev_object *) zend_object_store_get_object(getThis() TSRMLS_CC);
-	stat_watcher = (ev_stat *) PHP_EV_WATCHER_FETCH_FROM_OBJECT(ev_obj);
+	ev_obj = (php_ev_object *) zend_object_store_get_object(getThis() TSRMLS_CC);
+	w      = (ev_stat *) PHP_EV_WATCHER_FETCH_FROM_OBJECT(ev_obj);
 
-	st = &stat_watcher->attr;
+	st = &w->attr;
 
-	ev_stat_stat(PHP_EV_LOOP_FETCH_FROM_OBJECT(ev_obj), stat_watcher);
+	ev_stat_stat(PHP_EV_LOOP_FETCH_FROM_OBJECT(ev_obj), w);
 
 	if (st->st_nlink) {
 		RETURN_TRUE;

tests/02_ev_construct_variation.phpt

 var_dump($loop->data);
 
 // Multiple attempts to create the default loop
-$loop2 = EvLoop::default_loop();
-$loop2 = EvLoop::default_loop();
-$loop2 = EvLoop::default_loop();
+$loop2 = EvLoop::defaultLoop();
+$loop2 = EvLoop::defaultLoop();
+$loop2 = EvLoop::defaultLoop();
 
 // Overwriting previously created loop with the default loop
-$loop = EvLoop::default_loop();
+$loop = EvLoop::defaultLoop();
 
 // Overwriting previously created default loop with new loop
 $loop = new EvLoop(EVFLAG_AUTO);
 string(12) "new new data"
 string(8) "new_data"
 
-Warning: EvLoop::default_loop(): Returning previously created default loop in %s on line %d
+Warning: EvLoop::defaultLoop(): Returning previously created default loop in %s on line %d
 
-Warning: EvLoop::default_loop(): Returning previously created default loop in %s on line %d
+Warning: EvLoop::defaultLoop(): Returning previously created default loop in %s on line %d
 
-Warning: EvLoop::default_loop(): Returning previously created default loop in %s on line %d
+Warning: EvLoop::defaultLoop(): Returning previously created default loop in %s on line %d
 NULL

tests/03_ev_loop_props.phpt

 	return FALSE;
 }
 
-$l                   = EvLoop::default_loop();
+$l                   = EvLoop::defaultLoop();
 $l->data             = "ld";
 $l->io_interval      = 1.1;
 $l->timeout_interval = 1.2;

tests/05_timer.phpt

 		use (&$id, &$prev, $base, $i, $t, $fudge) {
 			$now = ev_now();
 
-			EvLoop::default_loop()->verify();
+			ev_verify();
 
 			echo $now + $fudge >= $prev ? "" : "not ", "ok ", ++$id,
 				" # t0 $i $now + $fudge >= $prev\n";
 		use (&$id, &$prev, $base, $i, $t) {
 			$now = ev_now();
 
-			EvLoop::default_loop()->verify();
+			ev_verify();
 
 			echo $now >= $prev ? "" : "not ", "ok ", ++$id,
 				" # p0 $i $now >= $prev\n";

tests/06_keepalive.phpt

 <?php 
 error_reporting(0);
 
-$timer = new EvTimer(1, 0.3, function ($w, $r) {
+$timer = EvTimer::createStopped(1, 0.3, function ($w, $r) {
 	echo "ok 7\n";
 	$w->stop();
 });
-
 $timer->keepalive(1);
 
 echo "ok 1\n";
 ev_run();
 echo "ok 5\n";
 
+
 $timer->keepalive(1);
 
 echo "ok 6\n";
 ok 1
 ok 2
 ok 3
+ok 4
 ok 5
 ok 6
 ok 7

tests/08_priority.phpt

 <?php 
 error_reporting(0);
 
-$loop = EvLoop::default_loop();
-
 $t0 = new EvTimer(-1, 0, function ($w, $r) { echo "ok 4\n"; });
 $t_ = new EvTimer(-1, 0, function ($w, $r) { echo "ok 5\n"; });
 $t_->priority = -1;
 $t1 = new EvTimer(-1, 0, function ($w, $r) { echo "ok 3\n"; });
 $t1->priority = 1;
 
-$i2 = new EvIdle(function ($w, $r) use ($loop) { echo $loop->iteration == 1 ? "" : "not ", "ok 2\n"; $w->stop(); });
+$i2 = new EvIdle(function ($w, $r) { ev_iteration() == 1 ? "" : "not ", "ok 2\n"; $w->stop(); });
 $i2->priority = 10;
-$i3 = new EvIdle(function ($w, $r) use ($loop) { echo $loop->iteration == 3 ? "" : "not ", "ok 7\n"; $w->stop(); });
-$i1 = new EvIdle(function ($w, $r) use ($loop) { echo $loop->iteration == 2 ? "" : "not ", "ok 6\n"; $w->stop(); });
+$i3 = new EvIdle(function ($w, $r) { ev_iteration() == 3 ? "" : "not ", "ok 7\n"; $w->stop(); });
+$i1 = new EvIdle(function ($w, $r) { ev_iteration() == 2 ? "" : "not ", "ok 6\n"; $w->stop(); });
 $i1->priority = 1;
-$i_ = new EvIdle(function ($w, $r) use ($loop) { echo $loop->iteration == 4 ? "" : "not ", "ok 8\n"; $w->stop(); });
+$i_ = new EvIdle(function ($w, $r) { ev_iteration() == 4 ? "" : "not ", "ok 8\n"; $w->stop(); });
 $i_->priority = -1;
 
 $t0->start();
 #include "watcher.h"
 
 /* {{{ php_ev_timer_object_ctor */
-void php_ev_timer_object_ctor(INTERNAL_FUNCTION_PARAMETERS, zval *loop)
+void php_ev_timer_object_ctor(INTERNAL_FUNCTION_PARAMETERS, zval *loop, zend_bool ctor, zend_bool start)
 {
 	double         after;
 	double         repeat;
 	zval          *self          = NULL;
 	php_ev_object *o_self;
 	php_ev_object *o_loop;
-	ev_timer      *timer_watcher;
+	ev_timer      *w;
 
 	zval                  *data       = NULL;
 	zend_fcall_info        fci        = empty_fcall_info;
 
 	PHP_EV_CHECK_REPEAT(repeat);
 
-	/* If loop is NULL, then we're in __construct() */
-	if (loop) {
+	if (ctor) {
+		self = getThis();
+	} else {
 		PHP_EV_INIT_CLASS_OBJECT(return_value, ev_timer_class_entry_ptr);
-
-		PHP_EV_ASSERT((self == NULL));
 		self = return_value; 
-	} else {
+	}
+
+	if (!loop) {
 		loop = php_ev_default_loop(TSRMLS_C);
-		self = getThis();
 	}
 
-	o_self        = (php_ev_object *) zend_object_store_get_object(self TSRMLS_CC);
-	o_loop        = (php_ev_object *) zend_object_store_get_object(loop TSRMLS_CC);
-	timer_watcher = (ev_timer *) php_ev_new_watcher(sizeof(ev_timer), self,
+	o_self = (php_ev_object *) zend_object_store_get_object(self TSRMLS_CC);
+	o_loop = (php_ev_object *) zend_object_store_get_object(loop TSRMLS_CC);
+	w      = (ev_timer *) php_ev_new_watcher(sizeof(ev_timer), self, 
 			PHP_EV_LOOP_OBJECT_FETCH_FROM_OBJECT(o_loop),
 			&fci, &fcc, data, priority TSRMLS_CC);
 
-	timer_watcher->type = EV_TIMER;
+	w->type = EV_TIMER;
 	
-	ev_timer_set(timer_watcher, after, repeat);
+	ev_timer_set(w, after, repeat);
 
-	o_self->ptr = (void *) timer_watcher;
+	o_self->ptr = (void *) w;
+
+	if (start) {
+		PHP_EV_WATCHER_START(ev_timer, (ev_watcher *) w);
+	}
 }
 /* }}} */
 
 /* {{{ proto EvTimer::__construct(double after, double repeat, callable callback[, mixed data = NULL[, int priority = 0]]) */
 PHP_METHOD(EvTimer, __construct)
 {
-	php_ev_timer_object_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, NULL);
+	PHP_EV_WATCHER_CTOR(timer, NULL);
+}
+/* }}} */
+
+/* {{{ proto EvTimer::createStopped(double after, double repeat, callable callback[, mixed data = NULL[, int priority = 0]]) */
+PHP_METHOD(EvTimer, createStopped)
+{
+	PHP_EV_WATCHER_FACTORY_NS(timer, NULL);
 }
 /* }}} */
 
 /* {{{ proto void EvTimer::set(double after, double repeat) */
 PHP_METHOD(EvTimer, set)
 {
-	double      after;
-	double      repeat;
-	ev_timer   *timer_watcher;
+	double    after;
+	double    repeat;
+	ev_timer *w;
 
 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "dd",
 				&after, &repeat) == FAILURE) {
 
 	PHP_EV_CHECK_REPEAT(repeat);
 
-	timer_watcher = (ev_timer *) PHP_EV_WATCHER_FETCH_FROM_THIS();
+	w = (ev_timer *) PHP_EV_WATCHER_FETCH_FROM_THIS();
 
-	PHP_EV_WATCHER_RESET(ev_timer, timer_watcher, (timer_watcher, after, repeat));
+	PHP_EV_WATCHER_RESET(ev_timer, w, (w, after, repeat));
 }
 /* }}} */
 
 /* {{{ proto void EvTimer::again(void) */
 PHP_METHOD(EvTimer, again)
 {
-	ev_timer *timer_watcher;
+	ev_timer *w;
 
 	if (zend_parse_parameters_none() == FAILURE) {
 		return;
 	}
 
-	timer_watcher = (ev_timer *) PHP_EV_WATCHER_FETCH_FROM_THIS();
+	w = (ev_timer *) PHP_EV_WATCHER_FETCH_FROM_THIS();
 
-	PHP_EV_CHECK_REPEAT(timer_watcher->repeat);
+	PHP_EV_CHECK_REPEAT(w->repeat);
 
-	ev_timer_again(php_ev_watcher_loop_ptr(timer_watcher), timer_watcher);
-	PHP_EV_WATCHER_UNREF(timer_watcher); 
+	ev_timer_again(php_ev_watcher_loop_ptr(w), w);
+	PHP_EV_WATCHER_UNREF(w); 
 }
 /* }}} */
 
 	w = PHP_EV_WATCHER_FETCH_FROM_THIS();
 
 	/* Returning previous state */
-	RETVAL_LONG((w->e_flags & PHP_EV_WATCHER_FLAG_KEEP_ALIVE));
+	RETVAL_LONG((php_ev_watcher_flags(w) & PHP_EV_WATCHER_FLAG_KEEP_ALIVE));
 	n_value = n_value ? PHP_EV_WATCHER_FLAG_KEEP_ALIVE : 0;
 
 	/* Update watcher flags, if keepalive flag changed */
-	if ((n_value ^ w->e_flags) & PHP_EV_WATCHER_FLAG_KEEP_ALIVE) {
-		w->e_flags = (w->e_flags & ~PHP_EV_WATCHER_FLAG_KEEP_ALIVE) | n_value;
+	if (ZEND_NUM_ARGS() > 0
+			&& ((n_value ^ php_ev_watcher_flags(w)) & PHP_EV_WATCHER_FLAG_KEEP_ALIVE)) {
+		php_ev_watcher_flags(w) = (php_ev_watcher_flags(w) & ~PHP_EV_WATCHER_FLAG_KEEP_ALIVE) | n_value;
 		PHP_EV_WATCHER_REF(w);
 		PHP_EV_WATCHER_UNREF(w);
 	}
             PHP_EV_WATCHER_START(ev_signal, w);  \
     } while (0)
 
+#define PHP_EV_WATCHER_CTOR(type, zloop) \
+	php_ev_ ## type ## _object_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, zloop, TRUE, TRUE)
+#define PHP_EV_WATCHER_FACTORY(type, zloop) \
+	php_ev_ ## type ## _object_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, zloop, FALSE, TRUE)
+#define PHP_EV_WATCHER_FACTORY_NS(type, zloop) \
+	php_ev_ ## type ## _object_ctor(INTERNAL_FUNCTION_PARAM_PASSTHRU, zloop, FALSE, FALSE)
+
 
 void php_ev_watcher_callback(EV_P_ ev_watcher *watcher, int revents);
 void php_ev_set_watcher(ev_watcher *w, size_t size, zval *self, php_ev_loop *loop,
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.