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