[Kos-cvs] kos/modules/test mutex_test.c,1.1,1.2 sem_test.c,1.6,1.7

thomas at kos.enix.org thomas at kos.enix.org
Tue Jan 4 22:57:52 CET 2005


Update of /home/kos/cvs/kos/modules/test
In directory the-doors:/tmp/cvs-serv8985/modules/test

Modified Files:
	mutex_test.c sem_test.c 
Log Message:
2005-01-04  Thomas Petazzoni  <thomas at crazy.kos.nx>

	* modules/test/sem_test.c: Added a test that is similar to the
	mutex one. Instead of using the semaphore as a synchronisation
	primitive, we use it as a mutual exclusion primitive.

	* modules/test/mutex_test.c: Added some usleep() to make sure
	interrupts are generated inside the critical section.

	* modules/kitc/_kmutex.c: doxygen-aware comments.
	(kmutex_lock, kmutex_unlock): Make sure that a mutex that has just
	been released cannot be stolen by an other thread but the one that
	has been woken up. Solution inspired from SOS implementation of
	mutexes.




Index: sem_test.c
===================================================================
RCS file: /home/kos/cvs/kos/modules/test/sem_test.c,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -d -r1.6 -r1.7
--- sem_test.c	29 Dec 2004 19:28:51 -0000	1.6
+++ sem_test.c	4 Jan 2005 21:57:50 -0000	1.7
@@ -23,6 +23,8 @@
 
 static SPINLOCK(sem_test_global_lock);
 
+static volatile uint value = 0;
+
 struct args
 {
   ksem_t *sem;
@@ -71,15 +73,107 @@
   write_spin_unlock(sem_test_global_lock, flags);
 }
 
+#define NB_ITERATIONS 100
+#define NB_THREADS    4
+
+static void sem_inc(void *data)
+{
+  struct ksem *s = (struct ksem *) data;
+  int i = 0;
+  spinlock_flags_t flags;
+
+  while(i < NB_ITERATIONS)
+    {
+
+      CONCEPTION_ASSERT(ksem_down(s) == ESUCCESS);
+
+      if(value == 0)
+	{
+	  value ++;
+	}
+
+      usleep(2);
+      ASSERT_FATAL(value == 1);
+
+      CONCEPTION_ASSERT(ksem_up(s) == ESUCCESS);
+
+      i++;
+    }
+
+  write_spin_lock(sem_test_global_lock, flags);
+  thread_count --;
+  write_spin_unlock(sem_test_global_lock, flags);
+}
+
+static void sem_dec(void *data)
+{
+  struct ksem *s = (struct ksem *) data;
+  int i = 0;
+  spinlock_flags_t flags;
+
+  while(i < NB_ITERATIONS)
+    {
+
+      CONCEPTION_ASSERT(ksem_down(s) == ESUCCESS);
+
+      if(value == 1)
+	{
+	  value --;
+	}
+
+      usleep(3);
+
+      ASSERT_FATAL(value == 0);
+
+      CONCEPTION_ASSERT(ksem_up(s) == ESUCCESS);
+
+      i++;
+    }
+
+  write_spin_lock(sem_test_global_lock, flags);
+  thread_count --;
+  write_spin_unlock(sem_test_global_lock, flags);
+}
+
 result_t sem_test()
 {
   int i, j;
   result_t result;
   k_ui32_t flags;
+  struct ksem sem;
+
+  __dbg_printk("\n\nSemaphore Test 1 Starting\n\n");
+
+  ksem_init(&sem, "none", 1);
+
+  for (i = 0; i < NB_THREADS; i++)
+    {
+      struct thread *t;
+
+      t = create_kernel_thread(NULL, sem_inc, (void *) & sem);
+      ASSERT_FATAL(t != NULL);
+
+      t = create_kernel_thread(NULL, sem_dec, (void *) & sem);
+      ASSERT_FATAL(t != NULL);
+
+      write_spin_lock(sem_test_global_lock, flags);
+      thread_count += 2;
+      write_spin_unlock(sem_test_global_lock, flags);
+    }
+
+  while(thread_count != 0)
+    {
+      usleep(1000000);
+      __dbg_printk("[Semaphore Test 1] Current thread_count = %d\n", thread_count);
+    }
+
+  __dbg_printk("\n\nSemaphore Test 1 Ok\n\n");
+
+  __dbg_printk("\n\nSemaphore Test 2 Starting\n\n");
 
   for (j = 0; j < TEST_NB_CONSUMER_PER_SEMAPHORE; j++)
     nb_reception[j] = 0;
-  
+
   for (i = 0; i < TEST_NB_SEMAPHORE; i++)
     {
       struct ksem * ksem = kmalloc(sizeof(struct ksem));
@@ -134,13 +228,13 @@
   while(thread_count != 0)
     {
       usleep(1000000);
-      __dbg_printk("Current thread_count = %d : ", thread_count);
+      __dbg_printk("[Semaphore Test 2] Current thread_count = %d : ", thread_count);
       for (i = 0; i < TEST_NB_CONSUMER_PER_SEMAPHORE; i++)
 	__dbg_printk("%d ", nb_reception[i]);
       __dbg_printk("\n");
     }
 
-  __dbg_printk("\n\nTest Ok\n\n");
+  __dbg_printk("\n\nSemaphore Test 2 Ok\n\n");
 
   return ESUCCESS;
 }

Index: mutex_test.c
===================================================================
RCS file: /home/kos/cvs/kos/modules/test/mutex_test.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- mutex_test.c	29 Dec 2004 19:28:51 -0000	1.1
+++ mutex_test.c	4 Jan 2005 21:57:50 -0000	1.2
@@ -14,8 +14,8 @@
 #include <task/task.h>
 #include <test/_test.h>
 
-#define NB_THREADS 1
-#define NB_ITERATIONS 10000000
+#define NB_THREADS 5
+#define NB_ITERATIONS 50
 
 static uint thread_count = 0;
 
@@ -31,7 +31,6 @@
 
   while(i < NB_ITERATIONS)
     {
-
       CONCEPTION_ASSERT(kmutex_lock(m) == ESUCCESS);
 
       if(value == 0)
@@ -39,6 +38,8 @@
 	  value ++;
 	}
 
+      usleep(1);
+
       ASSERT_FATAL(value == 1);
 
       CONCEPTION_ASSERT(kmutex_unlock(m) == ESUCCESS);
@@ -67,6 +68,8 @@
 	  value --;
 	}
 
+      usleep(1);
+
       ASSERT_FATAL(value == 0);
 
       CONCEPTION_ASSERT(kmutex_unlock(m) == ESUCCESS);
@@ -85,6 +88,8 @@
   spinlock_flags_t flags;
   struct kmutex mutex;
 
+  __dbg_printk("\n\nMutex Test Starting\n\n");
+
   kmutex_init(&mutex, "none", FALSE);
 
   for (i = 0; i < NB_THREADS; i++)
@@ -100,70 +105,15 @@
       write_spin_lock(mutex_test_global_lock, flags);
       thread_count += 2;
       write_spin_unlock(mutex_test_global_lock, flags);
-
-    }
-
-#if 0
-  for (i = 0; i < TEST_NB_SEMAPHORE; i++)
-    {
-      struct ksem * ksem = kmalloc(sizeof(struct ksem));
-
-      if(ksem == NULL)
-	return -ENOMEM;
-
-      result = ksem_init(ksem, "Sem test", 1);
-      if(result < 0)
-	return result;
-
-      for (j = 0; j < TEST_NB_CONSUMER_PER_SEMAPHORE; j++)
-	{
-	  struct args * args = kmalloc(sizeof(struct args));
-	  struct thread *t;
-
-	  if(args == NULL)
-	    return -ENOMEM;
-
-	  args->sem = ksem;
-	  args->id  = j;
-
-	  t = create_kernel_thread(NULL, sem_consumer, (void *) args);
-	  if(t == NULL)
-	    {
-	      FAILED_VERBOSE("Couldn't create sem_consumer thread\n");
-	    }
-
-	  write_spin_lock(sem_test_global_lock, flags);
-	  thread_count++;
-	  write_spin_unlock(sem_test_global_lock, flags);
-	}
-
-      for (j = 0; j < TEST_NB_PRODUCER_PER_SEMAPHORE; j++)
-	{
-	  struct thread *t;
-
-	  t = create_kernel_thread(NULL, sem_producer, (void *) ksem);
-
-	  if(t == NULL)
-	    {
-	      FAILED_VERBOSE("Couldn't create sem_producer thread\n");
-	    }
-
-	  write_spin_lock(sem_test_global_lock, flags);
-	  thread_count++;
-	  write_spin_unlock(sem_test_global_lock, flags);
-
-	}
     }
-#endif
 
   while(thread_count != 0)
     {
       usleep(1000000);
-      __dbg_printk("Current thread_count = %d : ", thread_count);
-      __dbg_printk("\n");
+      __dbg_printk("Current thread_count = %d\n", thread_count);
     }
 
-  __dbg_printk("\n\nTest Ok\n\n");
+  __dbg_printk("\n\nMutex Test Ok\n\n");
 
   return ESUCCESS;
 }



More information about the Kos-cvs mailing list