Skip to content

Replace pthread with CThreads#141

Open
ThePedroo wants to merge 7 commits into
Cogmasters:devfrom
ThePedroo:feat/thread
Open

Replace pthread with CThreads#141
ThePedroo wants to merge 7 commits into
Cogmasters:devfrom
ThePedroo:feat/thread

Conversation

@ThePedroo

Copy link
Copy Markdown
Collaborator

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

@lcsmuller lcsmuller left a comment

Copy link
Copy Markdown
Collaborator

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!

Comment thread core/cthreads.c Outdated
Comment thread core/cthreads.c Outdated
Comment thread core/cthreads.c Outdated
Comment thread core/cthreads.c Outdated
Comment thread core/cthreads.c Outdated
Comment thread src/discord-voice.c Outdated
bool self_mute,
bool self_deaf)
{
client_lock = __cthreads_mutex_init();

Copy link
Copy Markdown
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
Copy Markdown
Collaborator 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?

Comment thread core/cthreads.c Outdated
Comment thread core/cthreads.c Outdated
Comment thread core/cthreads.c Outdated
Comment thread core/cthreads.c Outdated
Comment thread core/cthreads.h

@HackerSmacker HackerSmacker left a comment

Copy link
Copy Markdown
Collaborator

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."

Comment thread core/cthreads.h
Comment thread core/cthreads.h Outdated
Comment thread core/threadpool.c
Comment thread core/cthreads.c Outdated
@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
Copy Markdown
Collaborator 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

ThePedroo commented Feb 11, 2023

Copy link
Copy Markdown
Collaborator Author

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
Copy Markdown
Collaborator 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
Copy Markdown
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
Copy Markdown
Collaborator 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 <lucas@muller.codes>
Co-authored-by: HackerSmacker <hackersmackertech@gmail.com>

@lcsmuller lcsmuller left a comment

Copy link
Copy Markdown
Collaborator

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

Anotra commented Feb 2, 2024

Copy link
Copy Markdown
Contributor

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
Copy Markdown
Collaborator 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
Copy Markdown
Collaborator 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

lcsmuller commented Dec 7, 2024

Copy link
Copy Markdown
Collaborator

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

@lcsmuller lcsmuller force-pushed the dev branch 2 times, most recently from 615a055 to 135d925 Compare April 23, 2025 22:30
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