Refactor most uses of uv_queue_work to go through a helper that keeps track of thread business, traces, and is generally less code.
This commit is contained in:
134
src/ssb.c
134
src/ssb.c
@ -765,7 +765,14 @@ void tf_ssb_connection_rpc_send(tf_ssb_connection_t* connection, uint8_t flags,
|
||||
}
|
||||
else if (!_tf_ssb_connection_get_request_callback(connection, request_number, NULL, NULL))
|
||||
{
|
||||
tf_printf("Dropping message with no active request (%d): %.*s\n", request_number, (int)size, message);
|
||||
if (flags & k_ssb_rpc_flag_binary)
|
||||
{
|
||||
tf_printf("Dropping message with no active request (%d): (%zd bytes).\n", request_number, size);
|
||||
}
|
||||
else
|
||||
{
|
||||
tf_printf("Dropping message with no active request (%d): %.*s\n", request_number, (int)size, message);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@ -3714,6 +3721,8 @@ typedef struct _connection_work_t
|
||||
{
|
||||
uv_work_t work;
|
||||
tf_ssb_connection_t* connection;
|
||||
const char* name;
|
||||
const char* after_name;
|
||||
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;
|
||||
@ -3725,7 +3734,9 @@ static void _tf_ssb_connection_work_callback(uv_work_t* work)
|
||||
tf_ssb_record_thread_busy(data->connection->ssb, true);
|
||||
if (data->work_callback)
|
||||
{
|
||||
tf_trace_begin(data->connection->ssb->trace, data->name);
|
||||
data->work_callback(data->connection, data->user_data);
|
||||
tf_trace_end(data->connection->ssb->trace);
|
||||
}
|
||||
tf_ssb_record_thread_busy(data->connection->ssb, false);
|
||||
}
|
||||
@ -3735,13 +3746,17 @@ 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)
|
||||
{
|
||||
tf_trace_begin(data->connection->ssb->trace, data->after_name);
|
||||
data->after_work_callback(data->connection, status, data->user_data);
|
||||
tf_trace_end(data->connection->ssb->trace);
|
||||
}
|
||||
data->connection->ref_count--;
|
||||
if (data->connection->ref_count == 0 && data->connection->closing)
|
||||
{
|
||||
_tf_ssb_connection_destroy(data->connection, "work completed");
|
||||
}
|
||||
tf_free((void*)data->name);
|
||||
tf_free((void*)data->after_name);
|
||||
tf_free(data);
|
||||
}
|
||||
|
||||
@ -3769,6 +3784,70 @@ void tf_ssb_connection_run_work(tf_ssb_connection_t* connection, void (*work_cal
|
||||
}
|
||||
}
|
||||
|
||||
typedef struct _ssb_work_t
|
||||
{
|
||||
uv_work_t work;
|
||||
tf_ssb_t* ssb;
|
||||
const char* name;
|
||||
const char* after_name;
|
||||
void (*work_callback)(tf_ssb_t* ssb, void* user_data);
|
||||
void (*after_work_callback)(tf_ssb_t* ssb, int result, void* user_data);
|
||||
void* user_data;
|
||||
} ssb_work_t;
|
||||
|
||||
static void _tf_ssb_work_callback(uv_work_t* work)
|
||||
{
|
||||
ssb_work_t* data = work->data;
|
||||
tf_ssb_record_thread_busy(data->ssb, true);
|
||||
if (data->work_callback)
|
||||
{
|
||||
tf_trace_begin(data->ssb->trace, data->name);
|
||||
data->work_callback(data->ssb, data->user_data);
|
||||
tf_trace_end(data->ssb->trace);
|
||||
}
|
||||
tf_ssb_record_thread_busy(data->ssb, false);
|
||||
}
|
||||
|
||||
static void _tf_ssb_after_work_callback(uv_work_t* work, int status)
|
||||
{
|
||||
ssb_work_t* data = work->data;
|
||||
if (data->after_work_callback)
|
||||
{
|
||||
tf_trace_begin(data->ssb->trace, data->after_name);
|
||||
data->after_work_callback(data->ssb, status, data->user_data);
|
||||
tf_trace_end(data->ssb->trace);
|
||||
}
|
||||
tf_ssb_unref(data->ssb);
|
||||
tf_free((void*)data->name);
|
||||
tf_free((void*)data->after_name);
|
||||
tf_free(data);
|
||||
}
|
||||
|
||||
void tf_ssb_run_work(tf_ssb_t* ssb, void (*work_callback)(tf_ssb_t* ssb, void* user_data), void (*after_work_callback)(tf_ssb_t* ssb, int result, void* user_data), void* user_data)
|
||||
{
|
||||
ssb_work_t* work = tf_malloc(sizeof(ssb_work_t));
|
||||
*work = (ssb_work_t)
|
||||
{
|
||||
.work =
|
||||
{
|
||||
.data = work,
|
||||
},
|
||||
.name = tf_util_function_to_string(work_callback),
|
||||
.after_name = tf_util_function_to_string(after_work_callback),
|
||||
.ssb = ssb,
|
||||
.work_callback = work_callback,
|
||||
.after_work_callback = after_work_callback,
|
||||
.user_data = user_data,
|
||||
};
|
||||
|
||||
tf_ssb_ref(ssb);
|
||||
int result = uv_queue_work(ssb->loop, &work->work, _tf_ssb_work_callback, _tf_ssb_after_work_callback);
|
||||
if (result)
|
||||
{
|
||||
_tf_ssb_connection_after_work_callback(&work->work, result);
|
||||
}
|
||||
}
|
||||
|
||||
bool tf_ssb_is_room(tf_ssb_t* ssb)
|
||||
{
|
||||
return ssb->is_room;
|
||||
@ -3792,8 +3871,6 @@ void tf_ssb_set_room_name(tf_ssb_t* ssb, const char* room_name)
|
||||
|
||||
typedef struct _update_settings_t
|
||||
{
|
||||
uv_work_t work;
|
||||
tf_ssb_t* ssb;
|
||||
bool is_room;
|
||||
char room_name[1024];
|
||||
} update_settings_t;
|
||||
@ -3847,58 +3924,35 @@ static bool _get_global_setting_bool(tf_ssb_t* ssb, const char* name, bool defau
|
||||
return result;
|
||||
}
|
||||
|
||||
static void _tf_ssb_update_settings_work(uv_work_t* work)
|
||||
static void _tf_ssb_update_settings_work(tf_ssb_t* ssb, void* user_data)
|
||||
{
|
||||
update_settings_t* update = work->data;
|
||||
tf_ssb_record_thread_busy(update->ssb, true);
|
||||
update->is_room = _get_global_setting_bool(update->ssb, "room", true);
|
||||
_get_global_setting_string(update->ssb, "room_name", update->room_name, sizeof(update->room_name));
|
||||
tf_ssb_record_thread_busy(update->ssb, false);
|
||||
update_settings_t* update = user_data;
|
||||
update->is_room = _get_global_setting_bool(ssb, "room", true);
|
||||
_get_global_setting_string(ssb, "room_name", update->room_name, sizeof(update->room_name));
|
||||
}
|
||||
|
||||
static void _tf_ssb_update_settings_after_work(uv_work_t* work, int result)
|
||||
static void _tf_ssb_update_settings_after_work(tf_ssb_t* ssb, int result, void* user_data)
|
||||
{
|
||||
update_settings_t* update = work->data;
|
||||
tf_ssb_unref(update->ssb);
|
||||
tf_ssb_set_is_room(update->ssb, update->is_room);
|
||||
tf_ssb_set_room_name(update->ssb, update->room_name);
|
||||
_tf_ssb_start_update_settings(update->ssb);
|
||||
update_settings_t* update = user_data;
|
||||
tf_ssb_set_is_room(ssb, update->is_room);
|
||||
tf_ssb_set_room_name(ssb, update->room_name);
|
||||
_tf_ssb_start_update_settings(ssb);
|
||||
tf_free(update);
|
||||
}
|
||||
|
||||
static void _tf_ssb_start_update_settings_timer(tf_ssb_t* ssb, void* user_data)
|
||||
{
|
||||
update_settings_t* update = tf_malloc(sizeof(update_settings_t));
|
||||
*update = (update_settings_t)
|
||||
{
|
||||
.work =
|
||||
{
|
||||
.data = update,
|
||||
},
|
||||
.ssb = ssb,
|
||||
};
|
||||
tf_ssb_ref(ssb);
|
||||
int result = uv_queue_work(tf_ssb_get_loop(ssb), &update->work, _tf_ssb_update_settings_work, _tf_ssb_update_settings_after_work);
|
||||
if (result)
|
||||
{
|
||||
_tf_ssb_update_settings_after_work(&update->work, result);
|
||||
}
|
||||
*update = (update_settings_t) { 0 };
|
||||
tf_ssb_run_work(ssb, _tf_ssb_update_settings_work, _tf_ssb_update_settings_after_work, update);
|
||||
}
|
||||
|
||||
static void _tf_ssb_update_settings(tf_ssb_t* ssb)
|
||||
{
|
||||
update_settings_t* update = tf_malloc(sizeof(update_settings_t));
|
||||
*update = (update_settings_t)
|
||||
{
|
||||
.work =
|
||||
{
|
||||
.data = update,
|
||||
},
|
||||
.ssb = ssb,
|
||||
};
|
||||
tf_ssb_ref(ssb);
|
||||
_tf_ssb_update_settings_work(&update->work);
|
||||
_tf_ssb_update_settings_after_work(&update->work, 0);
|
||||
*update = (update_settings_t) { 0 };
|
||||
_tf_ssb_update_settings_work(ssb, update);
|
||||
_tf_ssb_update_settings_after_work(ssb, 0, update);
|
||||
}
|
||||
|
||||
static void _tf_ssb_start_update_settings(tf_ssb_t* ssb)
|
||||
|
Reference in New Issue
Block a user