1 |
/* PEAK Library - Test file |
2 |
* |
3 |
* Copyright (c) 2004 |
4 |
* Stephane Thiell <mbuna@bugged.org>. All rights reserved. |
5 |
* |
6 |
* Redistribution and use in source and binary forms, with or without |
7 |
* modification, are permitted provided that the following conditions |
8 |
* are met: |
9 |
* |
10 |
* 1. Redistributions of source code must retain the above copyright |
11 |
* notice, this list of conditions and the following disclaimer. |
12 |
* |
13 |
* 2. Redistributions in binary form must reproduce the above copyright |
14 |
* notice, this list of conditions and the following disclaimer in the |
15 |
* documentation and/or other materials provided with the distribution. |
16 |
* |
17 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
18 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
19 |
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
20 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
21 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
22 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
23 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
24 |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
25 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
26 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
27 |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 |
* |
29 |
*/ |
30 |
#define RCSID "$Id: t_atomic2.c,v 1.1 2004/01/09 20:19:48 mbuna Exp $" |
31 |
|
32 |
/* t_atomic2.c - Atomic primitives verification program. |
33 |
* |
34 |
* Purpose |
35 |
* ======= |
36 |
* Configure the (main) task with several threads and test atomic access and |
37 |
* modifications on PEAK's atomic integer type (peak_atomic_t). This file |
38 |
* tests peak_atomic_set(), peak_atomic_add(), peak_atomic_sub() and |
39 |
* peak_atomic_read(). |
40 |
* |
41 |
* Returns |
42 |
* ======= |
43 |
* 0 -> test ok |
44 |
* 1 -> misc timer error (error) |
45 |
* 2 -> unable to set task info (error) |
46 |
* 3 -> atomic failure/result not coherent (error) |
47 |
* |
48 |
*/ |
49 |
|
50 |
#define T_ERR_TIMER_ERROR 1 |
51 |
#define T_ERR_TASK_INFO 2 |
52 |
#define T_ERR_ATOMIC_FAILURE 3 |
53 |
|
54 |
#ifdef HAVE_CONFIG_H |
55 |
#include "config.h" |
56 |
#endif |
57 |
|
58 |
#include <peak/peak.h> |
59 |
#include <stdio.h> |
60 |
|
61 |
#define CNT_START 857 |
62 |
#define MAX_ITER 10000 |
63 |
#define AMOUNT 10 |
64 |
#define N_TIMERS 60 |
65 |
|
66 |
static peak_atomic_t cnt; |
67 |
static time_t start_time; |
68 |
|
69 |
static void |
70 |
timer_inc_callback(peak_timer ti, void *context) |
71 |
{ |
72 |
int i; |
73 |
for (i = MAX_ITER; i-- > 0; ) |
74 |
peak_atomic_add(AMOUNT, &cnt); |
75 |
} |
76 |
|
77 |
static void |
78 |
timer_dec_callback(peak_timer ti, void *context) |
79 |
{ |
80 |
int i; |
81 |
for (i = MAX_ITER; i-- > 0; ) |
82 |
peak_atomic_sub(AMOUNT, &cnt); |
83 |
} |
84 |
|
85 |
static void |
86 |
timer_cnt_callback(peak_timer ti, void *context) |
87 |
{ |
88 |
/* Print current cnt number in STDOUT.log */ |
89 |
fprintf(stdout, "%ld t_atomic2: cnt=%d\n", peak_time(), |
90 |
peak_atomic_read(&cnt)); |
91 |
fflush(stdout); |
92 |
|
93 |
/* Break task after working for 5 seconds */ |
94 |
if (peak_time() - start_time > 5) |
95 |
peak_task_break(peak_task_self()); |
96 |
} |
97 |
|
98 |
int |
99 |
main(int argc, char *argv[]) |
100 |
{ |
101 |
peak_task task = peak_task_self(); |
102 |
peak_timer ti; |
103 |
int i; |
104 |
|
105 |
/* Force several threads. */ |
106 |
int val = 8; |
107 |
if (peak_task_set_info(task, PEAK_TASK_FLAVOR_NTHREADS, &val)) |
108 |
return T_ERR_TASK_INFO; |
109 |
|
110 |
start_time = peak_time(); |
111 |
|
112 |
peak_atomic_set(&cnt, CNT_START); |
113 |
|
114 |
/* Create some repeating timers with a very low repeat delay to improve |
115 |
* threads concurrency for this test. |
116 |
*/ |
117 |
for (i = 0; i < N_TIMERS; i++) |
118 |
{ |
119 |
ti = peak_timer_create(0.5, 0.000001, |
120 |
i & 1 ? timer_dec_callback : timer_inc_callback, |
121 |
0); |
122 |
if (ti == 0) |
123 |
exit(T_ERR_TIMER_ERROR); |
124 |
|
125 |
/* Add timer to task. |
126 |
*/ |
127 |
peak_task_timer_add(task, ti); |
128 |
} |
129 |
|
130 |
/* Create monitor */ |
131 |
ti = peak_timer_create(0.5, 0.5, timer_cnt_callback, 0); |
132 |
if (ti == 0) |
133 |
exit(T_ERR_TIMER_ERROR); |
134 |
peak_task_timer_add(task, ti); |
135 |
|
136 |
/* Run task. |
137 |
*/ |
138 |
peak_task_run(task); |
139 |
|
140 |
/* Verify coherence */ |
141 |
if ((peak_atomic_read(&cnt) - CNT_START) % (MAX_ITER*AMOUNT)) |
142 |
exit(T_ERR_ATOMIC_FAILURE); |
143 |
|
144 |
return 0; |
145 |
} |
146 |
|