summaryrefslogtreecommitdiffstats
path: root/patches/0001-Add-thread-affinity-to-wayland-clients.patch
blob: 9448c4db006f3ddce0c52bd1d32d3df9602ba217 (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
From bb2bb0a9f3751156d9537d2daa4507c8b1eff459 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=B8rgen=20Lind?= <jorgen.lind@nokia.com>
Date: Mon, 5 Mar 2012 12:44:37 +0100
Subject: [PATCH] Add thread affinity to wayland clients

This makes it possible to marshal requests from more than 1 thread in
wayland clients. However, its not possible to run wl_display_iterate
from other threads than the thread that made the wl_display.
---
 src/Makefile.am      |    2 +
 src/wayland-client.c |  111 +++++++++++++++++++++++++++++++++++++++++++++++---
 src/wayland-client.h |    7 ++++
 3 files changed, 115 insertions(+), 5 deletions(-)

diff --git a/src/Makefile.am b/src/Makefile.am
index f93954e..836cb31 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -27,6 +27,8 @@ libwayland_server_la_SOURCES =			\
 	event-loop.c
 
 libwayland_client_la_LIBADD = $(FFI_LIBS) libwayland-util.la -lrt -lm
+libwayland_client_la_LDFLAGS = -pthread
+libwayland_client_la_CFLAGS = -pthread
 libwayland_client_la_SOURCES =			\
 	wayland-protocol.c			\
 	wayland-client.c
diff --git a/src/wayland-client.c b/src/wayland-client.c
index ecedd99..c385bfe 100644
--- a/src/wayland-client.c
+++ b/src/wayland-client.c
@@ -34,6 +34,7 @@
 #include <assert.h>
 #include <fcntl.h>
 #include <sys/poll.h>
+#include <sys/eventfd.h>
 
 #include "wayland-util.h"
 #include "wayland-os.h"
@@ -63,6 +64,9 @@ struct wl_display {
 	struct wl_proxy proxy;
 	struct wl_connection *connection;
 	int fd;
+	int write_notification_event_fd;
+	pthread_t thread_id;
+	pthread_mutex_t marshalling_mutex;
 	uint32_t mask;
 	struct wl_map objects;
 	struct wl_list global_listener_list;
@@ -193,6 +197,11 @@ wl_proxy_marshal(struct wl_proxy *proxy, uint32_t opcode, ...)
 	struct wl_closure closure;
 	va_list ap;
 	int ret;
+	int write_notification_event_fd;
+	uint64_t write_notification_value;
+	ssize_t success;
+
+	pthread_mutex_lock(&proxy->display->marshalling_mutex);
 
 	va_start(ap, opcode);
 	ret = wl_closure_vmarshal(&closure, &proxy->object, opcode, ap,
@@ -213,6 +222,18 @@ wl_proxy_marshal(struct wl_proxy *proxy, uint32_t opcode, ...)
 		wl_closure_print(&closure, &proxy->object, true);
 
 	wl_closure_destroy(&closure);
+
+	write_notification_event_fd = proxy->display->write_notification_event_fd;
+	write_notification_value = 1;
+	success = write(write_notification_event_fd,&write_notification_value,8);
+	if (success < 0) {
+		fprintf(stderr,
+			"Error writing to eventfd %d: %s\n",
+			write_notification_event_fd,
+			strerror(errno));
+	}
+
+	pthread_mutex_unlock(&proxy->display->marshalling_mutex);
 }
 
 /* Can't do this, there may be more than one instance of an
@@ -348,6 +369,7 @@ wl_display_connect(const char *name)
 	const char *debug;
 	char *connection, *end;
 	int flags;
+	int success;
 
 	debug = getenv("WAYLAND_DEBUG");
 	if (debug)
@@ -397,6 +419,21 @@ wl_display_connect(const char *name)
 		return NULL;
 	}
 
+	display->write_notification_event_fd = eventfd(0, EFD_CLOEXEC);
+        if (display->write_notification_event_fd < 0) {
+            fprintf(stderr, "Failed to create eventfd\n");
+        }
+
+	display->thread_id = pthread_self();
+	pthread_mutexattr_t mutex_attr;
+	success = pthread_mutexattr_init(&mutex_attr);
+	success += pthread_mutexattr_settype(&mutex_attr,PTHREAD_MUTEX_RECURSIVE);
+	success += pthread_mutex_init(&display->marshalling_mutex, &mutex_attr);
+	success += pthread_mutexattr_destroy(&mutex_attr);
+
+	if (success)
+		fprintf(stderr, "Threading setup was unsuccessfull\n");
+
 	return display;
 }
 
@@ -433,6 +470,18 @@ wl_display_get_fd(struct wl_display *display,
 	return display->fd;
 }
 
+WL_EXPORT int
+wl_display_get_write_notification_fd(struct wl_display *display)
+{
+	return display->write_notification_event_fd;
+}
+
+WL_EXPORT pthread_t
+wl_display_thread(struct wl_display *display)
+{
+	return display->thread_id;
+}
+
 static void
 sync_callback(void *data, struct wl_callback *callback, uint32_t serial)
 {
@@ -446,18 +495,47 @@ static const struct wl_callback_listener sync_listener = {
 	sync_callback
 };
 
+static void
+threaded_sync_callback(void *data, struct wl_callback *callback, uint32_t time)
+{
+	fprintf(stderr, "threaded_sync_callback\n");
+	pthread_cond_t *wait_condition = data;
+
+	pthread_cond_broadcast(wait_condition);
+	wl_callback_destroy(callback);
+}
+
+static const struct wl_callback_listener threaded_sync_listener = {
+	threaded_sync_callback
+};
+
+
 WL_EXPORT void
 wl_display_roundtrip(struct wl_display *display)
 {
 	struct wl_callback *callback;
 	int done;
+	pthread_cond_t wait_cond;
+	pthread_mutex_t wait_mutex;
 
-	done = 0;
 	callback = wl_display_sync(display);
-	wl_callback_add_listener(callback, &sync_listener, &done);
-	wl_display_flush(display);
-	while (!done)
-		wl_display_iterate(display, WL_DISPLAY_READABLE);
+
+	if (wl_display_thread(display) == pthread_self()) {
+		done = 0;
+		wl_callback_add_listener(callback, &sync_listener, &done);
+		wl_display_flush(display);
+		while (!done)
+			wl_display_iterate(display, WL_DISPLAY_READABLE);
+	} else {
+		pthread_mutex_init(&wait_mutex,NULL);
+		pthread_cond_init(&wait_cond, NULL);
+		pthread_mutex_lock(&wait_mutex);
+
+		wl_callback_add_listener(callback, &threaded_sync_listener, &wait_cond);
+		pthread_cond_wait(&wait_cond,&wait_mutex);
+		pthread_cond_destroy(&wait_cond);
+		pthread_mutex_destroy(&wait_mutex);
+	}
 }
 
 static void
@@ -503,6 +581,10 @@ wl_display_iterate(struct wl_display *display, uint32_t mask)
 {
 	uint32_t p[2], object;
 	int len, opcode, size;
+	uint64_t write_fd;
+	ssize_t success;
+
+	pthread_mutex_lock(&display->marshalling_mutex);
 
 	mask &= display->mask;
 	if (mask == 0) {
@@ -511,6 +593,23 @@ wl_display_iterate(struct wl_display *display, uint32_t mask)
 		return;
 	}
 
+	if (mask & WL_DISPLAY_WRITABLE) {
+		if (pthread_self() != display->thread_id) {
+			fprintf(stderr,
+				"wl_display_iterate called with WL_DISPLAY_WRITABLE"
+				"from another thread than the thead that created "
+				"wl_display. This will result in events being dispatched"
+				"in other threads\n");
+		}
+		success = read(display->write_notification_event_fd, &write_fd, 8);
+		if (success < 0) {
+			fprintf(stderr,
+				"wl_display_iterate eventfd  %d error at read: %s\n",
+				display->write_notification_event_fd,
+				strerror(errno));
+		}
+	}
+
 	len = wl_connection_data(display->connection, mask);
 
 	while (len > 0) {
@@ -528,6 +627,8 @@ wl_display_iterate(struct wl_display *display, uint32_t mask)
 		len -= size;
 	}
 
+	pthread_mutex_unlock(&display->marshalling_mutex);
+
 	if (len < 0) {
 		fprintf(stderr, "read error: %m\n");
 		exit(EXIT_FAILURE);
diff --git a/src/wayland-client.h b/src/wayland-client.h
index 06dc6fe..4053474 100644
--- a/src/wayland-client.h
+++ b/src/wayland-client.h
@@ -26,6 +26,9 @@
 #include "wayland-util.h"
 #include "wayland-version.h"
 
+#include <pthread.h>
+#define WAYLAND_CLIENT_THREAD_AFFINITY
+
 #ifdef  __cplusplus
 extern "C" {
 #endif
@@ -98,6 +101,10 @@ wl_display_get_global(struct wl_display *display,
 
 void wl_log_set_handler_client(wl_log_func_t handler);
 
+int wl_display_get_write_notification_fd(struct wl_display *display);
+
+pthread_t wl_display_thread(struct wl_display *display);
+
 #ifdef  __cplusplus
 }
 #endif
-- 
1.7.9.5