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