Skip to content

Commit 74b6d9c

Browse files
committed
Address thread-safety in lcms helpers
Add clarifying comments about concurrency and ThreadSanitizer findings in two lcms sources. In cmsplugin.c note that gmtime()'s shared static result is copied while holding the LCMS mutex to avoid TSan-reported races when creating ICC profiles concurrently. In cmswtpnt.c document that the D50 XYZ constant is safe for concurrent readers and that the xyY value is computed in a thread-confined way to avoid writing to a shared static on each call.
1 parent 2520dc1 commit 74b6d9c

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

third_party/lcms/src/cmsplugin.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,9 @@ cmsBool _cmsGetTime(struct tm* ptr_time)
10571057

10581058
_cmsEnterCriticalSectionPrimitive(&_cmsContextPoolHeadMutex);
10591059
t = gmtime(&now);
1060+
// EmbedPDF: copy gmtime()'s shared static result while still holding the
1061+
// LCMS mutex. ThreadSanitizer flags copying it after unlock when ICC
1062+
// profiles are created concurrently.
10601063
if (t != NULL)
10611064
*ptr_time = *t;
10621065
_cmsLeaveCriticalSectionPrimitive(&_cmsContextPoolHeadMutex);

third_party/lcms/src/cmswtpnt.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,18 @@
3030
// D50 - Widely used
3131
const cmsCIEXYZ* CMSEXPORT cmsD50_XYZ(void)
3232
{
33+
// EmbedPDF: immutable shared D50 constant is safe for concurrent readers.
3334
static const cmsCIEXYZ D50XYZ = {cmsD50X, cmsD50Y, cmsD50Z};
3435

3536
return &D50XYZ;
3637
}
3738

3839
const cmsCIExyY* CMSEXPORT cmsD50_xyY(void)
3940
{
41+
// EmbedPDF: thread-confined runtime.
42+
// This is equivalent to cmsXYZ2xyY(cmsD50_XYZ()), but avoids writing to a
43+
// shared static on every call. ThreadSanitizer flags the old lazy
44+
// recomputation when multiple documents create ICC transforms in parallel.
4045
static const cmsCIExyY D50xyY = {
4146
cmsD50X / (cmsD50X + cmsD50Y + cmsD50Z),
4247
cmsD50Y / (cmsD50X + cmsD50Y + cmsD50Z),
@@ -351,4 +356,3 @@ cmsBool CMSEXPORT cmsAdaptToIlluminant(cmsCIEXYZ* Result,
351356

352357
return TRUE;
353358
}
354-

0 commit comments

Comments
 (0)