1
0
mirror of https://gitlab.gnome.org/GNOME/glib.git synced 2025-11-21 09:57:30 +01:00
Files
build
docs
gio
glib
gmodule
gobject
gthread
m4macros
po
tests
collate
gobject
refcount
.gitignore
Makefile.am
assert-msg-test.c
asyncqueue-test.c
atomic-test.c
bit-test.c
casefold.txt
casemap.txt
child-test.c
completion-test.c
cxx-test.C
datetime.c
dirname-test.c
env-test.c
file-test.c
gen-casefold-txt.pl
gen-casemap-txt.pl
gio-ls.c
gio-test.c
iochannel-test-infile
iochannel-test.c
libmoduletestplugin_a.c
libmoduletestplugin_b.c
mainloop-test.c
makefile.msc.in
mapping-test.c
memchunks.c
module-test.c
onceinit.c
qsort-test.c
relation-test.c
run-assert-msg-test.sh
run-collate-tests.sh
slice-color.c
slice-concurrent.c
slice-test.c
slice-threadinit.c
sources.c
spawn-test-win32-gui.c
spawn-test.c
testgdate.c
testgdateparser.c
testglib.c
thread-test.c
threadpool-test.c
timeloop-basic.c
timeloop-closure.c
timeloop.c
type-test.c
unicode-caseconv.c
unicode-collate.c
unicode-encoding.c
unicode-normalize.c
utf8.txt
.dir-locals.el
.gitignore
AUTHORS
COPYING
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-20
ChangeLog.pre-2-4
ChangeLog.pre-2-6
ChangeLog.pre-2-8
HACKING
INSTALL.in
Makefile.am
Makefile.decl
NEWS
NEWS.pre-1-3
README.commits
README.in
README.win32
acglib.m4
acinclude.m4
autogen.sh
config.h.win32.in
configure.ac
gio-2.0.pc.in
gio-unix-2.0.pc.in
gio-windows-2.0.pc.in
glib-2.0.pc.in
glib-gettextize.in
glib-zip.in
glib.doap
gmodule-2.0.pc.in
gmodule-export-2.0.pc.in
gmodule-no-export-2.0.pc.in
gobject-2.0.pc.in
gthread-2.0.pc.in
makefile.msc
msvc_recommended_pragmas.h
sanity_check
win32-fixup.pl
glib/tests/slice-concurrent.c

123 lines
3.3 KiB
C
Raw Normal View History

/* test for gslice cross thread allocation/free
* Copyright (C) 2006 Stefan Westerfeld
* Copyright (C) 2007 Tim Janik
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <glib.h>
#include <stdlib.h>
#include <unistd.h>
#define N_THREADS 8
#define N_ALLOCS 50000
#define MAX_BLOCK_SIZE 64
struct ThreadData
{
int thread_id;
GThread* gthread;
GMutex to_free_mutex;
void* to_free [N_THREADS * N_ALLOCS];
int bytes_to_free [N_THREADS * N_ALLOCS];
int n_to_free;
int n_freed;
} tdata[N_THREADS];
static void *
thread_func (void *arg)
{
struct ThreadData *td = arg;
int i;
/* g_print ("Thread %d starting\n", td->thread_id); */
for (i = 0; i < N_ALLOCS; i++)
{
int bytes;
char *mem;
int f;
int t;
if (rand() % (N_ALLOCS / 20) == 0)
g_print ("%c", 'a' - 1 + td->thread_id);
/* allocate block of random size and randomly fill */
bytes = rand() % MAX_BLOCK_SIZE + 1;
mem = g_slice_alloc (bytes);
for (f = 0; f < bytes; f++)
mem[f] = rand();
/* associate block with random thread */
t = rand() % N_THREADS;
g_mutex_lock (&tdata[t].to_free_mutex);
tdata[t].to_free[tdata[t].n_to_free] = mem;
tdata[t].bytes_to_free[tdata[t].n_to_free] = bytes;
tdata[t].n_to_free++;
g_mutex_unlock (&tdata[t].to_free_mutex);
/* shuffle thread execution order every once in a while */
if (rand() % 97 == 0)
{
if (rand() % 2)
g_thread_yield(); /* concurrent shuffling for single core */
else
g_usleep (1000); /* concurrent shuffling for multi core */
}
/* free a block associated with this thread */
g_mutex_lock (&td->to_free_mutex);
if (td->n_to_free > 0)
{
td->n_to_free--;
g_slice_free1 (td->bytes_to_free[td->n_to_free], td->to_free[td->n_to_free]);
td->n_freed++;
}
g_mutex_unlock (&td->to_free_mutex);
}
return NULL;
}
int
main (void)
{
int t;
for (t = 0; t < N_THREADS; t++)
{
tdata[t].thread_id = t + 1;
tdata[t].n_to_free = 0;
tdata[t].n_freed = 0;
}
g_print ("Starting %d threads for concurrent GSlice usage...\n", N_THREADS);
for (t = 0; t < N_THREADS; t++)
{
tdata[t].gthread = g_thread_create (thread_func, &tdata[t], TRUE, NULL);
g_assert (tdata[t].gthread != NULL);
}
for (t = 0; t < N_THREADS; t++)
{
g_thread_join (tdata[t].gthread);
}
g_print ("\n");
for (t = 0; t < N_THREADS; t++)
{
g_print ("Thread %d: %d blocks freed, %d blocks not freed\n",
tdata[t].thread_id, tdata[t].n_freed, tdata[t].n_to_free);
}
return 0;
}