1 /*
2  *  sync_test.c
3  *
4  *   Copyright 2012 Google, Inc
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  */
18 
19 #include <errno.h>
20 #include <pthread.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <unistd.h>
24 
25 #include <sync/sync.h>
26 #include "sw_sync.h"
27 
28 pthread_mutex_t printf_mutex = PTHREAD_MUTEX_INITIALIZER;
29 
30 struct sync_thread_data {
31     int thread_no;
32     int fd[2];
33 };
34 
sync_thread(void * data)35 void *sync_thread(void *data)
36 {
37     struct sync_thread_data *sync_data = data;
38     struct sync_fence_info_data *info;
39     int err;
40     int i;
41 
42     for (i = 0; i < 2; i++) {
43         err = sync_wait(sync_data->fd[i], 10000);
44 
45         pthread_mutex_lock(&printf_mutex);
46         if (err < 0) {
47             printf("thread %d wait %d failed: %s\n", sync_data->thread_no,
48                    i, strerror(errno));
49         } else {
50             printf("thread %d wait %d done\n", sync_data->thread_no, i);
51         }
52         info = sync_fence_info(sync_data->fd[i]);
53         if (info) {
54             struct sync_pt_info *pt_info = NULL;
55             printf("  fence %s %d\n", info->name, info->status);
56 
57             while ((pt_info = sync_pt_info(info, pt_info))) {
58                 int ts_sec = pt_info->timestamp_ns / 1000000000LL;
59                 int ts_usec = (pt_info->timestamp_ns % 1000000000LL) / 1000LL;
60                 printf("    pt %s %s %d %d.%06d", pt_info->obj_name,
61                        pt_info->driver_name, pt_info->status,
62                        ts_sec, ts_usec);
63                 if (!strcmp(pt_info->driver_name, "sw_sync"))
64                     printf(" val=%d\n", *(uint32_t *)pt_info->driver_data);
65                 else
66                     printf("\n");
67             }
68             sync_fence_info_free(info);
69         }
70         pthread_mutex_unlock(&printf_mutex);
71     }
72 
73     return NULL;
74 }
75 
main(int argc,char * argv[])76 int main(int argc __attribute__((unused)), char *argv[] __attribute__((unused)))
77 {
78     struct sync_thread_data sync_data[4];
79     pthread_t threads[4];
80     int sync_timeline_fd;
81     int i, j;
82     char str[256];
83 
84     sync_timeline_fd = sw_sync_timeline_create();
85     if (sync_timeline_fd < 0) {
86         perror("can't create sw_sync_timeline:");
87         return 1;
88     }
89 
90     for (i = 0; i < 3; i++) {
91         sync_data[i].thread_no = i;
92 
93         for (j = 0; j < 2; j++) {
94             unsigned val = i + j * 3 + 1;
95             sprintf(str, "test_fence%d-%d", i, j);
96             int fd = sw_sync_fence_create(sync_timeline_fd, str, val);
97             if (fd < 0) {
98                 printf("can't create sync pt %d: %s", val, strerror(errno));
99                 return 1;
100             }
101             sync_data[i].fd[j] = fd;
102             printf("sync_data[%d].fd[%d] = %d;\n", i, j, fd);
103 
104         }
105     }
106 
107     sync_data[3].thread_no = 3;
108     for (j = 0; j < 2; j++) {
109         sprintf(str, "merged_fence%d", j);
110         sync_data[3].fd[j] = sync_merge(str, sync_data[0].fd[j], sync_data[1].fd[j]);
111         if (sync_data[3].fd[j] < 0) {
112             printf("can't merge sync pts %d and %d: %s\n",
113                    sync_data[0].fd[j], sync_data[1].fd[j], strerror(errno));
114             return 1;
115         }
116     }
117 
118     for (i = 0; i < 4; i++)
119         pthread_create(&threads[i], NULL, sync_thread, &sync_data[i]);
120 
121 
122     for (i = 0; i < 3; i++) {
123         int err;
124         printf("press enter to inc to %d\n", i+1);
125         fgets(str, sizeof(str), stdin);
126         err = sw_sync_timeline_inc(sync_timeline_fd, 1);
127         if (err < 0) {
128             perror("can't increment sync obj:");
129             return 1;
130         }
131     }
132 
133     printf("press enter to close sync_timeline\n");
134     fgets(str, sizeof(str), stdin);
135 
136     close(sync_timeline_fd);
137 
138     printf("press enter to end test\n");
139     fgets(str, sizeof(str), stdin);
140 
141     for (i = 0; i < 3; i++) {
142         void *val;
143         pthread_join(threads[i], &val);
144     }
145 
146     return 0;
147 }
148