Commit a38895a1 authored by Adam Reichold's avatar Adam Reichold Committed by Albert Astals Cid

Replace GooMutex by std::recursive_mutex (and plain reference counter by std::atomic_int).

parent 2052dc60
......@@ -578,7 +578,6 @@ if(ENABLE_XPDF_HEADERS)
install(FILES
goo/GooList.h
goo/GooTimer.h
goo/GooMutex.h
goo/GooString.h
goo/gtypes.h
goo/gmem.h
......
//========================================================================
//
// GooMutex.h
//
// Portable mutex macros.
//
// Copyright 2002-2003 Glyph & Cog, LLC
//
//========================================================================
//========================================================================
//
// Modified under the Poppler project - http://poppler.freedesktop.org
//
// All changes made under the Poppler project to this file are licensed
// under GPL version 2 or later
//
// Copyright (C) 2009 Kovid Goyal <kovid@kovidgoyal.net>
// Copyright (C) 2013 Thomas Freitag <Thomas.Freitag@alfa.de>
// Copyright (C) 2013, 2018 Albert Astals Cid <aacid@kde.org>
// Copyright (C) 2013 Adam Reichold <adamreichold@myopera.com>
// Copyright (C) 2014 Bogdan Cristea <cristeab@gmail.com>
// Copyright (C) 2014 Peter Breitenlohner <peb@mppmu.mpg.de>
// Copyright (C) 2017 Adrian Johnson <ajohnson@redneon.com>
//
// To see a description of the changes please see the Changelog file that
// came with your tarball or type make ChangeLog if you are building from git
//
//========================================================================
#ifndef GMUTEX_H
#define GMUTEX_H
#ifdef MULTITHREADED
// Usage:
//
// GooMutex m;
// gInitMutex(&m);
// ...
// gLockMutex(&m);
// ... critical section ...
// gUnlockMutex(&m);
// ...
// gDestroyMutex(&m);
#ifdef _WIN32
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
typedef CRITICAL_SECTION GooMutex;
#define gInitMutex(m) InitializeCriticalSection(m)
#define gDestroyMutex(m) DeleteCriticalSection(m)
#define gLockMutex(m) EnterCriticalSection(m)
#define gUnlockMutex(m) LeaveCriticalSection(m)
#else // assume pthreads
#include <pthread.h>
typedef pthread_mutex_t GooMutex;
inline void gInitMutex(GooMutex *m) {
pthread_mutexattr_t mutexattr;
pthread_mutexattr_init(&mutexattr);
pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(m, &mutexattr);
pthread_mutexattr_destroy(&mutexattr);
}
#define gDestroyMutex(m) pthread_mutex_destroy(m)
#define gLockMutex(m) pthread_mutex_lock(m)
#define gUnlockMutex(m) pthread_mutex_unlock(m)
#endif
class MutexLocker {
public:
MutexLocker(GooMutex *mutexA) : mutex(mutexA) { gLockMutex(mutex); }
~MutexLocker() { gUnlockMutex(mutex); }
MutexLocker(const MutexLocker &) = delete;
MutexLocker& operator=(const MutexLocker &other) = delete;
private:
GooMutex *mutex;
};
#endif // MULTITHREADED
#endif // GMUTEX_H
......@@ -83,11 +83,9 @@
#include <algorithm>
#ifdef MULTITHREADED
# define annotLocker() MutexLocker locker(&mutex)
# define annotCondLocker(X) MutexLocker locker(&mutex, (X))
# define annotLocker() std::unique_lock<std::recursive_mutex> locker(mutex)
#else
# define annotLocker()
# define annotCondLocker(X)
#endif
#define fieldFlagReadOnly 0x00000001
......@@ -1399,10 +1397,6 @@ void Annot::initialize(PDFDoc *docA, Dict *dict) {
}
oc = dict->lookupNF("OC");
#ifdef MULTITHREADED
gInitMutex(&mutex);
#endif
}
void Annot::getRect(double *x1, double *y1, double *x2, double *y2) const {
......@@ -1634,24 +1628,13 @@ void Annot::removeReferencedObjects() {
}
void Annot::incRefCnt() {
annotLocker();
refCnt++;
}
void Annot::decRefCnt() {
#ifdef MULTITHREADED
gLockMutex(&mutex);
#endif
if (--refCnt == 0) {
#ifdef MULTITHREADED
gUnlockMutex(&mutex);
#endif
delete this;
return;
}
#ifdef MULTITHREADED
gUnlockMutex(&mutex);
#endif
}
Annot::~Annot() {
......@@ -1675,10 +1658,6 @@ Annot::~Annot() {
if (color)
delete color;
#ifdef MULTITHREADED
gDestroyMutex(&mutex);
#endif
}
void AnnotAppearanceBuilder::setDrawColor(const AnnotColor *drawColor, GBool fill) {
......
......@@ -43,6 +43,9 @@
#endif
#include <memory>
#include <atomic>
#include <mutex>
#include "Object.h"
class XRef;
......@@ -738,7 +741,7 @@ protected:
Object annotObj;
int refCnt;
std::atomic_int refCnt;
// required data
AnnotSubtype type; // Annotation type
......@@ -767,7 +770,7 @@ protected:
bool hasRef;
#ifdef MULTITHREADED
GooMutex mutex;
mutable std::recursive_mutex mutex;
#endif
};
......
......@@ -39,7 +39,7 @@
#include "Array.h"
#ifdef MULTITHREADED
# define arrayLocker() MutexLocker locker(&mutex)
# define arrayLocker() std::unique_lock<std::recursive_mutex> locker(mutex)
#else
# define arrayLocker()
#endif
......@@ -52,9 +52,6 @@ Array::Array(XRef *xrefA) {
elems = nullptr;
size = length = 0;
ref = 1;
#ifdef MULTITHREADED
gInitMutex(&mutex);
#endif
}
Array::~Array() {
......@@ -63,9 +60,6 @@ Array::~Array() {
for (i = 0; i < length; ++i)
elems[i].free();
gfree(elems);
#ifdef MULTITHREADED
gDestroyMutex(&mutex);
#endif
}
Object Array::copy(XRef *xrefA) const {
......@@ -77,18 +71,6 @@ Object Array::copy(XRef *xrefA) const {
return Object(a);
}
int Array::incRef() {
arrayLocker();
++ref;
return ref;
}
int Array::decRef() {
arrayLocker();
--ref;
return ref;
}
void Array::add(Object &&elem) {
arrayLocker();
if (length == size) {
......
......@@ -31,9 +31,11 @@
#pragma interface
#endif
#include <atomic>
#include <mutex>
#include "poppler-config.h"
#include "Object.h"
#include "goo/GooMutex.h"
class XRef;
......@@ -75,16 +77,16 @@ private:
friend class Object; // for incRef/decRef
// Reference counting.
int incRef();
int decRef();
int incRef() { return ++ref; }
int decRef() { return --ref; }
XRef *xref; // the xref table for this PDF file
Object *elems; // array of elements
int size; // size of <elems> array
int length; // number of elements in array
int ref; // reference count
std::atomic_int ref; // reference count
#ifdef MULTITHREADED
mutable GooMutex mutex;
mutable std::recursive_mutex mutex;
#endif
};
......
......@@ -313,9 +313,6 @@ CMap::CMap(GooString *collectionA, GooString *cMapNameA) {
vector[i].cid = 0;
}
refCnt = 1;
#ifdef MULTITHREADED
gInitMutex(&mutex);
#endif
}
CMap::CMap(GooString *collectionA, GooString *cMapNameA, int wModeA) {
......@@ -325,9 +322,6 @@ CMap::CMap(GooString *collectionA, GooString *cMapNameA, int wModeA) {
wMode = wModeA;
vector = nullptr;
refCnt = 1;
#ifdef MULTITHREADED
gInitMutex(&mutex);
#endif
}
void CMap::useCMap(CMapCache *cache, char *useName) {
......@@ -433,9 +427,6 @@ CMap::~CMap() {
if (vector) {
freeCMapVector(vector);
}
#ifdef MULTITHREADED
gDestroyMutex(&mutex);
#endif
}
void CMap::freeCMapVector(CMapVectorEntry *vec) {
......@@ -450,26 +441,11 @@ void CMap::freeCMapVector(CMapVectorEntry *vec) {
}
void CMap::incRefCnt() {
#ifdef MULTITHREADED
gLockMutex(&mutex);
#endif
++refCnt;
#ifdef MULTITHREADED
gUnlockMutex(&mutex);
#endif
}
void CMap::decRefCnt() {
GBool done;
#ifdef MULTITHREADED
gLockMutex(&mutex);
#endif
done = --refCnt == 0;
#ifdef MULTITHREADED
gUnlockMutex(&mutex);
#endif
if (done) {
if (--refCnt == 0) {
delete this;
}
}
......
......@@ -29,14 +29,12 @@
#pragma interface
#endif
#include <atomic>
#include "poppler-config.h"
#include "goo/gtypes.h"
#include "CharTypes.h"
#ifdef MULTITHREADED
#include "goo/GooMutex.h"
#endif
class GooString;
class Object;
struct CMapVectorEntry;
......@@ -116,10 +114,7 @@ private:
int wMode; // writing mode (0=horizontal, 1=vertical)
CMapVectorEntry *vector; // vector for first byte (NULL for
// identity CMap)
int refCnt;
#ifdef MULTITHREADED
GooMutex mutex;
#endif
std::atomic_int refCnt;
};
//------------------------------------------------------------------------
......
......@@ -61,7 +61,7 @@
#endif
#ifdef MULTITHREADED
# define fontEngineLocker() MutexLocker locker(&mutex)
# define fontEngineLocker() std::unique_lock<std::recursive_mutex> locker(mutex)
#else
# define fontEngineLocker()
#endif
......@@ -815,9 +815,6 @@ CairoFontEngine::CairoFontEngine(FT_Library libA) {
FT_Library_Version(lib, &major, &minor, &patch);
useCIDs = major > 2 ||
(major == 2 && (minor > 1 || (minor == 1 && patch > 7)));
#ifdef MULTITHREADED
gInitMutex(&mutex);
#endif
}
CairoFontEngine::~CairoFontEngine() {
......@@ -827,9 +824,6 @@ CairoFontEngine::~CairoFontEngine() {
if (fontCache[i])
delete fontCache[i];
}
#ifdef MULTITHREADED
gDestroyMutex(&mutex);
#endif
}
CairoFont *
......
......@@ -33,6 +33,8 @@
#pragma interface
#endif
#include <mutex>
#include "poppler-config.h"
#include "goo/gtypes.h"
#include <cairo-ft.h>
......@@ -126,7 +128,7 @@ private:
FT_Library lib;
GBool useCIDs;
#ifdef MULTITHREADED
GooMutex mutex;
mutable std::recursive_mutex mutex;
#endif
};
......
......@@ -67,7 +67,7 @@
#include "StructTreeRoot.h"
#ifdef MULTITHREADED
# define catalogLocker() MutexLocker locker(&mutex)
# define catalogLocker() std::unique_lock<std::recursive_mutex> locker(mutex)
#else
# define catalogLocker()
#endif
......@@ -76,9 +76,6 @@
//------------------------------------------------------------------------
Catalog::Catalog(PDFDoc *docA) {
#ifdef MULTITHREADED
gInitMutex(&mutex);
#endif
ok = gTrue;
doc = docA;
xref = doc->getXRef();
......@@ -171,9 +168,6 @@ Catalog::~Catalog() {
delete optContent;
delete viewerPrefs;
delete structTreeRoot;
#ifdef MULTITHREADED
gDestroyMutex(&mutex);
#endif
}
GooString *Catalog::readMetadata() {
......
......@@ -43,7 +43,6 @@
#include "poppler-config.h"
#include "Object.h"
#include "goo/GooMutex.h"
#include <vector>
......@@ -293,7 +292,7 @@ private:
NameTree *getJSNameTree();
LinkDest *createLinkDest(Object *obj);
#ifdef MULTITHREADED
GooMutex mutex;
mutable std::recursive_mutex mutex;
#endif
};
......
......@@ -488,9 +488,6 @@ CharCodeToUnicode::CharCodeToUnicode() {
sMapLen = sMapSize = 0;
refCnt = 1;
isIdentity = gFalse;
#ifdef MULTITHREADED
gInitMutex(&mutex);
#endif
}
CharCodeToUnicode::CharCodeToUnicode(GooString *tagA) {
......@@ -506,9 +503,6 @@ CharCodeToUnicode::CharCodeToUnicode(GooString *tagA) {
sMapLen = sMapSize = 0;
refCnt = 1;
isIdentity = gFalse;
#ifdef MULTITHREADED
gInitMutex(&mutex);
#endif
}
CharCodeToUnicode::CharCodeToUnicode(GooString *tagA, Unicode *mapA,
......@@ -528,9 +522,6 @@ CharCodeToUnicode::CharCodeToUnicode(GooString *tagA, Unicode *mapA,
sMapSize = sMapSizeA;
refCnt = 1;
isIdentity = gFalse;
#ifdef MULTITHREADED
gInitMutex(&mutex);
#endif
}
CharCodeToUnicode::~CharCodeToUnicode() {
......@@ -542,32 +533,14 @@ CharCodeToUnicode::~CharCodeToUnicode() {
for (int i = 0; i < sMapLen; ++i) gfree(sMap[i].u);
gfree(sMap);
}
#ifdef MULTITHREADED
gDestroyMutex(&mutex);
#endif
}
void CharCodeToUnicode::incRefCnt() {
#ifdef MULTITHREADED
gLockMutex(&mutex);
#endif
++refCnt;
#ifdef MULTITHREADED
gUnlockMutex(&mutex);
#endif
}
void CharCodeToUnicode::decRefCnt() {
GBool done;
#ifdef MULTITHREADED
gLockMutex(&mutex);
#endif
done = --refCnt == 0;
#ifdef MULTITHREADED
gUnlockMutex(&mutex);
#endif
if (done) {
if (--refCnt == 0) {
delete this;
}
}
......
......@@ -34,14 +34,12 @@
#pragma interface
#endif
#include <atomic>
#include "poppler-config.h"
#include "CharTypes.h"
#include "goo/gtypes.h"
#ifdef MULTITHREADED
#include "goo/GooMutex.h"
#endif
struct CharCodeToUnicodeString;
class GooString;
......@@ -120,11 +118,8 @@ private:
CharCode mapLen;
CharCodeToUnicodeString *sMap;
int sMapLen, sMapSize;
int refCnt;
std::atomic_int refCnt;
GBool isIdentity;
#ifdef MULTITHREADED
GooMutex mutex;
#endif
};
//------------------------------------------------------------------------
......
......@@ -41,7 +41,7 @@
#include "Dict.h"
#ifdef MULTITHREADED
# define dictLocker() MutexLocker locker(&mutex)
# define dictLocker() std::unique_lock<std::recursive_mutex> locker(mutex)
#else
# define dictLocker()
#endif
......@@ -66,9 +66,6 @@ struct Dict::CmpDictEntry {
Dict::Dict(XRef *xrefA) {
xref = xrefA;
ref = 1;
#ifdef MULTITHREADED
gInitMutex(&mutex);
#endif
sorted = false;
}
......@@ -76,9 +73,6 @@ Dict::Dict(XRef *xrefA) {
Dict::Dict(const Dict* dictA) {
xref = dictA->xref;
ref = 1;
#ifdef MULTITHREADED
gInitMutex(&mutex);
#endif
entries.reserve(dictA->entries.size());
for (const auto& entry : dictA->entries) {
......@@ -100,12 +94,6 @@ Dict *Dict::copy(XRef *xrefA) const {
return dictA;
}
Dict::~Dict() {
#ifdef MULTITHREADED
gDestroyMutex(&mutex);
#endif
}
void Dict::add(const char *key, Object &&val) {
dictLocker();
entries.emplace_back(key, std::move(val));
......
......@@ -35,13 +35,13 @@
#endif
#include <atomic>
#include <mutex>
#include <string>
#include <vector>
#include <utility>
#include "poppler-config.h"
#include "Object.h"
#include "goo/GooMutex.h"
//------------------------------------------------------------------------
// Dict
......@@ -55,9 +55,6 @@ public:
Dict(const Dict *dictA);
Dict *copy(XRef *xrefA) const;
// Destructor.
~Dict();
Dict(const Dict &) = delete;
Dict& operator=(const Dict &) = delete;
......@@ -115,7 +112,7 @@ private:
std::vector<DictEntry> entries;
std::atomic_int ref; // reference count
#ifdef MULTITHREADED
mutable GooMutex mutex;
mutable std::recursive_mutex mutex;
#endif
const DictEntry *find(const char *key) const;
......
......@@ -93,12 +93,12 @@
#endif
#ifdef MULTITHREADED
# define lockGlobalParams gLockMutex(&mutex)
# define lockUnicodeMapCache gLockMutex(&unicodeMapCacheMutex)
# define lockCMapCache gLockMutex(&cMapCacheMutex)
# define unlockGlobalParams gUnlockMutex(&mutex)
# define unlockUnicodeMapCache gUnlockMutex(&unicodeMapCacheMutex)
# define unlockCMapCache gUnlockMutex(&cMapCacheMutex)
# define lockGlobalParams mutex.lock()
# define lockUnicodeMapCache unicodeMapCacheMutex.lock()
# define lockCMapCache cMapCacheMutex.lock()
# define unlockGlobalParams mutex.unlock()
# define unlockUnicodeMapCache unicodeMapCacheMutex.unlock()
# define unlockCMapCache cMapCacheMutex.unlock()
#else
# define lockGlobalParams
# define lockUnicodeMapCache
......@@ -550,12 +550,6 @@ Plugin::~Plugin() {
GlobalParams::GlobalParams(const char *customPopplerDataDir)
: popplerDataDir(customPopplerDataDir)
{
#ifdef MULTITHREADED
gInitMutex(&mutex);
gInitMutex(&unicodeMapCacheMutex);
gInitMutex(&cMapCacheMutex);
#endif
initBuiltinFontTables();
// scan the encoding in reverse because we want the lowest-numbered
......@@ -747,12 +741,6 @@ GlobalParams::~GlobalParams() {
delete securityHandlers;
deleteGooList(plugins, Plugin);
#endif
#ifdef MULTITHREADED
gDestroyMutex(&mutex);
gDestroyMutex(&unicodeMapCacheMutex);
gDestroyMutex(&cMapCacheMutex);
#endif
}
//------------------------------------------------------------------------
......
......@@ -48,10 +48,7 @@
#include "UnicodeMap.h"
#include <unordered_map>
#include <string>
#ifdef MULTITHREADED
#include "goo/GooMutex.h"
#endif
#include <mutex>
class GooString;
class GooList;
......@@ -246,9 +243,9 @@ private:
#endif
#ifdef MULTITHREADED
GooMutex mutex;
GooMutex unicodeMapCacheMutex;
GooMutex cMapCacheMutex;
mutable std::recursive_mutex mutex;
mutable std::recursive_mutex unicodeMapCacheMutex;
mutable std::recursive_mutex cMapCacheMutex;
#endif
const char *popplerDataDir;
......
......@@ -92,7 +92,7 @@
#include "UTF.h"
#ifdef MULTITHREADED
# define pdfdocLocker() MutexLocker locker(&mutex)
# define pdfdocLocker() std::unique_lock<std::recursive_mutex> locker(mutex)
#else
# define pdfdocLocker()
#endif
......@@ -116,9 +116,6 @@
void PDFDoc::init()
{
#ifdef MULTITHREADED
gInitMutex(&mutex);
#endif
ok = gFalse;
errCode = errNone;
fileName = nullptr;
......@@ -368,9 +365,6 @@ PDFDoc::~PDFDoc() {
gfree(fileNameU);
}
#endif
#ifdef MULTITHREADED
gDestroyMutex(&mutex);
#endif
}
......
......@@ -45,9 +45,10 @@
#pragma interface
#endif
#include <mutex>
#include "poppler-config.h"
#include <stdio.h>
#include "goo/GooMutex.h"
#include "XRef.h"
#include "Catalog.h"
#include "Page.h"
......@@ -433,7 +434,7 @@ private:
Goffset startXRefPos; // offset of last xref table
#ifdef MULTITHREADED
GooMutex mutex;
mutable std::recursive_mutex mutex;
#endif
};
......
......@@ -62,7 +62,7 @@
#include "Form.h"
#ifdef MULTITHREADED
# define pageLocker() MutexLocker locker(&mutex)
# define pageLocker() std::unique_lock<std::recursive_mutex> locker(mutex)
#else
# define pageLocker()
#endif
......@@ -249,9 +249,6 @@ GBool PageAttrs::readBox(Dict *dict, const char *key, PDFRectangle *box) {
//------------------------------------------------------------------------
Page::Page(PDFDoc *docA, int numA, Object *pageDict, Ref pageRefA, PageAttrs *attrsA, Form *form) {
#ifdef MULTITHREADED
gInitMutex(&mutex);
#endif
ok = gTrue;
doc = docA;
xref = doc->getXRef();
......@@ -328,9 +325,6 @@ Page::Page(PDFDoc *docA, int numA, Object *pageDict, Ref pageRefA, PageAttrs *at
Page::~Page() {
delete attrs;
delete annots;
#ifdef MULTITHREADED
gDestroyMutex(&mutex);
#endif
}
Dict *Page::getResourceDict() {
......
......@@ -36,9 +36,10 @@
#pragma interface
#endif
#include <mutex>
#include "poppler-config.h"
#include "Object.h"