Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace pthread with CThreads #141

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from

Conversation

ThePedroo
Copy link
Contributor

Notice

  • I understand the code that I have edited, and have the means
    to test it before making changes to Concord.

What?

This PR replaces the pthread by cthread (a library that uses both pthread and Windows Threads).

Why?

To improve compability with native Windows, and to not be necessary to use pthread on Windows.

How?

All parts where pthread is used, was replaced with cthreads functions.

Testing?

Basic testing using Concord's testing was used, on Windows and Linux (Arch Linux), but more testings must be done.

@ThePedroo ThePedroo force-pushed the feat/thread branch 2 times, most recently from 9ec25be to 7796fdb Compare February 11, 2023 13:25
Copy link
Collaborator

@lcsmuller lcsmuller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks very promising, good progress so far. I've left a couple questions and suggestions regarding the implementation, but mostly nitpicks!

core/cthreads.c Outdated Show resolved Hide resolved
core/cthreads.c Outdated Show resolved Hide resolved
core/cthreads.c Outdated Show resolved Hide resolved
core/cthreads.c Outdated Show resolved Hide resolved
core/cthreads.c Outdated Show resolved Hide resolved
@@ -643,6 +643,8 @@ discord_voice_join(struct discord *client,
bool self_mute,
bool self_deaf)
{
client_lock = __cthreads_mutex_init();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unsure about this one.. does Windows have a PTHREAD_MUTEX_INITIALIZER equivalent? You can try searching for "initializing global mutex Windows"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I have seen no, according to this stack overflow answer
I wonder if you really need it, because if yes we can check if it exists in every cthreads action, so it will work anyways, but for using voice they will first use that one anyways, are they?

core/cthreads.c Outdated Show resolved Hide resolved
core/cthreads.c Outdated Show resolved Hide resolved
core/cthreads.c Outdated Show resolved Hide resolved
core/cthreads.c Outdated Show resolved Hide resolved
core/cthreads.h Outdated Show resolved Hide resolved
Copy link
Collaborator

@HackerSmacker HackerSmacker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it's a ton of work to replace those ifdefs, but, I feel it may be a better idea to use osname in lieu of #else, since that feels like a "solution of last resort."

core/cthreads.h Show resolved Hide resolved
core/cthreads.h Outdated Show resolved Hide resolved
core/threadpool.c Show resolved Hide resolved
core/cthreads.c Outdated Show resolved Hide resolved
@ThePedroo ThePedroo force-pushed the feat/thread branch 2 times, most recently from e024684 to bb1386f Compare February 11, 2023 16:03
@ThePedroo
Copy link
Contributor Author

Woah! That was... insane, well, I've applied the suggestions I've understood and fixed some errors and improved the functions, it should be better now.. although I need to test it on Windows.. I'm going to do later since now I'm a little tired for testing, but should work on Linux

@ThePedroo
Copy link
Contributor Author

ThePedroo commented Feb 11, 2023

I've got some bugs fixed (mainly on Windows threads but also on pthreads), now pthread is working fine, although I'm still dealing with cygwin "exception" with pthread & windows threads, I'm going to try to use MSYS2 and see if it works (It didn't work, it still uses cygwin for some wild reason)

@lcsmuller lcsmuller changed the title feat(thread): Replace pthread to CThreads feat(thread): Replace pthread with CThreads May 14, 2023
@lcsmuller lcsmuller changed the title feat(thread): Replace pthread with CThreads [WIP] Replace pthread with CThreads May 14, 2023
@ThePedroo
Copy link
Contributor Author

I'm going to check the ones I didn't really solve in the reviews soonly, but is anybody available to test it? I got it working on cygwin (manually removing the ifdefs to get the windows.h since for some reasons it doesn't set win flag) and should work on the rest

@ThePedroo ThePedroo force-pushed the feat/thread branch 3 times, most recently from 8955e85 to ea13c56 Compare May 16, 2023 17:27
@ThePedroo ThePedroo force-pushed the feat/thread branch 6 times, most recently from c2345fc to c717805 Compare June 12, 2023 23:27
@HackerSmacker
Copy link
Collaborator

Okay, time for my Official Review of Cthreads:

TESTED COMPILERS:

  • MSVC 2022, 2013 (Windows 10, 8.1)
  • OpenWatcom from June 2022 (Windows NT 4)
  • Cygwin GCC 11.3.0 (Windows 10)
  • MinGW GCC 11.3.0 (Windows 10)
  • GCC 11.3.0 (FreeBSD 586)
  • Clang 10.0 (FreeBSD 586)
  • GCC 8.3.0 (Linux 4.19.0 System/390 custom image)
  • acomp SVR5 (UnixWare 7.1.1)

Works on all of those. There is only one issue, and, that's the dilemma of a warning that you get on line 72 of cthreads.c. If you use the plain DWORD cast, Watcom is happy, but, GNU compilers yell.

Also, I should mention that this is a test of compiling and linking Cthreads, NOT all of Concord!

@ThePedroo ThePedroo force-pushed the feat/thread branch 2 times, most recently from b318f52 to 51b3073 Compare June 12, 2023 23:47
@ThePedroo
Copy link
Contributor Author

I've fixed some issues (in both pthread and Windows threads), but I'm still going to take a look in the Windows equivalents to be sure everything's alright

Tests are appreciated, but most things are the same, should still work

CThreads can also work in Windows using Windows threads, due to this, pthread was replaced by it.

Co-authored-by: Lucas Müller <[email protected]>
Co-authored-by: HackerSmacker <[email protected]>
@lcsmuller lcsmuller changed the title [WIP] Replace pthread with CThreads Replace pthread with CThreads Sep 14, 2023
Copy link
Collaborator

@HackerSmacker HackerSmacker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Took me a minute, but, I think it should be good. All the tests I've done were good, so, I think it's good to go.

core/cthreads.c Outdated Show resolved Hide resolved
core/cthreads.c Outdated Show resolved Hide resolved
core/cthreads.c Outdated Show resolved Hide resolved
test/cthreads.c Show resolved Hide resolved
Copy link
Collaborator

@lcsmuller lcsmuller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but we should merge this once the TLS issue is sorted out

@ThePedroo ThePedroo force-pushed the feat/thread branch 6 times, most recently from ccb44d1 to 50c8f29 Compare February 1, 2024 05:09
This commit updates CThreads library with latest fixes.
@ThePedroo ThePedroo changed the base branch from master to dev February 2, 2024 01:09
@ThePedroo ThePedroo force-pushed the feat/thread branch 3 times, most recently from f88f90d to 033bdc9 Compare February 2, 2024 16:17
@Anotra
Copy link
Contributor

Anotra commented Feb 2, 2024

I've looked though the code, it's not fully featured, and contains some serious flaws. If you want to support windows threads, I'd look into something such as this https://github.com/GerHobbelt/pthread-win32 first.

@ThePedroo
Copy link
Contributor Author

Thanks for the review! Could you provide me the list of the flaws?

I took a small look on pthread-win32, but I don't think it follows CThreads goals: uncomplicated "threading library", allowing cross-compatibility while having a small and easy (lib) codebase

About not being fully featured, CThreads was born to be for Concord primarily, and it covers all used pthread functions. I do plan to cover more parts

Thanks for the patience to review anyway

This commit improves CThreads usage by destroying properly the mutexes, and expanding its usage to examples.
@ThePedroo ThePedroo force-pushed the feat/thread branch 5 times, most recently from 8c9569f to ef13fba Compare March 17, 2024 03:56
This commit updates the library, allowing it to work properly on Windows.
@ThePedroo
Copy link
Contributor Author

I was able to get back to my city where I have a Windows machine and fix Windows part issues. Now, with proper edits to make Cygwin to use Windows part, it works flawlessly

@lcsmuller
Copy link
Collaborator

lcsmuller commented Dec 7, 2024

To give you a little update on this, we will need to hold until we have an actual structure for unit testing, integration tests, e2e tests. Otherwise, its a bit too risky to merge on the repo's current state

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants