ret_t
cherokee_handler_proxy_poll_get (cherokee_handler_proxy_poll_t  *poll,
				 cherokee_handler_proxy_conn_t **pconn,
				 cherokee_source_t              *src)
{
	ret_t            ret;
	cherokee_list_t *i;

	CHEROKEE_MUTEX_LOCK (&poll->mutex);

	if (poll->reuse_len > 0) {
		/* Reuse a prev connection */
		poll->reuse_len -= 1;

		i = poll->reuse.prev;
		cherokee_list_del (i);
		cherokee_list_add (i, &poll->active);

		*pconn = PROXY_CONN(i);
	} else {
		cherokee_handler_proxy_conn_t *n;

		/* Create a new connection */
		ret = cherokee_handler_proxy_conn_new (&n);
		if (ret != ret_ok)
			goto error;

		ret = cherokee_proxy_util_init_socket (&n->socket, src);
		if (ret != ret_ok) {
			cherokee_handler_proxy_conn_free (n);
			goto error;
		}

		cherokee_list_add (&n->listed, &poll->active);
		n->poll_ref = poll;
		*pconn = n;
	}

	CHEROKEE_MUTEX_UNLOCK (&poll->mutex);
	return ret_ok;
error:
	CHEROKEE_MUTEX_UNLOCK (&poll->mutex);
	return ret_error;
}
Exemplo n.º 2
0
ret_t
cherokee_list_invert (cherokee_list_t *head)
{
	cherokee_list_t *i, *tmp;
	cherokee_list_t new_list;

	INIT_LIST_HEAD (&new_list);

	list_for_each_safe (i, tmp, head) {
		cherokee_list_add (i, &new_list);
	}
static ret_t
poll_release (cherokee_handler_proxy_poll_t *poll,
	      cherokee_handler_proxy_conn_t *pconn)
{
	/* Not longer an active connection */
	cherokee_list_del (&pconn->listed);

	/* Don't reuse connection w/o keep-alive
	 */
	if (! pconn->keepalive_in) {
		cherokee_handler_proxy_conn_free (pconn);
		return ret_ok;
	}

	/* If the reuse-list is full, dispose the oldest obj
	 */
	if (poll->reuse_len > poll->reuse_max) {
		cherokee_handler_proxy_conn_t *oldest;

		oldest = PROXY_CONN(poll->reuse.prev);
		cherokee_list_del (&oldest->listed);
		poll->reuse_len -= 1;

		cherokee_handler_proxy_conn_free (oldest);
	}

	/* Clean up
	 */
	pconn->keepalive_in = false;
	pconn->size_in      = 0;
	pconn->sent_out     = 0;
	pconn->enc          = pconn_enc_none;

	pconn->post.do_buf_sent = true;
	pconn->post.sent        = 0;

	cherokee_buffer_clean (&pconn->post.buf_temp);
	cherokee_buffer_clean (&pconn->header_in_raw);

	/* Store it to be reused
	 */
	poll->reuse_len += 1;
	cherokee_list_add (&pconn->listed, &poll->reuse);

	return ret_ok;
}
Exemplo n.º 4
0
static ret_t
thread_launch (cherokee_list_t *threads, int num)
{
	int              i;
	int              re;
	cb_thread_t     *thread;
	cherokee_list_t *item;
	pthread_attr_t   attr;

	/* Create threads
	 */
	for (i=0; i<num; i++) {
		ALLOCATE (thread, cb_thread_t);

		INIT_LIST_HEAD (&thread->entry);
		cherokee_list_add (&thread->entry, threads);
		thread->curl = NULL;

		pthread_mutex_init (&thread->start_mutex, NULL);
		pthread_mutex_lock (&thread->start_mutex);

		pthread_attr_init (&attr);
		pthread_attr_setstacksize (&attr, THREAD_STACK_SIZE);

		re = pthread_create (&thread->pthread, &attr, thread_routine, thread);
		if (re != 0) {
			PRINT_ERROR_S ("Couldn't create pthread\n");

			free (thread);
			return ret_error;
		}
	}

	/* Activate threads
	 */
	list_for_each (item, threads) {
		pthread_mutex_unlock (&((cb_thread_t *)(item))->start_mutex);
	}