ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/vendor/libpeak-0.1.2/tests/t_atomic1.c
Revision: 3251
Committed: Wed Apr 2 16:58:30 2014 UTC (11 years, 4 months ago) by michael
Content type: text/x-csrc
File size: 4086 byte(s)
Log Message:
- Imported libpeak-0.1.2

File Contents

# User Rev Content
1 michael 3251 /* 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_atomic1.c,v 1.1 2004/01/09 20:19:48 mbuna Exp $"
31    
32     /* t_atomic1.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 the static initialization, peak_atomic_inc(), peak_atomic_dec()
39     * and peak_atomic_read(). See t_atomic2.c for more tests.
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 100000
63     #define N_TIMERS 60
64    
65     static peak_atomic_t cnt = PEAK_ATOMIC_INIT(CNT_START);
66     static time_t start_time;
67    
68     static void
69     timer_inc_callback(peak_timer ti, void *context)
70     {
71     int i;
72     for (i = MAX_ITER; i-- > 0; )
73     peak_atomic_inc(&cnt);
74     }
75    
76     static void
77     timer_dec_callback(peak_timer ti, void *context)
78     {
79     int i;
80     for (i = MAX_ITER; i-- > 0; )
81     peak_atomic_dec(&cnt);
82     }
83    
84     static void
85     timer_cnt_callback(peak_timer ti, void *context)
86     {
87     /* Print current cnt number in STDOUT.log */
88     fprintf(stdout, "%ld t_atomic1: cnt=%d\n", peak_time(),
89     peak_atomic_read(&cnt));
90     fflush(stdout);
91    
92     /* Break task after working for 5 seconds */
93     if (peak_time() - start_time > 5)
94     peak_task_break(peak_task_self());
95     }
96    
97     int
98     main(int argc, char *argv[])
99     {
100     peak_task task = peak_task_self();
101     peak_timer ti;
102     int i;
103    
104     /* Force several threads. */
105     int val = 8;
106     if (peak_task_set_info(task, PEAK_TASK_FLAVOR_NTHREADS, &val))
107     return T_ERR_TASK_INFO;
108    
109     start_time = peak_time();
110    
111     /* Create some repeating timers with a very low repeat delay to improve
112     * threads concurrency for this test.
113     */
114     for (i = 0; i < N_TIMERS; i++)
115     {
116     ti = peak_timer_create(0.5, 0.000001,
117     i & 1 ? timer_dec_callback : timer_inc_callback,
118     0);
119     if (ti == 0)
120     exit(T_ERR_TIMER_ERROR);
121    
122     /* Add timer to task.
123     */
124     peak_task_timer_add(task, ti);
125     }
126    
127     /* Create monitor */
128     ti = peak_timer_create(0.5, 0.5, timer_cnt_callback, 0);
129     if (ti == 0)
130     exit(T_ERR_TIMER_ERROR);
131     peak_task_timer_add(task, ti);
132    
133     /* Run task.
134     */
135     peak_task_run(task);
136    
137     /* Verify coherence */
138     if ((peak_atomic_read(&cnt) - CNT_START) % MAX_ITER)
139     exit(T_ERR_ATOMIC_FAILURE);
140    
141     return 0;
142     }
143