The International Simutrans Forum

Development => Patches & Projects => Topic started by: jrs on January 20, 2026, 05:00:54 AM

Title: tpl freelist mutex locking incorrect
Post by: jrs on January 20, 2026, 05:00:54 AM
I am working on a port of simutrans to openbsd and found their pthreads implementation was not happy with tpl/freelist_tpl.h, aborting due to an unlock with an unknown owner. While poking at the code trying to understand this I found the lock/unlock dance in tpl/freelist_tpl.h strange. Unlock was called twice. changing the first one to a lock allowed the game to run.

Index: simutrans/src/simutrans/tpl/freelist_tpl.h
--- simutrans/src/simutrans/tpl/freelist_tpl.h.orig
+++ simutrans/src/simutrans/tpl/freelist_tpl.h
@@ -170,7 +170,7 @@ class freelist_size_t (public)
#endif

#ifdef MULTI_THREAD
- pthread_mutex_unlock(&freelist_mutex);
+ pthread_mutex_lock(&freelist_mutex);
#endif

// putback to first node

One other small thing, this might just be an openbsd/clang peculiarity. but wanted to mention it just in case.
squirrel/squirrel/sqvm.h can not find a declaration for "va_list" I found it in stdarg.h

Index: simutrans/src/squirrel/squirrel/sqvm.h
--- simutrans/src/squirrel/squirrel/sqvm.h.orig
+++ simutrans/src/squirrel/squirrel/sqvm.h
@@ -2,6 +2,7 @@
 #ifndef _SQVM_H_
 #define _SQVM_H_
 
+#include <stdarg.h>
 #include "sqopcodes.h"
 #include "sqobject.h"
 #define MAX_NATIVE_CALLS 100



Title: Re: tpl freelist mutex locking incorrect
Post by: prissi on January 20, 2026, 05:50:49 AM
Well spotted, that should be indeed a lock unstead of unlock.

And the squirrel headers are patched, as we had to download squirrel verbatim. I am anyway trying to update simutrans to squirrel 3.2.

Title: Re: tpl freelist mutex locking incorrect
Post by: jrs on January 20, 2026, 04:53:12 PM
Salutes, And I just wanted to say thank you for providing a super positive experience for a first time poster with a one line diff.