• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
27 pthread_mutex_t printf_mutex = PTHREAD_MUTEX_INITIALIZER;
28 
29 struct sync_thread_data {
30     int thread_no;
31     int fd[2];
32 };
33 
sync_thread(void * data)34 void *sync_thread(void *data)
35 {
36     struct sync_thread_data *sync_data = data;
37     struct sync_fence_info_data *info;
38     int err;
39     int i;
40 
41     for (i = 0; i < 2; i++) {
42         err = sync_wait(sync_data->fd[i], 10000);
43 
44         pthread_mutex_lock(&printf_mutex);
45         if (err < 0) {
46             printf("thread %d wait %d failed: %s\n", sync_data->thread_no,
47                    i, strerror(errno));
48         } else {
49             printf("thread %d wait %d done\n", sync_data->thread_no, i);
50         }
51         info = sync_fence_info(sync_data->fd[i]);
52         if (info) {
53             struct sync_pt_info *pt_info = NULL;
54             printf("  fence %s %d\n", info->name, info->status);
55 
56             while ((pt_info = sync_pt_info(info, pt_info))) {
57                 int ts_sec = pt_info->timestamp_ns / 1000000000LL;
58                 int ts_usec = (pt_info->timestamp_ns % 1000000000LL) / 1000LL;
59                 printf("    pt %s %s %d %d.%06d", pt_info->obj_name,
60                        pt_info->driver_name, pt_info->status,
61                        ts_sec, ts_usec);
62                 if (!strcmp(pt_info->driver_name, "sw_sync"))
63                     printf(" val=%d\n", *(uint32_t *)pt_info->driver_data);
64                 else
65                     printf("\n");
66             }
67             sync_fence_info_free(info);
68         }
69         pthread_mutex_unlock(&printf_mutex);
70     }
71 
72     return NULL;
73 }
74 
main(int argc,char * argv[])75 int main(int argc, char *argv[])
76 {
77     struct sync_thread_data sync_data[4];
78     pthread_t threads[4];
79     int sync_timeline_fd;
80     int i, j;
81     char str[256];
82 
83     sync_timeline_fd = sw_sync_timeline_create();
84     if (sync_timeline_fd < 0) {
85         perror("can't create sw_sync_timeline:");
86         return 1;
87     }
88 
89     for (i = 0; i < 3; i++) {
90         sync_data[i].thread_no = i;
91 
92         for (j = 0; j < 2; j++) {
93             unsigned val = i + j * 3 + 1;
94             sprintf(str, "test_fence%d-%d", i, j);
95             int fd = sw_sync_fence_create(sync_timeline_fd, str, val);
96             if (fd < 0) {
97                 printf("can't create sync pt %d: %s", val, strerror(errno));
98                 return 1;
99             }
100             sync_data[i].fd[j] = fd;
101             printf("sync_data[%d].fd[%d] = %d;\n", i, j, fd);
102 
103         }
104     }
105 
106     sync_data[3].thread_no = 3;
107     for (j = 0; j < 2; j++) {
108         sprintf(str, "merged_fence%d", j);
109         sync_data[3].fd[j] = sync_merge(str, sync_data[0].fd[j], sync_data[1].fd[j]);
110         if (sync_data[3].fd[j] < 0) {
111             printf("can't merge sync pts %d and %d: %s\n",
112                    sync_data[0].fd[j], sync_data[1].fd[j], strerror(errno));
113             return 1;
114         }
115     }
116 
117     for (i = 0; i < 4; i++)
118         pthread_create(&threads[i], NULL, sync_thread, &sync_data[i]);
119 
120 
121     for (i = 0; i < 3; i++) {
122         int err;
123         printf("press enter to inc to %d\n", i+1);
124         fgets(str, sizeof(str), stdin);
125         err = sw_sync_timeline_inc(sync_timeline_fd, 1);
126         if (err < 0) {
127             perror("can't increment sync obj:");
128             return 1;
129         }
130     }
131 
132     printf("press enter to close sync_timeline\n");
133     fgets(str, sizeof(str), stdin);
134 
135     close(sync_timeline_fd);
136 
137     printf("press enter to end test\n");
138     fgets(str, sizeof(str), stdin);
139 
140     for (i = 0; i < 3; i++) {
141         void *val;
142         pthread_join(threads[i], &val);
143     }
144 
145     return 0;
146 }
147