예제 #1
0
/*
 * suspend current coroutine
 */
static PHP_METHOD(swoole_coroutine_util, yield)
{
    int cid = sw_get_current_cid();
    if (cid < 0)
    {
        swoole_php_fatal_error(E_ERROR, "can not yield outside coroutine");
        RETURN_FALSE;
    }

    swLinkedList *coros_list = swHashMap_find_int(defer_coros, cid);
    if (coros_list == NULL)
    {
        coros_list = swLinkedList_new(2, NULL);
        if (coros_list == NULL)
        {
            RETURN_FALSE;
        }
        if (swHashMap_add_int(defer_coros, cid, coros_list) == SW_ERR)
        {
            swLinkedList_free(coros_list);
            RETURN_FALSE;
        }
    }

    php_context *context = emalloc(sizeof(php_context));
    coro_save(context);
    if (swLinkedList_append(coros_list, (void *) context) == SW_ERR)
    {
        efree(context);
        RETURN_FALSE;
    }
    coro_yield();
}
예제 #2
0
static PHP_METHOD(swoole_coroutine_util, suspend)
{
    char *id;
    int id_len;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s",&id, &id_len) == FAILURE)
    {
        return;
    }

    swLinkedList *coros_list = swHashMap_find(defer_coros, id, id_len);
    if (coros_list == NULL)
    {
        coros_list = swLinkedList_new(2, NULL);
        if (coros_list == NULL)
        {
            RETURN_FALSE;
        }
        if (swHashMap_add(defer_coros, id, id_len, coros_list) == SW_ERR)
        {
            swLinkedList_free(coros_list);
            RETURN_FALSE;
        }
    }

    php_context *context = emalloc(sizeof(php_context));
    coro_save(context);
    if (swLinkedList_append(coros_list, (void *)context) == SW_ERR) {
        efree(context);
        RETURN_FALSE;
    }
    coro_yield();
}