Subversion Repositories HelenOS-historic

Rev

Rev 1658 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (C) 2001-2004 Jakub Jermar
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * - Redistributions of source code must retain the above copyright
  10.  *   notice, this list of conditions and the following disclaimer.
  11.  * - Redistributions in binary form must reproduce the above copyright
  12.  *   notice, this list of conditions and the following disclaimer in the
  13.  *   documentation and/or other materials provided with the distribution.
  14.  * - The name of the author may not be used to endorse or promote products
  15.  *   derived from this software without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  */
  28.  
  29. #include <test.h>
  30. #include <arch.h>
  31. #include <atomic.h>
  32. #include <print.h>
  33. #include <proc/thread.h>
  34. #include <arch/types.h>
  35. #include <arch/context.h>
  36. #include <context.h>
  37. #include <panic.h>
  38.  
  39. #include <synch/waitq.h>
  40. #include <synch/rwlock.h>
  41. #include <synch/synch.h>
  42. #include <synch/spinlock.h>
  43.  
  44. #define READERS     50
  45. #define WRITERS     50
  46.  
  47. static rwlock_t rwlock;
  48.  
  49. SPINLOCK_INITIALIZE(lock);
  50.  
  51. static waitq_t can_start;
  52.  
  53. uint32_t seed = 0xdeadbeef;
  54.  
  55. static uint32_t random(uint32_t max);
  56.  
  57. static void writer(void *arg);
  58. static void reader(void *arg);
  59. static void failed(void);
  60.  
  61. uint32_t random(uint32_t max)
  62. {
  63.     uint32_t rc;
  64.  
  65.     spinlock_lock(&lock);  
  66.     rc = seed % max;
  67.     seed = (((seed<<2) ^ (seed>>2)) * 487) + rc;
  68.     spinlock_unlock(&lock);
  69.     return rc;
  70. }
  71.  
  72.  
  73. void writer(void *arg)
  74. {
  75.     int rc, to;
  76.     thread_detach(THREAD);
  77.     waitq_sleep(&can_start);
  78.  
  79.     to = random(40000);
  80.     printf("cpu%d, tid %d w+ (%d)\n", CPU->id, THREAD->tid, to);
  81.     rc = rwlock_write_lock_timeout(&rwlock, to);
  82.     if (SYNCH_FAILED(rc)) {
  83.         printf("cpu%d, tid %d w!\n", CPU->id, THREAD->tid);
  84.         return;
  85.     };
  86.     printf("cpu%d, tid %d w=\n", CPU->id, THREAD->tid);
  87.  
  88.     if (rwlock.readers_in) panic("Oops.");
  89.     thread_usleep(random(1000000));
  90.     if (rwlock.readers_in) panic("Oops."); 
  91.  
  92.     rwlock_write_unlock(&rwlock);
  93.     printf("cpu%d, tid %d w-\n", CPU->id, THREAD->tid);
  94. }
  95.  
  96. void reader(void *arg)
  97. {
  98.     int rc, to;
  99.     thread_detach(THREAD);
  100.     waitq_sleep(&can_start);
  101.    
  102.     to = random(2000);
  103.     printf("cpu%d, tid %d r+ (%d)\n", CPU->id, THREAD->tid, to);
  104.     rc = rwlock_read_lock_timeout(&rwlock, to);
  105.     if (SYNCH_FAILED(rc)) {
  106.         printf("cpu%d, tid %d r!\n", CPU->id, THREAD->tid);
  107.         return;
  108.     }
  109.     printf("cpu%d, tid %d r=\n", CPU->id, THREAD->tid);
  110.     thread_usleep(30000);
  111.     rwlock_read_unlock(&rwlock);
  112.     printf("cpu%d, tid %d r-\n", CPU->id, THREAD->tid);    
  113. }
  114.  
  115. void failed(void)
  116. {
  117.     printf("Test failed prematurely.\n");
  118.     thread_exit();
  119. }
  120.  
  121. void test(void)
  122. {
  123.     context_t ctx;
  124.     uint32_t i, k;
  125.    
  126.     printf("Read/write locks test #4\n");
  127.    
  128.     waitq_initialize(&can_start);
  129.     rwlock_initialize(&rwlock);
  130.    
  131.  
  132.    
  133.     for (; ;) {
  134.         thread_t *thrd;
  135.        
  136.         context_save(&ctx);
  137.         printf("sp=%#x, readers_in=%d\n", ctx.sp, rwlock.readers_in);
  138.        
  139.         k = random(7) + 1;
  140.         printf("Creating %d readers\n", k);
  141.         for (i=0; i<k; i++) {
  142.             thrd = thread_create(reader, NULL, TASK, 0, "reader");
  143.             if (thrd)
  144.                 thread_ready(thrd);
  145.             else
  146.                 failed();
  147.         }
  148.  
  149.         k = random(5) + 1;
  150.         printf("Creating %d writers\n", k);
  151.         for (i=0; i<k; i++) {
  152.             thrd = thread_create(writer, NULL, TASK, 0, "writer");
  153.             if (thrd)
  154.                 thread_ready(thrd);
  155.             else
  156.                 failed();
  157.         }
  158.        
  159.         thread_usleep(20000);
  160.         waitq_wakeup(&can_start, WAKEUP_ALL);
  161.     }      
  162.  
  163. }
  164.