Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  *  sync stress test: parallelism
0003  *  Copyright 2015-2016 Collabora Ltd.
0004  *
0005  *  Based on the implementation from the Android Open Source Project,
0006  *
0007  *  Copyright 2012 Google, Inc
0008  *
0009  *  Permission is hereby granted, free of charge, to any person obtaining a
0010  *  copy of this software and associated documentation files (the "Software"),
0011  *  to deal in the Software without restriction, including without limitation
0012  *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
0013  *  and/or sell copies of the Software, and to permit persons to whom the
0014  *  Software is furnished to do so, subject to the following conditions:
0015  *
0016  *  The above copyright notice and this permission notice shall be included in
0017  *  all copies or substantial portions of the Software.
0018  *
0019  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0020  *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0021  *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
0022  *  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
0023  *  OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
0024  *  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
0025  *  OTHER DEALINGS IN THE SOFTWARE.
0026  */
0027 
0028 #include <pthread.h>
0029 
0030 #include "sync.h"
0031 #include "sw_sync.h"
0032 #include "synctest.h"
0033 
0034 static struct {
0035     int iterations;
0036     int timeline;
0037     int counter;
0038 } test_data_two_threads;
0039 
0040 static int test_stress_two_threads_shared_timeline_thread(void *d)
0041 {
0042     int thread_id = (long)d;
0043     int timeline = test_data_two_threads.timeline;
0044     int iterations = test_data_two_threads.iterations;
0045     int fence, valid, ret, i;
0046 
0047     for (i = 0; i < iterations; i++) {
0048         fence = sw_sync_fence_create(timeline, "fence",
0049                          i * 2 + thread_id);
0050         valid = sw_sync_fence_is_valid(fence);
0051         ASSERT(valid, "Failure allocating fence\n");
0052 
0053         /* Wait on the prior thread to complete */
0054         ret = sync_wait(fence, -1);
0055         ASSERT(ret > 0, "Problem occurred on prior thread\n");
0056 
0057         /*
0058          * Confirm the previous thread's writes are visible
0059          * and then increment
0060          */
0061         ASSERT(test_data_two_threads.counter == i * 2 + thread_id,
0062                "Counter got damaged!\n");
0063         test_data_two_threads.counter++;
0064 
0065         /* Kick off the other thread */
0066         ret = sw_sync_timeline_inc(timeline, 1);
0067         ASSERT(ret == 0, "Advancing timeline failed\n");
0068 
0069         sw_sync_fence_destroy(fence);
0070     }
0071 
0072     return 0;
0073 }
0074 
0075 int test_stress_two_threads_shared_timeline(void)
0076 {
0077     pthread_t a, b;
0078     int valid;
0079     int timeline = sw_sync_timeline_create();
0080 
0081     valid = sw_sync_timeline_is_valid(timeline);
0082     ASSERT(valid, "Failure allocating timeline\n");
0083 
0084     test_data_two_threads.iterations = 1 << 16;
0085     test_data_two_threads.counter = 0;
0086     test_data_two_threads.timeline = timeline;
0087 
0088     /*
0089      * Use a single timeline to synchronize two threads
0090      * hammmering on the same counter.
0091      */
0092 
0093     pthread_create(&a, NULL, (void *(*)(void *))
0094                test_stress_two_threads_shared_timeline_thread,
0095                (void *)0);
0096     pthread_create(&b, NULL, (void *(*)(void *))
0097                test_stress_two_threads_shared_timeline_thread,
0098                (void *)1);
0099 
0100     pthread_join(a, NULL);
0101     pthread_join(b, NULL);
0102 
0103     /* make sure the threads did not trample on one another */
0104     ASSERT(test_data_two_threads.counter ==
0105            test_data_two_threads.iterations * 2,
0106            "Counter has unexpected value\n");
0107 
0108     sw_sync_timeline_destroy(timeline);
0109 
0110     return 0;
0111 }