18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * sync stress test: parallelism 38c2ecf20Sopenharmony_ci * Copyright 2015-2016 Collabora Ltd. 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Based on the implementation from the Android Open Source Project, 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Copyright 2012 Google, Inc 88c2ecf20Sopenharmony_ci * 98c2ecf20Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 108c2ecf20Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 118c2ecf20Sopenharmony_ci * to deal in the Software without restriction, including without limitation 128c2ecf20Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 138c2ecf20Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 148c2ecf20Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 158c2ecf20Sopenharmony_ci * 168c2ecf20Sopenharmony_ci * The above copyright notice and this permission notice shall be included in 178c2ecf20Sopenharmony_ci * all copies or substantial portions of the Software. 188c2ecf20Sopenharmony_ci * 198c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 208c2ecf20Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 218c2ecf20Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 228c2ecf20Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 238c2ecf20Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 248c2ecf20Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 258c2ecf20Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE. 268c2ecf20Sopenharmony_ci */ 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci#include <pthread.h> 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci#include "sync.h" 318c2ecf20Sopenharmony_ci#include "sw_sync.h" 328c2ecf20Sopenharmony_ci#include "synctest.h" 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_cistatic struct { 358c2ecf20Sopenharmony_ci int iterations; 368c2ecf20Sopenharmony_ci int timeline; 378c2ecf20Sopenharmony_ci int counter; 388c2ecf20Sopenharmony_ci} test_data_two_threads; 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_cistatic int test_stress_two_threads_shared_timeline_thread(void *d) 418c2ecf20Sopenharmony_ci{ 428c2ecf20Sopenharmony_ci int thread_id = (long)d; 438c2ecf20Sopenharmony_ci int timeline = test_data_two_threads.timeline; 448c2ecf20Sopenharmony_ci int iterations = test_data_two_threads.iterations; 458c2ecf20Sopenharmony_ci int fence, valid, ret, i; 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci for (i = 0; i < iterations; i++) { 488c2ecf20Sopenharmony_ci fence = sw_sync_fence_create(timeline, "fence", 498c2ecf20Sopenharmony_ci i * 2 + thread_id); 508c2ecf20Sopenharmony_ci valid = sw_sync_fence_is_valid(fence); 518c2ecf20Sopenharmony_ci ASSERT(valid, "Failure allocating fence\n"); 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ci /* Wait on the prior thread to complete */ 548c2ecf20Sopenharmony_ci ret = sync_wait(fence, -1); 558c2ecf20Sopenharmony_ci ASSERT(ret > 0, "Problem occurred on prior thread\n"); 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci /* 588c2ecf20Sopenharmony_ci * Confirm the previous thread's writes are visible 598c2ecf20Sopenharmony_ci * and then increment 608c2ecf20Sopenharmony_ci */ 618c2ecf20Sopenharmony_ci ASSERT(test_data_two_threads.counter == i * 2 + thread_id, 628c2ecf20Sopenharmony_ci "Counter got damaged!\n"); 638c2ecf20Sopenharmony_ci test_data_two_threads.counter++; 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci /* Kick off the other thread */ 668c2ecf20Sopenharmony_ci ret = sw_sync_timeline_inc(timeline, 1); 678c2ecf20Sopenharmony_ci ASSERT(ret == 0, "Advancing timeline failed\n"); 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ci sw_sync_fence_destroy(fence); 708c2ecf20Sopenharmony_ci } 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci return 0; 738c2ecf20Sopenharmony_ci} 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ciint test_stress_two_threads_shared_timeline(void) 768c2ecf20Sopenharmony_ci{ 778c2ecf20Sopenharmony_ci pthread_t a, b; 788c2ecf20Sopenharmony_ci int valid; 798c2ecf20Sopenharmony_ci int timeline = sw_sync_timeline_create(); 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci valid = sw_sync_timeline_is_valid(timeline); 828c2ecf20Sopenharmony_ci ASSERT(valid, "Failure allocating timeline\n"); 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci test_data_two_threads.iterations = 1 << 16; 858c2ecf20Sopenharmony_ci test_data_two_threads.counter = 0; 868c2ecf20Sopenharmony_ci test_data_two_threads.timeline = timeline; 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci /* 898c2ecf20Sopenharmony_ci * Use a single timeline to synchronize two threads 908c2ecf20Sopenharmony_ci * hammmering on the same counter. 918c2ecf20Sopenharmony_ci */ 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ci pthread_create(&a, NULL, (void *(*)(void *)) 948c2ecf20Sopenharmony_ci test_stress_two_threads_shared_timeline_thread, 958c2ecf20Sopenharmony_ci (void *)0); 968c2ecf20Sopenharmony_ci pthread_create(&b, NULL, (void *(*)(void *)) 978c2ecf20Sopenharmony_ci test_stress_two_threads_shared_timeline_thread, 988c2ecf20Sopenharmony_ci (void *)1); 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_ci pthread_join(a, NULL); 1018c2ecf20Sopenharmony_ci pthread_join(b, NULL); 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci /* make sure the threads did not trample on one another */ 1048c2ecf20Sopenharmony_ci ASSERT(test_data_two_threads.counter == 1058c2ecf20Sopenharmony_ci test_data_two_threads.iterations * 2, 1068c2ecf20Sopenharmony_ci "Counter has unexpected value\n"); 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ci sw_sync_timeline_destroy(timeline); 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_ci return 0; 1118c2ecf20Sopenharmony_ci} 112