OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 5:29 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Windowing system callback question
PostPosted: Fri Feb 19, 2021 5:55 am 
Offline
Member
Member
User avatar

Joined: Wed Jan 13, 2021 8:49 am
Posts: 25
Hi!
I'm not really experienced in this topic, so maybe this will be a wrong question.
So: An application registers its main window, draws it, etc.
Then when user clicks on a button OS calls the application's GUI handler. How could it be a safe call,
if it's in the same address space with the app's main process (so it's able to modify app's data asynchronously)?

Thanks for the answer(s)


Top
 Profile  
 
 Post subject: Re: Windowing system callback question
PostPosted: Fri Feb 19, 2021 8:13 am 
Offline
Member
Member

Joined: Fri May 11, 2018 6:51 am
Posts: 274
Well, typically, GUI handler functions do not run by default asynchronously. For example, on Win32, you have an infinite loop in main() fetching and dispatching events. The dispatching happens in the same thread. Of course, for small and simple programs that might be OK, but for anything slightly more complicated, that's bad because the GUI seems frozen while a handler is executing. To overcome this, you have to manually create other threads. Typically, the main thread remains for the GUI events and re-drawing, while the other threads do "the job", whatever that might be. And, of course, you have to use synchronization explicitly with locks etc. in order to avoid data corruption.

High level UI frameworks might make all of that much easier by hiding the event loop, synchronization, the creation of threads etc. but, under the hood, it's the same thing. There are a ton of approaches to this problem, including the modern async/await constructs, but in no case you can completely forget that there are multiple threads of execution and one of them should be dedicated to the GUI.

_________________
Tilck, a Tiny Linux-Compatible Kernel: https://github.com/vvaltchev/tilck


Top
 Profile  
 
 Post subject: Re: Windowing system callback question
PostPosted: Fri Feb 19, 2021 8:46 am 
Offline
Member
Member
User avatar

Joined: Wed Jan 13, 2021 8:49 am
Posts: 25
Thanks for the answer. I was afraid there is no simple solution for this problem...


Top
 Profile  
 
 Post subject: Re: Windowing system callback question
PostPosted: Fri Feb 19, 2021 7:22 pm 
Offline
Member
Member

Joined: Mon Jul 05, 2010 4:15 pm
Posts: 595
Most of the callbacks aren't called from the kernel and the callbacks are implemented in userspace. Many times on Linux systems, the thread will go into the "select" system call and wait the for some event, then when it returns to the user space implementation will invoke the appropriate callback. Same should go for Windows that goes into WaitOfSingleObject or WaitForMultipleObjects. So in practice you just need one thread for your entire main loop that handles all the events.

When it comes to GUI design, most of the are single threaded or at least the update is single threaded. I think this is a good approach because making all GUI objects work in a multithreaded environment where several threads poke around with them would be difficult and open up for tons of bugs.


Top
 Profile  
 
 Post subject: Re: Windowing system callback question
PostPosted: Fri Feb 19, 2021 11:19 pm 
Offline
Member
Member

Joined: Tue Mar 04, 2014 5:27 am
Posts: 1108
Events can be buffered/queued by either the OS or the app until the app can safely consume them.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 26 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group