forked from cory/tildefriends
Move sending history streams to the worker threads.
git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4499 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
69
src/ssb.c
69
src/ssb.c
@ -257,6 +257,7 @@ typedef struct _tf_ssb_connection_t
|
||||
uv_tcp_t tcp;
|
||||
uv_connect_t connect;
|
||||
uv_async_t async;
|
||||
bool closing;
|
||||
|
||||
tf_ssb_connection_t* tunnel_connection;
|
||||
int32_t tunnel_request_number;
|
||||
@ -318,6 +319,7 @@ typedef struct _tf_ssb_connection_t
|
||||
int scheduled_count;
|
||||
|
||||
tf_ssb_debug_message_t* debug_messages[k_debug_close_message_count];
|
||||
int ref_count;
|
||||
} tf_ssb_connection_t;
|
||||
|
||||
static JSClassID _connection_class_id;
|
||||
@ -1767,6 +1769,7 @@ static void _tf_ssb_connection_dispatch_scheduled(tf_ssb_connection_t* connectio
|
||||
static void _tf_ssb_connection_destroy(tf_ssb_connection_t* connection, const char* reason)
|
||||
{
|
||||
tf_ssb_t* ssb = connection->ssb;
|
||||
connection->closing = true;
|
||||
if (!connection->destroy_reason)
|
||||
{
|
||||
connection->destroy_reason = reason;
|
||||
@ -1851,7 +1854,8 @@ static void _tf_ssb_connection_destroy(tf_ssb_connection_t* connection, const ch
|
||||
if (JS_IsUndefined(connection->object) &&
|
||||
!connection->async.data &&
|
||||
!connection->tcp.data &&
|
||||
!connection->connect.data)
|
||||
!connection->connect.data &&
|
||||
connection->ref_count == 0)
|
||||
{
|
||||
JS_FreeValue(ssb->context, connection->ebt_send_clock);
|
||||
connection->ebt_send_clock = JS_UNDEFINED;
|
||||
@ -3646,3 +3650,66 @@ void tf_ssb_set_main_thread(tf_ssb_t* ssb)
|
||||
{
|
||||
ssb->thread_self = uv_thread_self();
|
||||
}
|
||||
|
||||
typedef struct _connection_work_t
|
||||
{
|
||||
uv_work_t work;
|
||||
tf_ssb_connection_t* connection;
|
||||
void (*work_callback)(tf_ssb_connection_t* connection, void* user_data);
|
||||
void (*after_work_callback)(tf_ssb_connection_t* connection, int result, void* user_data);
|
||||
void* user_data;
|
||||
} connection_work_t;
|
||||
|
||||
static void _tf_ssb_connection_work_callback(uv_work_t* work)
|
||||
{
|
||||
connection_work_t* data = work->data;
|
||||
tf_ssb_record_thread_busy(data->connection->ssb, true);
|
||||
if (data->work_callback)
|
||||
{
|
||||
data->work_callback(data->connection, data->user_data);
|
||||
}
|
||||
tf_ssb_record_thread_busy(data->connection->ssb, false);
|
||||
}
|
||||
|
||||
static void _tf_ssb_connection_after_work_callback(uv_work_t* work, int status)
|
||||
{
|
||||
connection_work_t* data = work->data;
|
||||
if (data->after_work_callback)
|
||||
{
|
||||
data->after_work_callback(data->connection, status, data->user_data);
|
||||
}
|
||||
data->connection->ref_count--;
|
||||
if (data->connection->ref_count == 0 &&
|
||||
data->connection->closing)
|
||||
{
|
||||
_tf_ssb_connection_destroy(data->connection, "work completed");
|
||||
}
|
||||
tf_free(data);
|
||||
}
|
||||
|
||||
void tf_ssb_connection_run_work(
|
||||
tf_ssb_connection_t* connection,
|
||||
void (*work_callback)(tf_ssb_connection_t* connection, void* user_data),
|
||||
void (*after_work_callback)(tf_ssb_connection_t* connection, int result, void* user_data),
|
||||
void* user_data)
|
||||
{
|
||||
connection_work_t* work = tf_malloc(sizeof(connection_work_t));
|
||||
*work = (connection_work_t)
|
||||
{
|
||||
.work =
|
||||
{
|
||||
.data = work,
|
||||
},
|
||||
.connection = connection,
|
||||
.work_callback = work_callback,
|
||||
.after_work_callback = after_work_callback,
|
||||
.user_data = user_data,
|
||||
};
|
||||
|
||||
connection->ref_count++;
|
||||
int result = uv_queue_work(connection->ssb->loop, &work->work, _tf_ssb_connection_work_callback, _tf_ssb_connection_after_work_callback);
|
||||
if (result)
|
||||
{
|
||||
_tf_ssb_connection_after_work_callback(&work->work, result);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user