mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-08-02 07:23:41 +02:00
New files to implement atomic operations for different platforms. Fixes
2004-02-26 Sebastian Wilhelmi <seppi@seppi.de> * glib/gatomic.c, glib/gatomic.h: New files to implement atomic operations for different platforms. Fixes bug #63621. * glib/glib.h: Include gatomic.h. * configure.in: Add test for assembler routines for atomic operations. * glib/Makefile.am: Add gatomic.c, gatomic.h. * tests/Makefile.am, tests/atomic-test.c: Unit test for atomic operations. * glib/glib-overrides.txt, glib/glib-sections.txt, glib/glib-docs.sgml, glib/tmpl/atomic_operations.sgml: Add docs for atomic operations.
This commit is contained in:
committed by
Sebastian Wilhelmi
parent
fc9afe0d21
commit
dbbb29f608
@@ -64,6 +64,7 @@ timeloop_closure_LDADD = $(libglib) $(libgobject)
|
||||
endif
|
||||
|
||||
test_programs = \
|
||||
atomic-test \
|
||||
array-test \
|
||||
$(CXX_TEST) \
|
||||
child-test \
|
||||
@@ -115,6 +116,7 @@ progs_ldadd = $(EFENCE) $(libglib) $(EFENCE)
|
||||
thread_ldadd = $(libgthread) $(G_THREAD_LIBS) $(progs_ldadd)
|
||||
module_ldadd = $(libgmodule) $(G_MODULE_LIBS) $(progs_ldadd)
|
||||
|
||||
atomic_test_LDADD = $(progs_ldadd)
|
||||
array_test_LDADD = $(progs_ldadd)
|
||||
child_test_LDADD = $(thread_ldadd)
|
||||
completion_test_LDADD = $(progs_ldadd)
|
||||
|
57
tests/atomic-test.c
Normal file
57
tests/atomic-test.c
Normal file
@@ -0,0 +1,57 @@
|
||||
#undef G_DISABLE_ASSERT
|
||||
#undef G_LOG_DOMAIN
|
||||
|
||||
#include <glib.h>
|
||||
|
||||
/* Obviously we can't test that the operations are atomic, but we can
|
||||
* at least test, that they do, what they ought to do */
|
||||
|
||||
int
|
||||
main (int argc,
|
||||
char *argv[])
|
||||
{
|
||||
gint i;
|
||||
gint32 atomic = -5;
|
||||
gpointer atomic_pointer = NULL;
|
||||
gpointer biggest_pointer = atomic_pointer - 1;
|
||||
|
||||
for (i = 0; i < 15; i++)
|
||||
g_atomic_int_inc (&atomic);
|
||||
g_assert (atomic == 10);
|
||||
for (i = 0; i < 9; i++)
|
||||
g_assert (!g_atomic_int_dec_and_test (&atomic));
|
||||
g_assert (g_atomic_int_dec_and_test (&atomic));
|
||||
g_assert (atomic == 0);
|
||||
|
||||
g_assert (g_atomic_int_exchange_and_add (&atomic, 5) == 0);
|
||||
g_assert (atomic == 5);
|
||||
|
||||
g_assert (g_atomic_int_exchange_and_add (&atomic, -10) == 5);
|
||||
g_assert (atomic == -5);
|
||||
|
||||
g_atomic_int_add (&atomic, 20);
|
||||
g_assert (atomic == 15);
|
||||
|
||||
g_atomic_int_add (&atomic, -35);
|
||||
g_assert (atomic == -20);
|
||||
|
||||
g_assert (atomic == g_atomic_int_get (&atomic));
|
||||
|
||||
g_assert (g_atomic_int_compare_and_exchange (&atomic, -20, 20));
|
||||
g_assert (atomic == 20);
|
||||
|
||||
g_assert (!g_atomic_int_compare_and_exchange (&atomic, 42, 12));
|
||||
g_assert (atomic == 20);
|
||||
|
||||
g_assert (g_atomic_pointer_compare_and_exchange (&atomic_pointer,
|
||||
NULL, biggest_pointer));
|
||||
g_assert (atomic_pointer == biggest_pointer);
|
||||
|
||||
g_assert (atomic_pointer == g_atomic_pointer_get (&atomic_pointer));
|
||||
|
||||
g_assert (g_atomic_pointer_compare_and_exchange (&atomic_pointer,
|
||||
biggest_pointer, NULL));
|
||||
g_assert (atomic_pointer == NULL);
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user