Thread: No-opp loop

  1. #16
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Portland, OR
    Quote Originally Posted by DougDbug View Post
    * The operating system does run a do-nothing loop when the CUP is "idle", but this is the lowest priority task. And, it's OK (usually it's required) to use do-nothing loops in an embedded system, where you have full control of all running programs and you are in charge of allocating CPU cycles.
    On architectures capable of halting, most operating system initiate a halt when things go idle. The halt is broken at the next interrupt.

    Also, most OS kernels use spinlocks as the underlying lock primitive to implement more advanced locking. Spinlocks are busy loops, but they don't spin for very long. Only a few cycles at the most.

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    If you have an embedded system with "wait loops", it's either so trivial that it only needs an 8-bit micorcontroller and half a dozen kilobytes of code, or it's very badly designed.

    Wait-loops is a waste of useful processing time.

    And like brewbuck says, the "idle" in a modern processor is a HLT instruction, which is a way to say "Wait here until an interrupt comes in". Much more efficient than a loop, as the HLT instruction allows the processor to stop fetching, decoding and executing instructons, saving power in the process.

    Yes, there are situations where short busy/wait loops are necessary to force correct behaviour. spin-locks are the only way to wait when the schedular can't be run - a "sleep" waiting method won't work here, because that requires the scheduler to run. This is typically in the interrupt section of a driver or similar places. Of course, all accesses to the data accessed by the interrupt section must be protected by the same spinlock Also, spinlock enter should block interrupts, or you could dead-lock by having the same lock held by the non-interrupt path, and the interrupt coming in - if we hold a spin-lock. Spin-locks are often only implemented on SMP machines, and in the non-SMP systems, the same code is protected by a simple "block interrupts" instruction. This should naturally not be done for long running pieces of code - if that ends up being the case, you probably should redesign the data-model.

    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM