]> pd.if.org Git - liblfds/blob - liblfds/liblfds7.0.0/test/src/util_thread_starter.c
Initial import (all versions, including the new 7.1.0)
[liblfds] / liblfds / liblfds7.0.0 / test / src / util_thread_starter.c
1 /***** includes *****/
2 #include "internal.h"
3
4
5
6
7
8 /****************************************************************************/
9 void util_thread_starter_new( struct util_thread_starter_state **tts, lfds700_pal_uint_t number_threads )
10 {
11   lfds700_pal_uint_t
12     loop;
13
14   assert( tts != NULL );
15   // TRD : number_threads cam be any value in its range
16
17   *tts = util_malloc_wrapper( sizeof(struct util_thread_starter_state) );
18
19   (*tts)->tsts = util_malloc_wrapper( sizeof(struct util_thread_starter_thread_state) * number_threads );
20   (*tts)->thread_start_flag = LOWERED;
21   (*tts)->number_thread_states = number_threads;
22
23   for( loop = 0 ; loop < number_threads ; loop++ )
24   {
25     ((*tts)->tsts+loop)->thread_ready_flag = LOWERED;
26     ((*tts)->tsts+loop)->thread_start_flag = &(*tts)->thread_start_flag;
27   }
28
29   LFDS700_MISC_BARRIER_STORE;
30
31   lfds700_misc_force_store();
32
33   return;
34 }
35
36
37
38
39
40 /****************************************************************************/
41 void util_thread_starter_start( struct util_thread_starter_state *tts,
42                                      test_pal_thread_state_t *thread_state,
43                                      lfds700_pal_uint_t thread_number,
44                                      struct test_pal_logical_processor *lp,
45                                      test_pal_thread_return_t (TEST_PAL_CALLING_CONVENTION *thread_function)( void *thread_user_state ),
46                                      void *thread_user_state )
47 {
48   assert( tts != NULL );
49   assert( thread_state != NULL );
50   assert( lp != NULL );
51   assert( thread_function != NULL );
52   // TRD : thread_user_state can be NULL
53
54   (tts->tsts+thread_number)->thread_user_state = thread_user_state;
55
56   util_thread_start_wrapper( thread_state, lp, thread_function, tts->tsts+thread_number );
57
58   // TRD : wait for the thread to indicate it is ready and waiting
59   while( (tts->tsts+thread_number)->thread_ready_flag == LOWERED );
60
61   return;
62 }
63
64
65
66
67
68 /****************************************************************************/
69 void util_thread_starter_ready_and_wait( struct util_thread_starter_thread_state *tsts )
70 {
71   assert( tsts != NULL );
72
73   tsts->thread_ready_flag = RAISED;
74
75   LFDS700_MISC_BARRIER_FULL;
76
77   // TRD : threads here are all looping, so we don't need to force a store
78
79   while( *tsts->thread_start_flag == LOWERED )
80     LFDS700_MISC_BARRIER_LOAD;
81
82   return;
83 }
84
85
86
87
88
89 /****************************************************************************/
90 void util_thread_starter_run( struct util_thread_starter_state *tts )
91 {
92   assert( tts != NULL );
93
94   /* TRD : all threads at this point are ready to go
95            as we wait for their ready flag immediately after their spawn
96   */
97
98   tts->thread_start_flag = RAISED;
99
100   LFDS700_MISC_BARRIER_STORE;
101
102   lfds700_misc_force_store();
103
104   return;
105 }
106
107
108
109
110
111 /****************************************************************************/
112 void util_thread_starter_delete( struct util_thread_starter_state *tts )
113 {
114   assert( tts != NULL );
115
116   free( tts->tsts );
117
118   free( tts );
119
120   return;
121 }
122
123
124
125
126
127 /****************************************************************************/
128 void util_thread_start_wrapper( test_pal_thread_state_t *thread_state,
129                                      struct test_pal_logical_processor *lp,
130                                      test_pal_thread_return_t (TEST_PAL_CALLING_CONVENTION *thread_function)(void *thread_user_state),
131                                      void *thread_user_state )
132 {
133   int
134     rv;
135
136   assert( thread_state != NULL );
137   assert( lp != NULL );
138   assert( thread_function != NULL );
139   // TRD : thread_user_state can be NULL
140
141   rv = test_pal_thread_start( thread_state, lp, thread_function, thread_user_state );
142
143   if( rv == 0 )
144   {
145     puts( "test_pal_thread_start() failed." );
146     exit( EXIT_FAILURE );
147   }
148
149   return;
150 }
151