os/ossrv/glib/tsrc/BC/src/tasyncqueue.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 #undef G_DISABLE_ASSERT
     2 #undef G_LOG_DOMAIN
     3 
     4 #include <glib.h>
     5 #include <stdio.h>
     6 #include <unistd.h>
     7 
     8 #ifdef SYMBIAN
     9 #include <glib_global.h>
    10 #include "mrt2_glib2_test.h"
    11 #endif /*SYMBIAN*/
    12 
    13 
    14 static GAsyncQueue *queue;
    15 G_LOCK_DEFINE_STATIC (queue_lock);
    16 static guint queue_ready;
    17 #define THREADS 10
    18 
    19 static gpointer thread_push (gpointer data)
    20 {
    21 	g_async_queue_push (queue, data);
    22 	return NULL;
    23 }
    24 
    25 static gpointer thread_pop (gpointer data)
    26 {
    27 	gint flag = *(gint*)&data;
    28 	gpointer res = g_async_queue_pop (queue);
    29 	gint g_async_queue_pop_op = *(gint*)&res;
    30 	
    31 	G_LOCK (queue_lock);
    32 	queue_ready++;
    33 	G_UNLOCK (queue_lock);
    34 	g_assert (g_async_queue_pop_op == (34 + flag));
    35 	return NULL;
    36 }
    37 
    38 void basic_test()
    39 {
    40 	GThread *thread1[THREADS], *thread2[THREADS];
    41 	int i;
    42 	
    43 	queue = g_async_queue_new ();
    44 	
    45 	for(i = 0; i < THREADS; i++)
    46 	{
    47 		thread1[i] = g_thread_create (thread_push, GINT_TO_POINTER(34+i), TRUE, NULL);
    48 		thread2[i] = g_thread_create (thread_pop, GINT_TO_POINTER(i), TRUE, NULL);
    49 	}
    50 	
    51 	/*while(queue_ready != 9)
    52 		g_usleep (G_USEC_PER_SEC / 5);*/
    53 	
    54 	for(i = 0; i < THREADS; i++)
    55 	{
    56 		g_thread_join (thread1[i]);
    57 		g_thread_join (thread2[i]);
    58 	}
    59 	
    60 }
    61 
    62 
    63 static gpointer thread_ref_push (gpointer data)
    64 {
    65 	g_async_queue_lock (queue);					//Lock the queue
    66 	g_async_queue_ref (queue);					//Increase ref by 1
    67 	g_async_queue_unref_and_unlock (queue);		//Decrease ref by 1 and unlock
    68 	g_async_queue_push (queue, data);
    69 	sleep(2);
    70 	g_async_queue_push (queue, data);
    71 	return NULL;
    72 }
    73 
    74 static gpointer thread_ref_pop (gpointer data)
    75 {
    76 	gpointer res;
    77 	gint g_async_queue_try_pop_unlocked_op;
    78 	GTimeVal time_val = 
    79 	{
    80 		0,100
    81 	};
    82 	
    83 	g_async_queue_ref_unlocked (queue);			//Increase ref by 1 and unlock
    84 	
    85 	g_async_queue_lock (queue);					//Lock the queue
    86 	res = g_async_queue_try_pop_unlocked (queue);
    87 	g_async_queue_try_pop_unlocked_op = *(gint*)&res;
    88 	g_async_queue_unlock (queue);					//unlock the queue while the other thread sleeps
    89 	g_assert(g_async_queue_timed_pop (queue, &time_val) == NULL);
    90 	
    91 	G_LOCK (queue_lock);
    92 	queue_ready++;
    93 	G_UNLOCK (queue_lock);
    94 	g_assert (g_async_queue_try_pop_unlocked_op == 55);
    95 	return NULL;
    96 }
    97 
    98 void ref_test()
    99 {
   100 	GThread *thread1, *thread2;
   101 	
   102 	g_assert (g_async_queue_try_pop (queue) == NULL);
   103 	
   104 	thread1 = g_thread_create (thread_ref_push, GINT_TO_POINTER(55), TRUE, NULL);
   105 	thread2 = g_thread_create (thread_ref_pop, NULL, TRUE, NULL);
   106 		
   107 /*	while(!queue_ready)
   108 		g_usleep (G_USEC_PER_SEC / 5);*/
   109 	
   110 	g_thread_join (thread1);
   111 	g_thread_join (thread2);
   112 		
   113 }
   114 
   115 
   116 /* run all the tests */
   117 void
   118 run_all_tests()
   119 {
   120 	queue_ready = 0;
   121 	basic_test();
   122 	queue_ready = 0;
   123 	ref_test()	;
   124 }
   125 
   126 int 
   127 main (int   argc,
   128       char *argv[])
   129 {
   130   #ifdef SYMBIAN
   131   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);
   132   g_set_print_handler(mrtPrintHandler);
   133   #endif /*SYMBIAN*/
   134 	  
   135 
   136   /* Only run the test, if threads are enabled and a default thread
   137      implementation is available */
   138 #if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE)
   139   g_thread_init (NULL);
   140   run_all_tests ();
   141 #endif
   142 #ifdef SYMBIAN
   143   testResultXml("tasyncqueue");
   144 #endif /* EMULATOR */
   145   return 0;
   146 }