Slashdot Mirror


Apple's Grand Central Dispatch Ported To FreeBSD

bonch writes "Apple's Grand Central Dispatch, which was recently open sourced, has been ported to FreeBSD and is planned to be included by default in FreeBSD 8.1. Also known as libdispatch, the API allows the use of function-based callbacks but will also support blocks if built using FreeBSD's clang compiler package. There's already discussion of modifying BSD's system tools to use the new technology." The port was originally unveiled last month at the 2009 Developer Summit in Cambridge. Slides from that presentation are available via the Dev Summit wiki.

16 of 205 comments (clear)

  1. Bad Apple by 93+Escort+Wagon · · Score: 5, Funny

    Always taking from the open source community, and never giving back!

    --
    #DeleteChrome
  2. Multicore Enhancements!! :) by jpedlow · · Score: 5, Interesting

    My first question was "So...what does this do?" Apparently it is a more efficient way of scheduling threads on multi-core systems http://images.apple.com/macosx/technology/docs/GrandCentral_TB_brief_20090903.pdf apple's site says this: "Grand Central Dispatch (GCD) in Mac OS X Snow Leopard addresses this pressing need. It’s a set of first-of-their-kind technologies that makes it much easier for developers to squeeze every last drop of power from multicore systems. With GCD, threads are handled by the operating system, not by individual applications. GCD-enabled programs can automatically distribute their work across all available cores, resulting in the best possible performance whether they’re running on a dual-core Mac mini, an 8-core Mac Pro, or anything in between. Once developers start using GCD for their applications, you’ll start noticing significant improvements in performance. " So this seems good then.

    1. Re:Multicore Enhancements!! :) by zn0k · · Score: 4, Informative

      While you certainly don't deserve a "Troll" rating, the real improvement GDC brings over current implementations of concurrency is that one central authority is aware of all GDC enabled applications, and can therefore make decisions with knowledge of the entire system load and its capabilities when spinning of threads. When you spin of threads independently in each app you have to guess what the rest of the system looks like. You also have to either guess, investigate or have the user configure the general capabilities of the system.

    2. Re:Multicore Enhancements!! :) by moosesocks · · Score: 5, Informative

      Ars Technica has a great overview of what GCD is, and why it's important.

      I haven't done much multithreaded programming in C, although this looks like an absolute godsend to those who do. Maybe someday we can actually have a modern desktop as responsive as BeOS was...

      The article also contains a bit of information about Apple's compiler strategy and refinements to the C language itself. Most of these have been open-sourced under a permissive license, which is pretty damn cool.

      --
      -- If you try to fail and succeed, which have you done? - Uli's moose
    3. Re:Multicore Enhancements!! :) by wootest · · Score: 5, Insightful

      GCD is far more encompassing than just spawning threads. It's mainly a library for task-based parallelization (my wording, not theirs) and has some other goodies in it as well.

      For starters, unless it's not apparent by now, just spawning a new thread to run every such job is too heavy. Scheduling a new GCD job (and all of them will get run on one of a bunch of thread pool threads) is on the range of tens of CPU instructions; on Mac OS X, where GCD originated, spawning a new thread steals away in the vicinity of one megabyte (I can't seem to find an exact number) just for various bookkeeping. That's a lot of setup; even if there's a lot of fat to cut there, the best solution is probably not to spawn a new thread every time.

      The API is also very neatly designed. Tasks can be created and added to groups or queues and run synchronously or asynchronously. Semaphores, queues and event sources (which will trigger the addition of an event handler to a queue automatically) can all be paused and resumed to more easily control the flow. Neat, especially combined with the creation of queues that you set up to just funnel work onto another queue.

      Add to that the existence of several global queues with varying priorities (corresponding to a set of worker threads at each priority) and the potential for smarts for a system that can see the entire picture with regards to load within the program and across the system. I don't know the extent to which such smarts exist already, but I think you'll agree that with more metadata available, such a scheduler would be better equipped than one fiddling with coarser-grained threads.

      So yeah, this is nothing like "just" spawning threads. (I've never worked directly with pthread; it may very well reuse threads, but it didn't look like it from the man page you referred to.) None of it, as far as I can tell, is Apple inventing something new and breakthrough, but it's still a damn good API with a good consolidated set of computationally cheap features whose level of abstraction solves problems.

    4. Re:Multicore Enhancements!! :) by Stratoukos · · Score: 5, Insightful

      Maybe someday we can actually have a modern desktop as responsive as BeOS was...

      BeOS was indeed very responsive, but it was also notoriously difficult to program for. Apple seems to have done a great job at creating a powerful framework, while keeping it easy enough to be actually used by the developers.

      --
      It may be 7 digits, but at least it's a semiprime
    5. Re:Multicore Enhancements!! :) by Just+Some+Guy · · Score: 5, Insightful

      Been there, done that on Solaris and Linux 10 years ago in plain old C. No magic required, just

      #include <pthread.h>

      and away you go.

      Piece of cake! Of course, you have no idea how many other processes are tossing threads at their workload and so have no idea if this is a great time to spawn another 8 to really load out the CPU or whether you should just spawn 2 and let some other processes have their time. That's what GCD buys you.

      --
      Dewey, what part of this looks like authorities should be involved?
    6. Re:Multicore Enhancements!! :) by Anonymous Coward · · Score: 4, Informative

      In your zeal to take one statement out of context, you missed the point of the framework. Using dispatch queues, I don't have to create threads, an expensive operation, and I don't have to agonize over performance profiles to determine the right number of threads to use. The operating system automatically maintains a global pool of worker threads based on available system resources, and you submit an asynchronous block of code which the system will assign to a thread and run for you based on current system state--that's what the "automatically distribute their work across all available cores" part means. Quite different from merely creating a pthread. Your code will run in an optimal number of threads for the system it's on, whether it's a Mac Mini Core Solo or an eight-core Mac Pro.

      You can even distribute the iterations of a for-loop across queues using dispatch_apply(). Imagine doing a lot of expensive work on items in a collection, automatically threaded for the cores of the system it's running on. Here's an intro to Grand Central Dispatch. The followup articles on the site are also recommended.

      The API to submit a block for asynchronous execution is very simple:

      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
              do_something_computationally_expensive();
      });

      Posting anonymously since my "bonch" account has been modbombed.

  3. Re:They should use clang instead of GCC by larry+bagina · · Score: 4, Informative

    Apple maintains their own gcc fork which supports blocks/closures.

    --
    Do you even lift?

    These aren't the 'roids you're looking for.

  4. GCD is not multithreading, it's thread management by diamondsw · · Score: 5, Informative

    Grand Central is not introducing multithreading - it's introducing comprehensive thread management. So, how many threads are you going to spin for that task? Too many, and you waste a lot of time on thread management and preemption. Too few, and you have processors sitting idle. Now how will you handle this with multiple CPU's? Multiple cores? Hyperthreading? Different cache amounts and layout? OpenCL and GPU processing? Do you know what the rest of the operating system is doing to plan appropriately?

    In short, your program can at best make a stab at these issues, and possibly even do a reasonable job if you put a lot of time, effort, and profiling into it. Or you could just use GCD, and let the framework handle it all for you, regardless of whether you're on a Core Solo Mac Mini or a Mac Pro with mutliple OpenCL graphics cards.

    It's good stuff. And Apple gave it to the community (much like WebKit enhancements, launchd, etc).

    --
    I don't know what kind of crack I was on, but I suspect it was decaf.
  5. Re:In other news... by wootest · · Score: 5, Funny

    Richard Stallman does not cry into his beer. Microsoft cries into their beer. Richard Stallman cries into his freedom.

  6. Re:No. Really? by beelsebob · · Score: 5, Informative

    Yep, apple didn't give back all their code on WebKit, or all of Darwin, or all of launchd, or all their patches on zfs, or their code on MacPorts, or darwin streaming server, or CalDav, or iCal format, or their Calander server, or their code on their X server, or their code on ruby, or a bunch of code on smart card services...

    Wait, yes they did.

  7. Thank goodness for the GPL by Anonymous Coward · · Score: 4, Funny

    Thanks goodness for the GPL, or we might never have convinced Apple to release its code so that FreeBSD could use it!

    Wait... what is that? Oh, nevermind then...

  8. Re:No. Really? by DurendalMac · · Score: 4, Insightful

    Several things didn't need to be open sourced, such as WebObjects and GCD. Quit your crying. FOSS zealots love to whine about Apple only doing what's required, but they fail to realize that it's a symbiotic relationship. Apple can use existing code to fit their needs, and in return, the open source community gets all of the improvements made by professional coders. It's a win/win, but the mini-Stallmans will never see it that way.

  9. They are not mini-stallmans. They are Apple Haters by SuperKendall · · Score: 4, Interesting

    It's a win/win, but the mini-Stallmans will never see it that way.

    To the contrary: I am a huge fan of Stallman's philosophy and see Apple's work as win/win.

    The people that are complaining are the looney Apple Haters, who try and find any point possible by which to attack Apple - never realizing until it is to late the latest position they are attacking from makes no sense.

    Please do not taint all those of us who respect the GPL with the same brush the Haters paint themselves in corners with.

    --
    "There is more worth loving than we have strength to love." - Brian Jay Stanley
  10. Re:They should use clang instead of GCC by TheRaven64 · · Score: 4, Informative

    A massive case of NIH from the GCC team with regard to Apple. See also the patches that Apple submitted well over a year ago adding declared property support to GNU gcc. I found it easier to write all of the code required for clang to support the GNU Objective-C runtime than make even small changes to Objective-C support in GCC (in spite of not having used C++ for a few years and really hating the language).

    Oh, and I've had blocks working with clang on FreeBSD for quite a while (since several months before Apple publicly released an OS supporting them, in fact). I added the required runtime library support into Etoile's ObjectiveC2 framework a while ago, which also provides the run time support for most of the Objective-C 2 features. This framework is going to go away soon, because I'm now maintaining a fork of the GNU Objective-C runtime in the GNUstep repository, which merges these improvements and also incorporates most of the ideas from my experimental Objective-C runtime library (without breaking backwards compatibility, although you don't get all of the features if you don't use the new ABI).

    By the way, porting to *BSD is much easier than porting to Linux. Libdispatch is based around the kqueue mechanism for unifying kernel event sources, and there is currently no in-tree equivalent for Linux. There are four out-of-tree sets of patches providing equivalent functionality (that I know of, there may be more), as is common with Linux development. On Solaris you can do the same thing with completion ports, which are roughly semantically equivalent but have a different interface.

    --
    I am TheRaven on Soylent News