qdf_periodic_work_test.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (c) 2019 The Linux Foundation. All rights reserved.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for
  5. * any purpose with or without fee is hereby granted, provided that the
  6. * above copyright notice and this permission notice appear in all
  7. * copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  10. * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  11. * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
  12. * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  13. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  14. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  16. * PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #include "qdf_periodic_work.h"
  19. #include "qdf_periodic_work_test.h"
  20. #include "qdf_trace.h"
  21. #define pwork_iterations 2
  22. #define pwork_delay_ms 1
  23. struct qdf_pwork_ut_ctx {
  24. struct qdf_periodic_work pwork;
  25. uint32_t count;
  26. };
  27. static void __qdf_pwork_inside_cb(void *context)
  28. {
  29. struct qdf_pwork_ut_ctx *ut_ctx = context;
  30. /* stop before incrementing; the main thread is looking at @count */
  31. if (ut_ctx->count + 1 == pwork_iterations)
  32. qdf_periodic_work_stop_async(&ut_ctx->pwork);
  33. ut_ctx->count++;
  34. }
  35. static uint32_t qdf_pwork_stop_inside_cb(void)
  36. {
  37. struct qdf_pwork_ut_ctx ut_ctx = { .count = 0 };
  38. QDF_STATUS status;
  39. status = qdf_periodic_work_create(&ut_ctx.pwork,
  40. __qdf_pwork_inside_cb, &ut_ctx);
  41. QDF_BUG(QDF_IS_STATUS_SUCCESS(status));
  42. QDF_BUG(qdf_periodic_work_start(&ut_ctx.pwork, pwork_delay_ms));
  43. while (ut_ctx.count < pwork_iterations)
  44. schedule();
  45. QDF_BUG(!qdf_periodic_work_stop_sync(&ut_ctx.pwork));
  46. QDF_BUG(ut_ctx.count == pwork_iterations);
  47. qdf_periodic_work_destroy(&ut_ctx.pwork);
  48. return 0;
  49. }
  50. static void __qdf_pwork_outside_cb(void *context)
  51. {
  52. struct qdf_pwork_ut_ctx *ut_ctx = context;
  53. ut_ctx->count++;
  54. }
  55. static uint32_t qdf_pwork_stop_outside_cb(void)
  56. {
  57. struct qdf_pwork_ut_ctx ut_ctx = { .count = 0 };
  58. QDF_STATUS status;
  59. status = qdf_periodic_work_create(&ut_ctx.pwork,
  60. __qdf_pwork_outside_cb, &ut_ctx);
  61. QDF_BUG(QDF_IS_STATUS_SUCCESS(status));
  62. QDF_BUG(qdf_periodic_work_start(&ut_ctx.pwork, pwork_delay_ms));
  63. while (ut_ctx.count < pwork_iterations)
  64. schedule();
  65. QDF_BUG(qdf_periodic_work_stop_sync(&ut_ctx.pwork));
  66. QDF_BUG(ut_ctx.count >= pwork_iterations);
  67. qdf_periodic_work_destroy(&ut_ctx.pwork);
  68. return 0;
  69. }
  70. uint32_t qdf_periodic_work_unit_test(void)
  71. {
  72. uint32_t errors = 0;
  73. errors += qdf_pwork_stop_inside_cb();
  74. errors += qdf_pwork_stop_outside_cb();
  75. return errors;
  76. }