1
0
mirror of https://gitlab.gnome.org/GNOME/glib.git synced 2025-08-06 09:23:39 +02:00
Files
build
debian
docs
gio
glib
gmodule
gobject
gthread
m4macros
po
tests
bookmarks
collate
gobject
markups
refcount
.gitignore
Makefile.am
asyncqueue-test.c
atomic-test.c
base64-test.c
bit-test.c
bookmarkfile-test.c
casefold.txt
casemap.txt
checksum-test.c
child-test.c
completion-test.c
convert-test.c
cxx-test.C
date-test.c
dirname-test.c
env-test.c
errorcheck-mutex-test.c
file-test.c
gen-casefold-txt.pl
gen-casemap-txt.pl
gio-ls.c
gio-test.c
hash-test.c
iochannel-test-infile
iochannel-test.c
libmoduletestplugin_a.c
libmoduletestplugin_b.c
list-test.c
mainloop-test.c
makefile.msc.in
mapping-test.c
markup-collect.c
markup-escape-test.c
markup-test.c
memchunks.c
module-test.c
node-test.c
onceinit.c
patterntest.c
qsort-test.c
queue-test.c
regex-test.c
relation-test.c
run-bookmark-test.sh
run-collate-tests.sh
run-markup-tests.sh
scannerapi.c
sequence-test.c
shell-test.c
slice-color.c
slice-concurrent.c
slice-test.c
slice-threadinit.c
slist-test.c
spawn-test-win32-gui.c
spawn-test.c
testgdate.c
testgdateparser.c
testglib.c
testingbase64.c
thread-test.c
threadpool-test.c
timeloop-basic.c
timeloop-closure.c
timeloop.c
tree-test.c
type-test.c
unicode-caseconv.c
unicode-collate.c
unicode-encoding.c
unicode-normalize.c
uri-test.c
utf8-pointer.c
utf8-validate.c
utf8.txt
.gitignore
AUTHORS
COPYING
ChangeLog
ChangeLog.pre-1-2
ChangeLog.pre-2-0
ChangeLog.pre-2-10
ChangeLog.pre-2-12
ChangeLog.pre-2-14
ChangeLog.pre-2-16
ChangeLog.pre-2-18
ChangeLog.pre-2-2
ChangeLog.pre-2-4
ChangeLog.pre-2-6
ChangeLog.pre-2-8
HACKING
INSTALL
INSTALL.in
MAINTAINERS
Makefile.am
Makefile.decl
NEWS
NEWS.pre-1-3
README
README.in
README.win32
acglib.m4
acinclude.m4
autogen.sh
config.h.win32.in
configure.in
gio-2.0-uninstalled.pc.in
gio-2.0.pc.in
gio-unix-2.0-uninstalled.pc.in
gio-unix-2.0.pc.in
glib-2.0-uninstalled.pc.in
glib-2.0.pc.in
glib-gettextize.in
glib-zip.in
glibconfig.h.win32.in
gmodule-2.0-uninstalled.pc.in
gmodule-2.0.pc.in
gmodule-export-2.0.pc.in
gmodule-no-export-2.0-uninstalled.pc.in
gmodule-no-export-2.0.pc.in
gobject-2.0-uninstalled.pc.in
gobject-2.0.pc.in
gthread-2.0-uninstalled.pc.in
gthread-2.0.pc.in
makefile.msc
mkinstalldirs
msvc_recommended_pragmas.h
sanity_check
win32-fixup.pl
glib/tests/asyncqueue-test.c
Dan Winship 5c53925ed0 Bug 553447 $(Q#|(B g_assert_no_error()
* glib/gtestutils.h (g_assert_no_error, g_assert_error): Macros to
	assert that a GError is not set, or else is set to a particular
	error.

	* glib/gtestutils.c (g_assertion_message_error): utility for
	those macros

	* glib/tests/keyfile.c:
	* tests/asyncqueue-test.c:
	* tests/bookmarkfile-test.c:
	* tests/convert-test.c:
	* tests/file-test.c: Use g_assert_error/g_assert_no_error

svn path=/trunk/; revision=7555
2008-09-27 01:43:29 +00:00

195 lines
4.6 KiB
C

#undef G_DISABLE_ASSERT
#undef G_LOG_DOMAIN
#include <time.h>
#include <stdlib.h>
#include <glib.h>
#define DEBUG_MSG(args)
/* #define DEBUG_MSG(args) g_printerr args ; g_printerr ("\n"); */
#define PRINT_MSG(args)
/* #define PRINT_MSG(args) g_print args ; g_print ("\n"); */
#define MAX_THREADS 50
#define MAX_SORTS 5 /* only applies if
ASYC_QUEUE_DO_SORT is set to 1 */
#define MAX_TIME 20 /* seconds */
#define MIN_TIME 5 /* seconds */
#define SORT_QUEUE_AFTER 1
#define SORT_QUEUE_ON_PUSH 1 /* if this is done, the
SORT_QUEUE_AFTER is ignored */
#define QUIT_WHEN_DONE 1
#if SORT_QUEUE_ON_PUSH == 1
# undef SORT_QUEUE_AFTER
# define SORT_QUEUE_AFTER 0
#endif
static GMainLoop *main_loop = NULL;
static GThreadPool *thread_pool = NULL;
static GAsyncQueue *async_queue = NULL;
static gint
sort_compare (gconstpointer p1, gconstpointer p2, gpointer user_data)
{
gint32 id1;
gint32 id2;
id1 = GPOINTER_TO_INT (p1);
id2 = GPOINTER_TO_INT (p2);
DEBUG_MSG (("comparing #1:%d and #2:%d, returning %d",
id1, id2, (id1 > id2 ? +1 : id1 == id2 ? 0 : -1)));
return (id1 > id2 ? +1 : id1 == id2 ? 0 : -1);
}
static gboolean
sort_queue (gpointer user_data)
{
static gint sorts = 0;
static gpointer last_p = NULL;
gpointer p;
gboolean can_quit = FALSE;
gint sort_multiplier;
gint len;
gint i;
sort_multiplier = GPOINTER_TO_INT (user_data);
if (SORT_QUEUE_AFTER) {
PRINT_MSG (("sorting async queue..."));
g_async_queue_sort (async_queue, sort_compare, NULL);
sorts++;
if (sorts >= sort_multiplier) {
can_quit = TRUE;
}
g_async_queue_sort (async_queue, sort_compare, NULL);
len = g_async_queue_length (async_queue);
PRINT_MSG (("sorted queue (for %d/%d times, size:%d)...", sorts, MAX_SORTS, len));
} else {
can_quit = TRUE;
len = g_async_queue_length (async_queue);
DEBUG_MSG (("printing queue (size:%d)...", len));
}
for (i = 0, last_p = NULL; i < len; i++) {
p = g_async_queue_pop (async_queue);
DEBUG_MSG (("item %d ---> %d", i, GPOINTER_TO_INT (p)));
if (last_p) {
g_assert (GPOINTER_TO_INT (last_p) <= GPOINTER_TO_INT (p));
}
last_p = p;
}
if (can_quit && QUIT_WHEN_DONE) {
g_main_loop_quit (main_loop);
}
return !can_quit;
}
static void
enter_thread (gpointer data, gpointer user_data)
{
gint len;
gint id;
gulong ms;
id = GPOINTER_TO_INT (data);
ms = g_random_int_range (MIN_TIME * 1000, MAX_TIME * 1000);
DEBUG_MSG (("entered thread with id:%d, adding to queue in:%ld ms", id, ms));
g_usleep (ms * 1000);
if (SORT_QUEUE_ON_PUSH) {
g_async_queue_push_sorted (async_queue, GINT_TO_POINTER (id), sort_compare, NULL);
} else {
g_async_queue_push (async_queue, GINT_TO_POINTER (id));
}
len = g_async_queue_length (async_queue);
DEBUG_MSG (("thread id:%d added to async queue (size:%d)",
id, len));
}
int
main (int argc, char *argv[])
{
#if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE)
gint i;
gint max_threads = MAX_THREADS;
gint max_unused_threads = MAX_THREADS;
gint sort_multiplier = MAX_SORTS;
gint sort_interval;
gchar *msg;
g_thread_init (NULL);
PRINT_MSG (("creating async queue..."));
async_queue = g_async_queue_new ();
g_return_val_if_fail (async_queue != NULL, EXIT_FAILURE);
PRINT_MSG (("creating thread pool with max threads:%d, max unused threads:%d...",
max_threads, max_unused_threads));
thread_pool = g_thread_pool_new (enter_thread,
async_queue,
max_threads,
FALSE,
NULL);
g_return_val_if_fail (thread_pool != NULL, EXIT_FAILURE);
g_thread_pool_set_max_unused_threads (max_unused_threads);
PRINT_MSG (("creating threads..."));
for (i = 1; i <= max_threads; i++) {
GError *error = NULL;
g_thread_pool_push (thread_pool, GINT_TO_POINTER (i), &error);
g_assert_no_error (error);
}
if (!SORT_QUEUE_AFTER) {
sort_multiplier = 1;
}
sort_interval = ((MAX_TIME / sort_multiplier) + 2) * 1000;
g_timeout_add (sort_interval, sort_queue, GINT_TO_POINTER (sort_multiplier));
if (SORT_QUEUE_ON_PUSH) {
msg = "sorting when pushing into the queue, checking queue is sorted";
} else {
msg = "sorting";
}
PRINT_MSG (("%s %d %s %d ms",
msg,
sort_multiplier,
sort_multiplier == 1 ? "time in" : "times, once every",
sort_interval));
DEBUG_MSG (("entering main event loop"));
main_loop = g_main_loop_new (NULL, FALSE);
g_main_loop_run (main_loop);
#endif
return EXIT_SUCCESS;
}