162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci *  sync stress test: parallelism
362306a36Sopenharmony_ci *  Copyright 2015-2016 Collabora Ltd.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci *  Based on the implementation from the Android Open Source Project,
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci *  Copyright 2012 Google, Inc
862306a36Sopenharmony_ci *
962306a36Sopenharmony_ci *  Permission is hereby granted, free of charge, to any person obtaining a
1062306a36Sopenharmony_ci *  copy of this software and associated documentation files (the "Software"),
1162306a36Sopenharmony_ci *  to deal in the Software without restriction, including without limitation
1262306a36Sopenharmony_ci *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
1362306a36Sopenharmony_ci *  and/or sell copies of the Software, and to permit persons to whom the
1462306a36Sopenharmony_ci *  Software is furnished to do so, subject to the following conditions:
1562306a36Sopenharmony_ci *
1662306a36Sopenharmony_ci *  The above copyright notice and this permission notice shall be included in
1762306a36Sopenharmony_ci *  all copies or substantial portions of the Software.
1862306a36Sopenharmony_ci *
1962306a36Sopenharmony_ci *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2062306a36Sopenharmony_ci *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2162306a36Sopenharmony_ci *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
2262306a36Sopenharmony_ci *  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
2362306a36Sopenharmony_ci *  OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
2462306a36Sopenharmony_ci *  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2562306a36Sopenharmony_ci *  OTHER DEALINGS IN THE SOFTWARE.
2662306a36Sopenharmony_ci */
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci#include <pthread.h>
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci#include "sync.h"
3162306a36Sopenharmony_ci#include "sw_sync.h"
3262306a36Sopenharmony_ci#include "synctest.h"
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_cistatic struct {
3562306a36Sopenharmony_ci	int iterations;
3662306a36Sopenharmony_ci	int timeline;
3762306a36Sopenharmony_ci	int counter;
3862306a36Sopenharmony_ci} test_data_two_threads;
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_cistatic int test_stress_two_threads_shared_timeline_thread(void *d)
4162306a36Sopenharmony_ci{
4262306a36Sopenharmony_ci	int thread_id = (long)d;
4362306a36Sopenharmony_ci	int timeline = test_data_two_threads.timeline;
4462306a36Sopenharmony_ci	int iterations = test_data_two_threads.iterations;
4562306a36Sopenharmony_ci	int fence, valid, ret, i;
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci	for (i = 0; i < iterations; i++) {
4862306a36Sopenharmony_ci		fence = sw_sync_fence_create(timeline, "fence",
4962306a36Sopenharmony_ci					     i * 2 + thread_id);
5062306a36Sopenharmony_ci		valid = sw_sync_fence_is_valid(fence);
5162306a36Sopenharmony_ci		ASSERT(valid, "Failure allocating fence\n");
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ci		/* Wait on the prior thread to complete */
5462306a36Sopenharmony_ci		ret = sync_wait(fence, -1);
5562306a36Sopenharmony_ci		ASSERT(ret > 0, "Problem occurred on prior thread\n");
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci		/*
5862306a36Sopenharmony_ci		 * Confirm the previous thread's writes are visible
5962306a36Sopenharmony_ci		 * and then increment
6062306a36Sopenharmony_ci		 */
6162306a36Sopenharmony_ci		ASSERT(test_data_two_threads.counter == i * 2 + thread_id,
6262306a36Sopenharmony_ci		       "Counter got damaged!\n");
6362306a36Sopenharmony_ci		test_data_two_threads.counter++;
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci		/* Kick off the other thread */
6662306a36Sopenharmony_ci		ret = sw_sync_timeline_inc(timeline, 1);
6762306a36Sopenharmony_ci		ASSERT(ret == 0, "Advancing timeline failed\n");
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ci		sw_sync_fence_destroy(fence);
7062306a36Sopenharmony_ci	}
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci	return 0;
7362306a36Sopenharmony_ci}
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ciint test_stress_two_threads_shared_timeline(void)
7662306a36Sopenharmony_ci{
7762306a36Sopenharmony_ci	pthread_t a, b;
7862306a36Sopenharmony_ci	int valid;
7962306a36Sopenharmony_ci	int timeline = sw_sync_timeline_create();
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ci	valid = sw_sync_timeline_is_valid(timeline);
8262306a36Sopenharmony_ci	ASSERT(valid, "Failure allocating timeline\n");
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci	test_data_two_threads.iterations = 1 << 16;
8562306a36Sopenharmony_ci	test_data_two_threads.counter = 0;
8662306a36Sopenharmony_ci	test_data_two_threads.timeline = timeline;
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci	/*
8962306a36Sopenharmony_ci	 * Use a single timeline to synchronize two threads
9062306a36Sopenharmony_ci	 * hammmering on the same counter.
9162306a36Sopenharmony_ci	 */
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ci	pthread_create(&a, NULL, (void *(*)(void *))
9462306a36Sopenharmony_ci		       test_stress_two_threads_shared_timeline_thread,
9562306a36Sopenharmony_ci		       (void *)0);
9662306a36Sopenharmony_ci	pthread_create(&b, NULL, (void *(*)(void *))
9762306a36Sopenharmony_ci		       test_stress_two_threads_shared_timeline_thread,
9862306a36Sopenharmony_ci		       (void *)1);
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ci	pthread_join(a, NULL);
10162306a36Sopenharmony_ci	pthread_join(b, NULL);
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ci	/* make sure the threads did not trample on one another */
10462306a36Sopenharmony_ci	ASSERT(test_data_two_threads.counter ==
10562306a36Sopenharmony_ci	       test_data_two_threads.iterations * 2,
10662306a36Sopenharmony_ci	       "Counter has unexpected value\n");
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ci	sw_sync_timeline_destroy(timeline);
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci	return 0;
11162306a36Sopenharmony_ci}
112