2
0

testthreadpools.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 <stdlib.h>
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include "tap.h"
  27. typedef struct ks_dht_nodeid_s { uint8_t id[20]; } ks_dht_nodeid_t;
  28. struct x {
  29. int i;
  30. ks_pool_t *pool;
  31. };
  32. static void *test1_thread(ks_thread_t *thread, void *data)
  33. {
  34. struct x *mydata = (struct x *) data;
  35. ks_log(KS_LOG_DEBUG, "Thread %d\n", mydata->i);
  36. ks_sleep(100000);
  37. ks_pool_free(&mydata);
  38. return NULL;
  39. }
  40. static int test1()
  41. {
  42. ks_pool_t *pool;
  43. ks_thread_pool_t *tp = NULL;
  44. int i = 0;
  45. ks_pool_open(&pool);
  46. ks_thread_pool_create(&tp, 2, 10, KS_THREAD_DEFAULT_STACK, KS_PRI_NORMAL, 5);
  47. for (i = 0; i < 500; i++) {
  48. struct x *data = ks_pool_alloc(pool, sizeof(*data));
  49. data->i = i;
  50. data->pool = pool;
  51. ks_sleep(1);
  52. ks_thread_pool_add_job(tp, test1_thread, data);
  53. }
  54. while(ks_thread_pool_backlog(tp)) {
  55. ks_sleep(100000);
  56. }
  57. // ks_sleep(10000000);
  58. ks_thread_pool_destroy(&tp);
  59. ks_pool_close(&pool);
  60. return 1;
  61. }
  62. int main(int argc, char **argv)
  63. {
  64. ks_init();
  65. plan(1);
  66. ok(test1());
  67. ks_shutdown();
  68. done_testing();
  69. }