os/ossrv/glib/tsrc/BC/src/tasyncqueue.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/glib/tsrc/BC/src/tasyncqueue.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,146 @@
     1.4 +#undef G_DISABLE_ASSERT
     1.5 +#undef G_LOG_DOMAIN
     1.6 +
     1.7 +#include <glib.h>
     1.8 +#include <stdio.h>
     1.9 +#include <unistd.h>
    1.10 +
    1.11 +#ifdef SYMBIAN
    1.12 +#include <glib_global.h>
    1.13 +#include "mrt2_glib2_test.h"
    1.14 +#endif /*SYMBIAN*/
    1.15 +
    1.16 +
    1.17 +static GAsyncQueue *queue;
    1.18 +G_LOCK_DEFINE_STATIC (queue_lock);
    1.19 +static guint queue_ready;
    1.20 +#define THREADS 10
    1.21 +
    1.22 +static gpointer thread_push (gpointer data)
    1.23 +{
    1.24 +	g_async_queue_push (queue, data);
    1.25 +	return NULL;
    1.26 +}
    1.27 +
    1.28 +static gpointer thread_pop (gpointer data)
    1.29 +{
    1.30 +	gint flag = *(gint*)&data;
    1.31 +	gpointer res = g_async_queue_pop (queue);
    1.32 +	gint g_async_queue_pop_op = *(gint*)&res;
    1.33 +	
    1.34 +	G_LOCK (queue_lock);
    1.35 +	queue_ready++;
    1.36 +	G_UNLOCK (queue_lock);
    1.37 +	g_assert (g_async_queue_pop_op == (34 + flag));
    1.38 +	return NULL;
    1.39 +}
    1.40 +
    1.41 +void basic_test()
    1.42 +{
    1.43 +	GThread *thread1[THREADS], *thread2[THREADS];
    1.44 +	int i;
    1.45 +	
    1.46 +	queue = g_async_queue_new ();
    1.47 +	
    1.48 +	for(i = 0; i < THREADS; i++)
    1.49 +	{
    1.50 +		thread1[i] = g_thread_create (thread_push, GINT_TO_POINTER(34+i), TRUE, NULL);
    1.51 +		thread2[i] = g_thread_create (thread_pop, GINT_TO_POINTER(i), TRUE, NULL);
    1.52 +	}
    1.53 +	
    1.54 +	/*while(queue_ready != 9)
    1.55 +		g_usleep (G_USEC_PER_SEC / 5);*/
    1.56 +	
    1.57 +	for(i = 0; i < THREADS; i++)
    1.58 +	{
    1.59 +		g_thread_join (thread1[i]);
    1.60 +		g_thread_join (thread2[i]);
    1.61 +	}
    1.62 +	
    1.63 +}
    1.64 +
    1.65 +
    1.66 +static gpointer thread_ref_push (gpointer data)
    1.67 +{
    1.68 +	g_async_queue_lock (queue);					//Lock the queue
    1.69 +	g_async_queue_ref (queue);					//Increase ref by 1
    1.70 +	g_async_queue_unref_and_unlock (queue);		//Decrease ref by 1 and unlock
    1.71 +	g_async_queue_push (queue, data);
    1.72 +	sleep(2);
    1.73 +	g_async_queue_push (queue, data);
    1.74 +	return NULL;
    1.75 +}
    1.76 +
    1.77 +static gpointer thread_ref_pop (gpointer data)
    1.78 +{
    1.79 +	gpointer res;
    1.80 +	gint g_async_queue_try_pop_unlocked_op;
    1.81 +	GTimeVal time_val = 
    1.82 +	{
    1.83 +		0,100
    1.84 +	};
    1.85 +	
    1.86 +	g_async_queue_ref_unlocked (queue);			//Increase ref by 1 and unlock
    1.87 +	
    1.88 +	g_async_queue_lock (queue);					//Lock the queue
    1.89 +	res = g_async_queue_try_pop_unlocked (queue);
    1.90 +	g_async_queue_try_pop_unlocked_op = *(gint*)&res;
    1.91 +	g_async_queue_unlock (queue);					//unlock the queue while the other thread sleeps
    1.92 +	g_assert(g_async_queue_timed_pop (queue, &time_val) == NULL);
    1.93 +	
    1.94 +	G_LOCK (queue_lock);
    1.95 +	queue_ready++;
    1.96 +	G_UNLOCK (queue_lock);
    1.97 +	g_assert (g_async_queue_try_pop_unlocked_op == 55);
    1.98 +	return NULL;
    1.99 +}
   1.100 +
   1.101 +void ref_test()
   1.102 +{
   1.103 +	GThread *thread1, *thread2;
   1.104 +	
   1.105 +	g_assert (g_async_queue_try_pop (queue) == NULL);
   1.106 +	
   1.107 +	thread1 = g_thread_create (thread_ref_push, GINT_TO_POINTER(55), TRUE, NULL);
   1.108 +	thread2 = g_thread_create (thread_ref_pop, NULL, TRUE, NULL);
   1.109 +		
   1.110 +/*	while(!queue_ready)
   1.111 +		g_usleep (G_USEC_PER_SEC / 5);*/
   1.112 +	
   1.113 +	g_thread_join (thread1);
   1.114 +	g_thread_join (thread2);
   1.115 +		
   1.116 +}
   1.117 +
   1.118 +
   1.119 +/* run all the tests */
   1.120 +void
   1.121 +run_all_tests()
   1.122 +{
   1.123 +	queue_ready = 0;
   1.124 +	basic_test();
   1.125 +	queue_ready = 0;
   1.126 +	ref_test()	;
   1.127 +}
   1.128 +
   1.129 +int 
   1.130 +main (int   argc,
   1.131 +      char *argv[])
   1.132 +{
   1.133 +  #ifdef SYMBIAN
   1.134 +  g_log_set_handler (NULL,  G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL);
   1.135 +  g_set_print_handler(mrtPrintHandler);
   1.136 +  #endif /*SYMBIAN*/
   1.137 +	  
   1.138 +
   1.139 +  /* Only run the test, if threads are enabled and a default thread
   1.140 +     implementation is available */
   1.141 +#if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE)
   1.142 +  g_thread_init (NULL);
   1.143 +  run_all_tests ();
   1.144 +#endif
   1.145 +#ifdef SYMBIAN
   1.146 +  testResultXml("tasyncqueue");
   1.147 +#endif /* EMULATOR */
   1.148 +  return 0;
   1.149 +}