0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
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
0054 ret = sync_wait(fence, -1);
0055 ASSERT(ret > 0, "Problem occurred on prior thread\n");
0056
0057
0058
0059
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
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
0090
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
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 }