2 * Copyright (C) 2001 Richard W.M. Jones <rich@annexia.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the Free
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 * $Id: test_mutex.c,v 1.3 2002/12/01 14:29:31 rich Exp $
34 #include "pthr_mutex.h"
37 #define NR_INCREMENTS 50
39 static int var = 0; /* The contended variable. */
40 static mutex lock; /* The lock. */
41 static int nr_threads = NR_THREADS;
44 start_monitor_thread (void *data)
50 while (nr_threads > 0)
52 /* Get value of contended variable and draw a scale. */
53 v = 72 * var / (NR_THREADS * NR_INCREMENTS);
66 /* Check v is correct at the end. */
67 assert (var == NR_THREADS * NR_INCREMENTS);
72 start_thread (void *data)
76 for (i = 0; i < NR_INCREMENTS; ++i)
81 v = var; /* Do a slow R/M/W. */
95 pseudothread pth[NR_THREADS];
96 pseudothread monitor_pth;
100 /* Create the lock. */
101 lock = new_mutex (global_pool);
103 /* Create the monitoring thread. */
104 p = new_subpool (global_pool);
105 monitor_pth = new_pseudothread (p, start_monitor_thread, 0,
107 pth_start (monitor_pth);
109 /* Create the threads. */
110 for (i = 0; i < NR_THREADS; ++i)
112 p = new_subpool (global_pool);
113 pth[i] = new_pseudothread (p, start_thread, 0,
114 psprintf (p, "thread %d", i));
117 /* Start all the threads running. */
118 for (i = 0; i < NR_THREADS; ++i)
121 for (;;) reactor_invoke ();