Design of Mutex and Event Synchronization Primitives for a Multithreaded Managed Runtime with Stackful Coroutines

Ivan Panferov

Abstract


Operating system synchronization primitives are unsuitable for managed runtimes with coroutines: acquiring an OS mutex blocks the entire carrier thread, which leads to deadlocks under cooperative multitasking. Existing coroutine-aware solutions are either tightly coupled to the internals of a specific runtime (Go, JVM), or target native environments without a garbage collector (Boost.Fiber), or allow waiting only in an asynchronous context (C#, Kotlin). This paper proposes a self-contained architecture of Mutex and Event synchronization primitives for the multithreaded managed ArkTS/OpenHarmony runtime with stackful coroutines. The primitives are managed-heap objects with built-in queues of waiting coroutines allowing the architecture to be ported to other managed runtimes whose scheduler provides coroutine suspend/resume operations. The one-shot Event replaces the condition variable with simpler semantics. The mutex is adapted to short critical sections with an exponential-backoff spin phase and an adaptive starvation mode guaranteeing acquisition within a bounded number of wakeups. Promise<T> and AsyncLock with wait-for-graph deadlock detection are built on top. On an 8-core arm64 device, the average acquisition time stays within hundreds of nanoseconds for up to 1000 coroutines, with overhead below 1.5x in most configurations versus 10–14x for Go's sync.Mutex. The gain comes at the cost of higher maximum (tail) latencies under long critical sections and high contention.

Full Text:

PDF (Russian)

References


N. G. Leveson and C. S. Turner, "An investigation of the Therac-25 accidents," Computer, vol. 26, no. 7, pp. 18–41, Jul. 1993.

S. Lu, S. Park, E. Seo, and Y. Zhou, "Learning from mistakes: A comprehensive study on real world concurrency bug characteristics," in Proc. 13th Int. Conf. Architectural Support for Programming Languages and Operating Systems (ASPLOS XIII), Seattle, WA, USA, 2008, pp. 329–339.

OpenHarmony Project Documentation. [Online]. Available: https://gitee.com/openharmony/docs [дата обращения: 12.07.2026]

L. Lamport, "Time, clocks, and the ordering of events in a distributed system," Communications of the ACM, vol. 21, no. 7, pp. 558–565, Jul. 1978.

The Go Programming Language, "Package sync." [Online]. Available: https://pkg.go.dev/sync [дата обращения: 12.07.2026]

D. Vyukov, "Scalable Go Scheduler Design Doc," 2012. [Online]. Available: https://golang.org/s/go11sched [дата обращения: 12.07.2026]

R. Pressler and A. Bateman, "JEP 444: Virtual Threads," OpenJDK. [Online]. Available: https://openjdk.org/jeps/444 [дата обращения: 12.07.2026]

"JEP 491: Synchronize Virtual Threads without Pinning," OpenJDK. [Online]. Available: https://openjdk.org/jeps/491 [дата обращения: 12.07.2026]

O. Kowalke, "Boost.Fiber documentation." [Online]. Available: https://www.boost.org/doc/libs/release/libs/fiber/ [дата обращения: 12.07.2026]

Microsoft, "Task-based asynchronous pattern (TAP) in .NET." [Online]. Available: https://learn.microsoft.com/en-us/dotnet/standard/asynchronous-programming-patterns/task-based-asynchronous-pattern-tap [дата обращения: 12.07.2026]

R. Elizarov, M. Belyaev, M. Akhin, and I. Usmanov, "Kotlin coroutines: design and implementation," in Proc. 2021 ACM SIGPLAN Int. Symp. on New Ideas, New Paradigms, and Reflections on Programming and Software (Onward! 2021), 2021, pp. 68–84.

R. Nystrom, "What Color is Your Function?," 2015. [Online]. Available: https://journal.stuffwithstuff.com/2015/02/01/what-color-is-your-function/ [дата обращения: 12.07.2026]

H. Franke, R. Russell, and M. Kirkwood, "Fuss, futexes and furwocks: Fast userlevel locking in Linux," in Proc. Ottawa Linux Symposium, Ottawa, Canada, 2002, pp. 479–495.

H.-J. Boehm and S. V. Adve, "Foundations of the C++ concurrency memory model," in Proc. 29th ACM SIGPLAN Conf. on Programming Language Design and Implementation (PLDI'08), Tucson, AZ, USA, 2008, pp. 68–78.

N. Koval, D. Khalanskiy, and D. Alistarh, "CQS: A formally-verified framework for fair and abortable synchronization," Proceedings of the ACM on Programming Languages, vol. 7, no. PLDI, art. 116, Jun. 2023.

D. Lea, "The java.util.concurrent synchronizer framework," Science of Computer Programming, vol. 58, no. 3, pp. 293–309, Dec. 2005.

R. K. Treiber, "Systems programming: Coping with parallelism," IBM Almaden Research Center, San Jose, CA, USA, Tech. Rep. RJ 5118, 1986.

J. M. Mellor-Crummey and M. L. Scott, "Algorithms for scalable synchronization on shared-memory multiprocessors," ACM Transactions on Computer Systems, vol. 9, no. 1, pp. 21–65, Feb. 1991.

T. S. Craig, "Building FIFO and priority-queuing spin locks from atomic swap," Dept. of Computer Science, University of Washington, Seattle, WA, USA, Tech. Rep. UW-CSE-93-02-02, Feb. 1993.

P. S. Magnusson, A. Landin, and E. Hagersten, "Queue locks on cache coherent multiprocessors," in Proc. 8th Int. Symp. on Parallel Processing (IPPS), Cancún, Mexico, 1994, pp. 165–171.

D. Vyukov, "Intrusive MPSC node-based queue," 1024cores. [Online]. Available: https://www.1024cores.net/home/lock-free-algorithms/queues/intrusive-mpsc-node-based-queue [дата обращения: 12.07.2026].

T. E. Anderson, "The performance of spin lock alternatives for shared-memory multiprocessors," IEEE Transactions on Parallel and Distributed Systems, vol. 1, no. 1, pp. 6–16, Jan. 1990.

M. Herlihy and N. Shavit, The Art of Multiprocessor Programming. San Francisco, CA, USA: Morgan Kaufmann, 2008.

E. G. Coffman, M. J. Elphick, and A. Shoshani, "System deadlocks," ACM Computing Surveys, vol. 3, no. 2, pp. 67–78, Jun. 1971.


Refbacks

  • There are currently no refbacks.


Abava  Кибербезопасность Monetec 2026 СНЭ

ISSN: 2307-8162