Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2009-10

QueueUserAPC Similar Cross platform Functionality

4 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Andrew Brunner

Hi there,

I've got a unit I'm porting from Windows to Linux and I came across a QueueUserAPC (Kernel32 Windows) I make to add a callback method that gets executed by the thread I added this to.

function QueueUserAPC(Callback: Pointer; hThread: THandle;
dwData:DWORD): boolean; stdcall;

When the Thread gets around to it, it processes the callback function I passed in during the queue call.

Judging by the performance of this method I would say that the Windows Kernel maintains a queue of events to process for each thread. And this function allows me to attach a callback to that queue so I can allocate memory within that stack rather than the stack of the calling thread. This feature is important as I want to keep memory allocation to specific threads.

Does Threading under Linux have anything like this? Have any of you done anything like this under *nix?

Thanks for any feedback.

Reply by email

Andrew Brunner
to Andrew Brunner

After two days of research I've concluded that the best way to implement this for cross platform support is to go with a FiFo queue and have the individual threads transverse the list at a convenient point during the Execute method. It's gonna be CPU intensive compared to a Windows Kernel call but hey... my options are limited.

If anyone has a better solution for Linux/Unix/Mac- I'm all ears.

Thanks.

Andrew Brunner wrote 5 lines
Does Threading under Linux have anything like this?  Have any of you done anything like this under *nix? Thanks for any feedback.

Reply by email

David Emerson
to Andrew Brunner: I've got a unit I'm porting from Windows to Linux and I came across a QueueUserAPC…

Hi Andrew,

I don't really understand your question as well as I'd like to (in particular, I don't understand the difference between a "callback" and any other sort of procedure; as well as other confusions) So this might be bunk advice, but here it is...

Have you considered making your own thread function wrapper that could duplicate the functionality you need?

type
  thread_param_and_callback_func = record
    thread_func : function (p : pointer) : ptrint;  // as required
    thread_param : pointer;  // the parameter to the main thread func
    callback_proc : procedure (...);
    callback_param : ???;
    end;
  p_thread_param_and_callback_func = ^thread_param_and_callback_func;
function thread_proc_wrapper (p : pointer) : ptrint;
begin
  with thread_param_and_callback_func (p^) do begin
    thread_func (thread_param);
    callback_proc (callback_param);
    end;
end;

I've only made one multithreaded program at this point, but it was a huge success, and I used something similar to this-- a thread handling function that would repeatedly request tasks and execute them, until the tasks were all complete. (I added a repeat loop...)

Hope this helps... (or inspires you to think of something that will work :)

~David.

Andrew Brunner wrote 27 lines
Hi there, I've got a unit I'm porting from Windows to Linux and I came across a QueueUserAPC (Kernel32 Windows) I make to add a callback method that gets executed by the thread I added this to. function QueueUserAPC(Callback: Pointer; hThread: THandle; dwData:DWORD): boolean; stdcall; When the Thread gets around to it, it processes the callback function I passed in during the queue call. Judging by the performance of this method I would say that the Windows Kernel maintains a queue of events to process for each thread. And this function allows me to attach a callback to that queue so I can allocate memory within that stack rather than the stack of the calling thread. This feature is important as I want to keep memory allocation to specific threads. Does Threading under Linux have anything like this? Have any of you done anything like this under *nix? Thanks for any feedback. _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Andrew Brunner
to David Emerson

In theory that's what I'm planning to do to emulate the functionality. I'm going to create a queue using a FIFO technique and empty out the queue executing the callbacks for each item in the queue - from a manager thread.

Thanks for the advice. -Andrew

David Emerson wrote 73 lines
Hi Andrew, I don't really understand your question as well as I'd like to (in particular, I don't understand the difference between a "callback" and any other sort of procedure; as well as other confusions) So this might be bunk advice, but here it is... Have you considered making your own thread function wrapper that could duplicate the functionality you need? type  thread_param_and_callback_func = record    thread_func : function (p : pointer) : ptrint;  // as required    thread_param : pointer;  // the parameter to the main thread func    callback_proc : procedure (...);    callback_param : ???;    end;  p_thread_param_and_callback_func = ^thread_param_and_callback_func; function thread_proc_wrapper (p : pointer) : ptrint; begin  with thread_param_and_callback_func (p^) do begin    thread_func (thread_param);    callback_proc (callback_param);    end; end; I've only made one multithreaded program at this point, but it was a huge success, and I used something similar to this-- a thread handling function that would repeatedly request tasks and execute them, until the tasks were all complete. (I added a repeat loop...) Hope this helps... (or inspires you to think of something that will work :) ~David. On Thu 8 Oct 2009, Andrew Brunner wrote: > Hi there, > > I've got a unit I'm porting from Windows to Linux and I came across a > QueueUserAPC (Kernel32 Windows) I make to add a callback method that > gets executed by the thread I added this to. > > function QueueUserAPC(Callback: Pointer; hThread: THandle; > dwData:DWORD): boolean; stdcall; > > When the Thread gets around to it, it processes the callback function > I passed in during the queue call. > > Judging by the performance of this method I would say that the Windows > Kernel maintains a queue of events to process for each thread.  And > this function allows me to attach a callback to that queue so I can > allocate memory within that stack rather than the stack of the calling > thread.  This feature is important as I want to keep memory allocation > to specific threads. > > Does Threading under Linux have anything like this?  Have any of you > done anything like this under *nix? > > Thanks for any feedback. > _______________________________________________ > fpc-pascal maillist  -   > http://lists.freepascal.org/mailman/listinfo/fpc-pascal > _______________________________________________ fpc-pascal maillist  -   http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email