Prex Home / Browse Source - Prex Version: 0.9.0

root/usr/test/deadlock/deadlock.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. thread_run
  2. thread_1
  3. thread_2
  4. main

   1 /*-
   2  * Copyright (c) 2005, Kohsuke Ohtani
   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  * 1. Redistributions of source code must retain the above copyright
   9  *    notice, this list of conditions and the following disclaimer.
  10  * 2. Redistributions in binary form must reproduce the above copyright
  11  *    notice, this list of conditions and the following disclaimer in the
  12  *    documentation and/or other materials provided with the distribution.
  13  * 3. Neither the name of the author nor the names of any co-contributors
  14  *    may be used to endorse or promote products derived from this software
  15  *    without specific prior written permission.
  16  *
  17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27  * SUCH DAMAGE.
  28  */
  29 
  30 /*
  31  * deadlock.c - test program for a kernel deadlock detection.
  32  */
  33 
  34 /*
  35  * Prex kernel has the feature of deadlock detection. The following
  36  * senario is tested with this program.
  37  *
  38  * 1) Thread-2 locks mutex-A.
  39  * 2) Thread-1 locks mutex-B.
  40  * 3) Thread-1 locks mutex-A.
  41  * 4) Thread-2 locks mutex-B.
  42  *
  43  * The deadlock occurs at 4) because mutex-B has been already locked by
  44  * thread-1 and thread-1 is waiting for thread-2 (mutex-A).
  45  * The kernel will detect this condition, and the mutex_lock() system
  46  * call will return EDEADLK error code.
  47  */
  48 
  49 #include <sys/prex.h>
  50 #include <stdio.h>
  51 #include <errno.h>
  52 
  53 static char stack[2][1024];
  54 static thread_t th_1, th_2;
  55 static mutex_t mtx_A, mtx_B;
  56 
  57 thread_t
  58 thread_run(void (*start)(void), void *stack)
  59 {
  60         thread_t t;
  61         int error;
  62 
  63         error = thread_create(task_self(), &t);
  64         if (error)
  65                 panic("thread_create is failed");
  66 
  67         error = thread_load(t, start, stack);
  68         if (error)
  69                 panic("thread_load is failed");
  70 
  71         return t;
  72 }
  73 
  74 /*
  75  * Thread 1 - Priority = 100
  76  */
  77 static void
  78 thread_1(void)
  79 {
  80         int error;
  81 
  82         printf("thread_1: starting\n");
  83 
  84         /*
  85          * 2) Lock mutex B
  86          */
  87         printf("thread_1: 2) lock B\n");
  88         error = mutex_lock(&mtx_B);
  89         if (error) printf("error=%d\n", error);
  90 
  91         /*
  92          * 3) Lock mutex A
  93          *
  94          * Switch to thread 1.
  95          */
  96         printf("thread_1: 3) lock A\n");
  97         error = mutex_lock(&mtx_A);
  98         if (error) printf("error=%d\n", error);
  99 
 100         printf("thread_1: exit\n");
 101         thread_terminate(th_1);
 102 }
 103 
 104 /*
 105  * Thread 2 - Priority = 101
 106  */
 107 static void
 108 thread_2(void)
 109 {
 110         int error;
 111 
 112         printf("thread_2: starting\n");
 113 
 114         /*
 115          * 1) Lock mutex A
 116          */
 117         printf("thread_2: 1) lock A\n");
 118         error = mutex_lock(&mtx_A);
 119         if (error) printf("error=%d\n", error);
 120 
 121         /*
 122          * Switch to thread 1
 123          */
 124         thread_resume(th_1);
 125 
 126         printf("thread_2: running\n");
 127         /*
 128          * 4) Lock mutex B
 129          *
 130          * Deadlock occurs here!
 131          */
 132         printf("thread_2: 4) lock B\n");
 133         error = mutex_lock(&mtx_B);
 134         if (error) printf("error=%d\n", error);
 135         if (error == EDEADLK)
 136                 printf("**** DEADLOCK!! ****\n");
 137 
 138         printf("thread_2: exit\n");
 139         thread_terminate(th_2);
 140 }
 141 
 142 int
 143 main(int argc, char *argv[])
 144 {
 145         printf("Deadlock test program\n");
 146 
 147         /*
 148          * Boost priority of this thread
 149          */
 150         thread_setpri(thread_self(), 90);
 151 
 152         /*
 153          * Initialize mutexes.
 154          */
 155         mutex_init(&mtx_A);
 156         mutex_init(&mtx_B);
 157 
 158         /*
 159          * Create new threads
 160          */
 161         th_1 = thread_run(thread_1, stack[0]+1024);
 162         thread_setpri(th_1, 100);
 163 
 164         th_2 = thread_run(thread_2, stack[1]+1024);
 165         thread_setpri(th_2, 101);
 166 
 167         /*
 168          * Start thread 2
 169          */
 170         thread_resume(th_2);
 171 
 172         /*
 173          * Wait...
 174          */
 175         thread_suspend(thread_self());
 176 
 177         return 0;
 178 }

/* [<][>][^][v][top][bottom][index][help] */