On Sat, Oct 01, 2005 at 15:28:47 +0700, Mulyadi Santosa wrote: > 1. declare a global counter. You can use "int", "long" or whatever you > want. Declare it as volatile so it won't be cached on CPU register Volatile does not work. You need to use atomic_t. The problem is, that on volatile, only assignment is atomic and even that only wrt interrupts, but not smp concurency. But increment is NOT atomic. This can be proven by actual gcc output. If you declare: volatile int variable; then for: variable = 1 gcc will generate instruction: movl $1, variable(%rip) which, as I said, lacks the lock prefix necessary on SMP (unless gcc somehow knows the test is not multi-threaded, so it does not need lock prefixes). but for: variable += 1 gcc will generate instruction triplet: movl variable(%rip), %eax incl %eax movl %eax, variable(%rip) which obviously isn't atomic even wrt. interrupts (and signals for that matter, by the way). The code is output from gcc 4.0.2 for x86-64 target using -O3 optimization. By the way, while there is a way to do atomic inc on x86 (and x86-64), using "lock incl" instruction, but there is no such way on other platforms, eg. sparc. Such platforms implement atomic_t with spinlocks. > during certain interval. let's name it "actual_interrupt_counter". > Declare another global counter and name it "current_tasklet_counter". > Initialize both as 0 somewhere.... -- Jan 'Bulb' Hudec <bulb@xxxxxx>
Attachment:
signature.asc
Description: Digital signature