LCOV - code coverage report
Current view: top level - source/lib - module_init.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 7 27 25.9 %
Date: 2026-12-03 01:19:39 Functions: 1 2 50.0 %

          Line data    Source code
       1             : /* Copyright (C) 2025 Wildfire Games.
       2             :  *
       3             :  * Permission is hereby granted, free of charge, to any person obtaining
       4             :  * a copy of this software and associated documentation files (the
       5             :  * "Software"), to deal in the Software without restriction, including
       6             :  * without limitation the rights to use, copy, modify, merge, publish,
       7             :  * distribute, sublicense, and/or sell copies of the Software, and to
       8             :  * permit persons to whom the Software is furnished to do so, subject to
       9             :  * the following conditions:
      10             :  *
      11             :  * The above copyright notice and this permission notice shall be included
      12             :  * in all copies or substantial portions of the Software.
      13             :  *
      14             :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
      15             :  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
      16             :  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
      17             :  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
      18             :  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
      19             :  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
      20             :  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
      21             :  */
      22             : 
      23             : /*
      24             :  * helpers for module initialization/shutdown.
      25             :  */
      26             : 
      27             : #include "precompiled.h"
      28             : 
      29             : #include "module_init.h"
      30             : 
      31             : #include "lib/debug.h"
      32             : #include "lib/sysdep/cpu.h"
      33             : 
      34             : // not yet initialized, or already shutdown
      35             : static const Status UNINITIALIZED = 0;  // value documented in header
      36             : // running user callback - concurrent ModuleInit callers must spin
      37             : static const Status BUSY = ERR::AGAIN;  // never returned
      38             : // init succeeded; allow shutdown
      39             : static const Status INITIALIZED = INFO::SKIPPED;
      40             : 
      41             : 
      42           1 : Status ModuleInit(volatile ModuleInitState* initState, Status (*init)())
      43             : {
      44           1 :     for(;;)
      45             :     {
      46           1 :         Status expected{ UNINITIALIZED };
      47           1 :         if(initState->compare_exchange_strong(expected, BUSY))
      48             :         {
      49           1 :             Status ret = init();
      50           1 :             initState->store(ret == INFO::OK ? INITIALIZED : ret);
      51           1 :             return ret;
      52             :         }
      53             : 
      54           0 :         const ModuleInitState latchedInitState = *initState;
      55           0 :         if(latchedInitState == UNINITIALIZED || latchedInitState == BUSY)
      56             :         {
      57           0 :             cpu_Pause();
      58           0 :             continue;
      59             :         }
      60             : 
      61           0 :         ENSURE(latchedInitState == INITIALIZED || latchedInitState < 0);
      62           0 :         return (Status)latchedInitState;
      63             :     }
      64             : }
      65             : 
      66             : 
      67           0 : Status ModuleShutdown(volatile ModuleInitState* initState, void (*shutdown)())
      68             : {
      69           0 :     for(;;)
      70             :     {
      71           0 :         Status expected{ INITIALIZED };
      72           0 :         if(initState->compare_exchange_strong(expected, BUSY))
      73             :         {
      74           0 :             shutdown();
      75           0 :             initState->store(UNINITIALIZED);
      76           0 :             return INFO::OK;
      77             :         }
      78             : 
      79           0 :         const ModuleInitState latchedInitState = *initState;
      80           0 :         if(latchedInitState == INITIALIZED || latchedInitState == BUSY)
      81             :         {
      82           0 :             cpu_Pause();
      83           0 :             continue;
      84             :         }
      85             : 
      86           0 :         if(latchedInitState == UNINITIALIZED)
      87             :             return INFO::SKIPPED;
      88             : 
      89           0 :         ENSURE(latchedInitState < 0);
      90           0 :         return (Status)latchedInitState;
      91             :     }
      92             : }

Generated by: LCOV version 1.16