testthreadmutex.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. * Copyright (c) 2018-2023 SignalWire, Inc
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. #include "libks/ks.h"
  23. #include "tap.h"
  24. #define MAX_STUFF 200
  25. static ks_thread_t *threads[MAX_STUFF];
  26. static ks_thread_t *thread_p;
  27. static ks_pool_t *pool;
  28. static ks_mutex_t *mutex;
  29. static ks_mutex_t *mutex_non_recursive;
  30. static ks_rwl_t *rwlock;
  31. static ks_cond_t *cond;
  32. static int counter1 = 0;
  33. static int counter2 = 0;
  34. static int counter3 = 0;
  35. static int counter4 = 0;
  36. static int counter5 = 0;
  37. static int counter6 = 0;
  38. static int threadscount = 0;
  39. static int cpu_count = 0;
  40. #define LOOP_COUNT 10000
  41. static void *thread_priority(ks_thread_t *thread, void *data)
  42. {
  43. while (!ks_thread_stop_requested(thread)) {
  44. ks_sleep(100000);
  45. }
  46. return NULL;
  47. }
  48. static void *thread_test_cond_producer_func(ks_thread_t *thread, void *data)
  49. {
  50. for (;;) {
  51. ks_cond_lock(cond);
  52. if (counter5 >= LOOP_COUNT) {
  53. ks_cond_unlock(cond);
  54. break;
  55. }
  56. counter5++;
  57. if (counter6 == 0) {
  58. ks_cond_signal(cond);
  59. }
  60. counter6++;
  61. ks_cond_unlock(cond);
  62. *((int *) data) += 1;
  63. }
  64. return NULL;
  65. }
  66. static void *thread_test_cond_consumer_func(ks_thread_t *thread, void *data)
  67. {
  68. int i;
  69. for (i = 0; i < LOOP_COUNT; i++) {
  70. ks_cond_lock(cond);
  71. while (counter6 == 0) {
  72. ks_cond_wait(cond);
  73. }
  74. counter6--;
  75. ks_cond_unlock(cond);
  76. }
  77. return NULL;
  78. }
  79. static void check_cond(void)
  80. {
  81. int count[MAX_STUFF] = { 0 };
  82. ks_thread_t * threads[MAX_STUFF] = { 0 };
  83. int ttl = 0;
  84. ok( (ks_pool_open(&pool) == KS_STATUS_SUCCESS) );
  85. ok( (ks_cond_create(&cond, pool) == KS_STATUS_SUCCESS) );
  86. ks_log(KS_LOG_DEBUG, "Allocated condition");
  87. int i;
  88. for(i = 0; i < cpu_count; i++) {
  89. ok( (ks_thread_create(&threads[i], thread_test_cond_producer_func, &count[i], pool) == KS_STATUS_SUCCESS) );
  90. }
  91. ok( (ks_thread_create(&thread_p, thread_test_cond_consumer_func, NULL, pool) == KS_STATUS_SUCCESS) );
  92. for(i = 0; i < cpu_count; i++) {
  93. ks_thread_join(threads[i]);
  94. ttl += count[i];
  95. }
  96. ok( (ttl == LOOP_COUNT) );
  97. ks_thread_join(thread_p);
  98. ok( (ks_pool_close(&pool) == KS_STATUS_SUCCESS) );
  99. }
  100. static void *thread_test_rwlock_func(ks_thread_t *thread, void *data)
  101. {
  102. int loop = 1;
  103. while (1) {
  104. ks_rwl_read_lock(rwlock);
  105. if (counter4 == LOOP_COUNT) {
  106. loop = 0;
  107. }
  108. ks_rwl_read_unlock(rwlock);
  109. if (!loop) {
  110. break;
  111. }
  112. ks_rwl_write_lock(rwlock);
  113. if (counter4 != LOOP_COUNT) {
  114. counter4++;
  115. }
  116. ks_rwl_write_unlock(rwlock);
  117. }
  118. return NULL;
  119. }
  120. static void check_rwl(void)
  121. {
  122. ks_status_t status;
  123. ok( (ks_pool_open(&pool) == KS_STATUS_SUCCESS) );
  124. ok( (ks_rwl_create(&rwlock, pool) == KS_STATUS_SUCCESS) );
  125. ks_rwl_read_lock(rwlock);
  126. status = ks_rwl_try_read_lock(rwlock);
  127. ok( status == KS_STATUS_SUCCESS );
  128. if ( status == KS_STATUS_SUCCESS ) {
  129. ks_rwl_read_unlock(rwlock);
  130. }
  131. ks_rwl_read_unlock(rwlock);
  132. int i;
  133. for(i = 0; i < cpu_count; i++) {
  134. ok( (ks_thread_create(&threads[i], thread_test_rwlock_func, NULL, pool) == KS_STATUS_SUCCESS) );
  135. }
  136. for(i = 0; i < cpu_count; i++) {
  137. ks_thread_join(threads[i]);
  138. }
  139. ok( (ks_pool_close(&pool) == KS_STATUS_SUCCESS) );
  140. ok( (counter4 == LOOP_COUNT) );
  141. }
  142. static void *thread_test_function_cleanup(ks_thread_t *thread, void *data)
  143. {
  144. int d = (int)(intptr_t)data;
  145. if ( d == 1 ) {
  146. ks_mutex_lock(mutex);
  147. counter3++;
  148. ks_mutex_unlock(mutex);
  149. }
  150. return NULL;
  151. }
  152. static void *thread_test_function_detatched(ks_thread_t *thread, void *data)
  153. {
  154. int i;
  155. int d = (int)(intptr_t)data;
  156. for (i = 0; i < LOOP_COUNT; i++) {
  157. ks_mutex_lock(mutex);
  158. if (d == 1) {
  159. counter2++;
  160. }
  161. ks_mutex_unlock(mutex);
  162. }
  163. ks_mutex_lock(mutex);
  164. threadscount++;
  165. ks_mutex_unlock(mutex);
  166. return NULL;
  167. }
  168. static void *thread_test_function_attached(ks_thread_t *thread, void *data)
  169. {
  170. int i;
  171. int d = (int)(intptr_t)data;
  172. void *mem, *last_mem = NULL;
  173. for (i = 0; i < LOOP_COUNT; i++) {
  174. if (last_mem) {
  175. free(last_mem);
  176. }
  177. mem = calloc(1, 1024);
  178. last_mem = mem;
  179. }
  180. if (last_mem)
  181. free(last_mem);
  182. for (i = 0; i < LOOP_COUNT; i++) {
  183. ks_mutex_lock(mutex);
  184. if (d == 1) {
  185. counter1++;
  186. }
  187. ks_mutex_unlock(mutex);
  188. }
  189. return NULL;
  190. }
  191. static void create_threads_cleanup(void)
  192. {
  193. void *d = (void *)(intptr_t)1;
  194. int i;
  195. for(i = 0; i < cpu_count; i++) {
  196. ok( (ks_thread_create(&threads[i], thread_test_function_cleanup, d, pool) == KS_STATUS_SUCCESS) );
  197. }
  198. for(i = 0; i < cpu_count; i++) {
  199. ks_thread_join(threads[i]);
  200. }
  201. for(i = 0; i < cpu_count; i++) {
  202. ks_thread_destroy(&threads[i]);
  203. }
  204. }
  205. static void create_threads_attached(void)
  206. {
  207. void *d = (void *)(intptr_t)1;
  208. int i;
  209. for(i = 0; i < cpu_count; i++) {
  210. ok( (ks_thread_create(&threads[i], thread_test_function_attached, d, pool) == KS_STATUS_SUCCESS) );
  211. }
  212. }
  213. static void create_threads_detatched(void)
  214. {
  215. ks_status_t status;
  216. void *d = (void *)(intptr_t)1;
  217. int i;
  218. for(i = 0; i < cpu_count; i++) {
  219. status = ks_thread_create_ex(&threads[i], thread_test_function_detatched, d, KS_THREAD_FLAG_DETACHED, KS_THREAD_DEFAULT_STACK, KS_PRI_DEFAULT, pool);
  220. ok( status == KS_STATUS_SUCCESS );
  221. }
  222. }
  223. static void check_thread_priority(void)
  224. {
  225. ks_status_t status;
  226. void *d = (void *)(intptr_t)1;
  227. status = ks_thread_create_ex(&thread_p, thread_priority, d, 0, KS_THREAD_DEFAULT_STACK, KS_PRI_IMPORTANT, pool);
  228. ok( status == KS_STATUS_SUCCESS );
  229. todo("Run docker container with an argument --cap-add=sys_nice to satisfy permission to set the scheduling policy\n");
  230. ok( ks_thread_priority(thread_p) == KS_PRI_IMPORTANT );
  231. ks_thread_destroy(&thread_p); // To visualize race issue around creating/destroying threads
  232. ks_thread_request_stop(thread_p);
  233. ks_thread_join(thread_p);
  234. ks_thread_destroy(&thread_p);
  235. end_todo;
  236. }
  237. static void join_threads(void)
  238. {
  239. int i;
  240. for(i = 0; i < cpu_count; i++) {
  241. ok( (KS_STATUS_SUCCESS == ks_thread_join(threads[i])) );
  242. }
  243. }
  244. static void check_attached(void)
  245. {
  246. ok( counter1 == (LOOP_COUNT * cpu_count) );
  247. }
  248. static void check_detached(void)
  249. {
  250. ok( counter2 == (LOOP_COUNT * cpu_count) );
  251. }
  252. static void create_pool(void)
  253. {
  254. ok( (ks_pool_open(&pool) == KS_STATUS_SUCCESS) );
  255. }
  256. static void check_cleanup(void)
  257. {
  258. ok( (counter3 == cpu_count) );
  259. }
  260. static void check_pool_close(void)
  261. {
  262. ok( (ks_pool_close(&pool) == KS_STATUS_SUCCESS) );
  263. }
  264. static void create_mutex(void)
  265. {
  266. ok( (ks_mutex_create(&mutex, KS_MUTEX_FLAG_DEFAULT, pool) == KS_STATUS_SUCCESS) );
  267. }
  268. static void create_mutex_non_recursive(void)
  269. {
  270. ok( (ks_mutex_create(&mutex_non_recursive, KS_MUTEX_FLAG_NON_RECURSIVE, pool) == KS_STATUS_SUCCESS) );
  271. }
  272. static void test_recursive_mutex(void)
  273. {
  274. ks_status_t status;
  275. ks_mutex_lock(mutex);
  276. status = ks_mutex_trylock(mutex);
  277. if (status == KS_STATUS_SUCCESS) {
  278. ks_mutex_unlock(mutex);
  279. }
  280. ok(status == KS_STATUS_SUCCESS);
  281. ks_mutex_unlock(mutex);
  282. }
  283. static void test_non_recursive_mutex(void)
  284. {
  285. ks_status_t status;
  286. ks_mutex_lock(mutex_non_recursive);
  287. status = ks_mutex_trylock(mutex_non_recursive);
  288. if (status == KS_STATUS_SUCCESS) {
  289. ks_mutex_unlock(mutex_non_recursive);
  290. }
  291. ok(status != KS_STATUS_SUCCESS);
  292. ks_mutex_unlock(mutex_non_recursive);
  293. }
  294. int main(int argc, char **argv)
  295. {
  296. ks_init();
  297. cpu_count = ks_env_cpu_count() * 4;
  298. plan(21 + cpu_count * 6);
  299. diag("Starting testing for %d tests\n", 21 + cpu_count * 6);
  300. create_pool();
  301. create_mutex();
  302. create_mutex_non_recursive();
  303. test_recursive_mutex();
  304. test_non_recursive_mutex();
  305. check_thread_priority();
  306. create_threads_attached();
  307. join_threads();
  308. check_attached();
  309. create_threads_detatched();
  310. while (threadscount != cpu_count) ks_sleep(1000000);
  311. check_detached();
  312. create_threads_cleanup();
  313. check_pool_close();
  314. check_cleanup();
  315. check_rwl();
  316. check_cond();
  317. ks_shutdown();
  318. done_testing();
  319. }