diff --git a/AI.md b/AI.md index f3be3cc..9fd03f5 100644 --- a/AI.md +++ b/AI.md @@ -47,7 +47,7 @@ unfinished work out of `TODO.md` merely because its architecture is documented. Latest patch marker expected in an up-to-date bundle: -- `0483 nwfs: document NSS namespace bottom-up dependency order` +- `0484 core: add Linux pthread spinlock backend for NSS runtime` When a later chat receives a new `mars-nwe-master` bundle, compare `git log -1` with this marker. If the uploaded bundle already contains this commit subject, @@ -67,23 +67,19 @@ The active line is expected to include: Last generated patch: -- `0483 nwfs: document NSS namespace bottom-up dependency order` +- `0484 core: add Linux pthread spinlock backend for NSS runtime` Purpose of that patch: -- Record the bottom-up dependency order for the real NSS namespace import. -- Keep `dosNSpace.c` imported but not built until `nameSpace.c` and its real - beast/admin-volume/runtime dependencies are present. -- Identify the next code work as the smallest real NSS runtime base below - `comnBeasts.h`: `latch.h`, `xCache.h`, FSM/scheduler/cache headers and their - source owners, while reusing existing `include/core` headers instead of - duplicating them under `include/nwfs`. -- Preserve the `_ADMIN` volume policy for the later admin-volume import: - `SYS` is volume ID `0`, `_ADMIN` is volume ID `1`, and normal volumes start - at `2`; `_ADMIN` may be built before NCP exposure but must remain hidden until - the runtime path is ready. -- Extend the namespace import-layout test so it fails if future patches skip the - documented runtime/latch/cache/beast/admin-volume order or reintroduce shims. +- Add the first real bottom-up NSS runtime primitive in `include/core`: a + Linux-userspace spinlock backend backed by POSIX `pthread_spinlock_t`. +- Keep the backend generic in core because `latch.h`, MPK/scheduler headers and + future common-layer imports are not NWFS-specific. +- Add a local `nwcore.spinlock` CTest that checks init/destroy, lock/unlock and + trylock-on-held-lock behavior without enabling namespace or `_ADMIN` at run + time. +- Record that mars-nwe is now a Linux-only userspace target for this NSS runtime + import path; no BSD fallback or NWFS-local spinlock shim should be added. Directory/NDS work order before any FLAIM storage conversion: diff --git a/doc/NSS_NAMESPACE_AUDIT.md b/doc/NSS_NAMESPACE_AUDIT.md index ae4988d..7ceefaa 100644 --- a/doc/NSS_NAMESPACE_AUDIT.md +++ b/doc/NSS_NAMESPACE_AUDIT.md @@ -180,17 +180,21 @@ not add private copies under `src/nwfs/`. The planned code order is therefore: -1. `nwfs-nss-runtime-base`: the smallest real subset of NSS MPK/scheduler/FSM +1. `nwcore-nss-spinlock`: Linux-userspace spinlock primitive backed by POSIX + `pthread_spinlock_t`. This is core-owned because the NSS latch/MPK/runtime + layer is shared infrastructure, not a filesystem object. mars-nwe is Linux + userspace only for this path; do not add BSD portability scaffolding here. +2. `nwcore-nss-runtime-base`: the smallest real subset of NSS MPK/scheduler/FSM headers and sources needed by `latch.h`/`xCache.h`, with Linux-kernel-only or NDPS/DDS-only includes removed or guarded only when the filesystem path does not use them. -2. `nwfs-latch-cache-base`: direct imports for `latch`/cache types required by - beast structures. -3. `nwfs-beast-base`: beast class and root-beast structure support. -4. `nwfs-admin-volume`: build the `_ADMIN` virtual volume model, but do not +3. `nwfs-latch-cache-base`: direct imports for `latch`/cache types required by + beast structures once the generic runtime primitives are in core. +4. `nwfs-beast-base`: beast class and root-beast structure support. +5. `nwfs-admin-volume`: build the `_ADMIN` virtual volume model, but do not expose it through NCP until the runtime path is ready. -5. `nwfs-namespace-registry`: `nameSpace.c` and registration state. -6. DOS namespace activation: build `dosNSpace.c` only after the registry and its +6. `nwfs-namespace-registry`: `nameSpace.c` and registration state. +7. DOS namespace activation: build `dosNSpace.c` only after the registry and its real dependencies are present. The `_ADMIN` volume ID policy for that later import is fixed even while the diff --git a/include/core/spinlock.h b/include/core/spinlock.h new file mode 100644 index 0000000..7cb0b62 --- /dev/null +++ b/include/core/spinlock.h @@ -0,0 +1,54 @@ +#ifndef MARS_NWE_CORE_SPINLOCK_H +#define MARS_NWE_CORE_SPINLOCK_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * Linux userspace backend for NSS runtime spin locks. + * + * The imported NSS common-layer headers were written for NetWare/NLM and + * Linux-kernel style spinlock APIs. mars-nwe now targets Linux userspace, so + * keep the primitive in libnwcore and map it directly to POSIX pthread + * spinlocks instead of carrying a fake NWFS-local shim. + */ +typedef pthread_spinlock_t spinlock_t; + +static inline void +spin_lock_init(spinlock_t *lock) +{ + (void)pthread_spin_init(lock, PTHREAD_PROCESS_PRIVATE); +} + +static inline void +spin_lock_destroy(spinlock_t *lock) +{ + (void)pthread_spin_destroy(lock); +} + +static inline void +spin_lock(spinlock_t *lock) +{ + (void)pthread_spin_lock(lock); +} + +static inline void +spin_unlock(spinlock_t *lock) +{ + (void)pthread_spin_unlock(lock); +} + +static inline int +spin_trylock(spinlock_t *lock) +{ + return pthread_spin_trylock(lock) == 0; +} + +#ifdef __cplusplus +} +#endif + +#endif /* MARS_NWE_CORE_SPINLOCK_H */ diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 78b4fbe..b2215d8 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -63,6 +63,10 @@ configure_file( "${CMAKE_SOURCE_DIR}/include/core/que.h" "${NWCORE_BUILD_INCLUDE_DIR}/que.h" COPYONLY) +configure_file( + "${CMAKE_SOURCE_DIR}/include/core/spinlock.h" + "${NWCORE_BUILD_INCLUDE_DIR}/spinlock.h" + COPYONLY) configure_file( "${CMAKE_SOURCE_DIR}/include/core/size_t.h" "${NWCORE_BUILD_INCLUDE_DIR}/size_t.h" diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index ed15749..a6fc430 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -17,5 +17,8 @@ if(MARS_NWE_BUILD_TESTS) endif() if(MARS_NWE_BUILD_NWFS_TESTS) + if(NOT MARS_NWE_BUILD_TESTS) + add_subdirectory(core/spinlock) + endif() add_subdirectory(nwfs) endif() diff --git a/tests/core/CMakeLists.txt b/tests/core/CMakeLists.txt index 6e2ec25..daa48f6 100644 --- a/tests/core/CMakeLists.txt +++ b/tests/core/CMakeLists.txt @@ -4,6 +4,7 @@ add_subdirectory(ini) add_subdirectory(log) +add_subdirectory(spinlock) add_subdirectory(bit) add_subdirectory(bitmap) add_subdirectory(crc) diff --git a/tests/core/spinlock/CMakeLists.txt b/tests/core/spinlock/CMakeLists.txt new file mode 100644 index 0000000..13fe1d5 --- /dev/null +++ b/tests/core/spinlock/CMakeLists.txt @@ -0,0 +1,9 @@ +add_executable(test_nwcore_spinlock + test_nwcore_spinlock.c) + +target_compile_features(test_nwcore_spinlock PRIVATE c_std_99) +target_include_directories(test_nwcore_spinlock PRIVATE + "${CMAKE_SOURCE_DIR}/include/core") +target_link_libraries(test_nwcore_spinlock PRIVATE mars_nwe::core) + +add_test(NAME nwcore.spinlock COMMAND test_nwcore_spinlock) diff --git a/tests/core/spinlock/test_nwcore_spinlock.c b/tests/core/spinlock/test_nwcore_spinlock.c new file mode 100644 index 0000000..0de4157 --- /dev/null +++ b/tests/core/spinlock/test_nwcore_spinlock.c @@ -0,0 +1,29 @@ +#include "spinlock.h" + +#include +#include + +#define CHECK(expr) \ + do { \ + if (!(expr)) { \ + fprintf(stderr, "CHECK failed at %s:%d: %s\n", __FILE__, __LINE__, #expr); \ + return 1; \ + } \ + } while (0) + +int main(void) +{ + spinlock_t lock; + + spin_lock_init(&lock); + + CHECK(spin_trylock(&lock)); + CHECK(!spin_trylock(&lock)); + spin_unlock(&lock); + + spin_lock(&lock); + spin_unlock(&lock); + + spin_lock_destroy(&lock); + return 0; +}