Convert tests/slice-concurrent.c to glib test framework

This commit is contained in:
Emmanuel Fleury 2022-03-20 20:00:48 +01:00 committed by Philip Withnall
parent 6211971c41
commit 1f2f3d7f37

View File

@ -15,11 +15,10 @@
* You should have received a copy of the GNU Lesser General Public * You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/ */
#include <glib.h> #include <glib.h>
#include <stdlib.h> #include <stdlib.h>
#ifdef G_OS_UNIX
#include <unistd.h>
#endif
#define N_THREADS 8 #define N_THREADS 8
#define N_ALLOCS 50000 #define N_ALLOCS 50000
@ -40,18 +39,16 @@ struct ThreadData
static void * static void *
thread_func (void *arg) thread_func (void *arg)
{ {
struct ThreadData *td = arg;
int i; int i;
/* g_print ("Thread %d starting\n", td->thread_id); */ struct ThreadData *td = arg;
for (i = 0; i < N_ALLOCS; i++) for (i = 0; i < N_ALLOCS; i++)
{ {
int bytes; int bytes, f, t;
char *mem; char *mem;
int f;
int t;
if (rand() % (N_ALLOCS / 20) == 0) if (rand() % (N_ALLOCS / 20) == 0)
g_print ("%c", 'a' - 1 + td->thread_id); g_test_message ("%c", 'a' - 1 + td->thread_id);
/* allocate block of random size and randomly fill */ /* allocate block of random size and randomly fill */
bytes = rand() % MAX_BLOCK_SIZE + 1; bytes = rand() % MAX_BLOCK_SIZE + 1;
@ -82,7 +79,8 @@ thread_func (void *arg)
if (td->n_to_free > 0) if (td->n_to_free > 0)
{ {
td->n_to_free--; td->n_to_free--;
g_slice_free1 (td->bytes_to_free[td->n_to_free], td->to_free[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++; td->n_freed++;
} }
g_mutex_unlock (&td->to_free_mutex); g_mutex_unlock (&td->to_free_mutex);
@ -91,8 +89,8 @@ thread_func (void *arg)
return NULL; return NULL;
} }
int static void
main (void) test_concurrent_slice (void)
{ {
int t; int t;
@ -102,21 +100,31 @@ main (void)
tdata[t].n_to_free = 0; tdata[t].n_to_free = 0;
tdata[t].n_freed = 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++) for (t = 0; t < N_THREADS; t++)
{ {
tdata[t].gthread = g_thread_create (thread_func, &tdata[t], TRUE, NULL); tdata[t].gthread = g_thread_new (NULL, thread_func, &tdata[t]);
g_assert (tdata[t].gthread != NULL); g_assert_nonnull (tdata[t].gthread);
} }
for (t = 0; t < N_THREADS; t++) for (t = 0; t < N_THREADS; t++)
{ {
g_thread_join (tdata[t].gthread); g_thread_join (tdata[t].gthread);
} }
g_print ("\n");
for (t = 0; t < N_THREADS; t++) for (t = 0; t < N_THREADS; t++)
{ {
g_print ("Thread %d: %d blocks freed, %d blocks not freed\n", g_test_message ("Thread %d: %d blocks freed, %d blocks not freed",
tdata[t].thread_id, tdata[t].n_freed, tdata[t].n_to_free); tdata[t].thread_id, tdata[t].n_freed, tdata[t].n_to_free);
} }
return 0; }
int
main (int argc, char **argv)
{
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/slice/concurrent", test_concurrent_slice);
return g_test_run ();
} }