Hi, I noticed the cause for the hanging of threads.
I have not figured out exactly why or the elegant solution, but, in the situation where I have a simple program (known, now, as the "main thread") that then creates a thread (known, now, as the "child thread"); where, child thread loops constantly until some request from the main thread - in my case - pthraed_cancel(childThread);
In this case, it seems like (pthread_join.c):
Wait(SIGF_PARENT);
Will never receive the signal.
At (StartFunc / pthread_create.c)
Printf("[%s] Finishing stuff\n", inf->name);
the issue seems to be that
if (inf->status == THREAD_STATE_RUNNING) {
is NOT true. Then, no "Signal(inf->parent, SIGF_PARENT);" is performed.
In that case, the parent thread is left WAITING.
To test it, I hacked in
Forbid();
Signal(inf->parent, SIGF_PARENT);
to be executed - regardless. Now, child thread shuts down, which it does anyway, but also the main thread shuts down because it receives the signal.
===
Alternatively to modifying anything in the clib2/pthread branch, you can issue something like a Delay(150) between "pthread_cancel" and "pthread_join" in your own user program without adding any hacks to the current clib2/pthreads branch - and you may be able to see that in that case the main thread is also not left WAITING and that it successfully closes.
===
Here is the test code:
Can toggle the commenting of IDOS->Delay. Forget about the "sound thread" that was just some extra testing. Ofc, requires SDL2.
int main(int argc, char *argv[])
{
(void)argc;(void)argv;
if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO))
{
BAIL(SDL_GetError(),10);
}
if(SDL_CreateWindowAndRenderer(800,600,0/*no flags*/,&window,&render))
{
BAIL(SDL_GetError(),20);
}
SDL_SetWindowTitle(window,"thread_test");
// /* now let's create a thread for some sound */
// if(pthread_create(&thread_s,NULL,&thread_sound,NULL))
// {
// BAIL("Unable to create a thread for sound",30);
// }
/* and, a thread for some sort of engine */
if(pthread_create(&thread_e,NULL,&thread_engine,NULL))
{
BAIL("Unable to create a thread for engine",31);
}
Printf("\n[%s] pthread_create done\n", inf->name);
return OK;
-}
\ No newline at end of file
+}^M
Since we can get in the situation where the code in pthread_join is called (setting the status to JOINING) before needing to give the final signal to the parent thread (which assumes that the state will only be in the RUNNING state).
But, I could be way off! But, this works for my personal example. I should run the full tests since it may have broken other things!
=== PS: DOS line endings are expected in this repository?
If liberty means anything at all, it means the right to tell people what they do not want to hear. George Orwell.
Thank you! Tomorrow I'll travel to Germany for holidays so I can't test this changes at moment. I'll be back in a week and I'll try your changes. In the meanwhile, if you find other issues and you can test everything deeply would be really apreciated! :)
However no luck. If you try try_to_time or simple examples in threads test_programs folder you will see the error. It seems that a function is called when thread is no more running and so you get an ISI error
Fixed and merged :) Everything is in beta7 branch, working and tested (plus some other improvements..) So we have a non closed pthread implementation that "seems" to work correctly. Of course more tests needs to be done
@Raziel I mean this error happens with original code (without const char **cast being added). And to make it compiles over clib2, i had to add this const, while should't.
In the old clib2 was defined in a different way. I have changed the definition to match the correct function declaration but I have to test the changes. You will find it in next commit on beta8
1. Are clib2 didn't have working ieeefp stuff ? I just found that once i enable HAVE_IEEEFP_H as for newlib, it says that ieeefp.h include not found (i checked in clib2, there is none, whyle newlib have one)
2. Also i tried to build Salas00's OpenAll for latest clib2, and have that:
./OpenAL32/Include/alMain.h:985:48: error: format ‘%lu’ expects argument of type ‘long unsigned int’, but argument 5 has type ‘off_t’ {aka ‘long long int’} [-Werror=format=]
985 | #define AL_PRINT(T, MSG, ...) fprintf(LogFile, "AL lib: %s %s: "MSG, T, __FUNCTION__ , ## __VA_ARGS__)
Not sure through if it issue with clib2, or with more warnings in gcc 11.3.0
3. Tried to rebuild some stuff which builds ok on newlib, and have that on linking:
SoundFileWriterWav.cpp:(.text+0xf4): undefined reference to `_ZNSo5seekpESt4fposI10_mbstate_tE' SoundFileWriterWav.cpp:(.text+0x148): undefined reference to `_ZNSo5seekpESt4fposI10_mbstate_tE' ../../build/lib/test-system-s.a(Err.cpp.obj):(.rodata+0x1c): undefined reference to `_ZNSt15basic_streambufIcSt11char_traitsIcEE7seekposESt4fposI10_mbstate_tESt13_Ios_Openmode'
Have any idea wtf happens here ?
That happens as i see for "m_file.seekp(4)" calls. And m_file opened like this:
m_file.open(filename.c_str(), std::ios::binary);
So something seems wrong in there and we have 2 errors in my example:
First one with seekp(). Second one with static std::ostream stream(&buffer);
But both of them mention "mbstate", so i assume issue is same for both problems.
Rechecked on newlib - works and builds ok
Edited by kas1e on 2022/9/19 7:52:01 Edited by kas1e on 2022/9/19 8:03:33 Edited by kas1e on 2022/9/19 8:06:02 Edited by kas1e on 2022/9/19 8:06:53 Edited by kas1e on 2022/9/19 8:21:42 Edited by kas1e on 2022/9/19 8:24:33