1
0
Fork 0
mirror of https://github.com/deltachat/deltachat-core.git synced 2025-10-05 19:42:04 +02:00

remove unneded locks

This commit is contained in:
B. Petersen 2018-06-29 16:48:13 +02:00
parent e1e649dc8d
commit a5c09d003d
7 changed files with 53 additions and 134 deletions

View file

@ -48,34 +48,30 @@ int dc_reset_tables(dc_context_t* context, int bits)
dc_log_info(context, 0, "Resetting tables (%i)...", bits);
dc_sqlite3_lock(context->m_sql);
if( bits & 1 ) {
dc_sqlite3_execute(context->m_sql, "DELETE FROM jobs;");
dc_log_info(context, 0, "(1) Jobs reset.");
}
if( bits & 1 ) {
dc_sqlite3_execute(context->m_sql, "DELETE FROM jobs;");
dc_log_info(context, 0, "(1) Jobs reset.");
}
if( bits & 2 ) {
dc_sqlite3_execute(context->m_sql, "DELETE FROM acpeerstates;");
dc_log_info(context, 0, "(2) Peerstates reset.");
}
if( bits & 2 ) {
dc_sqlite3_execute(context->m_sql, "DELETE FROM acpeerstates;");
dc_log_info(context, 0, "(2) Peerstates reset.");
}
if( bits & 4 ) {
dc_sqlite3_execute(context->m_sql, "DELETE FROM keypairs;");
dc_log_info(context, 0, "(4) Private keypairs reset.");
}
if( bits & 4 ) {
dc_sqlite3_execute(context->m_sql, "DELETE FROM keypairs;");
dc_log_info(context, 0, "(4) Private keypairs reset.");
}
if( bits & 8 ) {
dc_sqlite3_execute(context->m_sql, "DELETE FROM contacts WHERE id>" DC_STRINGIFY(DC_CONTACT_ID_LAST_SPECIAL) ";"); /* the other IDs are reserved - leave these rows to make sure, the IDs are not used by normal contacts*/
dc_sqlite3_execute(context->m_sql, "DELETE FROM chats WHERE id>" DC_STRINGIFY(DC_CHAT_ID_LAST_SPECIAL) ";");
dc_sqlite3_execute(context->m_sql, "DELETE FROM chats_contacts;");
dc_sqlite3_execute(context->m_sql, "DELETE FROM msgs WHERE id>" DC_STRINGIFY(DC_MSG_ID_LAST_SPECIAL) ";");
dc_sqlite3_execute(context->m_sql, "DELETE FROM config WHERE keyname LIKE 'imap.%' OR keyname LIKE 'configured%';");
dc_sqlite3_execute(context->m_sql, "DELETE FROM leftgrps;");
dc_log_info(context, 0, "(8) Rest but server config reset.");
}
dc_sqlite3_unlock(context->m_sql);
if( bits & 8 ) {
dc_sqlite3_execute(context->m_sql, "DELETE FROM contacts WHERE id>" DC_STRINGIFY(DC_CONTACT_ID_LAST_SPECIAL) ";"); /* the other IDs are reserved - leave these rows to make sure, the IDs are not used by normal contacts*/
dc_sqlite3_execute(context->m_sql, "DELETE FROM chats WHERE id>" DC_STRINGIFY(DC_CHAT_ID_LAST_SPECIAL) ";");
dc_sqlite3_execute(context->m_sql, "DELETE FROM chats_contacts;");
dc_sqlite3_execute(context->m_sql, "DELETE FROM msgs WHERE id>" DC_STRINGIFY(DC_MSG_ID_LAST_SPECIAL) ";");
dc_sqlite3_execute(context->m_sql, "DELETE FROM config WHERE keyname LIKE 'imap.%' OR keyname LIKE 'configured%';");
dc_sqlite3_execute(context->m_sql, "DELETE FROM leftgrps;");
dc_log_info(context, 0, "(8) Rest but server config reset.");
}
context->m_cb(context, DC_EVENT_MSGS_CHANGED, 0, 0);
@ -101,11 +97,7 @@ static int dc_cleanup_contacts(dc_context_t* context)
dc_log_info(context, 0, "Cleaning up contacts ...");
dc_sqlite3_lock(context->m_sql);
dc_sqlite3_execute(context->m_sql, "DELETE FROM contacts WHERE id>" DC_STRINGIFY(DC_CONTACT_ID_LAST_SPECIAL) " AND blocked=0 AND NOT EXISTS (SELECT contact_id FROM chats_contacts where contacts.id = chats_contacts.contact_id) AND NOT EXISTS (select from_id from msgs WHERE msgs.from_id = contacts.id);");
dc_sqlite3_unlock(context->m_sql);
dc_sqlite3_execute(context->m_sql, "DELETE FROM contacts WHERE id>" DC_STRINGIFY(DC_CONTACT_ID_LAST_SPECIAL) " AND blocked=0 AND NOT EXISTS (SELECT contact_id FROM chats_contacts where contacts.id = chats_contacts.contact_id) AND NOT EXISTS (select from_id from msgs WHERE msgs.from_id = contacts.id);");
return 1;
}

View file

@ -363,7 +363,7 @@ cleanup:
void dc_job_do_DC_JOB_CONFIGURE_IMAP(dc_context_t* context, dc_job_t* job)
{
int success = 0, locked = 0, i;
int success = 0, i;
int imap_connected_here = 0, smtp_connected_here = 0, ongoing_allocated_here = 0;
dc_loginparam_t* param = NULL;
@ -397,15 +397,9 @@ void dc_job_do_DC_JOB_CONFIGURE_IMAP(dc_context_t* context, dc_job_t* job)
dc_imap_disconnect(context->m_imap);
dc_smtp_disconnect(context->m_smtp);
dc_sqlite3_lock(context->m_sql);
locked = 1;
//dc_sqlite3_set_config_int__(context->m_sql, "configured", 0); -- NO: we do _not_ reset this flag if it was set once; otherwise the user won't get back to his chats (as an alternative, we could change the UI). Moreover, and not changeable in the UI, we use this flag to check if we shall search for backups.
context->m_smtp->m_log_connect_errors = 1;
context->m_imap->m_log_connect_errors = 1;
dc_sqlite3_unlock(context->m_sql);
locked = 0;
//dc_sqlite3_set_config_int__(context->m_sql, "configured", 0); -- NO: we do _not_ reset this flag if it was set once; otherwise the user won't get back to his chats (as an alternative, we could change the UI). Moreover, and not changeable in the UI, we use this flag to check if we shall search for backups.
context->m_smtp->m_log_connect_errors = 1;
context->m_imap->m_log_connect_errors = 1;
dc_log_info(context, 0, "Configure ...");
@ -662,7 +656,6 @@ void dc_job_do_DC_JOB_CONFIGURE_IMAP(dc_context_t* context, dc_job_t* job)
PROGRESS(940)
cleanup:
if( locked ) { dc_sqlite3_unlock(context->m_sql); }
context->m_cb(context, DC_EVENT_CONFIGURE_PROGRESS, 950, 0);
if( imap_connected_here ) { dc_imap_disconnect(context->m_imap); }

View file

@ -224,15 +224,11 @@ static void update_config_cache(dc_context_t* context, const char* key)
int dc_open(dc_context_t* context, const char* dbfile, const char* blobdir)
{
int success = 0;
int db_locked = 0;
if( context == NULL || context->m_magic != DC_CONTEXT_MAGIC || dbfile == NULL ) {
goto cleanup;
}
dc_sqlite3_lock(context->m_sql);
db_locked = 1;
/* Open() sets up the object and connects to the given database
from which all configuration is read/written to. */
@ -265,8 +261,6 @@ cleanup:
}
}
if( db_locked ) { dc_sqlite3_unlock(context->m_sql); }
return success;
}
@ -287,19 +281,15 @@ void dc_close(dc_context_t* context)
dc_imap_disconnect(context->m_imap);
dc_smtp_disconnect(context->m_smtp);
dc_sqlite3_lock(context->m_sql);
if( dc_sqlite3_is_open(context->m_sql) ) {
dc_sqlite3_close__(context->m_sql);
}
if( dc_sqlite3_is_open(context->m_sql) ) {
dc_sqlite3_close__(context->m_sql);
}
free(context->m_dbfile);
context->m_dbfile = NULL;
free(context->m_dbfile);
context->m_dbfile = NULL;
free(context->m_blobdir);
context->m_blobdir = NULL;
dc_sqlite3_unlock(context->m_sql);
free(context->m_blobdir);
context->m_blobdir = NULL;
}
@ -1653,7 +1643,7 @@ void dc_archive_chat(dc_context_t* context, uint32_t chat_id, int archive)
void dc_delete_chat(dc_context_t* context, uint32_t chat_id)
{
/* Up to 2017-11-02 deleting a group also implied leaving it, see above why we have changed this. */
int locked = 0, pending_transaction = 0;
int pending_transaction = 0;
dc_chat_t* obj = dc_chat_new(context);
char* q3 = NULL;
@ -1703,7 +1693,6 @@ void dc_delete_chat(dc_context_t* context, uint32_t chat_id)
cleanup:
if( pending_transaction ) { dc_sqlite3_rollback(context->m_sql); }
if( locked ) { dc_sqlite3_unlock(context->m_sql); }
dc_chat_unref(obj);
sqlite3_free(q3);
}

View file

@ -315,7 +315,7 @@ void dc_e2ee_encrypt(dc_context_t* context, const clist* recipients_addr,
int min_verified,
struct mailmime* in_out_message, dc_e2ee_helper_t* helper)
{
int locked = 0, col = 0, do_encrypt = 0;
int col = 0, do_encrypt = 0;
dc_aheader_t* autocryptheader = dc_aheader_new();
struct mailimf_fields* imffields_unprotected = NULL; /*just a pointer into mailmime structure, must not be freed*/
dc_keyring_t* keyring = dc_keyring_new();
@ -494,7 +494,6 @@ void dc_e2ee_encrypt(dc_context_t* context, const clist* recipients_addr,
mailimf_fields_add(imffields_unprotected, mailimf_field_new_custom(strdup("Autocrypt"), p/*takes ownership of pointer*/));
cleanup:
if( locked ) { dc_sqlite3_unlock(context->m_sql); }
dc_aheader_unref(autocryptheader);
dc_keyring_unref(keyring);
dc_key_unref(sign_key);
@ -802,7 +801,6 @@ void dc_e2ee_decrypt(dc_context_t* context, struct mailmime* in_out_message,
dc_aheader_t* autocryptheader = NULL;
time_t message_time = 0;
dc_apeerstate_t* peerstate = dc_apeerstate_new(context);
int locked = 0;
char* from = NULL, *self_addr = NULL;
dc_keyring_t* private_keyring = dc_keyring_new();
dc_keyring_t* public_keyring_for_validate = dc_keyring_new();
@ -926,7 +924,6 @@ void dc_e2ee_decrypt(dc_context_t* context, struct mailmime* in_out_message,
//mailmime_print(in_out_message);
cleanup:
if( locked ) { dc_sqlite3_unlock(context->m_sql); }
if( gossip_headers ) { mailimf_fields_free(gossip_headers); }
dc_aheader_unref(autocryptheader);
dc_apeerstate_unref(peerstate);

View file

@ -839,9 +839,9 @@ The macro avoids weird values of 0% or 100% while still working. */
static int export_backup(dc_context_t* context, const char* dir)
{
int success = 0, locked = 0, closed = 0;
int success = 0, closed = 0;
char* dest_pathNfilename = NULL;
dc_sqlite3_t* dest_sql = NULL;
dc_sqlite3_t* dest_sql = NULL;
time_t now = time(NULL);
DIR* dir_handle = NULL;
struct dirent* dir_entry;
@ -868,8 +868,10 @@ static int export_backup(dc_context_t* context, const char* dir)
}
/* temporary lock and close the source (we just make a copy of the whole file, this is the fastest and easiest approach) */
dc_sqlite3_lock(context->m_sql);
locked = 1;
//dc_sqlite3_lock(context->m_sql); // TODO: check if this works while threads running
//locked = 1;
dc_sqlite3_close__(context->m_sql);
closed = 1;
@ -882,8 +884,9 @@ static int export_backup(dc_context_t* context, const char* dir)
/* unlock and re-open the source and make it availabe again for the normal use */
dc_sqlite3_open__(context->m_sql, context->m_dbfile, 0);
closed = 0;
dc_sqlite3_unlock(context->m_sql);
locked = 0;
//dc_sqlite3_unlock(context->m_sql); // TODO: check if this works while threads running
//locked = 0;
/* add all files as blobs to the database copy (this does not require the source to be locked, neigher the destination as it is used only here) */
if( (dest_sql=dc_sqlite3_new(context/*for logging only*/))==NULL
@ -970,7 +973,8 @@ static int export_backup(dc_context_t* context, const char* dir)
cleanup:
if( dir_handle ) { closedir(dir_handle); }
if( closed ) { dc_sqlite3_open__(context->m_sql, context->m_dbfile, 0); }
if( locked ) { dc_sqlite3_unlock(context->m_sql); }
//if( locked ) { dc_sqlite3_unlock(context->m_sql); } // TODO: check if this works while threads running
sqlite3_finalize(stmt);
dc_sqlite3_close__(dest_sql);
@ -1008,7 +1012,6 @@ static int import_backup(dc_context_t* context, const char* backup_to_import)
*/
int success = 0;
int locked = 0;
int processed_files_count = 0, total_files_count = 0;
sqlite3_stmt* stmt = NULL;
char* pathNfilename = NULL;
@ -1023,8 +1026,9 @@ static int import_backup(dc_context_t* context, const char* backup_to_import)
}
/* close and delete the original file - FIXME: we should import to a .bak file and rename it on success. however, currently it is not clear it the import exists in the long run (may be replaced by a restore-from-imap) */
dc_sqlite3_lock(context->m_sql);
locked = 1;
//dc_sqlite3_lock(context->m_sql); // TODO: check if this works while threads running
//locked = 1;
if( dc_sqlite3_is_open(context->m_sql) ) {
dc_sqlite3_close__(context->m_sql);
@ -1117,7 +1121,9 @@ cleanup:
free(repl_from);
free(repl_to);
sqlite3_finalize(stmt);
if( locked ) { dc_sqlite3_unlock(context->m_sql); }
// if( locked ) { dc_sqlite3_unlock(context->m_sql); } // TODO: check if this works while threads running
return success;
}

View file

@ -130,8 +130,6 @@ dc_sqlite3_t* dc_sqlite3_new(dc_context_t* context)
sql->m_context = context;
pthread_mutex_init(&sql->m_critical_, NULL);
return sql;
}
@ -143,12 +141,9 @@ void dc_sqlite3_unref(dc_sqlite3_t* sql)
}
if( sql->m_cobj ) {
pthread_mutex_lock(&sql->m_critical_); /* as a very exeception, we do the locking inside the dc_sqlite3-class - normally, this should be done by the caller! */
dc_sqlite3_close__(sql);
pthread_mutex_unlock(&sql->m_critical_);
dc_sqlite3_close__(sql);
}
pthread_mutex_destroy(&sql->m_critical_);
free(sql);
}
@ -655,44 +650,6 @@ int dc_sqlite3_set_config_int(dc_sqlite3_t* sql, const char* key, int32_t value)
}
/*******************************************************************************
* Locking
******************************************************************************/
#ifdef DC_USE_LOCK_DEBUG
void dc_sqlite3_lockNdebug(dc_sqlite3_t* sql, const char* filename, int linenum) /* wait and lock */
#else
void dc_sqlite3_lock(dc_sqlite3_t* sql) /* wait and lock */
#endif
{
#ifdef DC_USE_LOCK_DEBUG
clock_t start = clock();
dc_log_info(sql->m_context, 0, " waiting for lock at %s#L%i", filename, linenum);
#endif
pthread_mutex_lock(&sql->m_critical_);
#ifdef DC_USE_LOCK_DEBUG
dc_log_info(sql->m_context, 0, "{{{ LOCK AT %s#L%i after %.3f ms", filename, linenum, (double)(clock()-start)*1000.0/CLOCKS_PER_SEC);
#endif
}
#ifdef DC_USE_LOCK_DEBUG
void dc_sqlite3_unlockNdebug(dc_sqlite3_t* sql, const char* filename, int linenum)
#else
void dc_sqlite3_unlock(dc_sqlite3_t* sql)
#endif
{
#ifdef DC_USE_LOCK_DEBUG
dc_log_info(sql->m_context, 0, " UNLOCK AT %s#L%i }}}", filename, linenum);
#endif
pthread_mutex_unlock(&sql->m_critical_);
}
/*******************************************************************************
* Transactions
******************************************************************************/

View file

@ -47,7 +47,6 @@ typedef struct dc_sqlite3_t
/** @privatesection */
sqlite3* m_cobj; /**< is the database given as dbfile to Open() */
dc_context_t* m_context; /**< used for logging and to acquire wakelocks, there may be N dc_sqlite3_t objects per context! In practise, we use 2 on backup, 1 otherwise. */
pthread_mutex_t m_critical_; /**< the user must make sure, only one thread uses sqlite at the same time! for this purpose, all calls must be enclosed by a locked m_critical; use dc_sqlite3_lock() for this purpose */
} dc_sqlite3_t;
@ -73,20 +72,6 @@ int dc_sqlite3_execute (dc_sqlite3_t*, const char* sql);
int dc_sqlite3_table_exists__ (dc_sqlite3_t*, const char* name);
void dc_sqlite3_log_error (dc_sqlite3_t*, const char* msg, ...);
/* tools for locking, may be called nested, see also m_critical_ above.
the user of dc_sqlite3 must make sure that the dc_sqlite3-object is only used by one thread at the same time.
In general, we will lock the hightest level as possible - this avoids deadlocks and massive on/off lockings.
Low-level-functions, eg. the dc_sqlite3-methods, do not lock. */
#ifdef DC_USE_LOCK_DEBUG
#define dc_sqlite3_lock(a) dc_sqlite3_lockNdebug((a), __FILE__, __LINE__)
#define dc_sqlite3_unlock(a) dc_sqlite3_unlockNdebug((a), __FILE__, __LINE__)
void dc_sqlite3_lockNdebug (dc_sqlite3_t*, const char* filename, int line);
void dc_sqlite3_unlockNdebug (dc_sqlite3_t*, const char* filename, int line);
#else
void dc_sqlite3_lock (dc_sqlite3_t*); /* lock or wait; these calls must not be nested in a single thread */
void dc_sqlite3_unlock (dc_sqlite3_t*);
#endif
void dc_sqlite3_begin_transaction (dc_sqlite3_t*);
void dc_sqlite3_commit (dc_sqlite3_t*);
void dc_sqlite3_rollback (dc_sqlite3_t*);