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

View File

@ -224,18 +224,18 @@ done:;
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)
{
FileOp *fop;
bool fatal;
fop = fop_new(FileOpSave, context);
fop = fop_new(FileOpSave, const_cast<Context*>(context));
if (!fop)
return NULL;
// Document to save
fop->document = document;
fop->document = const_cast<Document*>(document);
// Get the extension of the filename (in lower case)
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.
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_done(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.
if (!document) {
@ -204,7 +204,7 @@ void update_screen_for_document(Document* document)
}
// With a document.
else {
document->notifyGeneralUpdate();
const_cast<Document*>(document)->notifyGeneralUpdate();
// Update the tabs (maybe the modified status has been changed).
app_rebuild_documents_tabs();

View File

@ -34,7 +34,7 @@ namespace app {
int init_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_feedback();