xref: /OK3568_Linux_fs/external/mpp/osal/test/mpp_eventfd_test.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright 2015 Rockchip Electronics Co. LTD
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define MODULE_TAG "mpp_eventfd_test"
18 
19 #include "mpp_log.h"
20 #include "mpp_time.h"
21 #include "mpp_common.h"
22 #include "mpp_thread.h"
23 #include "mpp_eventfd.h"
24 
25 #define WR_VAL      1
26 #define RD_VAL      10
27 
28 static RK_S32 data_fd = -1;
29 static RK_S32 sync_fd = -1;
30 static RK_U64 wr_val_base = WR_VAL;
31 static RK_U64 rd_val_base = RD_VAL;
32 static RK_U64 wr_val = WR_VAL;
33 static RK_U64 rd_val = RD_VAL;
34 static RK_S32 wr_ret = -1;
35 static RK_S32 rd_ret = -1;
36 static RK_S64 wr_timeout = -1;
37 static RK_S64 rd_timeout = -1;
38 
reset_test()39 void reset_test()
40 {
41     wr_val = wr_val_base;
42     rd_val = rd_val_base;
43     wr_ret = -1;
44     rd_ret = -1;
45     rd_val_base++;
46 
47     mpp_eventfd_write(data_fd, 0);
48     mpp_eventfd_read(data_fd, NULL, 0);
49 
50     mpp_log("eventfd test on writing %lld -> %lld\n", wr_val, rd_val);
51 }
52 
thread_read(void * arg)53 void *thread_read(void *arg)
54 {
55     (void) arg;
56     mpp_eventfd_write(sync_fd, 1);
57 
58     rd_ret = mpp_eventfd_read(data_fd, &rd_val, rd_timeout);
59     mpp_log("eventfd read %d timeout %d ret %lld val %lld\n", data_fd, rd_timeout, rd_ret, rd_val);
60 
61     return NULL;
62 }
63 
main()64 int main()
65 {
66     pthread_t td;
67 
68     mpp_log("eventfd test start\n");
69 
70     data_fd = mpp_eventfd_get(0);
71     sync_fd = mpp_eventfd_get(0);
72 
73     mpp_log("eventfd get data_fd %d sync_fd %d\n", data_fd, sync_fd);
74 
75     reset_test();
76     rd_timeout = -1;
77     wr_timeout = 0;
78 
79     pthread_create(&td, NULL, thread_read, NULL);
80     mpp_eventfd_read(sync_fd, NULL, -1);
81     mpp_log("eventfd write %d val %lld\n", data_fd, wr_val);
82     wr_ret = mpp_eventfd_write(data_fd, wr_val);
83     pthread_join(td, NULL);
84     mpp_log("eventfd block mode     test wr: %lld ret %d rd: %lld ret %d - %s\n",
85             wr_val, wr_ret, rd_val, rd_ret,
86             (wr_val == rd_val) ? "success" : "failed");
87 
88     reset_test();
89     rd_timeout = 0;
90     wr_timeout = 100;
91 
92     pthread_create(&td, NULL, thread_read, NULL);
93     mpp_eventfd_read(sync_fd, NULL, -1);
94     msleep(wr_timeout);
95     mpp_log("eventfd write %d val %lld\n", data_fd, wr_val);
96     wr_ret = mpp_eventfd_write(data_fd, wr_val);
97     pthread_join(td, NULL);
98     mpp_log("eventfd non-block mode test wr: %lld ret %d rd: %lld ret %d - %s\n",
99             wr_val, wr_ret, rd_val, rd_ret,
100             (wr_val != rd_val) ? "success" : "failed");
101 
102     reset_test();
103     rd_timeout = 100;
104     wr_timeout = 1;
105 
106     pthread_create(&td, NULL, thread_read, NULL);
107     mpp_eventfd_read(sync_fd, NULL, -1);
108     msleep(wr_timeout);
109     mpp_log("eventfd write %d val %lld\n", data_fd, wr_val);
110     wr_ret = mpp_eventfd_write(data_fd, wr_val);
111     pthread_join(td, NULL);
112     mpp_log("eventfd timeout   mode test wr: %lld ret %d rd: %lld ret %d - %s\n",
113             wr_val, wr_ret, rd_val, rd_ret,
114             (wr_val == rd_val) ? "success" : "failed");
115 
116     reset_test();
117     rd_timeout = 1;
118     wr_timeout = 100;
119 
120     pthread_create(&td, NULL, thread_read, NULL);
121     mpp_eventfd_read(sync_fd, NULL, -1);
122     msleep(wr_timeout);
123     mpp_log("eventfd write %d val %lld\n", data_fd, wr_val);
124     wr_ret = mpp_eventfd_write(data_fd, wr_val);
125     pthread_join(td, NULL);
126     mpp_log("eventfd timeout   mode test wr: %lld ret %d rd: %lld ret %d - %s\n",
127             wr_val, wr_ret, rd_val, rd_ret,
128             (wr_val != rd_val) ? "success" : "failed");
129 
130     mpp_eventfd_put(data_fd);
131     mpp_eventfd_put(sync_fd);
132 
133     mpp_log("eventfd test done\n");
134 
135     return 0;
136 }
137 
138