]> pd.if.org Git - liblfds/blob - liblfds/liblfds6.0.0/test/src/test_stack.c
Initial import (all versions, including the new 7.1.0)
[liblfds] / liblfds / liblfds6.0.0 / test / src / test_stack.c
1 #include "internal.h"
2
3
4
5
6
7 /****************************************************************************/
8 void test_lfds600_stack( void )
9 {
10   unsigned int
11     loop,
12     cpu_count;
13
14   struct lfds600_stack_state
15     *ss;
16
17   thread_state_t
18     *thread_handles;
19
20   /* TRD : there are 5 tests
21
22            1. single reader thread per CPU
23               - stack always empty
24            2. single writer thread per CPU
25               - stack always full
26            3. one reader and one writer thread per CPU
27               - stack balanced
28            4. one reader and two writer threads per CPU
29               - stack grows
30            5. two reader and one writer thread per CPU
31               - stack tends to empty
32   */
33
34   cpu_count = abstraction_cpu_count();
35
36   printf( "\n"
37           "Stack Test\n"
38           "==========\n" );
39
40   // TRD : 1. single reader thread per CPU
41
42   printf( "\n"
43           "1. single reader thread per CPU\n"
44           "===============================\n" );
45
46   lfds600_stack_new( &ss, 10000 );
47
48   thread_handles = (thread_state_t *) malloc( sizeof(thread_state_t) * cpu_count * 1 );
49
50   for( loop = 0 ; loop < cpu_count ; loop++ )
51     abstraction_thread_start( &thread_handles[loop], loop, lfds600_stack_internal_thread_reader, ss );
52
53   for( loop = 0 ; loop < cpu_count ; loop++ )
54     abstraction_thread_wait( thread_handles[loop] );
55
56   lfds600_stack_delete( ss, NULL, NULL );
57
58   free( thread_handles );
59
60   // TRD : 2. single writer thread per CPU
61
62   printf( "\n"
63           "2. single writer thread per CPU\n"
64           "===============================\n" );
65
66   lfds600_stack_new( &ss, 10000 );
67
68   thread_handles = (thread_state_t *) malloc( sizeof(thread_state_t) * cpu_count * 1 );
69
70   for( loop = 0 ; loop < cpu_count ; loop++ )
71     abstraction_thread_start( &thread_handles[loop], loop, lfds600_stack_internal_thread_writer, ss );
72
73   for( loop = 0 ; loop < cpu_count ; loop++ )
74     abstraction_thread_wait( thread_handles[loop] );
75
76   lfds600_stack_delete( ss, NULL, NULL );
77
78   free( thread_handles );
79
80   // TRD : 3. one reader and one writer thread per CPU
81
82   printf( "\n"
83           "3. one reader and one writer thread per CPU\n"
84           "===========================================\n" );
85
86   lfds600_stack_new( &ss, 10000 );
87
88   thread_handles = (thread_state_t *) malloc( sizeof(thread_state_t) * cpu_count * 2 );
89
90   for( loop = 0 ; loop < cpu_count ; loop++ )
91   {
92     abstraction_thread_start( &thread_handles[loop], loop, lfds600_stack_internal_thread_reader, ss );
93     abstraction_thread_start( &thread_handles[loop+cpu_count], loop, lfds600_stack_internal_thread_writer, ss );
94   }
95
96   for( loop = 0 ; loop < cpu_count * 2 ; loop++ )
97     abstraction_thread_wait( thread_handles[loop] );
98
99   lfds600_stack_delete( ss, NULL, NULL );
100
101   free( thread_handles );
102
103   // TRD : 4. one reader and two writer threads per CPU
104
105   printf( "\n"
106           "4. one reader and two writer threads per CPU\n"
107           "============================================\n" );
108
109   lfds600_stack_new( &ss, 10000 );
110
111   thread_handles = (thread_state_t *) malloc( sizeof(thread_state_t) * cpu_count * 3 );
112
113   for( loop = 0 ; loop < cpu_count ; loop++ )
114   {
115     abstraction_thread_start( &thread_handles[loop], loop, lfds600_stack_internal_thread_reader, ss );
116     abstraction_thread_start( &thread_handles[loop+cpu_count], loop, lfds600_stack_internal_thread_writer, ss );
117     abstraction_thread_start( &thread_handles[loop+cpu_count*2], loop, lfds600_stack_internal_thread_writer, ss );
118   }
119
120   for( loop = 0 ; loop < cpu_count * 3 ; loop++ )
121     abstraction_thread_wait( thread_handles[loop] );
122
123   lfds600_stack_delete( ss, NULL, NULL );
124
125   free( thread_handles );
126
127   // TRD : 5. two reader and one writer thread per CPU
128
129   printf( "\n"
130           "5. two reader and one writer thread per CPU\n"
131           "===========================================\n" );
132
133   lfds600_stack_new( &ss, 10000 );
134
135   thread_handles = (thread_state_t *) malloc( sizeof(thread_state_t) * cpu_count * 3 );
136
137   for( loop = 0 ; loop < cpu_count ; loop++ )
138   {
139     abstraction_thread_start( &thread_handles[loop], loop, lfds600_stack_internal_thread_reader, ss );
140     abstraction_thread_start( &thread_handles[loop+cpu_count], loop, lfds600_stack_internal_thread_reader, ss );
141     abstraction_thread_start( &thread_handles[loop+cpu_count*2], loop, lfds600_stack_internal_thread_writer, ss );
142   }
143
144   for( loop = 0 ; loop < cpu_count * 3 ; loop++ )
145     abstraction_thread_wait( thread_handles[loop] );
146
147   lfds600_stack_delete( ss, NULL, NULL );
148
149   free( thread_handles );
150
151   return;
152 }
153
154
155
156
157
158 /****************************************************************************/
159 thread_return_t CALLING_CONVENTION lfds600_stack_internal_thread_reader( void *lfds600_stack_state )
160 {
161   struct lfds600_stack_state
162     *ss;
163
164   void
165     *user_data;
166
167   time_t
168     start_time;
169
170   unsigned long long int
171     count = 0;
172
173   assert( lfds600_stack_state != NULL );
174
175   ss = (struct lfds600_stack_state *) lfds600_stack_state;
176
177   time( &start_time );
178
179   while( time(NULL) < start_time + 10 )
180   {
181     if( lfds600_stack_pop(ss, &user_data) )
182       count++;
183   }
184
185   printf( "read count = %llu\n", count );
186
187   return( (thread_return_t) EXIT_SUCCESS );
188 }
189
190
191
192
193
194 /****************************************************************************/
195 thread_return_t CALLING_CONVENTION lfds600_stack_internal_thread_writer( void *lfds600_stack_state )
196 {
197   struct lfds600_stack_state
198     *ss;
199
200   time_t
201     start_time;
202
203   unsigned long long int
204     count = 0;
205
206   assert( lfds600_stack_state != NULL );
207
208   ss = (struct lfds600_stack_state *) lfds600_stack_state;
209
210   time( &start_time );
211
212   while( time(NULL) < start_time + 10 )
213   {
214     // TRD : we don't store any user data
215     if( lfds600_stack_push(ss, NULL) )
216       count++;
217   }
218
219   printf( "write count = %llu\n", count );
220
221   return( (thread_return_t) EXIT_SUCCESS );
222 }
223