Exemple #1
0
pid_t swProcessPool_spawn(swWorker *worker)
{
	pid_t pid = fork();
	swProcessPool *pool = worker->pool;

	switch (pid)
	{
	//child
	case 0:
		if(pool->onWorkerStart != NULL)
		{
			pool->onWorkerStart(pool, worker->id);
		}
		exit(pool->main_loop(pool, worker));
		break;
	case -1:
		swWarn("[swProcessPool_run] fork failed. Error: %s [%d]", strerror(errno), errno);
		break;
		//parent
	default:
		worker->pid = pid;
		swHashMap_add_int(&pool->map, pid, worker);
		break;
	}
	return pid;
}
Exemple #2
0
int swTimer_add(swTimer *timer, int ms)
{
	swTimer_node *node = sw_malloc(sizeof(swTimer_node));
	if (node == NULL)
	{
		swWarn("malloc failed.");
		return SW_ERR;
	}

	bzero(node, sizeof(swTimer_node));
	node->lasttime = swTimer_get_ms();
	node->interval = ms;

	if (ms < timer->interval)
	{
		int new_interval = swoole_common_divisor(ms, timer->interval);
		timer->interval = new_interval;
		if (SwooleG.use_timerfd)
		{
			swTimer_timerfd_set(timer, new_interval);
		}
		else
		{
			swTimer_signal_set(timer, new_interval);
		}
	}
	swHashMap_add_int(timer->list, ms, node);
	timer->num++;
	return SW_OK;
}
Exemple #3
0
static long swTimer_add(swTimer *timer, int msec, int interval, void *data)
{
    if (interval == 0)
    {
        return swTimer_addtimeout(timer, msec, data);
    }
    swTimer_node *node = sw_malloc(sizeof(swTimer_node));
    if (node == NULL)
    {
        swWarn("malloc failed.");
        return SW_ERR;
    }

    bzero(node, sizeof(swTimer_node));
    node->interval = msec;
    if (gettimeofday(&node->lasttime, NULL) < 0)
    {
        swSysError("gettimeofday() failed.");
        return SW_ERR;
    }
    if (msec < timer->interval)
    {
        int new_interval = swoole_common_divisor(msec, timer->interval);
        timer->interval = new_interval;
        swTimer_set(timer, new_interval);
    }
    swHashMap_add_int(timer->list, msec, node, NULL);
    timer->num++;
    return SW_OK;
}
/*
 * 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();
}
Exemple #5
0
int swTimer_add(swTimer *timer, int ms)
{
	swTimer_node *node = sw_malloc(sizeof(swTimer_node));
	if (node == NULL)
	{
		swWarn("swTimer_add malloc fail");
		return SW_ERR;
	}
	bzero(node, sizeof(swTimer_node));
	node->lasttime = swTimer_get_ms();
	node->interval = ms;

	if(ms < timer->interval)
	{
		int new_interval = swoole_common_divisor(ms, timer->interval);
		timer->interval = new_interval;
#if defined(HAVE_TIMERFD) && SW_WORKER_IPC_MODE == 1
		swTimer_timerfd_set(timer, new_interval);
#else
		swTimer_signal_set(timer, new_interval);
#endif
	}
	swHashMap_add_int(&timer->list, ms, node);
	timer->num++;
	return SW_OK;
}
Exemple #6
0
swDataBuffer_item* swDataBuffer_newItem(swDataBuffer *data_buffer, int fd, int trunk_size)
{
	swDataBuffer_item *newItem = sw_malloc(sizeof(swDataBuffer_item));
	//内存分配失败
	if (newItem == NULL)
	{
		swWarn("malloc for newItem failed. Error: %s[%d]", strerror(errno), errno);
		return NULL;
	}

	bzero(newItem, sizeof(swDataBuffer_item));

	//创建item时,自动建立一个trunk
	swDataBuffer_trunk *newTrunk = swDataBuffer_newTrunk(data_buffer, newItem);
	if (newTrunk == NULL)
	{
		sw_free(newItem);
		swWarn("malloc for newTrunk failed. Error: %s[%d]", strerror(errno), errno);
		return NULL;
	}

	newItem->fd = fd;
	swHashMap_add_int(&data_buffer->map, fd, newItem);

	return newItem;
}
Exemple #7
0
int swDataBuffer_clear(swDataBuffer *data_buffer, int fd)
{
	swDataBuffer_item *item = NULL;
	swHashMap_add_int(&data_buffer->map, fd, item);
	if (item == NULL)
	{
		swTrace("buffer item not found\n");
		return SW_ERR;
	}
	else
	{
		swDataBuffer_trunk *trunk = item->head;
		swDataBuffer_trunk *will_free_trunk; //保存trunk的指针,用于释放内存
		while (trunk != NULL)
		{
			sw_free(trunk->data);
			will_free_trunk = trunk;
			trunk = trunk->next;
			sw_free(will_free_trunk);
		}
		swHashMap_del_int(&data_buffer->map, fd);
		sw_free(item);
	}
	return SW_OK;
}
Exemple #8
0
static http_client* http_client_new(int fd TSRMLS_DC)
{
    http_client *client = emalloc(sizeof(http_client));
    bzero(client, sizeof(http_client));
    client->fd = fd;
    swHashMap_add_int(php_sw_http_clients, fd, client, NULL);
    return client;
}
Exemple #9
0
void swTimeWheel_add(swTimeWheel *tw, swConnection *conn)
{
    uint16_t index = tw->current == 0 ? tw->size - 1 : tw->current - 1;
    swHashMap *new_set = tw->wheel[index];
    swHashMap_add_int(new_set, conn->fd, conn);

    conn->timewheel_index = index;

    swTraceLog(SW_TRACE_REACTOR, "current=%d, fd=%d, index=%d.", tw->current, conn->fd, index);
}
Exemple #10
0
void swTimeWheel_update(swTimeWheel *tw, swConnection *conn)
{
    uint16_t new_index = swTimeWheel_new_index(tw);
    swHashMap *new_set = tw->wheel[new_index];
    swHashMap_add_int(new_set, conn->fd, conn);

    swHashMap *old_set = tw->wheel[conn->timewheel_index];
    swHashMap_del_int(old_set, conn->fd);

    swTraceLog(SW_TRACE_REACTOR, "current=%d, fd=%d, old_index=%d, new_index=%d.", tw->current, conn->fd, new_index, conn->timewheel_index);

    conn->timewheel_index = new_index;
}
Exemple #11
0
int swTimer_add(swTimer *timer, int ms)
{
	swTimer_node *node = sw_malloc(sizeof(swTimer_node));
	if (node == NULL)
	{
		swWarn("swTimer_add malloc fail");
		return SW_ERR;
	}
	bzero(node, sizeof(swTimer_node));
	node->lasttime = swTimer_get_ms();
	node->interval = ms;
	swHashMap_add_int(&timer->list, ms, node);
	timer->num++;
	return SW_OK;
}
Exemple #12
0
static swTimer_node* swTimer_add(swTimer *timer, int _msec, int interval, void *data, swTimerCallback callback)
{
    swTimer_node *tnode = sw_malloc(sizeof(swTimer_node));
    if (!tnode)
    {
        swSysError("malloc(%ld) failed.", sizeof(swTimer_node));
        return NULL;
    }

    int64_t now_msec = swTimer_get_relative_msec();
    if (now_msec < 0)
    {
        sw_free(tnode);
        return NULL;
    }

    tnode->data = data;
    tnode->type = SW_TIMER_TYPE_KERNEL;
    tnode->exec_msec = now_msec + _msec;
    tnode->interval = interval ? _msec : 0;
    tnode->remove = 0;
    tnode->callback = callback;
    tnode->round = timer->round;

    if (timer->_next_msec < 0 || timer->_next_msec > _msec)
    {
        timer->set(timer, _msec);
        timer->_next_msec = _msec;
    }

    tnode->id = timer->_next_id++;
    if (unlikely(tnode->id < 0))
    {
        tnode->id = 1;
        timer->_next_id = 2;
    }
    timer->num++;

    tnode->heap_node = swHeap_push(timer->heap, tnode->exec_msec, tnode);
    if (tnode->heap_node == NULL)
    {
        sw_free(tnode);
        return NULL;
    }
    swTrace("id=%ld, exec_msec=%" PRId64 ", msec=%d, round=%" PRIu64, tnode->id, tnode->exec_msec, _msec, tnode->round);
    swHashMap_add_int(timer->map, tnode->id, tnode);
    return tnode;
}
Exemple #13
0
pid_t swProcessPool_spawn(swWorker *worker)
{
    pid_t pid = fork();
    swProcessPool *pool = worker->pool;

    switch (pid)
    {
    //child
    case 0:
        /**
         * Process start
         */
        if (pool->onWorkerStart != NULL)
        {
            pool->onWorkerStart(pool, worker->id);
        }
        /**
         * Process main loop
         */
        int ret_code = pool->main_loop(pool, worker);
        /**
         * Process stop
         */
        if (pool->onWorkerStop != NULL)
        {
            pool->onWorkerStop(pool, worker->id);
        }
        exit(ret_code);
        break;
    case -1:
        swWarn("fork() failed. Error: %s [%d]", strerror(errno), errno);
        break;
        //parent
    default:
        //remove old process
        if (worker->pid)
        {
            swHashMap_del_int(pool->map, worker->pid);
        }
        worker->deleted = 0;
        worker->pid = pid;
        //insert new process
        swHashMap_add_int(pool->map, pid, worker);
        break;
    }
    return pid;
}
Exemple #14
0
static pid_t swProcessPool_spawn(swProcessPool *ma, swWorker *worker)
{
	pid_t pid = fork();
	switch (pid)
	{
	//child
	case 0:
		worker->call(worker);
		exit(0);
		break;
	case -1:
		swWarn("[swProcessPool_run] fork fail. Error: %s [%d]", strerror(errno), errno)
		;
		break;
		//parent
	default:
		worker->pid = pid;
		swHashMap_add_int(&ma->map, pid, worker);
		break;
	}
	return pid;
}
Exemple #15
0
/**
 * add a worker to pool
 */
int swProcessPool_add_worker(swProcessPool *pool, swWorker *worker)
{
    swHashMap_add_int(pool->map, worker->pid, worker);
    return SW_OK;
}