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

Update sdlthread.inc to version 2.26.2 #114

Merged

Conversation

Free-Pascal-meets-SDL-Website
Copy link
Collaborator

  • Updates sdlthread.inc to version 2.26.2 (for the most part)
  • OS/2-block left out (was not translated beforehand)
  • Some Todos are left (see comments in code for reference); to my mind they are not of high importance (see TpfnSDL_CurrentBeginThread, TThreadID); nobody ever complained about these structures => raises the question, if anybody uses SDL threads in Pascal anyway, since Pascal provides high quality procedural and object-oriented thread management

Copy link
Collaborator

@suve suve left a comment

Choose a reason for hiding this comment

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

For now, a quick review; I'll take a moment to thoroughly review the Windows-specific CreateThread stuff later.

units/ctypes.inc Show resolved Hide resolved
units/sdlthread.inc Outdated Show resolved Hide resolved
units/sdlthread.inc Outdated Show resolved Hide resolved
function SDL_TLSSet(id: TSDL_TLSID; value: Pointer; destructor_: Pointer): cint32 cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_TLSSet' {$ENDIF} {$ENDIF};
* Set the current thread's value associated with a thread local storage ID.
*
* The function prototype for `destructor` is:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we introduce a function-pointer type for the destructor? We'd deviate slightly from the SDL API, but get better type checking in return.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Something along the lines as following?

TTLSDestructor = procedure(value: Pointer); cdecl;

function SDL_TLSSet(id: TSDL_TLSID; const value: Pointer; destructor_: TTLSDestructor): cint; cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_TLSSet' {$ENDIF} {$ENDIF};

Would accompany this by a comment to explain why we did it this way.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, exactly.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks for your replies :). I have not a lot of time, so I reply only to this part at the moment.

On reconsidering this, I fear I do not like the idea. I fully agree that it would be beneficial to have type-checking, but 1) we generally try to stay as close as possible to the original C header code, 2) someone using threads should be able to make sure it is really a destructor function pointer by himself, 3) this would raise the question about other function pointers, how should we treat them, always introduce a specific type (or is there a special reason why we should introduce a type-check explicitly for this function pointer)?

Best regards
Matthias

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

On closer examination I saw that the original headers use a function pointer (in contrast to a simple pointer), too. Therefore I implemented the function pointer as discussed here.

units/sdlthread.inc Outdated Show resolved Hide resolved
Copy link
Collaborator

@suve suve left a comment

Choose a reason for hiding this comment

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

Finally took a better look at the Windows-specific stuff (and it's quite a mess to figure out). Sorry for making you wait so long.

{ SDL2-for-Pascal #todo : Explanation needed }
TpfnSDL_CurrentBeginThread = function(
SecurityAttributes: Pointer; StackSize: LongWord; ThreadFunc: TThreadFunc;
Parameter: Pointer {arg}; CreationFlags: LongWord; var ThreadId: TThreadID {threadID}): cuintptr_t; cdecl;
Copy link
Collaborator

Choose a reason for hiding this comment

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

  1. The signature here uses Pascal's LongWord. Should we use cuint instead? In the SDL headers (and Microsoft documentation) these arguments are of type unsigned.
  2. In the C headers, the last argument is a pointer. Should we replace the pass-by-reference?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

  1. I agree and fixed this.
  2. I'm not sure why ThreadFunc and ThreadID rely on Pascal system definitions (TThreadFunc from RTL's threadh.inc and TThreadID from RTL's sysosh.inc) There is no pointer type of TThreadID defined, which means we would need to introduce it by ourselves which would be kind of ugly to mix in a pointer type definition for a RTL type within the SDL unit.

I wonder if we should decouple this altogether like:

type
  TpfnSDL_CurrentBeginThreadFunc = function(params: Pointer):cuint; cdecl;

  TpfnSDL_CurrentBeginThread = function(
    SecurityAttributes: Pointer; StackSize: cuint; func: TpfnSDL_CurrentBeginThreadFunc;
    Parameter: Pointer {arg}; CreationFlags: cuint; ThreadId: pcuint {threadID}): cuintptr_t; cdecl;


{ SDL2-For-Pascal: #note : In the C header are two versions
of these macro functions. One for SDL's dynamic API and one without.
We can go with this for the moment. Improvement surely possible. }
Copy link
Collaborator

Choose a reason for hiding this comment

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

Looking at SDL's source code, the library by default is built in the "dynamic API" mode, and building without it requires manually editing the source. Based on that, I think we should translate the "dynamic API" versions of SDL_CreateThread() and SDL_CreateThreadWithStackSize(). Since said macros call SDL_CreateThread_REAL() and SDL_CreateThreadWithStackSize_REAL(), we could avoid having to overload the identifiers.

function SDL_TLSSet(id: TSDL_TLSID; value: Pointer; destructor_: Pointer): cint32 cdecl; external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_TLSSet' {$ENDIF} {$ENDIF};
* Set the current thread's value associated with a thread local storage ID.
*
* The function prototype for `destructor` is:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, exactly.

@suve
Copy link
Collaborator

suve commented Apr 30, 2024

@Free-Pascal-meets-SDL-Website Can you sync this PR with the latest master branch and also check if SDL 2.28 or SDL 2.30 added anything?

@Free-Pascal-meets-SDL-Website
Copy link
Collaborator Author

@Free-Pascal-meets-SDL-Website Can you sync this PR with the latest master branch and also check if SDL 2.28 or SDL 2.30 added anything?

I sync'd it. No new functions got introduced.

@suve
Copy link
Collaborator

suve commented May 21, 2024

Looks ok, though my comment regarding the dynamic API stuff for Windows still stands. I can take care of that in a follow-up PR, if you'd like.

@Free-Pascal-meets-SDL-Website
Copy link
Collaborator Author

Looks ok, though my comment regarding the dynamic API stuff for Windows still stands. I can take care of that in a follow-up PR, if you'd like.

Sure, would be great.

@suve suve merged commit c094a60 into PascalGameDevelopment:master May 23, 2024
3 checks passed
@Free-Pascal-meets-SDL-Website Free-Pascal-meets-SDL-Website deleted the update-sdlthread branch May 23, 2024 20:52
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.

None yet

2 participants