Friday, February 11, 2011

Managing Coupling Part 2 — Polling, Callbacks and Events


In my last post, I talked a bit about the importance of decoupling and how one of the fundamental challenges in system design is to keep systems decoupled while still allowing the necessary interactions to take place.

This time I will look at one specific such challenge: when a low level system needs to notify a high level system that something has happened. For example, the animation system may want to notify the gameplay system that the character’s foot has touched the ground, so that a footstep sound can be played.

(Note that the reverse is not a problem. The high level system knows about the low level system and can call it directly. But the low level system shouldn’t know or care about the high level system.)

There are three common techniques for handling such notifications: polling, callbacks and events.

Polling

A polling system calls some function every frame to check if the event it is interested in has occurred. Has the file been downloaded yet? What about now? Are we there yet?

Polling is often considered “ugly” or “inefficient”. And indeed, in the desktop world, polling is very impolite, since it means busy-waiting and tying up 100 % of the CPU in doing nothing.

But in game development the situation is completely different. We are already doing a ton of stuff every 33 ms (or half a ton of stuff every 17 ms). As long as we don’t poll a huge amount of objects, polling won’t have any impact on the framerate.

And code that uses polling is often easier to write and ends up better designed than code that uses callbacks or events. For example, it is much easier to just check if the A key is pressed inside the character controller, than to write a callback that gets notified if A is pressed and somehow forward that information to the character controller.

So, in my opinion, you should actually prefer to use polling whenever possible (i.e., when you don’t have to monitor a huge number of objects).

Some areas where polling work well are: file downloads, server browsing, game saving, controller input, etc.

An area less suited for polling is physics collisions, since there are N*N possible collisions that you would have to poll for. (You could argue that rather than polling for a collision between two specific objects, you could poll for a collision between any two objects. My reply would be that in that case you are no longer strictly polling, you are in fact using a rudimentary effect system.)

Callbacks

In a callback solution, the low level system stores a list of high level functions to call when certain events occur.

An important question when it comes to callbacks is if the callback should be called immediately when the event occurs, or if it should be queued up and scheduled for execution later in the frame.

I much prefer the latter approach. If you do callbacks immediately you not only trash your instruction and data caches. You also prevent multithreading (unless you use locks everywhere to prevent the callbacks from stepping on each other). And you open yourself up to the nasty bug where a callback through a chain of events ends up destroying the very objects you are looping over.

It is much better to queue up all callbacks and only execute them when the high level system asks for it (with an execute_callbacks() call). That way you always know when the callbacks occur. Side effects can be minimized and the code flow is clearer. Also, with this approach there is no problem with generating callbacks on the SPU and merging the queue with other callback queues later.

The only thing you need to worry about with delayed callbacks is that the objects that the callback refers to might have been destroyed between the time when the callback was generated and the time when it was actually called. But this is neatly handled by using the ID reference system that I talked about in the previous post. Using that technique, the callback can always determine if the objects still exist.

Note that the callback system outlined here has some similarities with the polling system — in that the callbacks only happen when we explicitly poll for them.

It is not self-evident how to represent a callback in C++. You might be tempted to use a member function pointer. Don’t. The casting and typing rules make it near impossible to use them for any kind of generic callback mechanism. Also, don’t use an “observer pattern”, where the callback must be some object that inherits from an AnimationEventObserver class and overrides handle_animation_event(). That just leads to tons of typing and unnecessary heap allocation.

There is an interesting article about fast and efficient C++ delegates at http://www.codeproject.com/KB/cpp/FastDelegate.aspx. It looks solid, but personally I’m not comfortable with making something that requires so many platform specific tricks one of the core mechanisms of my engine. 

So instead I use regular C function pointers for callbacks. This means that if I want to call a member function, I have to make a little static function that calls the member function. That is a bit annoying, but better than the alternatives.

(Isn’t it interesting that when you try to design a clean and flexible C++ API it often ends up as pure C.)

When you use C callbacks you typically also want to pass some data to them. The typical approach in the C world is to use a void * to “user data” that is passed to the callback function. I actually prefer a slightly different approach. Since I sometimes want to pass more data than a single void * I use something like this:

struct Callback16
{
  void (*f)(void);
  char data[12];
};

There aren’t a huge amount of callbacks, so using 16 bytes instead of 8 to store them doesn’t matter. You could go to Callback32 if you want the option to store even more data.

When calling the callback, I cast the function pointer to the appropriate type and pass a pointer to its data as the first parameter.

typedef void (*AnimationEventCallback)(void *, unsigned);
AnimationEventCallback f = (AnimationEventCallback)callback.f;
f(callback.data, event_id);

I’m not worried about casting the function pointer back and forth between a generic type and a specific one or about casting the data in and out of a raw buffer. Type safety is nice, but there is an awful lot of power in juggling blocks of raw memory. And you don’t have to worry that much about someone casting the data to the wrong type, because doing so will 99% of the time cause a huge spectacular crash, and the error will be fixed immediately.

Events

Event systems are in many ways similar to callback systems. The only difference is that instead of storing a direct pointer to a callback function, they store an event enum. The high level system that polls the events decides what action to take for each enum.

In my opinion, callbacks work better when you want to listen to specific notifications: “Tell me when this sound has finished playing.” Events work better when you process them in bulk: “Check all collision notifications to see if the forces involved are strong enough to break the objects.” But much of it is a matter of taste.

For storing the event queues (or callback queues) I just use a raw buffer (Vector orchar[FIXED_SIZE]) where I concatenate all events and their data:

[event_1_enum] [event_1_data] [event_2_enum] [event_2_data] …

The high level system just steps through this buffer, processing each event in turn. Note that event queues like this are easy to move, copy, merge and transfer between cores. (Again, the power of raw data buffers.)

In this design there is only a single high level system that polls the events of a particular low level system. It understands what all the events mean, what data they use and knows how to act on them. The sole purpose of the event system (it is not even much of a “system”, just a stream of data) is to pass notifications from the low level to the high.

This is in my opinion exactly what an event system should be. It should not be a magic global switchboard that dispatches events from all over the code to whoever wants to listen to them. Because that would be horrid!

29 comments:

  1. Interesting articles, thanks! :)

    ReplyDelete
  2. Another great article Niklas, thanks! A small question about your events implementation: which sub-system is responsible for removing old events? Is it the high level system which tells the lower level system that it doesn't need specified events any longer?

    ReplyDelete
  3. Yes the high level system that processes the events "consumes" them as they are processed.

    ReplyDelete
    Replies
    1. I'm not sure if you see these old messages... What do you do if a higher level system doesn't consume events as fast as the system produces them? Do you throw away events if you hit a ceiling?

      I've been perusing through your blog for a while now. Amazing stuff! Thanks for posting up all your thoughts!

      Delete
    2. The higher level system consumes all events every frame. So the only possible problem is if the low level system produces so many events that it overflows the buffer. In that case you can choose to resize the buffer to throw away events or to temporarily freeze the low level system. Different choices might be right for different systems.

      Delete
  4. If you want to know more about it then you can visit our website Disneyplus.com/beginand take advantage of it. Click Here for the DisneyPlus.com Login: Sign In to Disney+ If you’ve been using Disney Plus on your iPhone or Samsung TV but now want to use it on your TV, then you need to know theDisneyplus.com/begin page. Hbogo.com/activate - Are you facing any problem to activate HBO Go on Roku or other device just follow simple steps to activate.It provides a wider range Hbogo.com/activate of streaming channels and videos which users can choose according to their current moods. Download the HBO Max on your device to experience excellent sound and video quality of unlimited movies, shows and much more withHbogo.com/activate.

    ReplyDelete
  5. The process to get the HBO MAX activation code is to first open your web browser and go to Hbogo.com/activate and your hbo max account by registered email ID.OpenHbogo.com/activate on your Mac/PC/mobile device. Download and install HBO MAX on your smart tv.HBO Max offers TV shows, blockbuster movies, and new Max Originals to its users. Visit hbomax.com/tvsignin and choose your subscription plan and start streaming with hbo max. Go to hbomax.com/tvsignin and follow the simple procedure to create an account for hbomax. Hbo max is easy to setup and connect with your smart tv. To know more details about hbo max setup and sign up visit its official website: hbomax.com/tvsignin and complete the steps for setup.

    ReplyDelete
  6. Using a Canon printer service phone, you can get a full installation of the Canon.com/ijsetup and go to the installed Canon printer to download the Canon.com/ijsetup .To get more and more information, visit our websiteCanon.com/ijsetup and get the information according to convenience.Download the Canon printer from Canon.com/ijsetupand set it up on your device. Canon printers are all in one printer that facilitates print, copy and scan. And to download Canon ijsetup printer drivers you can visit our website Canon.com/ijsetup .

    ReplyDelete
  7. Canon printers are designed for personal and business use. To learn more about Canon printers, visit our website Canon.com/ijsetup and take advantage.The Canon printer enhances scan functionality, and includes a robust security feature set. Using a Canon printer service phone, you can get a full installation of the Ij.start.canon and go to the installed Canon printer to download the ij.start.canon To get more and more information, visit our website Ij.start.canon and get the information according to convenience .Canon printer is the best printer out of all printers. Which allows for the best print, copy and scan. Canon printers are designed for personal as well as commercial use. You can visit the Ij.start.canon website to download the Ij.start.canon.For more information visit our website

    ReplyDelete
  8. Using a Canon printer service phone, you can get a full installation of the Ij.start.canon and go to the installed Canon printer to download the [url=https://sites.google.com/view/ijstartcanonn/]Ij.start.canon[/url].To get more and more information, visit our website Ij.start.canon and get the information according to convenience .Canon.Com ijsetup will manual you Ij.start.canon to Install Canon printer brand new updated drivers, for Canon printer setup you could additionally visit.If we talk about printers the first Canon Printer Setup name comes in our thoughts is Canon printer, on this internet site we will inform you a way to setup & deploy your canon printer with little information approximately computers.Download from and setup on your device.Canon Printer Setup Canon printers are all in one printer that facilitates print, copy and scan. The canon printers are designed for personal as well as business use.

    ReplyDelete
  9. Canon.Com ijsetup will manual you Canon Printer Setupto Install Canon printer brand new updated drivers, for Canon printer setup you could additionally visit.If we talk about printers the first Canon.com/ijsetup name comes in our thoughts is Canon printer, on this internet site we will inform you a way to setup & deploy your canon printer with little information approximately computers.Download from and setup on your device.Canon Printer Drivers Canon printers are all in one printer that facilitates print, copy and scan. The canon printers are designed for personal as well as business use.Canon printer that can be downloaded Canon Printer Drivers via canon.comijsetup page is the best wireless printer that you can connect to your device and print data smoothly.You can visit the Canon Printer Drivers website to get information about Canon Multifunction Printers, Canon Printers Dealers, Retailers, Stores and Distributors.

    ReplyDelete
  10. Download the driver or software for the printer scanner. To install your additional Canon driver or software, go to the Canon Printer Drivers link and follow the steps on it. To get more information about it, visit our website Canon Printer Drivers and enjoy.The Canon printer enhances scan functionality, and includes a robust security feature set. Using a Canon printer service phone, you can get a full installation of the Canon Printer Drivers and go to the installed Canon printer to download the Canon Printer Drivers .To get more and more information, visit our website and get the information according to convenience .To setup and download the Hp priter driver software, visit our website 123.hp.com/setup. The 123 hp setup HP printer makes printing hassle-free and easy. Install the hp printer on your Windows or Mac computer and install the drivers via 123.hp.com/setup For more information, contact our team at 123.hp.com/setup. We are always available to help you.

    ReplyDelete
  11. To setup and install your 123 HP printer go to 123.hp.com/setup for mac . For installation of drivers, seek help immediately from the website 123.hp.com/setup. We’ll help you download the right HP printer software and drivers. Be sure to install cartridges, load paper and power on the printer. Download the 123.hp.com/setup to get the recommended software for your printer.HP printers offer high-quality printing documents with high speed and have become an integral requirement of our personal as well as professional life. Hp printer is an affordable home printer that generates extraordinary quality documents and photos. The steps to download and install a hp printer driver are here at HP Printer Setup Go-ahead to get your hp printer setup now.HP printers are best known for their high-quality printing. These printers are the most trusted and useful printer brand that helps the users to enjoy high-quality printing and installation facility. Get your HP Printer connected with your device and download the setup from HP Printer Setup and learn how to install it.The Amazon My TV Code program requires the customer to have an Amazon account and a streaming device or TV.Amazon.com/mytv Amazon Prime Video is available on almost all streaming devices like Roku, Amazon Fire TV, Chromecast.

    ReplyDelete
  12. People's media needs can be met through the Amazon Prime platform. Amazon Prime is a subscription service, which can be subscribed by going to the link Amazon.com/mytv.Prime video is a subscription service that caters to the media needs of people through the Primevideo.com/mytv
    platform.The Amazon Prime Activation Code is a six digit alphanumeric code that can be found on your TV screen when you click on “Register” on Amazon.com/mytv.Amazon prime paid for online streaming services used at Amazon.com/mytv Gives users access to a variety of services such as instant delivery.Search for “Amazon” to find the Primevideo.com/mytv app.Get more information

    ReplyDelete
  13. Amazon Prime vedio throughPrimevideo.com/mytv is accessible to owners of streaming platforms and devices like Chromecast, Fire TV, Amazon TV, and Roku, among others.If you eagerly wanted to know what is Amazon.com/mytvthen the answer is amazing. It is more than a fast delivery option. Amazon Prime is a subscription service provided by the Amazon.com/mytv with unlimited entertainment with lots of other benefits.Enter the link Primevideo.com/mytv on your browser.Go to Xfinity.com/authorize from any other device such as mobile phone, tablet or desktop. From the web browser, enter the six-digit xfinity stream authorized code provided to the Roku device.

    ReplyDelete
  14. Microsoft Office suite contains reliably used Microsoft365.com/setup ventures and organizations. The most used tasks of Microsoft Office are Word, a word taking care of program, Excel, a spreadsheet program, PowerPoint, a presentation maker, and Access, a database application.Microsoft Office Setup is the complete suite Microsoft365.com/setup of Microsoft limit programming as it joins a variety of jobs, servers, and affiliations like PowerPoint, Excel, Word, Outlook, Publisher, OneNote, and Access. Microsoft Office Setup is the complete Microsoft365.com/setup bundle of Microsoft programs as it takes to the a variety of jobs, servers, and affiliations like PowerPoint, Excel, Wo, Outlook, Publisher, OneNote, and Access.You will still log on with your usual MIT staff username and password, but in addition to that, you will be asked to confirm login via the Microsoft MFA App. This guide explains how you would register your mobile phone, download the Microsoft MFA APP to log on externally going forward from Microsoft365.com/setup.Activating Aka.ms/mfasetup to protect your identity is very important. If someone gets access to your mailbox this person can also misuse your identity. But important is not only to protect your mailbox but also your Google Account, your Facebook, PayPal and other important accounts you use.Please go through this Aka.ms/mfasetup and get all the details you need to know about Microsoft multi factor authentication setup (MFA Setup).

    ReplyDelete
  15. Setup mfa and get complete security. If you still face troubles while enabling MFA on Office 365, then visit Aka.ms/mfasetup and follow the steps for setting up Multi-Factor Authentication in Office 365.Enable MFA on Office 365 admin site by going to the Microsoft Admin Portal, and then click the “Active users” option. After that choose “More” and then you will get the option of “Multifactor Authentication setup”. Setup mfa there and get complete security. If you still face troubles while enabling MFA on Office 365, then visit Aka.ms/mfasetup and follow the steps for setting up Multi-Factor Authentication in Office 365.You will still log on with your usual MIT staff username and password, but in addition to that, you will be asked to confirm login via the Microsoft MFA App. This guide explains how you would register your mobile phone, download the Microsoft MFA APP to log on externally going forward from Aka.ms/mfasetup.Activating Aka.ms/mfasetup to protect your identity is very important. If someone gets access to your mailbox this person can also misuse your identity. But important is not only to protect your mailbox but also your Google Account, your Facebook, PayPal and other important accounts you use.netgear router login - NETGEAR has its own service of websites,Netgear router login i.e., routerlogin.com and routerlogin.net. sites will work particularly only when accessed from NETGEAR routers. These websites allow users to access their consoles by username rather than IP addresses.For more information, visit our website.Bet 365 is an international betting company and one of the oldest online resources created for betting. To access the Bet365 Login page, if you are new, you need to know more about Bet 365's bet account. You can visit our website " bet365 login" to get more information about Bet365 and access the bet365 login page.

    ReplyDelete
  16. Credit one bank online enrollment process is quite easy. The Credit one bank has its official webpage for online account enrollment at creditonebank.com. You can enroll for an online account and simply signin to that account using username and password at Creditonebank.com login page.Visit US BANK LOGIN page and click on "Forgot ID" link.Choose Personal account, then click Continue.The system will prompt you through the remaining steps of recovering your Personal ID.Vanguard Login funds not held in a brokerage account are held by The Vanguard Group, Inc., and are not protected by SIPC. Brokerage assets are held by Vanguard ...To log in to Xero, go to the Xero Login screen. To log in to Xero Central, click Login at the top of any Xero Central page. · Enter your email.If you want to login to the Discover Card Login account, you have to use your online account user ID and the password. If you are not registered yet with the online account, then you need to register your account online.

    ReplyDelete
  17. Office it the heap of office devices to make yourwww.office.com/setup working smooth and effective.visit: to downloaded in your PC with the snappy help.On the off chance that client needs to utilize office.com/setup the Office setup on the web, at that point open the internet browser you are utilizing on your framework and visit today In case customer needs to use the Office office.com/setup setup on the web, by then open the web program you are using on your system and visit today.office setup is the best programming which is office.com/setup comprehensively used in globe .It is an astonishing affiliation that causes you discharge your best considerations, complete things, and remain related on the go.for more nuances visit: office.com setup today.The need for a reliable antivirus like Norton Enrollment is increasing, you can download it by visiting the website norton.cn/setup

    ReplyDelete
  18. The need for a reliable antivirus like Norton Enrollment is increasing, you can download it by visiting the website norton.com/setup . Not only companies as a measure of data security, but everyone is now relying on antivirus programs that can be found on the norton.com/setup with product key page. If you have any problem you can visit our website norton.com/setup and get help.In case customer needs to use the Office office.com/setup setup on the web, by then open the web program you are using on your system and visit today.office setup is the best programming which is office.com/setup comprehensively used in globe .Mcafee antivirus is a commonly used antivirus that detects and kills PC infections, enables mail worms, Trojan programs, etc. For a more accurate visit, to save your PC or PC from infection and ensure data, download the Mcafee Antivirus by visiting the Mcafee.com/activate website.The Canon printer enhances scan functionality, and includes a robust security feature set. Using a Canon printer service phone, you can get a full installation of the CANON.COM/IJSETUP printer and go to the installed Canon printer to download the canon.com/ijsetup driver.

    ReplyDelete
  19. The Amazon My TV Code program requires the customer to have an Amazon account and a streaming device or TV. Amazon Prime Video is available on almost all streaming devices like Roku, Amazon Fire TV, Chromecast. All you have to do is go to amazon.com/mytv and activate Amazon.com/mytv with the help of Amazon's activation code. You can visit our website amazon.com/mytv for details of the entire process.Sign in to your Hulu account. A Hulu account gives you access to an amazing selection of movies, TV shows, music and more from the www.hulu.com/activate Channel Store. With thousands of available channels activate my hulu account visit here for more details. To activate your hulu activation code for tv.Go to primevideo.com/mytv on your computer. You need to open or primevideo.com/mytv on any web browser and enter the activation code Register on the Prime Web Page at primevideo.com/mytv to enroll.Are you looking for ways to activate your Xfinity Stream Beta App on Roku using the Xfinity.com/authorize link? You are at the right place.123hpcomsetup provides support for all HP Printer models installation, setup, troubleshooting and driver download.

    ReplyDelete
  20. 123hpcomsetup provides support for all HP Printer models installation, setup, troubleshooting and driver download.The Canon printer enhances scan functionality, and includes a robust security feature set. Using a Canon printer service phone, you can get a full installation of the CANON.COM/IJSETUP printer and go to the installed Canon printer to download the canon.com/ijsetup driver. To activate avg antivirus , you need to redeem your avg activation code which you will find in your registered mail ID. Through www.avg.com/retail you can download, install avg antivirus . create a avg account to manage www.avg.com/retail and avg retail installation subscription.Apart from the Americanexpress.com Login official website, you can access it anywhere from your Smartphone thanks to the American Express App. Download on Android and iOS.To Pay Your AT&T Bill Online, you need to visit att.com/pay or www.att.com/pay and sign in to att. This is the easiest way to pay all the att bills.

    ReplyDelete
  21. webroot Mind blowing, lightweight, fused protection for PC, Mac and Android, cloud-based Webroot Internet Security Complete with antivirus guarantees singular information by thwarting the latest malware, phishing, and advanced ambushes. Visit to Webroot.com/safe of webroot antivirus : Secure up to PCs or Macs, Protect yourself from identity theft and No time-consuming updates or slow scans. The Canon IJ Network Tool is a free application that allows you to set, view, or configure the printer's network settings that are connected through the network. For more information about this, you can visit our website Canon.com/ijsetupe printer and go to the installed Canon printer to download the canon.com/ijsetup driver.The Amazon My TV Code program requires the customer to have an Amazon account and a streaming device or TV.<a href="https://sites.google.com/view/amazoncom-mytvactivate/>Amazon.com/mytv</a> Amazon Prime Video is available on almost all streaming devices like Roku, Amazon Fire TV, Chromecast.

    ReplyDelete
  22. People's media needs can be met through the Amazon Prime platform. Amazon Prime is a subscription service, which can be subscribed by going to the link Ij.start.canon and go to the installed Canon printer to download the 123.hp.com/setup are compact and durable, ready to deal with your print needs. Set your printer setup from HP to begin. The entire data referenced in this regard is straightforward and simple.We will help you download the correct HP printer software and drivers. Which includes Windows, MacOS, iOS and Android. You can then install your 123.hp.com/setup .For installation of drives, you can take help of our website 123.hp.com/setup . Enter your email.If you want to login to the <a href="https://sites.google.com/view/discover-card-login-0/>Discover Card Login</a> account, you have to use your online account user ID and the password. If you are not registered yet with the online account, then you need to register your account online.

    ReplyDelete
  23. If you got a new credit card, then you have to activate it from roadrunner login an East Coast-based email program. PayPal is one of the largest online payment processors in the world. Sign Up Now! Secure all your Transactions Paypal.com/activatecard of Stores. 180-days Buyer Protection. B2B Payments. Faster Checkout.Shop Millions Sygic.com/activate- you do not always need a product code. To activate these products, you just need to go to our app's Menu → Sign In, enter your account email, password and then sign in.

    ReplyDelete
  24. Download the Canon printer from Norton.com/setup Norton Antivirus provides protection from viruses, malware, online threats without harming device performance. It also blocks harmful websites. You can get Office.com/setup. If your version of Ms Office setup came with your PC, you can download or order a latest version from office.com/setup. The Canon printer enhances scan functionality, and includes a robust security feature set. Using a Canon printer service phone, you can get a full installation of the <a href="https://sites.google.com/site/installijstartcanon/>Ij.Start.Canon</a> and go to the installed Canon printer to download the ij.start.canon.

    ReplyDelete
  25. A disconnected printer error can likewise disapprove of the
    hp printer in error state software or driver. This can differ contingent upon the utilization of your printer and whether or not you have introduced refreshes. The printer investigating apparatus works with a wide scope of printers, including Canon, Brother, Epson, HP, and many others.

    ReplyDelete