GLib: atomic integer operations
- From: "Gustavo J. A. M. " Carneiro <gjc inescporto pt>
- To: GTK Development list <gtk-devel-list gnome org>
- Subject: GLib: atomic integer operations
- Date: 30 Dec 2002 15:29:51 +0000
I would like to propose that a set of "atomic integer operations" be
included in a future glib. Something like this:
http://www.mozilla.org/projects/nspr/reference/html/pratom.html#20841
These functions are very useful for threaded programs. Advantages:
1. These operations do not require any locking/mutexes, so they are
super fast!;
2. Thread-safe reference counting of objects without any performance
penalty;
3. They're very simple to implement; It takes only a few asm
instructions. Example (from NSPR):
/ PRInt32 _PR_x86_AtomicIncrement(PRInt32 *val)
/
/ Atomically increment the integer pointed to by 'val' and return
/ the result of the increment.
/
.text
.globl _PR_x86_AtomicIncrement
.align 4
_PR_x86_AtomicIncrement:
movl 4(%esp), %ecx
movl $1, %eax
lock
xaddl %eax, (%ecx)
incl %eax
ret
For unsupported platforms, the behaviour can be emulated with mutexes, of course.
Let me know what you think.
--
Gustavo João Alves Marques Carneiro
<gjc inescporto pt> <gustavo users sourceforge net>
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]