4 * Copyright 2012 Google, Inc
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
25 #include <sync/sync.h>
27 pthread_mutex_t printf_mutex
= PTHREAD_MUTEX_INITIALIZER
;
29 struct sync_thread_data
{
34 void *sync_thread(void *data
)
36 struct sync_thread_data
*sync_data
= data
;
37 struct sync_fence_info_data
*info
;
41 for (i
= 0; i
< 2; i
++) {
42 err
= sync_wait(sync_data
->fd
[i
], 10000);
44 pthread_mutex_lock(&printf_mutex
);
46 printf("thread %d wait %d failed: %s\n", sync_data
->thread_no
,
49 printf("thread %d wait %d done\n", sync_data
->thread_no
, i
);
51 info
= sync_fence_info(sync_data
->fd
[i
]);
53 struct sync_pt_info
*pt_info
= NULL
;
54 printf(" fence %s %d\n", info
->name
, info
->status
);
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
,
62 if (!strcmp(pt_info
->driver_name
, "sw_sync"))
63 printf(" val=%d\n", *(uint32_t *)pt_info
->driver_data
);
67 sync_fence_info_free(info
);
69 pthread_mutex_unlock(&printf_mutex
);
75 int main(int argc
__attribute__((unused
)), char *argv
[] __attribute__((unused
)))
77 struct sync_thread_data sync_data
[4];
83 sync_timeline_fd
= sw_sync_timeline_create();
84 if (sync_timeline_fd
< 0) {
85 perror("can't create sw_sync_timeline:");
89 for (i
= 0; i
< 3; i
++) {
90 sync_data
[i
].thread_no
= i
;
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
);
97 printf("can't create sync pt %d: %s", val
, strerror(errno
));
100 sync_data
[i
].fd
[j
] = fd
;
101 printf("sync_data[%d].fd[%d] = %d;\n", i
, j
, fd
);
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
));
117 for (i
= 0; i
< 4; i
++)
118 pthread_create(&threads
[i
], NULL
, sync_thread
, &sync_data
[i
]);
121 for (i
= 0; i
< 3; i
++) {
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);
127 perror("can't increment sync obj:");
132 printf("press enter to close sync_timeline\n");
133 fgets(str
, sizeof(str
), stdin
);
135 close(sync_timeline_fd
);
137 printf("press enter to end test\n");
138 fgets(str
, sizeof(str
), stdin
);
140 for (i
= 0; i
< 3; i
++) {
142 pthread_join(threads
[i
], &val
);