Primary Color A Tribute to Human-Crafted Code Wizardry: The Two Standard C Functions That 99% of C Programmers Don't Know About that Have An Incredible Embedded Payoff
Early in my career in the early 1980s I was fortunate enough to work with an engineer who wrote some of the coolest C code I’ve seen in 40+ years. This series is my attempt to recreate it and explain how it worked.
That code powered the proprietary pipeline in FactoryLink1, the industrial control and automation software we worked on at United States Data Corporation (USDATA). I was in the Kernel Group, where I helped maintain the runtime kernel and wrote the Compiled Math & Logic toolchain.
1 FactoryLink launched in 1986, and by 2003 USDATA reported that it had shipped more than 79,000 copies to more than 110 countries. When USDATA sold the FactoryLink business to Tecnomatix in 2003, it kept the patent on FactoryLink’s Open Software Bus and licensed it to Tecnomatix. U.S. Patent 4,908,746 expired on March 13, 2007. Later that year Siemens bought UGS, which had acquired Tecnomatix in 2005. On May 29, 2026, the USPTO cancelled Siemens Industry Software’s trademark registration for the Factory Link name.
The two functions are: setjmp(...) and longjmp(...).
A goto can jump to a label in the current function. A longjmp can take you back to a point that any still-active caller in the same thread has saved using setjmp, even from deep in the call stack.
Here’s how we put that idea to work in the FactoryLink recreation. We can declare hundreds of variables at compile time. When a reader asks for one, it gets the value right away if there’s a change it hasn’t seen. Otherwise it saves its place and goes to sleep right there inside the read. Several readers can be waiting on the same variable, each with its own stack. When any writer changes that variable, every reader waiting on it picks up where it left off and its read returns with the new value. Until then, those readers spend absolutely no CPU time polling. That can be especially valuable in an embedded system.
Before the writer even gets to its next statement, every reader that was waiting on that variable has already returned from its read and had a chance to use the new value. And all of that happens on a single thread.
If you’ve used MQTT, you’ll recognize the pattern. A writer publishes a value without knowing who’s listening. Here it all happens in memory. When a writer changes the value, longjmp takes each waiting reader right back into the read call it made earlier. That same call finally returns with the new value. But unlike MQTT, this architecture lets the writer hand its own thread to each waiting reader in turn, without sending a message through a broker. It took me a long time to appreciate how much work that little handoff saved.
In the next article I’ll introduce the API and use a small sketch to show how long that handoff takes. Then we’ll try the same example with an RTOS and with a cooperative scheduling library and see how the timings compare.