]> pd.if.org Git - liblfds/blob - liblfds/liblfds7.0.0/test/src/test_lfds700_queue_enqueuing_with_malloc_and_dequeuing_with_free.c
Initial import (all versions, including the new 7.1.0)
[liblfds] / liblfds / liblfds7.0.0 / test / src / test_lfds700_queue_enqueuing_with_malloc_and_dequeuing_with_free.c
1 /***** includes *****/
2 #include "internal.h"
3
4 /***** structs *****/
5 struct test_state
6 {
7   struct lfds700_queue_state
8     *qs;
9 };
10
11 /***** private prototypes *****/
12 static test_pal_thread_return_t TEST_PAL_CALLING_CONVENTION thread_enqueuer_with_malloc_and_dequeuer_with_free( void *util_thread_starter_thread_state );
13 static void queue_element_cleanup_callback( struct lfds700_queue_state *qs, struct lfds700_queue_element *qe, enum lfds700_misc_flag dummy_element_flag );
14
15
16
17
18
19 /****************************************************************************/
20 void test_lfds700_queue_enqueuing_with_malloc_and_dequeuing_with_free( struct lfds700_list_asu_state *list_of_logical_processors )
21 {
22   enum lfds700_misc_validity
23     dvs = LFDS700_MISC_VALIDITY_VALID;
24
25   lfds700_pal_uint_t
26     loop,
27     number_logical_processors;
28
29   struct lfds700_list_asu_element
30     *lasue;
31
32   struct lfds700_misc_prng_state
33     ps;
34
35   struct lfds700_queue_element
36     *qe;
37
38   struct lfds700_queue_state
39     qs;
40
41   struct lfds700_misc_validation_info
42     vi;
43
44   struct test_pal_logical_processor
45     *lp;
46
47   struct util_thread_starter_state
48     *tts;
49
50   struct test_state
51     *ts;
52
53   test_pal_thread_state_t
54     *thread_handles;
55
56   assert( list_of_logical_processors != NULL );
57   // TRD : qt can be any value in its range
58
59   /* TRD : one thread per logical core
60            each thread loops for ten seconds
61            mallocs and enqueues 1k elements, then dequeues and frees 1k elements
62   */
63
64   internal_display_test_name( "Enqueuing with malloc dequeuing with free (%d seconds)", TEST_DURATION_IN_SECONDS );
65
66   lfds700_list_asu_query( list_of_logical_processors, LFDS700_LIST_ASU_QUERY_GET_POTENTIALLY_INACCURATE_COUNT, NULL, (void **) &number_logical_processors );
67
68   lfds700_misc_prng_init( &ps );
69
70   qe = util_aligned_malloc( sizeof(struct lfds700_queue_element), LFDS700_PAL_ATOMIC_ISOLATION_IN_BYTES );
71
72   lfds700_queue_init_valid_on_current_logical_core( &qs, qe, &ps, NULL );
73
74   ts = util_malloc_wrapper( sizeof(struct test_state) * number_logical_processors );
75
76   for( loop = 0 ; loop < number_logical_processors ; loop++ )
77     (ts+loop)->qs = &qs;
78
79   thread_handles = util_malloc_wrapper( sizeof(test_pal_thread_state_t) * number_logical_processors );
80
81   util_thread_starter_new( &tts, number_logical_processors );
82
83   LFDS700_MISC_BARRIER_STORE;
84
85   lfds700_misc_force_store();
86
87   loop = 0;
88   lasue = NULL;
89
90   while( LFDS700_LIST_ASU_GET_START_AND_THEN_NEXT(*list_of_logical_processors, lasue) )
91   {
92     lp = LFDS700_LIST_ASU_GET_VALUE_FROM_ELEMENT( *lasue );
93     util_thread_starter_start( tts, &thread_handles[loop], loop, lp, thread_enqueuer_with_malloc_and_dequeuer_with_free, ts+loop );
94     loop++;
95   }
96
97   util_thread_starter_run( tts );
98
99   for( loop = 0 ; loop < number_logical_processors ; loop++ )
100     test_pal_thread_wait( thread_handles[loop] );
101
102   util_thread_starter_delete( tts );
103
104   free( thread_handles );
105
106   LFDS700_MISC_BARRIER_LOAD;
107
108   vi.min_elements = vi.max_elements = 0;
109
110   lfds700_queue_query( &qs, LFDS700_QUEUE_QUERY_SINGLETHREADED_VALIDATE, &vi, &dvs );
111
112   free( ts );
113
114   lfds700_queue_cleanup( &qs, queue_element_cleanup_callback );
115
116   internal_display_test_result( 1, "queue", dvs );
117
118   return;
119 }
120
121
122
123
124
125 /****************************************************************************/
126 static test_pal_thread_return_t TEST_PAL_CALLING_CONVENTION thread_enqueuer_with_malloc_and_dequeuer_with_free( void *util_thread_starter_thread_state )
127 {
128   lfds700_pal_uint_t
129     loop,
130     time_loop = 0;
131
132   struct lfds700_misc_prng_state
133     ps;
134
135   struct lfds700_queue_element
136     *qe;
137
138   struct test_state
139     *ts;
140
141   struct util_thread_starter_thread_state
142     *tsts;
143
144   time_t
145     current_time,
146     start_time;
147
148   LFDS700_MISC_MAKE_VALID_ON_CURRENT_LOGICAL_CORE_INITS_COMPLETED_BEFORE_NOW_ON_ANY_OTHER_LOGICAL_CORE;
149
150   assert( util_thread_starter_thread_state != NULL );
151
152   tsts = (struct util_thread_starter_thread_state *) util_thread_starter_thread_state;
153   ts = (struct test_state *) tsts->thread_user_state;
154
155   lfds700_misc_prng_init( &ps );
156
157   util_thread_starter_ready_and_wait( tsts );
158
159   current_time = start_time = time( NULL );
160
161   while( current_time < start_time + TEST_DURATION_IN_SECONDS )
162   {
163     for( loop = 0 ; loop < 1000 ; loop++ )
164     {
165       qe = util_aligned_malloc( sizeof(struct lfds700_queue_element), LFDS700_PAL_ATOMIC_ISOLATION_IN_BYTES );
166       lfds700_queue_enqueue( ts->qs, qe, &ps );
167     }
168
169     for( loop = 0 ; loop < 1000 ; loop++ )
170     {
171       lfds700_queue_dequeue( ts->qs, &qe, &ps );
172       util_aligned_free( qe );
173     }
174
175     if( time_loop++ == REDUCED_TIME_LOOP_COUNT )
176     {
177       time_loop = 0;
178       time( &current_time );
179     }
180   }
181
182   LFDS700_MISC_BARRIER_STORE;
183
184   lfds700_misc_force_store();
185
186   return( (test_pal_thread_return_t) EXIT_SUCCESS );
187 }
188
189
190
191
192
193 /****************************************************************************/
194 #pragma warning( disable : 4100 )
195
196 static void queue_element_cleanup_callback( struct lfds700_queue_state *qs, struct lfds700_queue_element *qe, enum lfds700_misc_flag dummy_element_flag )
197 {
198   assert( qs != NULL );
199   assert( qe != NULL );
200   // TRD : dummy_element_flag can be any value in its range
201
202   util_aligned_free( qe );
203
204   return;
205 }
206
207 #pragma warning( default : 4100 )
208