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:
@ -76,35 +76,30 @@ static bool _tf_ssb_connections_get_next_connection(tf_ssb_connections_t* connec
|
||||
|
||||
typedef struct _tf_ssb_connections_get_next_t
|
||||
{
|
||||
uv_work_t work;
|
||||
tf_ssb_connections_t* connections;
|
||||
tf_ssb_t* ssb;
|
||||
bool ready;
|
||||
char host[256];
|
||||
int port;
|
||||
char key[k_id_base64_len];
|
||||
} tf_ssb_connections_get_next_t;
|
||||
|
||||
static void _tf_ssb_connections_get_next_work(uv_work_t* work)
|
||||
static void _tf_ssb_connections_get_next_work(tf_ssb_t* ssb, void* user_data)
|
||||
{
|
||||
tf_ssb_connections_get_next_t* next = work->data;
|
||||
tf_ssb_record_thread_busy(next->ssb, true);
|
||||
tf_ssb_connections_get_next_t* next = user_data;
|
||||
next->ready = _tf_ssb_connections_get_next_connection(next->connections, next->host, sizeof(next->host), &next->port, next->key, sizeof(next->key));
|
||||
tf_ssb_record_thread_busy(next->ssb, false);
|
||||
}
|
||||
|
||||
static void _tf_ssb_connections_get_next_after_work(uv_work_t* work, int status)
|
||||
static void _tf_ssb_connections_get_next_after_work(tf_ssb_t* ssb, int status, void* user_data)
|
||||
{
|
||||
tf_ssb_connections_get_next_t* next = work->data;
|
||||
tf_ssb_connections_get_next_t* next = user_data;
|
||||
if (next->ready)
|
||||
{
|
||||
uint8_t key_bin[k_id_bin_len];
|
||||
if (tf_ssb_id_str_to_bin(key_bin, next->key))
|
||||
{
|
||||
tf_ssb_connect(next->ssb, next->host, next->port, key_bin);
|
||||
tf_ssb_connect(ssb, next->host, next->port, key_bin);
|
||||
}
|
||||
}
|
||||
tf_ssb_unref(next->ssb);
|
||||
tf_free(next);
|
||||
}
|
||||
|
||||
@ -116,21 +111,10 @@ static void _tf_ssb_connections_timer(uv_timer_t* timer)
|
||||
if (count < (int)_countof(active))
|
||||
{
|
||||
tf_ssb_connections_get_next_t* next = tf_malloc(sizeof(tf_ssb_connections_get_next_t));
|
||||
*next = (tf_ssb_connections_get_next_t)
|
||||
{
|
||||
.work =
|
||||
{
|
||||
.data = next,
|
||||
},
|
||||
.ssb = connections->ssb,
|
||||
*next = (tf_ssb_connections_get_next_t) {
|
||||
.connections = connections,
|
||||
};
|
||||
tf_ssb_ref(connections->ssb);
|
||||
int result = uv_queue_work(tf_ssb_get_loop(connections->ssb), &next->work, _tf_ssb_connections_get_next_work, _tf_ssb_connections_get_next_after_work);
|
||||
if (result)
|
||||
{
|
||||
_tf_ssb_connections_get_next_after_work(&next->work, result);
|
||||
}
|
||||
tf_ssb_run_work(connections->ssb, _tf_ssb_connections_get_next_work, _tf_ssb_connections_get_next_after_work, next);
|
||||
}
|
||||
}
|
||||
|
||||
@ -165,8 +149,6 @@ void tf_ssb_connections_destroy(tf_ssb_connections_t* connections)
|
||||
|
||||
typedef struct _tf_ssb_connections_update_t
|
||||
{
|
||||
uv_work_t work;
|
||||
tf_ssb_t* ssb;
|
||||
char host[256];
|
||||
int port;
|
||||
char key[k_id_base64_len];
|
||||
@ -174,12 +156,11 @@ typedef struct _tf_ssb_connections_update_t
|
||||
bool succeeded;
|
||||
} tf_ssb_connections_update_t;
|
||||
|
||||
static void _tf_ssb_connections_update_work(uv_work_t* work)
|
||||
static void _tf_ssb_connections_update_work(tf_ssb_t* ssb, void* user_data)
|
||||
{
|
||||
tf_ssb_connections_update_t* update = work->data;
|
||||
tf_ssb_record_thread_busy(update->ssb, true);
|
||||
tf_ssb_connections_update_t* update = user_data;
|
||||
sqlite3_stmt* statement;
|
||||
sqlite3* db = tf_ssb_acquire_db_writer(update->ssb);
|
||||
sqlite3* db = tf_ssb_acquire_db_writer(ssb);
|
||||
if (update->attempted)
|
||||
{
|
||||
if (sqlite3_prepare(db, "UPDATE connections SET last_attempt = strftime('%s', 'now') WHERE host = ?1 AND port = ?2 AND key = ?3", -1, &statement, NULL) == SQLITE_OK)
|
||||
@ -226,31 +207,23 @@ static void _tf_ssb_connections_update_work(uv_work_t* work)
|
||||
sqlite3_finalize(statement);
|
||||
}
|
||||
}
|
||||
tf_ssb_release_db_writer(update->ssb, db);
|
||||
tf_ssb_record_thread_busy(update->ssb, false);
|
||||
tf_ssb_release_db_writer(ssb, db);
|
||||
}
|
||||
|
||||
static void _tf_ssb_connections_update_after_work(uv_work_t* work, int status)
|
||||
static void _tf_ssb_connections_update_after_work(tf_ssb_t* ssb, int status, void* user_data)
|
||||
{
|
||||
tf_ssb_connections_update_t* update = work->data;
|
||||
tf_free(update);
|
||||
tf_free(user_data);
|
||||
}
|
||||
|
||||
static void _tf_ssb_connections_queue_update(tf_ssb_connections_t* connections, tf_ssb_connections_update_t* update)
|
||||
{
|
||||
update->work.data = update;
|
||||
int result = uv_queue_work(tf_ssb_get_loop(connections->ssb), &update->work, _tf_ssb_connections_update_work, _tf_ssb_connections_update_after_work);
|
||||
if (result)
|
||||
{
|
||||
_tf_ssb_connections_update_after_work(&update->work, result);
|
||||
}
|
||||
tf_ssb_run_work(connections->ssb, _tf_ssb_connections_update_work, _tf_ssb_connections_update_after_work, update);
|
||||
}
|
||||
|
||||
void tf_ssb_connections_store(tf_ssb_connections_t* connections, const char* host, int port, const char* key)
|
||||
{
|
||||
tf_ssb_connections_update_t* update = tf_malloc(sizeof(tf_ssb_connections_update_t));
|
||||
*update = (tf_ssb_connections_update_t) {
|
||||
.ssb = connections->ssb,
|
||||
.port = port,
|
||||
};
|
||||
snprintf(update->host, sizeof(update->host), "%s", host);
|
||||
@ -262,7 +235,6 @@ void tf_ssb_connections_set_attempted(tf_ssb_connections_t* connections, const c
|
||||
{
|
||||
tf_ssb_connections_update_t* update = tf_malloc(sizeof(tf_ssb_connections_update_t));
|
||||
*update = (tf_ssb_connections_update_t) {
|
||||
.ssb = connections->ssb,
|
||||
.port = port,
|
||||
.attempted = true,
|
||||
};
|
||||
@ -275,7 +247,6 @@ void tf_ssb_connections_set_succeeded(tf_ssb_connections_t* connections, const c
|
||||
{
|
||||
tf_ssb_connections_update_t* update = tf_malloc(sizeof(tf_ssb_connections_update_t));
|
||||
*update = (tf_ssb_connections_update_t) {
|
||||
.ssb = connections->ssb,
|
||||
.port = port,
|
||||
.succeeded = true,
|
||||
};
|
||||
|
Reference in New Issue
Block a user