diff options
| author | fusion32 <marcopuzziello@gmail.com> | 2025-07-16 17:22:39 -0300 |
|---|---|---|
| committer | fusion32 <marcopuzziello@gmail.com> | 2025-07-16 17:22:39 -0300 |
| commit | 6d498fe17dab48cfd8fc8a4da62a573b799e10ba (patch) | |
| tree | 1c8d55bd907b35abf014385dbedb90b2ae98f96a /src/threads.cc | |
| parent | e65dc561acab7a9e49f59777f16cb040c574c64d (diff) | |
| download | game-6d498fe17dab48cfd8fc8a4da62a573b799e10ba.tar.gz game-6d498fe17dab48cfd8fc8a4da62a573b799e10ba.zip | |
fix most init and parsing bugs + critical bug with dynamic strings
With an appropriate query manager, the server now starts up and
seems to work but won't properly handle connections until we fix
the problem with threads and signals described in `TODO.md`.
Diffstat (limited to 'src/threads.cc')
| -rw-r--r-- | src/threads.cc | 24 |
1 files changed, 18 insertions, 6 deletions
diff --git a/src/threads.cc b/src/threads.cc index a7acb41..fbced09 100644 --- a/src/threads.cc +++ b/src/threads.cc @@ -132,12 +132,24 @@ Semaphore::Semaphore(int Value){ } Semaphore::~Semaphore(void){ - if(pthread_mutex_destroy(&this->mutex) != 0){ - error("Semaphore::~Semaphore: Kann Mutex nicht freigeben.\n"); - } - - if(pthread_cond_destroy(&this->condition) != 0){ - error("Semaphore::~Semaphore: Kann Wartebedingung nicht freigeben.\n"); + // IMPORTANT(fusion): Due to how initialization is rolled out, `exit` may be + // called after threads are spawned but before `ExitAll` is registered as an + // exit handler. This means such threads may still be running or left global + // semaphores in an inconsistent state if abruptly terminated. Either way, + // they are still considered "in use". + // In this case, calling `destroy` on either mutex or condition variable is + // undefined behaviour as per the manual but the actual implementation would + // fail on `mutex_destroy` with `EBUSY` and hang on `cond_destroy`. + // The temporary solution is to check the result from `mutex_destroy` before + // attempting to call `cond_destroy` to avoid hanging at exit. + + int ErrorCode; + if((ErrorCode = pthread_mutex_destroy(&this->mutex)) != 0){ + error("Semaphore::~Semaphore: Kann Mutex nicht freigeben: (%d) %s.\n", + ErrorCode, strerrordesc_np(ErrorCode)); + }else if((ErrorCode = pthread_cond_destroy(&this->condition)) != 0){ + error("Semaphore::~Semaphore: Kann Wartebedingung nicht freigeben: (%d) %s.\n", + ErrorCode, strerrordesc_np(ErrorCode)); } } |
