Subversion Repositories HelenOS

Rev

Rev 3343 | 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/asm.h>
  35. #include <arch/types.h>
  36. #include <arch/context.h>
  37. #include <context.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 atomic_t thread_count;
  48. static rwlock_t rwlock;
  49. static atomic_t threads_fault;
  50. static bool sh_quiet;
  51.  
  52. SPINLOCK_INITIALIZE(rw_lock);
  53.  
  54. static waitq_t can_start;
  55.  
  56. static uint32_t seed = 0xdeadbeef;
  57.  
  58. static uint32_t random(uint32_t max)
  59. {
  60.     uint32_t rc;
  61.  
  62.     spinlock_lock(&rw_lock);   
  63.     rc = seed % max;
  64.     seed = (((seed<<2) ^ (seed>>2)) * 487) + rc;
  65.     spinlock_unlock(&rw_lock);
  66.     return rc;
  67. }
  68.  
  69. static void writer(void *arg)
  70. {
  71.     int rc, to;
  72.     thread_detach(THREAD);
  73.     waitq_sleep(&can_start);
  74.  
  75.     to = random(40000);
  76.    
  77.     if (!sh_quiet)
  78.         printf("cpu%u, tid %" PRIu64 " w+ (%d)\n", CPU->id, THREAD->tid, to);
  79.    
  80.     rc = rwlock_write_lock_timeout(&rwlock, to);
  81.     if (SYNCH_FAILED(rc)) {
  82.         if (!sh_quiet)
  83.             printf("cpu%u, tid %" PRIu64 " w!\n", CPU->id, THREAD->tid);
  84.         atomic_dec(&thread_count);
  85.         return;
  86.     }
  87.    
  88.     if (!sh_quiet)
  89.         printf("cpu%u, tid %" PRIu64 " w=\n", CPU->id, THREAD->tid);
  90.  
  91.     if (rwlock.readers_in) {
  92.         if (!sh_quiet)
  93.             printf("Oops.");
  94.         atomic_inc(&threads_fault);
  95.         atomic_dec(&thread_count);
  96.         return;
  97.     }
  98.     thread_usleep(random(1000000));
  99.     if (rwlock.readers_in) {
  100.         if (!sh_quiet)
  101.             printf("Oops.");   
  102.         atomic_inc(&threads_fault);
  103.         atomic_dec(&thread_count);
  104.         return;
  105.     }
  106.  
  107.     rwlock_write_unlock(&rwlock);
  108.    
  109.     if (!sh_quiet)
  110.         printf("cpu%u, tid %" PRIu64 " w-\n", CPU->id, THREAD->tid);
  111.     atomic_dec(&thread_count);
  112. }
  113.  
  114. static void reader(void *arg)
  115. {
  116.     int rc, to;
  117.     thread_detach(THREAD);
  118.     waitq_sleep(&can_start);
  119.    
  120.     to = random(2000);
  121.    
  122.     if (!sh_quiet)
  123.         printf("cpu%u, tid %" PRIu64 " r+ (%d)\n", CPU->id, THREAD->tid, to);
  124.    
  125.     rc = rwlock_read_lock_timeout(&rwlock, to);
  126.     if (SYNCH_FAILED(rc)) {
  127.         if (!sh_quiet)
  128.             printf("cpu%u, tid %" PRIu64 " r!\n", CPU->id, THREAD->tid);
  129.         atomic_dec(&thread_count);
  130.         return;
  131.     }
  132.    
  133.     if (!sh_quiet)
  134.         printf("cpu%u, tid %" PRIu64 " r=\n", CPU->id, THREAD->tid);
  135.    
  136.     thread_usleep(30000);
  137.     rwlock_read_unlock(&rwlock);
  138.    
  139.     if (!sh_quiet)
  140.         printf("cpu%u, tid %" PRIu64 " r-\n", CPU->id, THREAD->tid);
  141.     atomic_dec(&thread_count);
  142. }
  143.  
  144. char * test_rwlock4(bool quiet)
  145. {
  146.     context_t ctx;
  147.     uint32_t i;
  148.     sh_quiet = quiet;
  149.    
  150.     waitq_initialize(&can_start);
  151.     rwlock_initialize(&rwlock);
  152.     atomic_set(&threads_fault, 0);
  153.    
  154.     uint32_t rd = random(7) + 1;
  155.     uint32_t wr = random(5) + 1;
  156.    
  157.     atomic_set(&thread_count, rd + wr);
  158.    
  159.     thread_t *thrd;
  160.    
  161.     context_save(&ctx);
  162.     if (!quiet) {
  163.         printf("sp=%#x, readers_in=%" PRIc "\n", ctx.sp, rwlock.readers_in);
  164.         printf("Creating %" PRIu32 " readers\n", rd);
  165.     }
  166.    
  167.     for (i = 0; i < rd; i++) {
  168.         thrd = thread_create(reader, NULL, TASK, 0, "reader", false);
  169.         if (thrd)
  170.             thread_ready(thrd);
  171.         else if (!quiet)
  172.             printf("Could not create reader %" PRIu32 "\n", i);
  173.     }
  174.  
  175.     if (!quiet)
  176.         printf("Creating %" PRIu32 " writers\n", wr);
  177.    
  178.     for (i = 0; i < wr; i++) {
  179.         thrd = thread_create(writer, NULL, TASK, 0, "writer", false);
  180.         if (thrd)
  181.             thread_ready(thrd);
  182.         else if (!quiet)
  183.             printf("Could not create writer %" PRIu32 "\n", i);
  184.     }
  185.    
  186.     thread_usleep(20000);
  187.     waitq_wakeup(&can_start, WAKEUP_ALL);
  188.    
  189.     while (atomic_get(&thread_count) > 0) {
  190.         if (!quiet)
  191.             printf("Threads left: %ld\n", atomic_get(&thread_count));
  192.         thread_sleep(1);
  193.     }
  194.    
  195.     if (atomic_get(&threads_fault) == 0)
  196.         return NULL;
  197.    
  198.     return "Test failed";
  199. }
  200.