Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions docs/embedpdf/fork-ledger.md
Original file line number Diff line number Diff line change
Expand Up @@ -486,10 +486,12 @@ taking upstream's side without putting `EPDF_TLS` back, on the variable renamed

### per-thread-init
**Rule.** Library start-up runs once per thread. A process-wide value it writes must be
thread-local, or identical on every thread and on the allowlist.
**Why.** `EPDF_InitThread` calls `FPDF_InitLibrary` on every worker thread.
thread-local, or identical on every thread and on the allowlist. `EPDF_InitThread` and
`EPDF_ShutdownThread` are counted per thread: the last `EPDF_ShutdownThread` tears PDFium down.
**Why.** `EPDF_InitThread` calls `FPDF_InitLibrary` on every worker thread. Two runtimes on one
thread (two engines in a Node thread) share its PDFium, and each calls the pair.
**Where.** `fpdf_view.cpp` (`FPDF_InitLibrary`), `epdf_threading.cpp`.
**Guarded by.** The thread soak.
**Guarded by.** The thread soak; `EPDFViewEmbedderTest.ThreadLifecycleIsCounted`.
**When upstream touches this.** Anything new in `FPDF_InitLibraryWithConfig` or
`FPDF_DestroyLibrary`.

Expand Down
32 changes: 27 additions & 5 deletions fpdfsdk/epdf_threading.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,47 @@
// explicit for callers that run PDFium on worker threads. With
// embedpdf_thread_local_globals enabled (see core/fxcrt/epdf_tls.h) each worker
// thread owns its own PDFium globals, so every such thread must initialize and
// tear down PDFium itself. With the flag disabled these are exact aliases of
// FPDF_InitLibrary / FPDF_DestroyLibrary.
// tear down PDFium itself. With the flag disabled they start and tear down the
// process's one PDFium, as FPDF_InitLibrary / FPDF_DestroyLibrary do.
//
// The calls are counted per thread. Callers that share a thread share its
// PDFium (two engines in one Node thread, each over the same native library),
// so a caller that leaves must not tear it down under the ones still there.
//
// These are intentionally exported for ALL targets (including wasm, where they
// simply wrap the normal init/destroy) so shared runtime code can call a single
// lifecycle entry point regardless of platform.

#include "public/epdf_view.h"

#include "core/fxcrt/epdf_tls.h"
#include "public/fpdfview.h"

namespace {

// How many EPDF_InitThread() calls on this thread still wait for their
// EPDF_ShutdownThread().
EPDF_TLS int g_thread_users = 0;

} // namespace

FPDF_EXPORT void FPDF_CALLCONV EPDF_InitThread() {
// Routes through FPDF_InitLibrary so the standard config/init path runs for
// the calling thread.
// the calling thread. It does nothing on a thread that is already started.
FPDF_InitLibrary();
++g_thread_users;
}

FPDF_EXPORT void FPDF_CALLCONV EPDF_ShutdownThread() {
// Lifecycle-strict: callers must have already closed every PDFium handle
// created on this thread before invoking this.
// Lifecycle-strict: a caller must have already closed every PDFium handle
// it created on this thread before invoking this.
if (g_thread_users > 1) {
// Others still use this thread's PDFium: the last one out tears it down.
--g_thread_users;
return;
}
// The last one out, or a call with no EPDF_InitThread() before it, which
// tears down as FPDF_DestroyLibrary() does.
g_thread_users = 0;
FPDF_DestroyLibrary();
}
59 changes: 59 additions & 0 deletions fpdfsdk/epdf_view_embeddertest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <cstdint>
#include <limits>
#include <string>
#include <thread>
#include <vector>

#include "constants/page_object.h"
Expand All @@ -28,7 +29,9 @@
#include "fpdfsdk/epdf_c_api_test.h"
#include "fpdfsdk/epdf_edit_test_util.h"
#include "public/cpp/fpdf_scopers.h"
#include "public/epdf_annot.h"
#include "public/epdf_progressive.h"
#include "public/fpdf_annot.h"
#include "public/fpdf_edit.h"
#include "public/fpdf_progressive.h"
#include "public/fpdf_save.h"
Expand Down Expand Up @@ -987,3 +990,59 @@ TEST_F(EPDFViewEmbedderTest, ImageOfAnotherDocumentDraws) {
EXPECT_EQ(render(document.get(), document.get()),
render(document.get(), other.get()));
}

#if defined(EPDF_THREAD_LOCAL_GLOBALS)
// EPDF_InitThread() and EPDF_ShutdownThread() are counted (rule
// per-thread-init): callers that share a thread share its PDFium, and one
// that leaves does not take it from the ones still there. A second engine in
// a Node thread did: once it was destroyed, the first read content with the
// thread's operator table gone. Runs on a thread of its own, as a worker
// does: the harness started the main thread's PDFium, and the last
// EPDF_ShutdownThread() here would tear that down.
TEST_F(EPDFViewEmbedderTest, ThreadLifecycleIsCounted) {
// A page with a square whose appearance multiplies.
const std::string pdf = MakePdf({
"<< /Type /Catalog /Pages 2 0 R >>",
"<< /Type /Pages /Kids [3 0 R] /Count 1 >>",
"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 200] "
"/Annots [4 0 R] >>",
"<< /Type /Annot /Subtype /Square /Rect [10 10 60 60] "
"/AP << /N 5 0 R >> >>",
Stream("/Type /XObject /Subtype /Form /BBox [0 0 50 50] "
"/Resources << /ExtGState << /GS << /BM /Multiply >> >> >>",
"/GS gs 1 0 0 rg 0 0 50 50 re f"),
});
// The square's blend mode, which is read by parsing its appearance.
auto blend_mode = [&pdf]() -> int {
embedpdf_test::ScopedTestDocument doc(
embedpdf_test::LoadMemDocumentForTest(pdf.data(), pdf.size(), nullptr));
if (!doc) {
return -1;
}
ScopedFPDFPage page(FPDF_LoadPage(doc.get(), 0));
if (!page) {
return -1;
}
ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page.get(), 0));
return annot ? EPDFAnnot_GetBlendMode(annot.get()) : -1;
};

int with_one_left = -1;
int started_again = -1;
std::thread worker([&] {
EPDF_InitThread();
EPDF_InitThread();
EPDF_ShutdownThread();
with_one_left = blend_mode();
EPDF_ShutdownThread();

// The last one out tore PDFium down; the next one in starts it again.
EPDF_InitThread();
started_again = blend_mode();
EPDF_ShutdownThread();
});
worker.join();
EXPECT_EQ(FPDF_BLENDMODE_Multiply, with_one_left);
EXPECT_EQ(FPDF_BLENDMODE_Multiply, started_again);
}
#endif // defined(EPDF_THREAD_LOCAL_GLOBALS)
15 changes: 10 additions & 5 deletions public/epdf_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,16 @@ extern "C" {
// PDFium state, so every thread that uses PDFium MUST call
// EPDF_InitThread before creating any handle and EPDF_ShutdownThread
// after closing every handle it created. Handles must never cross
// threads. When per-thread globals are disabled, these are exact
// aliases of FPDF_InitLibrary / FPDF_DestroyLibrary and are safe to
// call once on a single thread. EPDF_ShutdownThread is lifecycle-
// strict: only legal after all handles on the calling thread are
// closed.
// threads. When per-thread globals are disabled, they start and tear
// down the process's one PDFium, as FPDF_InitLibrary and
// FPDF_DestroyLibrary do.
// The calls are counted per thread. Callers that share a thread
// (two runtimes loaded over the same native library) each call
// EPDF_InitThread once and EPDF_ShutdownThread once, and only the
// last EPDF_ShutdownThread tears PDFium down: a caller that leaves
// does not take PDFium from the ones still using it.
// EPDF_ShutdownThread is lifecycle-strict: a caller makes it only
// after closing every handle it created on the calling thread.
FPDF_EXPORT void FPDF_CALLCONV EPDF_InitThread();
FPDF_EXPORT void FPDF_CALLCONV EPDF_ShutdownThread();

Expand Down