# syncobj-eventfd.c -rw-r--r-- 1.2 KiB View raw
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/* Compile with:
 *
 *     gcc -osyncobj-eventfd $(pkg-config libdrm --cflags --libs) syncobj-eventfd.c
 */

#define _GNU_SOURCE
#include <assert.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/eventfd.h>
#include <unistd.h>
#include <xf86drm.h>

#define DRM_IOCTL_SYNCOBJ_EVENTFD DRM_IOWR(0xCE, struct drm_syncobj_eventfd)

struct drm_syncobj_eventfd {
	__u32 handle;
	__u32 flags;
	__u64 point;
	__s32 fd;
	__u32 pad;
};

int main(void)
{
	int drm_fd, ev_fd, ret;
	uint32_t handle;
	struct drm_syncobj_eventfd arg;
	uint64_t ev_fd_value;

	drm_fd = open("/dev/dri/renderD128", O_RDWR | O_CLOEXEC);
	assert(drm_fd >= 0);

	ret = drmSyncobjCreate(drm_fd, 0, &handle);
	assert(ret == 0);

	ev_fd = eventfd(0, EFD_CLOEXEC);
	assert(ev_fd >= 0);

	arg = (struct drm_syncobj_eventfd) {
		.handle = handle,
		.fd = ev_fd,
	};
	ret = drmIoctl(drm_fd, DRM_IOCTL_SYNCOBJ_EVENTFD, &arg);
	assert(ret == 0);

	ret = fork();
	assert(ret >= 0);
	if (ret == 0) {
		ret = read(ev_fd, &ev_fd_value, sizeof(ev_fd_value));
		assert(ret > 0);
		printf("Event FD signalled!\n");
		_exit(0);
	}

	sleep(3);

	printf("Signalling drm_syncobj\n");
	ret = drmSyncobjSignal(drm_fd, &handle, 1);
	assert(ret == 0);

	sleep(1);
	return 0;
}