File: thread_test.c

package info (click to toggle)
globus-common 18.15-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,684 kB
  • sloc: ansic: 36,506; sh: 4,534; makefile: 354; perl: 151
file content (428 lines) | stat: -rw-r--r-- 11,356 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*
 * Copyright 1999-2006 University of Chicago
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @file thread_test.c
 * @brief Globus Thread Test
 */

#include "globus_common.h"
#include "globus_test_tap.h"

#include <stdio.h>

#include "globus_preload.h"

#define DEBUG_LEVEL 1

typedef struct
{
    globus_mutex_t              mutex;
    globus_cond_t               cond;
    globus_bool_t                   done;
}
prod_data_t;


int                     nproducers;
int                     nconsumers;
int                     nliveproducers;
int                     nliveconsumers;

globus_fifo_t                   queue;
globus_mutex_t                  queue_mutex;
globus_cond_t                   queue_cond;

globus_mutex_t                  common_mutex;
globus_cond_t                   common_cond;

/******************************************************************************
               Module specific functions
******************************************************************************/
long
thread_id_assign()
{
    static int                  cnt = 0;
    long                        thread_id;

    globus_mutex_lock(&common_mutex);
    {
        thread_id = cnt++;
    }
    globus_mutex_unlock(&common_mutex);
    return thread_id;
}

void
wait_for_all(long thread_id)
{
    static int                  cnt_arrived = 0;
    static int                  cnt_leaving = 0;

    globus_mutex_lock(&common_mutex);
    {
        while(cnt_leaving > 0)
        {
            if (DEBUG_LEVEL > 1)
            {
                printf("%04ld: wait_for_all() - waiting (next)\n",
                       thread_id);
            }

            globus_cond_wait(&common_cond, &common_mutex);
        }

        cnt_arrived++;

        if (cnt_arrived  < nproducers + nconsumers + 1)
        {
            if (DEBUG_LEVEL > 1)
            {
                printf("%04ld: wait_for_all() - waiting (current)\n",
                       thread_id);
            }
            
            do
            {
                globus_cond_wait(&common_cond, &common_mutex);
            }
            while(cnt_arrived % (nproducers + nconsumers + 1) != 0);

            cnt_leaving--;

            if (cnt_leaving == 0)
            {
                cnt_arrived = 0;
                
                globus_cond_broadcast(&common_cond);
                
                if (DEBUG_LEVEL > 1)
                {
                    printf("%04ld: wait_for_all() - signalling (next)\n",
                           thread_id);
                }
            }
        }
        else
        {
            if (DEBUG_LEVEL > 1)
            {
                printf("%04ld: wait_for_all() - signalling (current)\n",
                       thread_id);
            }

            cnt_leaving = nproducers + nconsumers;
            
            globus_cond_broadcast(&common_cond);
        }
    }
    globus_mutex_unlock(&common_mutex);

    if (DEBUG_LEVEL > 1)
    {
        printf("%04ld: wait_for_all() - exiting\n", thread_id);
    }
}

void *
producer(
    void *                  nitems_arg)
{
    long                        i;
    long                        nitems;
    prod_data_t                 data;
    long                        thread_id;

    nitems = (intptr_t) nitems_arg;

    globus_mutex_init(&data.mutex, (globus_mutexattr_t *) GLOBUS_NULL);
    globus_cond_init(&data.cond, (globus_condattr_t *) GLOBUS_NULL);
    data.done = GLOBUS_FALSE;

    thread_id = thread_id_assign();
    
    wait_for_all(thread_id);
    
    for (i = 0; i < nitems ; i++)
    {

        globus_mutex_lock(&queue_mutex);
        {
            globus_fifo_enqueue(&queue, &data);
            globus_cond_signal(&queue_cond);
        }
        globus_mutex_unlock(&queue_mutex);

        
        globus_mutex_lock(&data.mutex);
        {
            while(data.done == GLOBUS_FALSE)
            {
                if (DEBUG_LEVEL > 1)
                {
                    printf("%04ld: producer() - "
                           "waiting for confirmation %ld\n",
                           thread_id,
                           i);
                }

                globus_cond_wait(&data.cond, &data.mutex);
            }
            data.done = GLOBUS_FALSE;
        }
        globus_mutex_unlock(&data.mutex);
    }

    globus_cond_destroy(&data.cond);
    globus_mutex_destroy(&data.mutex);

    wait_for_all(thread_id);

    globus_mutex_lock(&common_mutex);
    nliveproducers--;
    if (nliveproducers == 0 && nliveconsumers == 0)
    {
        globus_cond_broadcast(&common_cond);
    }
    globus_mutex_unlock(&common_mutex);

    return NULL;
}

void *
consumer(
    void *                  nitems_arg)
{
    int                     i;
    long                        nitems;
    prod_data_t *               data;
    long                        thread_id;
    
    nitems = (intptr_t) nitems_arg;

    thread_id = thread_id_assign();
    
    wait_for_all(thread_id);
    
    for (i = 0; i < nitems ; i++)
    {
        globus_mutex_lock(&queue_mutex);
        {
            while(globus_fifo_empty(&queue))
            {
                if (DEBUG_LEVEL > 1)
                {
                    printf("%04ld: consumer() - waiting for data item %d\n",
                           thread_id,
                           i);
                }
            
                globus_cond_wait(&queue_cond, &queue_mutex);
            }
            data = globus_fifo_dequeue(&queue);
        }
        globus_mutex_unlock(&queue_mutex);
        
        globus_mutex_lock(&data->mutex);
        {
            data->done = GLOBUS_TRUE;

            globus_cond_broadcast(&data->cond);
        }
        globus_mutex_unlock(&data->mutex);
    }

    wait_for_all(thread_id);

    globus_mutex_lock(&common_mutex);
    nliveconsumers--;
    if (nliveproducers == 0 && nliveconsumers == 0)
    {
        globus_cond_broadcast(&common_cond);
    }
    globus_mutex_unlock(&common_mutex);
    
    return NULL;
}

/**
 * @brief Thread Consumer / Producer Test
 * @test
 * The thread_test creates a p producer threads and c consumer threads,
 * and then has the producers (combined) create n pieces of data to be
 * consumed by the consumer threads.
 *
 * This test exercises globus_mutex_lock(), globus_mutex_unlock(),
 * globus_cond_wait(), globus_cond_signal(), and globus_cond_broadcast()
 */
int
thread_test(int np, int nc, int ni)
{
    int                     i;
    int                     nitems;
    long                    thread_id;
    globus_thread_t                             thread;

    /*
     * Assign a thread id to the main thread so that it's output is uniquely
     * tagged.  Note: we do not use the return value of globus_thread_self()
     * since it could be a pointer or a structure, the latter which is
     * extremely hard to print without knowing the implementation details.
     */
    printf(" thread test begin\n");

    globus_module_activate(GLOBUS_COMMON_MODULE);

    nliveproducers = nproducers = np;
    nliveconsumers = nconsumers = nc;
    nitems = ni;

    /*
     * Initialize queue and queue concurrency control structures
     */
    globus_fifo_init(&queue);
    globus_mutex_init(&queue_mutex, (globus_mutexattr_t *) GLOBUS_NULL);
    globus_cond_init(&queue_cond, (globus_condattr_t *) GLOBUS_NULL);

    /*
     * Initialize shared (common) concurrency control structures
     */
    globus_mutex_init(&common_mutex, (globus_mutexattr_t *) GLOBUS_NULL);
    globus_cond_init(&common_cond, (globus_condattr_t *) GLOBUS_NULL);

    thread_id = thread_id_assign();

    /*
     * Start producer and consumer threads
     */
    printf(" %04ld: starting %d producer and %d consumer threads\n",
            thread_id,
            nproducers,
            nconsumers);
    
    for (i = 0 ; i < nproducers ; i ++)
    {
        int                     rc;
        intptr_t                nitems_per_thread;

        nitems_per_thread = nitems / nproducers +
            ((i < nitems % nproducers) ? 1 : 0);
        
        rc =
            globus_thread_create(
            &thread,
            NULL,
            producer,
            (void *) nitems_per_thread);
        
        if (rc != 0)
        {
            fprintf(stderr, "%04ld: main() - ERROR: "
                    "unable to create producer thread %d\n",
                    thread_id,
                    i);
            exit(1);
        }
    }
    
    for (i = 0 ; i < nconsumers ; i ++)
    {
        int                     rc;
        intptr_t                nitems_per_thread;

        nitems_per_thread = nitems / nconsumers +
            ((i < nitems % nconsumers) ? 1 : 0);
        
        rc =
            globus_thread_create(
            &thread,
            NULL,
            consumer,
            (void *) nitems_per_thread);

        if (rc != 0)
        {
            fprintf(stderr, " %04ld: main() - ERROR: "
                    "unable to create consumer thread %d\n",
                    thread_id,
                    i);
        }
    }

    /*
     * Wait for all threads to be started
     */
    wait_for_all(thread_id);

    printf(" %04ld: main() - all threads started\n",
            thread_id);
    
    /*
     * Wait for all threads to complete their work
     */
    wait_for_all(thread_id);

    printf(" %04ld: main() - all threads have completed their work\n",
            thread_id);

    globus_mutex_lock(&common_mutex);
    while (nliveconsumers > 0 || nliveproducers > 0)
    {
        globus_cond_wait(&common_cond, &common_mutex);
    }
    printf(" %04ld: main() - all threads terminated\n", thread_id);
    globus_mutex_unlock(&common_mutex);

    globus_cond_destroy(&common_cond);
    globus_mutex_destroy(&common_mutex);
    
    globus_cond_destroy(&queue_cond);
    globus_mutex_destroy(&queue_mutex);
    globus_fifo_destroy(&queue);
    
    globus_module_deactivate(GLOBUS_COMMON_MODULE);

    return 0;
}

int
main(
    int                     argc,
    char *                  argv[])
{
    const char * thread_model = THREAD_MODEL;
    globus_bool_t no_threads = GLOBUS_FALSE;

    LTDL_SET_PRELOADED_SYMBOLS();
    globus_thread_set_model(thread_model);

    setvbuf(stdout, NULL, _IONBF, 0);
    setvbuf(stderr, NULL, _IONBF, 0);

    printf("1..4\n");

    no_threads = (thread_model == NULL || strcmp(thread_model, "none") == 0);
    
    skip(no_threads,
        ok(thread_test(10, 5, 10) == 0, "10_producers_5_consumers_10_items"));
    skip(no_threads, 
        ok(thread_test(10, 5, 100) == 0, "10_producers_5_consumers_100_items"));
    skip(no_threads,
        ok(thread_test(5, 10, 10) == 0, "5_producers_10_consumers_10_items"));
    skip(no_threads, 
        ok(thread_test(5, 10, 100) == 0, "5_producers_10_consumers_100_items"));
    return TEST_EXIT_CODE;
}