Don't lock for write when we save a document (we just need to read it)

This commit is contained in:
David Capello 2015-04-20 17:28:10 -03:00
parent fedf818cda
commit 395baa3296
5 changed files with 26 additions and 18 deletions

View File

@ -70,8 +70,9 @@ private:
FileOp* m_fop; FileOp* m_fop;
}; };
static void save_document_in_background(Context* context, Document* document, static void save_document_in_background(const Context* context,
bool mark_as_saved, const std::string& fn_format) const Document* document, bool mark_as_saved,
const std::string& fn_format)
{ {
base::UniquePtr<FileOp> fop(fop_to_save_document(context, base::UniquePtr<FileOp> fop(fop_to_save_document(context,
document, document->filename().c_str(), fn_format.c_str())); document, document->filename().c_str(), fn_format.c_str()));
@ -87,16 +88,16 @@ static void save_document_in_background(Context* context, Document* document,
// We don't know if the file was saved correctly or not. So mark // We don't know if the file was saved correctly or not. So mark
// it as it should be saved again. // it as it should be saved again.
document->impossibleToBackToSavedState(); const_cast<Document*>(document)->impossibleToBackToSavedState();
} }
// If the job was cancelled, mark the document as modified. // If the job was cancelled, mark the document as modified.
else if (fop_is_stop(fop)) { else if (fop_is_stop(fop)) {
document->impossibleToBackToSavedState(); const_cast<Document*>(document)->impossibleToBackToSavedState();
} }
else if (context->isUiAvailable()) { else if (context->isUiAvailable()) {
App::instance()->getRecentFiles()->addRecentFile(document->filename().c_str()); App::instance()->getRecentFiles()->addRecentFile(document->filename().c_str());
if (mark_as_saved) if (mark_as_saved)
document->markAsSaved(); const_cast<Document*>(document)->markAsSaved();
StatusBar::instance() StatusBar::instance()
->setStatusText(2000, "File %s, saved.", ->setStatusText(2000, "File %s, saved.",
@ -174,26 +175,33 @@ void SaveFileBaseCommand::saveAsDialog(const ContextReader& reader, const char*
} }
} }
std::string oldFilename;
{ {
ContextWriter writer(reader); ContextWriter writer(reader);
Document* documentWriter = writer.document(); Document* documentWriter = writer.document();
std::string oldFilename = documentWriter->filename(); oldFilename = documentWriter->filename();
// Change the document file name // Change the document file name
documentWriter->setFilename(filename.c_str()); documentWriter->setFilename(filename.c_str());
m_selectedFilename = filename; m_selectedFilename = filename;
}
// Save the document // Save the document
save_document_in_background(writer.context(), documentWriter, save_document_in_background(
markAsSaved, m_filenameFormat); reader.context(), const_cast<Document*>(document),
markAsSaved, m_filenameFormat);
{
ContextWriter writer(reader);
Document* documentWriter = writer.document();
if (documentWriter->isModified()) if (documentWriter->isModified())
documentWriter->setFilename(oldFilename); documentWriter->setFilename(oldFilename);
else else
documentWriter->incrementVersion(); documentWriter->incrementVersion();
update_screen_for_document(documentWriter);
} }
update_screen_for_document(document);
} }
//static //static

View File

@ -224,18 +224,18 @@ done:;
return fop; return fop;
} }
FileOp* fop_to_save_document(Context* context, Document* document, FileOp* fop_to_save_document(const Context* context, const Document* document,
const char* filename, const char* fn_format_arg) const char* filename, const char* fn_format_arg)
{ {
FileOp *fop; FileOp *fop;
bool fatal; bool fatal;
fop = fop_new(FileOpSave, context); fop = fop_new(FileOpSave, const_cast<Context*>(context));
if (!fop) if (!fop)
return NULL; return NULL;
// Document to save // Document to save
fop->document = document; fop->document = const_cast<Document*>(document);
// Get the extension of the filename (in lower case) // Get the extension of the filename (in lower case)
std::string extension = base::string_to_lower(base::get_file_extension(filename)); std::string extension = base::string_to_lower(base::get_file_extension(filename));

View File

@ -123,7 +123,7 @@ namespace app {
// Low-level routines to load/save documents. // Low-level routines to load/save documents.
FileOp* fop_to_load_document(Context* context, const char* filename, int flags); FileOp* fop_to_load_document(Context* context, const char* filename, int flags);
FileOp* fop_to_save_document(Context* context, Document* document, const char* filename, const char* fn_format); FileOp* fop_to_save_document(const Context* context, const Document* document, const char* filename, const char* fn_format);
void fop_operate(FileOp* fop, IFileOpProgress* progress); void fop_operate(FileOp* fop, IFileOpProgress* progress);
void fop_done(FileOp* fop); void fop_done(FileOp* fop);
void fop_stop(FileOp* fop); void fop_stop(FileOp* fop);

View File

@ -191,7 +191,7 @@ static void save_gui_config()
} }
} }
void update_screen_for_document(Document* document) void update_screen_for_document(const Document* document)
{ {
// Without document. // Without document.
if (!document) { if (!document) {
@ -204,7 +204,7 @@ void update_screen_for_document(Document* document)
} }
// With a document. // With a document.
else { else {
document->notifyGeneralUpdate(); const_cast<Document*>(document)->notifyGeneralUpdate();
// Update the tabs (maybe the modified status has been changed). // Update the tabs (maybe the modified status has been changed).
app_rebuild_documents_tabs(); app_rebuild_documents_tabs();

View File

@ -34,7 +34,7 @@ namespace app {
int init_module_gui(); int init_module_gui();
void exit_module_gui(); void exit_module_gui();
void update_screen_for_document(Document* document); void update_screen_for_document(const Document* document);
void gui_run(); void gui_run();
void gui_feedback(); void gui_feedback();