Skip to content

libContour: no process-wide state; drop libcontour_mutex - #924

Merged
transfix merged 2 commits into
masterfrom
refactor/libcontour-no-globals
Nov 6, 2026
Merged

transfix merged 2 commits into
masterfrom
refactor/libcontour-no-globals

Conversation

@transfix

@transfix transfix commented Oct 31, 2026 •

Copy link
Copy Markdown
Owner

Based on master (#919 merged as 44826cc). Besides the libContour change, this branch carries one build fix; see "Also on this branch".

#919 made LBIE's LIBISOCONTOUR extraction take the lock the contour spectrum's libContour oracle already took, because the vendored libContour kept mutable process-wide state. This PR removes that state, then the lock. LIBISOCONTOUR meshes and the oracle now run in parallel, and nothing carries over from one call, test or thread to the next.

What was shared, and what it is now

The inventory comes from llvm-nm over the library's compiled objects (every writable data symbol), not only from a source grep. The sweep found four items the grep missed: triCases, Datareg3::cellFaceVert, min_cutoff in preprocessData, and hashFun_iq's static.

State Where Now
IntTree *global_tree, set by Done() for its qsort comparators inttree.cpp Each seed goes to qsort with the bound it is sorted by (BoundedSeed). The comparisons are unchanged, so tied bounds keep the order they had.
maxqsize and the MAXQUEUESIZE high-water printout rangesweep.cpp removed
PropagateRegion's static IndexedQueue rangesweep.cpp compSeeds' queue, passed down (empty between calls)
hashFun_iq's static double A iqueue.h local const
static Range results of -=, ^, Complement, Disjoint range.cpp locals; operator^ returns by value (it returned a reference to its static; it has no callers)
Data::funtopol1/2 statics data.h, data.cpp members, 0 for a one-variable dataset (the statics' value in libcvc, which only builds one-variable datasets)
function-local static float min_cutoff (reset by each call before use) data.h readData, preprocessData locals
int verbose = 1, setVerboseLevel contour.cpp, 31 externs constexpr int verbose = 0 in the new contour/contour_verbose.h; setVerboseLevel removed
errorHandler function pointer, setErrorHandler contour.cpp a plain function (the old default handler). Its fatal branch throws std::runtime_error instead of exit(0). The only fatal caller is the file-reading Data constructor, which libcvc doesn't use.
lookup tables cubes, adjfaces, cubeverts, cubeedges, unst2d, unst3d, tetadj, rect2d, edgeinfo, triCases, Datareg3::cellFaceVert various const (read-only sections)

Then libcontour_mutex.h is deleted. LBIE_Mesher.cpp is back to master's text. The oracle loses its lock and its quiet_libcontour toggle, and its docs say it is safe to call concurrently. Neither setter had a caller in the repo, and libContour's headers aren't installed. After the rebuild, no libContour object holds writable data.

Verification (Windows, Release)

  • Bit-identical. A temporary test (not committed) dumped compute_contour_spectrum_libcontour in both unit modes and cvc::iso(..., LIBISOCONTOUR) meshes (points and triangles) as hex floats. It covered ten volumes: spheres, noise and sines; uchar, ushort, float, double and int; cubic and non-cubic, two isovalues each. The 18 MB dump is byte-for-byte the same before and after (SHA-256 40EAB8CB…), and two runs before the change also matched each other.
  • lbie_mesher_test 69/69 and contour_spectrum_test 25/25 pass. LBIE: LIBISOCONTOUR extraction takes the libContour lock the contour spectrum oracle takes #919's two concurrency tests now run with no lock and pass 20 of 20 repeats; the mesh-plus-spectrum test went from 0.91 s to 0.68 s.
  • git clang-format --diff is clean. The vendored tree has DisableFormat, so its own style is kept.

Also on this branch

  • iqueue.h includes <unistd.h> (its own commit). rangesweep.h now includes iqueue.h before data.h, which is where rangesweep.cpp used to get <unistd.h>. Clang looks up the non-dependent sleep() at the template's definition, so both macOS recipe builds failed without it. recipe / macos-latest / Debug passed with it.

Re-verified on master 9d74793 (Windows Release): lbie_mesher_test 69/69, contour_spectrum_test 25/25, and the three LIBISOCONTOUR tests 10/10 repeats. #953's heat-stop test fix and #946's macOS Jolt test fix, which this branch carried or needed earlier, are now on master.

Not covered

LBIE's and SDF's own copies of the cubes tables (LBIE/cubes.h, SDF/SignDistanceFunction_v2/cubes.h) are file-local non-const statics. They are only read, and they aren't libContour.

Base automatically changed from fix/lbie-libisocontour-mutex to master October 31, 2026 08:44
@transfix
transfix force-pushed the refactor/libcontour-no-globals branch 3 times, most recently from 110f1cf to d27974a Compare November 1, 2026 04:00
The vendored libContour kept mutable state shared by every caller in the
process, so #919 serialized its two callers (the contour spectrum's
libContour oracle and LBIE's LIBISOCONTOUR extraction) on
libcontour_mutex(). A sweep of every writable data symbol in the
library's objects (llvm-nm) found:

- IntTree's global_tree (inttree.cpp), set by Done() for its qsort
  comparators. Each seed now goes to qsort with the bound it is sorted
  by (BoundedSeed). The comparisons are the same, so qsort leaves the
  seeds, tied bounds included, in the order it did before.
- rangesweep.cpp: maxqsize and the MAXQUEUESIZE high-water printout
  (gone), and PropagateRegion's static IndexedQueue, now compSeeds' queue
  passed down (it is empty between calls).
- iqueue.h: hashFun_iq's lazily initialized static constant, now a local
  const.
- range.cpp: the static results of operator-=, operator^, Complement and
  Disjoint, now locals. operator^, which returned a reference to its
  static, returns by value; nothing calls it.
- Data: the static funtopol1/funtopol2, now members (0 for a
  one-variable dataset, as the statics were in libcvc, which builds no
  other kind), and the function-local static min_cutoff in readData and
  preprocessData, which each call reset before use, now locals.
- `verbose` (1 by default; the oracle zeroed it around each computation)
  is a constexpr 0 in contour_verbose.h. setVerboseLevel is gone.
- The errorHandler function pointer and setErrorHandler are gone.
  errorHandler is the old default handler, and its fatal branch throws
  std::runtime_error instead of calling exit(0). The only fatal caller is
  the file-reading Data constructor, which libcvc does not use.
- The lookup tables (cubes, adjfaces, cubeverts, cubeedges, unst2d,
  unst3d, tetadj, rect2d, edgeinfo, triCases, Datareg3::cellFaceVert) are
  const, so they live in read-only sections.

libcontour_mutex.h, its lock in LBIE (LBIE_Mesher.cpp is master's again)
and the oracle's lock and verbose toggle are gone. The rebuilt objects
hold no writable data.

Bit-identical: a dump of compute_contour_spectrum_libcontour (both unit
modes) and cvc::iso(..., LIBISOCONTOUR) meshes, as hex floats, over ten
volumes (spheres, noise and sines; uchar, ushort, float, double and int;
cubic and non-cubic) is byte-for-byte the same before and after (SHA-256
40EAB8CB...). #919's concurrency tests pass with no lock, 20 of 20
repeats.
rangesweep.h now includes iqueue.h before data.h, which was where
rangesweep.cpp got <unistd.h> from. Clang looks up the non-dependent
sleep() at the template's definition, so the macOS build failed.
@transfix
transfix force-pushed the refactor/libcontour-no-globals branch from d27974a to 0af8321 Compare November 6, 2026 06:51
@transfix
transfix enabled auto-merge (squash) November 6, 2026 06:52
@transfix
transfix merged commit 462900e into master Nov 6, 2026
14 of 28 checks passed
@transfix
transfix deleted the refactor/libcontour-no-globals branch November 6, 2026 07:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant