testthreadmutex.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. * Copyright (c) 2018 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. for (i = 0; i < LOOP_COUNT; i++) {
  181. ks_mutex_lock(mutex);
  182. if (d == 1) {
  183. counter1++;
  184. }
  185. ks_mutex_unlock(mutex);
  186. }
  187. return NULL;
  188. }
  189. static void create_threads_cleanup(void)
  190. {
  191. void *d = (void *)(intptr_t)1;
  192. int i;
  193. for(i = 0; i < cpu_count; i++) {
  194. ok( (ks_thread_create(&threads[i], thread_test_function_cleanup, d, pool) == KS_STATUS_SUCCESS) );
  195. }
  196. for(i = 0; i < cpu_count; i++) {
  197. ks_thread_destroy(&threads[i]);
  198. }
  199. }
  200. static void create_threads_attached(void)
  201. {
  202. void *d = (void *)(intptr_t)1;
  203. int i;
  204. for(i = 0; i < cpu_count; i++) {
  205. ok( (ks_thread_create(&threads[i], thread_test_function_attached, d, pool) == KS_STATUS_SUCCESS) );
  206. }
  207. }
  208. static void create_threads_detatched(void)
  209. {
  210. ks_status_t status;
  211. void *d = (void *)(intptr_t)1;
  212. int i;
  213. for(i = 0; i < cpu_count; i++) {
  214. status = ks_thread_create_ex(&threads[i], thread_test_function_detatched, d, KS_THREAD_FLAG_DETACHED, KS_THREAD_DEFAULT_STACK, KS_PRI_NORMAL, pool);
  215. ok( status == KS_STATUS_SUCCESS );
  216. }
  217. }
  218. static void check_thread_priority(void)
  219. {
  220. ks_status_t status;
  221. void *d = (void *)(intptr_t)1;
  222. status = ks_thread_create_ex(&thread_p, thread_priority, d, 0, KS_THREAD_DEFAULT_STACK, KS_PRI_IMPORTANT, pool);
  223. ok( status == KS_STATUS_SUCCESS );
  224. ks_sleep(100000);
  225. todo("Add check to see if has permission to set thread priority\n");
  226. ok( ks_thread_priority(thread_p) == KS_PRI_IMPORTANT );
  227. ks_thread_destroy(&thread_p);
  228. end_todo;
  229. }
  230. static void join_threads(void)
  231. {
  232. int i;
  233. for(i = 0; i < cpu_count; i++) {
  234. ok( (KS_STATUS_SUCCESS == ks_thread_join(threads[i])) );
  235. }
  236. }
  237. static void check_attached(void)
  238. {
  239. ok( counter1 == (LOOP_COUNT * cpu_count) );
  240. }
  241. static void check_detached(void)
  242. {
  243. ok( counter2 == (LOOP_COUNT * cpu_count) );
  244. }
  245. static void create_pool(void)
  246. {
  247. ok( (ks_pool_open(&pool) == KS_STATUS_SUCCESS) );
  248. }
  249. static void check_cleanup(void)
  250. {
  251. ok( (counter3 == cpu_count) );
  252. }
  253. static void check_pool_close(void)
  254. {
  255. ok( (ks_pool_close(&pool) == KS_STATUS_SUCCESS) );
  256. }
  257. static void create_mutex(void)
  258. {
  259. ok( (ks_mutex_create(&mutex, KS_MUTEX_FLAG_DEFAULT, pool) == KS_STATUS_SUCCESS) );
  260. }
  261. static void create_mutex_non_recursive(void)
  262. {
  263. ok( (ks_mutex_create(&mutex_non_recursive, KS_MUTEX_FLAG_NON_RECURSIVE, pool) == KS_STATUS_SUCCESS) );
  264. }
  265. static void test_recursive_mutex(void)
  266. {
  267. ks_status_t status;
  268. ks_mutex_lock(mutex);
  269. status = ks_mutex_trylock(mutex);
  270. if (status == KS_STATUS_SUCCESS) {
  271. ks_mutex_unlock(mutex);
  272. }
  273. ok(status == KS_STATUS_SUCCESS);
  274. ks_mutex_unlock(mutex);
  275. }
  276. static void test_non_recursive_mutex(void)
  277. {
  278. ks_status_t status;
  279. ks_mutex_lock(mutex_non_recursive);
  280. status = ks_mutex_trylock(mutex_non_recursive);
  281. if (status == KS_STATUS_SUCCESS) {
  282. ks_mutex_unlock(mutex_non_recursive);
  283. }
  284. ok(status != KS_STATUS_SUCCESS);
  285. ks_mutex_unlock(mutex_non_recursive);
  286. }
  287. int main(int argc, char **argv)
  288. {
  289. ks_init();
  290. cpu_count = ks_env_cpu_count() * 4;
  291. plan(21 + cpu_count * 6);
  292. diag("Starting testing for %d tests\n", 21 + cpu_count * 6);
  293. create_pool();
  294. create_mutex();
  295. create_mutex_non_recursive();
  296. test_recursive_mutex();
  297. test_non_recursive_mutex();
  298. check_thread_priority();
  299. create_threads_attached();
  300. join_threads();
  301. check_attached();
  302. create_threads_detatched();
  303. while (threadscount != cpu_count) ks_sleep(1000000);
  304. check_detached();
  305. create_threads_cleanup();
  306. check_pool_close();
  307. check_cleanup();
  308. check_rwl();
  309. check_cond();
  310. ks_shutdown();
  311. done_testing();
  312. }