Fix problems that occurred when multiple threads were trying to read/convert files at the same time (ie: indexing and previewing threads in the GUI calling internfile()). Either get rid of or lock-protect all shared data, eliminate misc initialization possible conflicts by using static initializers. Hopefuly closes issue #51

This commit is contained in:
Jean-Francois Dockes
2011-04-28 10:58:33 +02:00
parent 01f24fa5fd
commit 55f124725f
33 changed files with 318 additions and 231 deletions
+5 -23
View File
@@ -19,7 +19,6 @@
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
@@ -44,6 +43,7 @@
#include "smallut.h"
#include "netcon.h"
#include "closefrom.h"
#include "ptmutex.h"
#ifndef NO_NAMESPACES
using namespace std;
@@ -316,38 +316,20 @@ private:
// The netcon selectloop that doexec() uses for reading/writing would
// be complicated to render thread-safe. Use locking to ensure only
// one thread in there
class ExecLocking {
public:
pthread_mutex_t m_mutex;
ExecLocking()
{
pthread_mutex_init(&m_mutex, 0);
}
};
ExecLocking o_lock;
class ExecLocker {
public:
ExecLocker()
{
pthread_mutex_lock(&o_lock.m_mutex);
}
~ExecLocker()
{
pthread_mutex_unlock(&o_lock.m_mutex);
}
};
static PTMutexInit o_lock;
int ExecCmd::doexec(const string &cmd, const list<string>& args,
const string *input, string *output)
{
// Only one thread allowed in here...
PTMutexLocker locker(o_lock);
if (startExec(cmd, args, input != 0, output != 0) < 0) {
return -1;
}
// Cleanup in case we return early
ExecCmdRsrc e(this);
// Only one thread allowed in here...
ExecLocker locker;
int ret = 0;
if (input || output) {
+52
View File
@@ -0,0 +1,52 @@
/* Copyright (C) 2011 J.F.Dockes
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef _PTMUTEX_H_INCLUDED_
#define _PTMUTEX_H_INCLUDED_
#include <pthread.h>
/// A trivial wrapper/helper for pthread mutex locks
/// Init lock. Used as a single PTMutexInit static object.
class PTMutexInit {
public:
pthread_mutex_t m_mutex;
PTMutexInit()
{
pthread_mutex_init(&m_mutex, 0);
}
};
/// Take the lock when constructed, release when deleted
class PTMutexLocker {
public:
PTMutexLocker(PTMutexInit& l) : m_lock(l)
{
m_status = pthread_mutex_lock(&m_lock.m_mutex);
}
~PTMutexLocker()
{
pthread_mutex_unlock(&m_lock.m_mutex);
}
int ok() {return m_status == 0;}
private:
PTMutexInit& m_lock;
int m_status;
};
#endif /* _PTMUTEX_H_INCLUDED_ */