testhandle.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (c) 2018-2019 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 MY_DATA_TYPE 1
  25. typedef struct my_data {
  26. ks_handle_base_t base;
  27. int foo;
  28. } my_data_t;
  29. static void my_data_deinit(void *context)
  30. {
  31. // TODO
  32. }
  33. static void *destroy_thread(ks_thread_t *thread, void *data)
  34. {
  35. ks_handle_t handle = (ks_handle_t)data;
  36. ks_handle_destroy(&handle);
  37. return NULL;
  38. }
  39. int main(void)
  40. {
  41. ks_init();
  42. ks_handle_init();
  43. plan(14);
  44. // create the handles and set up parent-child relationship
  45. ks_handle_t parent_handle = { 0 };
  46. ks_handle_t handle = { 0 };
  47. my_data_t *data = NULL;
  48. ks_handle_alloc(MY_DATA_TYPE, sizeof(my_data_t), &data, &parent_handle, my_data_deinit);
  49. ks_handle_set_ready(parent_handle);
  50. ok(data != NULL);
  51. ok(parent_handle > 0);
  52. ks_handle_alloc(MY_DATA_TYPE, sizeof(my_data_t), &data, &handle, my_data_deinit);
  53. ks_handle_set_ready(handle);
  54. ok(data != NULL);
  55. ok(handle > 0);
  56. ok(ks_handle_set_parent(handle, parent_handle) == KS_STATUS_SUCCESS);
  57. // test destroy when child has a ref count > 0
  58. ok(ks_handle_get(MY_DATA_TYPE, handle, &data) == KS_STATUS_SUCCESS);
  59. ok(ks_handle_destroy(&parent_handle) == KS_STATUS_HANDLE_PENDING_CHILDREN);
  60. ok(ks_handle_destroy(&parent_handle) == KS_STATUS_SUCCESS);
  61. ok(ks_handle_put(MY_DATA_TYPE, &data) == KS_STATUS_SUCCESS);
  62. ok(ks_handle_destroy(&handle) == KS_STATUS_SUCCESS);
  63. // reset setup to continue test with parent and child
  64. ks_handle_alloc(MY_DATA_TYPE, sizeof(my_data_t), &data, &parent_handle, my_data_deinit);
  65. ks_handle_alloc(MY_DATA_TYPE, sizeof(my_data_t), &data, &handle, my_data_deinit);
  66. ks_handle_set_parent(handle, parent_handle);
  67. ks_handle_set_ready(parent_handle);
  68. ks_handle_set_ready(handle);
  69. ks_pool_t *pool = NULL;
  70. ks_pool_open(&pool);
  71. // Get reference to parent and spawn 10 threads to attempt to destroy it
  72. // the threads will block trying to set the handle to not ready
  73. ok(ks_handle_get(MY_DATA_TYPE, parent_handle, &data) == KS_STATUS_SUCCESS);
  74. ks_thread_t *threads[10] = { 0 };
  75. for (int i = 0; i < 10; i++) {
  76. ks_thread_create(&threads[i], destroy_thread, (void *)parent_handle, pool);
  77. }
  78. ok(ks_handle_put(MY_DATA_TYPE, &data) == KS_STATUS_SUCCESS);
  79. for (int i = 0; i < 10; i++) {
  80. ks_thread_join(threads[i]);
  81. }
  82. // should already be destroyed now
  83. ok(ks_handle_destroy(&parent_handle) != KS_STATUS_SUCCESS);
  84. ok(ks_handle_destroy(&handle) != KS_STATUS_SUCCESS);
  85. ks_pool_close(&pool);
  86. ks_handle_shutdown();
  87. ks_shutdown();
  88. done_testing();
  89. }