menios icon indicating copy to clipboard operation
menios copied to clipboard

Implement thread-safe libc with proper locking

Open pbalduino opened this issue 3 months ago • 0 comments

Goal

Add thread-safety to global libc state with proper locking to enable safe concurrent use of C standard library.

Context

Currently, libc has no concurrency protection:

  • stdio buffers (FILE* streams) aren't locked
  • Environment variable access isn't protected
  • Allocator (malloc/free) may not be thread-safe
  • Global state (errno, strtok static buffer, etc.) has no protection

With pthread support (#109), programs will crash or corrupt data when multiple threads use libc simultaneously.

Definition of Done

  • [ ] Thread-safe stdio (flockfile/funlockfile, per-FILE mutexes)
  • [ ] Thread-safe environment (getenv/putenv/setenv locking)
  • [ ] Thread-safe allocator (malloc/free locking if needed)
  • [ ] Thread-local errno
  • [ ] Reentrant variants (_r functions: strtok_r, localtime_r, etc.)
  • [ ] Testing with multithreaded programs

Dependencies

Required

  • #109 - pthread API and POSIX threading

Related

  • #110 - Thread-safe C library (parent issue, may be duplicate)

Priority

Medium - Required after pthread implementation

Estimated Time

6-8 weeks part-time

pbalduino avatar Oct 21 '25 14:10 pbalduino