/* GStreamer
 * Copyright (C) 2005 Andy Wingo <wingo at pobox.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library 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 <stdlib.h>
#include <glib.h>
#include <gmodule.h>
#include "memchunk.h"


#define MAX_THREADS  100
#define CHUNK_SIZE 32
#define CHUNKS_PER_AREA 32 

typedef gpointer (*alloc_func_t) (void);
typedef void (*free_func_t) (gpointer);


static gint num_allocs;
static gint num_threads;

static GCond *ready_cond;
static GCond *start_cond;
static GMutex *sync_mutex;


static gdouble
get_current_time (void)
{
  GTimeVal tv;

  g_get_current_time (&tv);
  return tv.tv_sec + ((gdouble) tv.tv_usec) / G_USEC_PER_SEC;
}


void *
worker_thread (void *threadid)
{
  gint i, j;
  GList *head, *next;

  g_mutex_lock (sync_mutex);
  g_cond_signal (ready_cond);
  g_cond_wait (start_cond, sync_mutex);
  g_mutex_unlock (sync_mutex);

  i = 0;
  while (i < num_allocs)
   {
#if 0     
     head = NULL;
     for (j = 0; j < 3 * CHUNKS_PER_AREA && i < num_allocs; j++, i++)
       {
	 head = g_list_prepend (head, NULL);
       }
     g_list_free (head);
#else     
     
     head = g_malloc (sizeof (GList));
     head->next = NULL;
     for (j = 0; j < 3 * CHUNKS_PER_AREA && i < num_allocs; j++, i++)
       {
	 next = g_malloc (sizeof (GList));
	 next->next = NULL;
	 head->next = next;
       }
     while (head)
       {
	 next = head->next;
	 g_free (head);
	 head = next;
       }
#endif
   }

  return NULL;
}

gdouble
run_test (void)
{
  gdouble start, end;
  GThread *threads[MAX_THREADS];
  GError *error;
  int t;

  g_mutex_lock (sync_mutex);
  for (t = 0; t < num_threads; t++) {
    error = NULL;
    threads[t] =
        g_thread_create (worker_thread, GINT_TO_POINTER (t), TRUE, &error);
    g_assert (threads[t]);
    g_cond_wait (ready_cond, sync_mutex);
  }

  g_cond_broadcast (start_cond);
  start = get_current_time ();
  g_mutex_unlock (sync_mutex);

  for (t = 0; t < num_threads; t++)
    g_thread_join (threads[t]);

  end = get_current_time ();

  return end - start;
}

gint
main (gint argc, gchar * argv[])
{
  gdouble time;
  GModule *google_lib;
  int impl = -1;

  g_thread_init (NULL);

  ready_cond = g_cond_new ();
  start_cond = g_cond_new ();
  sync_mutex = g_mutex_new ();

  if (argc < 3 || argc > 4) {
    g_print ("usage: %s <num_threads> <num_allocs> [<impl>]\n", argv[0]);
    exit (-1);
  }

  num_threads = atoi (argv[1]);
  num_allocs = atoi (argv[2]);
  g_assert (num_threads > 0);
  g_assert (num_allocs > 0);

  time = run_test ();
  g_print ("%fs (%fs/thread)\n", time, time / num_threads);

  return 0;
}
