Repository navigation
libContour: no process-wide state; drop libcontour_mutex - #924
Merged
Merged
Conversation
transfix
force-pushed
the
refactor/libcontour-no-globals
branch
3 times, most recently
from
November 1, 2026 04:00
110f1cf to
d27974a
Compare
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
force-pushed
the
refactor/libcontour-no-globals
branch
from
November 6, 2026 06:51
d27974a to
0af8321
Compare
transfix
enabled auto-merge (squash)
November 6, 2026 06:52
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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-nmover 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_cutoffinpreprocessData, andhashFun_iq's static.IntTree *global_tree, set byDone()for itsqsortcomparatorsqsortwith the bound it is sorted by (BoundedSeed). The comparisons are unchanged, so tied bounds keep the order they had.maxqsizeand theMAXQUEUESIZEhigh-water printoutPropagateRegion'sstatic IndexedQueuecompSeeds' queue, passed down (empty between calls)hashFun_iq'sstatic double Aconststatic Rangeresults of-=,^,Complement,Disjointoperator^returns by value (it returned a reference to its static; it has no callers)Data::funtopol1/2staticsstatic float min_cutoff(reset by each call before use)readData,preprocessDataint verbose = 1,setVerboseLevelexternsconstexpr int verbose = 0in the newcontour/contour_verbose.h;setVerboseLevelremovederrorHandlerfunction pointer,setErrorHandlerstd::runtime_errorinstead ofexit(0). The only fatal caller is the file-readingDataconstructor, which libcvc doesn't use.cubes,adjfaces,cubeverts,cubeedges,unst2d,unst3d,tetadj,rect2d,edgeinfo,triCases,Datareg3::cellFaceVertconst(read-only sections)Then
libcontour_mutex.his deleted.LBIE_Mesher.cppis back to master's text. The oracle loses its lock and itsquiet_libcontourtoggle, 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)
compute_contour_spectrum_libcontourin both unit modes andcvc::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-25640EAB8CB…), and two runs before the change also matched each other.lbie_mesher_test69/69 andcontour_spectrum_test25/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 --diffis clean. The vendored tree hasDisableFormat, so its own style is kept.Also on this branch
iqueue.hincludes<unistd.h>(its own commit).rangesweep.hnow includesiqueue.hbeforedata.h, which is whererangesweep.cppused to get<unistd.h>. Clang looks up the non-dependentsleep()at the template's definition, so both macOS recipe builds failed without it.recipe / macos-latest / Debugpassed with it.Re-verified on master 9d74793 (Windows Release):
lbie_mesher_test69/69,contour_spectrum_test25/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.