summaryrefslogtreecommitdiff
path: root/x11-libs/libX11/files/xlib-0001-Use-XCB-s-new-socket-handoff-mechanism-rather-than-t.patch
blob: e5004925b12958638a62790aea691d244b47304f (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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
From 50d5c8363da0cd10217d4f24e8561ed9a1e1881e Mon Sep 17 00:00:00 2001
From: Josh Triplett <josh@freedesktop.org>
Date: Sat, 15 Mar 2008 17:22:00 -0700
Subject: [PATCH 1/4] Use XCB's new socket handoff mechanism rather than the old XCB Xlib lock.

Previously, Xlib/XCB used XCB's Xlib lock to prevent XCB from sending
requests between calls to Xlib's LockDisplay and UnlockDisplay macros.
Xlib/XCB then sent all of its requests using XCB's xcb_send_request, and
had to flush its requests when unlocking the display.

XCB 1.2 adds a new socket handoff mechanism, xcb_take_socket.  Replace
much of the existing Xlib/XCB implementation with the use of
xcb_take_socket to take ownership of the write side of the X connection
socket, and a return_socket callback which writes any outstanding requests
with xcb_writev.  This approach allows Xlib/XCB to use the same buffering
as traditional Xlib did.  In particular, programs which use Xlib/XCB and
never make XCB calls will never need to hand the socket back to XCB, and
vice versa.

This allows us to discard large quantities of synchronization code from
Xlib/XCB, together with the synchronization bugs present in that code.
Several test cases which previously failed now work perfectly, including
multi-threaded ico.  In addition, the infamous locking correctness
assertions, triggered when double-locking or when unlocking without a
previous lock, no longer exist, because Xlib/XCB no longer has any reason
to care more about application locking than traditional Xlib does.

Furthermore, the handoff approach provides great improvements to
performance.  Results from x11perf's XNoOp test, which represented the
worst case for the lock-based Xlib/XCB:

Traditional Xlib:       average 19100000/sec
Lock-based Xlib/XCB:    average  3350000/sec
Handoff-based Xlib/XCB: average 17400000/sec

Thus, for no-ops, the handoff mechanism provides more than a 4x speedup to
Xlib/XCB, bringing Xlib/XCB within 9% of traditional Xlib no-op
performance.  Of course, real-world workloads do not use no-op, so your
mileage may vary.  In particular, since no-ops represent the worst case,
we expect real workloads to more closely match the performance of
traditional Xlib.

While removing synchronization code, we changed _XReply to not drop
any locks when calling xcb_wait_for_reply; previously, we had to carefully
avoid a deadlock between the Display lock and the XCB Xlib lock. Holding
the locks reduces implementation complexity and should not impact applications.

Commit by Jamey Sharp and Josh Triplett.
XCB's handoff mechanism inspired by Keith Packard.
---
 configure.ac    |    4 +-
 src/Makefile.am |    1 -
 src/OpenDis.c   |   27 +++----
 src/Xxcbint.h   |   27 +-----
 src/xcb_io.c    |  194 +++++++++++++++++++++++++++++-----------------
 src/xcb_lock.c  |  235 -------------------------------------------------------
 6 files changed, 140 insertions(+), 348 deletions(-)
 delete mode 100644 src/xcb_lock.c

diff --git a/configure.ac b/configure.ac
index 5febfb3..6141fa4 100644
--- a/configure.ac
+++ b/configure.ac
@@ -52,8 +52,8 @@ no)
 	AC_DEFINE(USE_XCB, 0, [Use XCB for low-level protocol implementation])
 	;;
 *)
-	X11_REQUIRES="xcb-xlib >= 1.1.90"
-	X11_EXTRA_DEPS="xcb-xlib"
+	X11_REQUIRES="xcb >= 1.2"
+	X11_EXTRA_DEPS="xcb >= 1.2"
 	xdmauth="no" # XCB handles all auth
 	AC_DEFINE(USE_XCB, 1, [Use XCB for low-level protocol implementation])
 	;;
diff --git a/src/Makefile.am b/src/Makefile.am
index 564e03f..3380f81 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -342,7 +342,6 @@ EXTRA_DIST = \
 
 if XCB
 libX11_la_SOURCES += \
-                  xcb_lock.c \
                   xcb_disp.c \
                   xcb_io.c \
                   Xxcbint.h
diff --git a/src/OpenDis.c b/src/OpenDis.c
index d5089e8..aa2541f 100644
--- a/src/OpenDis.c
+++ b/src/OpenDis.c
@@ -261,13 +261,6 @@ XOpenDisplay (
 		return(NULL);
 	}
 
-#if USE_XCB
-	if (!_XCBInitDisplayLock(dpy)) {
-	        OutOfMemory (dpy, setup);
-		return(NULL);
-	}
-#endif
-
 	if (!_XPollfdCacheInit(dpy)) {
 	        OutOfMemory (dpy, setup);
 		return(NULL);
@@ -292,6 +285,10 @@ XOpenDisplay (
          return(NULL);
     }
     dpy->bufmax = dpy->buffer + conn_buf_size;
+#if USE_XCB
+    dpy->xcb->real_bufmax = dpy->bufmax;
+    dpy->bufmax = dpy->buffer;
+#endif
 
 	/* Set up the input event queue and input event queue parameters. */
 	dpy->head = dpy->tail = NULL;
@@ -652,6 +649,12 @@ XOpenDisplay (
 	UnlockDisplay(dpy);
 #endif /* !USE_XCB */
 
+#if USE_XCB
+	dpy->bigreq_size = xcb_get_maximum_request_length(dpy->xcb->connection);
+	if(dpy->bigreq_size <= dpy->max_request_size)
+		dpy->bigreq_size = 0;
+#endif /* USE_XCB */
+
 /*
  * Set up other stuff clients are always going to use.
  */
@@ -673,12 +676,6 @@ XOpenDisplay (
  */
 	(void) XSynchronize(dpy, _Xdebug);
 
-#if USE_XCB
-	dpy->bigreq_size = xcb_get_maximum_request_length(dpy->xcb->connection);
-	if(dpy->bigreq_size <= dpy->max_request_size)
-		dpy->bigreq_size = 0;
-#endif /* USE_XCB */
-
 /*
  * get availability of large requests, and
  * get the resource manager database off the root window.
@@ -883,10 +880,6 @@ void _XFreeDisplayStructure(dpy)
  	    Xfree (dpy->scratch_buffer);
 	FreeDisplayLock(dpy);
 
-#if USE_XCB
-	_XCBShutdownDisplayLock(dpy);
-#endif /* USE_XCB */
-
 	if (dpy->qfree) {
 	    register _XQEvent *qelt = dpy->qfree;
 
diff --git a/src/Xxcbint.h b/src/Xxcbint.h
index cc73749..1b3137b 100644
--- a/src/Xxcbint.h
+++ b/src/Xxcbint.h
@@ -5,34 +5,28 @@
 #define XXCBINT_H
 
 #include <assert.h>
+#include <stdint.h>
 #include <X11/Xlibint.h>
 #include <X11/Xlib-xcb.h>
-#include "locking.h" /* really just want X11/Xthreads.h but can't have it. */
 
-#define XCB_SEQUENCE_COMPARE(a,op,b)	((int) ((a) - (b)) op 0)
-#define assert_sequence_less(a,b) assert(XCB_SEQUENCE_COMPARE((a), <=, (b)))
+#define XLIB_SEQUENCE_COMPARE(a,op,b)	(((long) (a) - (long) (b)) op 0)
 
 typedef struct PendingRequest PendingRequest;
 struct PendingRequest {
 	PendingRequest *next;
-	xcondition_rec condition;
-	int waiters; /* Number of threads waiting; -1 if no wait needed */
-	unsigned int sequence;
+	unsigned long sequence;
 };
 
 typedef struct _X11XCBPrivate {
-	struct _XLockPtrs lock_fns;
 	xcb_connection_t *connection;
 	PendingRequest *pending_requests;
 	PendingRequest **pending_requests_tail;
 	xcb_generic_event_t *next_event;
-	const char *request_extra;
-	int request_extra_size;
-	char *partial_request;
-	int partial_request_offset;
+	char *real_bufmax;
 	char *reply_data;
 	int reply_length;
 	int reply_consumed;
+	uint64_t last_flushed;
 	enum XEventQueueOwner event_owner;
 	XID next_xid;
 } _X11XCBPrivate;
@@ -42,15 +36,4 @@ typedef struct _X11XCBPrivate {
 int _XConnectXCB(Display *dpy, _Xconst char *display, char **fullnamep, int *screenp);
 void _XFreeX11XCBStructure(Display *dpy);
 
-/* xcb_lock.c */
-
-int _XCBInitDisplayLock(Display *dpy);
-void _XCBShutdownDisplayLock(Display *dpy);
-
-/* _XGetXCBBuffer and _XPutXCBBuffer calls must be paired and must not
- * be nested. */
-
-void _XGetXCBBuffer(Display *dpy);
-void _XPutXCBBuffer(Display *dpy);
-
 #endif /* XXCBINT_H */
diff --git a/src/xcb_io.c b/src/xcb_io.c
index dcbe5a8..830ddb9 100644
--- a/src/xcb_io.c
+++ b/src/xcb_io.c
@@ -5,12 +5,50 @@
 #include "locking.h"
 #include "Xxcbint.h"
 #include <xcb/xcbext.h>
-#include <xcb/xcbxlib.h>
 
 #include <assert.h>
+#include <inttypes.h>
+#include <stdint.h>
 #include <stdlib.h>
 #include <string.h>
 
+static void return_socket(void *closure)
+{
+	Display *dpy = closure;
+	LockDisplay(dpy);
+	_XSend(dpy, 0, 0);
+	dpy->bufmax = dpy->buffer;
+	UnlockDisplay(dpy);
+}
+
+static void require_socket(Display *dpy)
+{
+	if(dpy->bufmax == dpy->buffer)
+	{
+		uint64_t sent;
+		int flags = 0;
+		/* if we don't own the event queue, we have to ask XCB
+		 * to set our errors aside for us. */
+		if(dpy->xcb->event_owner != XlibOwnsEventQueue)
+			flags = XCB_REQUEST_CHECKED;
+		if(!xcb_take_socket(dpy->xcb->connection, return_socket, dpy,
+		                    flags, &sent))
+			_XIOError(dpy);
+		/* Xlib uses unsigned long for sequence numbers.  XCB
+		 * uses 64-bit internally, but currently exposes an
+		 * unsigned int API.  If these differ, Xlib cannot track
+		 * the full 64-bit sequence number if 32-bit wrap
+		 * happens while Xlib does not own the socket.  A
+		 * complete fix would be to make XCB's public API use
+		 * 64-bit sequence numbers. */
+		assert(!(sizeof(unsigned long) > sizeof(unsigned int)
+		         && dpy->xcb->event_owner == XlibOwnsEventQueue
+		         && (sent - dpy->last_request_read >= (UINT64_C(1) << 32))));
+		dpy->xcb->last_flushed = dpy->request = sent;
+		dpy->bufmax = dpy->xcb->real_bufmax;
+	}
+}
+
 /* Call internal connection callbacks for any fds that are currently
  * ready to read. This function will not block unless one of the
  * callbacks blocks.
@@ -68,15 +106,6 @@ static void check_internal_connections(Display *dpy)
 		}
 }
 
-static void condition_wait(Display *dpy, xcondition_t cv)
-{
-	_XPutXCBBuffer(dpy);
-	xcb_xlib_unlock(dpy->xcb->connection);
-	ConditionWait(dpy, cv);
-	xcb_xlib_lock(dpy->xcb->connection);
-	_XGetXCBBuffer(dpy);
-}
-
 static void call_handlers(Display *dpy, xcb_generic_reply_t *buf)
 {
 	_XAsyncHandler *async, *next;
@@ -105,7 +134,17 @@ static xcb_generic_event_t * wait_or_poll_for_event(Display *dpy, int wait)
 	return event;
 }
 
-static void process_responses(Display *dpy, int wait_for_first_event, xcb_generic_error_t **current_error, unsigned int current_request)
+/* Widen a 32-bit sequence number into a native-word-size (unsigned long)
+ * sequence number.  Treating the comparison as a 1 and shifting it avoids a
+ * conditional branch, and shifting by 16 twice avoids a compiler warning when
+ * sizeof(unsigned long) == 4. */
+static void widen(unsigned long *wide, unsigned int narrow)
+{
+	unsigned long new = (*wide & ~0xFFFFFFFFUL) | narrow;
+	*wide = new + ((unsigned long) (new < *wide) << 16 << 16);
+}
+
+static void process_responses(Display *dpy, int wait_for_first_event, xcb_generic_error_t **current_error, unsigned long current_request)
 {
 	void *reply;
 	xcb_generic_event_t *event = dpy->xcb->next_event;
@@ -114,13 +153,18 @@ static void process_responses(Display *dpy, int wait_for_first_event, xcb_generi
 	if(!event && dpy->xcb->event_owner == XlibOwnsEventQueue)
 		event = wait_or_poll_for_event(dpy, wait_for_first_event);
 
+	require_socket(dpy);
+
 	while(1)
 	{
 		PendingRequest *req = dpy->xcb->pending_requests;
-		assert(!(req && current_request && !XCB_SEQUENCE_COMPARE(req->sequence, <=, current_request)));
-		if(event && (!req || XCB_SEQUENCE_COMPARE(event->full_sequence, <=, req->sequence)))
+		unsigned long event_sequence = dpy->last_request_read;
+		if(event)
+			widen(&event_sequence, event->full_sequence);
+		assert(!(req && current_request && !XLIB_SEQUENCE_COMPARE(req->sequence, <=, current_request)));
+		if(event && (!req || XLIB_SEQUENCE_COMPARE(event_sequence, <=, req->sequence)))
 		{
-			dpy->last_request_read = event->full_sequence;
+			dpy->last_request_read = event_sequence;
 			if(event->response_type != X_Error)
 			{
 				/* GenericEvents may be > 32 bytes. In this
@@ -139,7 +183,7 @@ static void process_responses(Display *dpy, int wait_for_first_event, xcb_generi
 				_XEnq(dpy, (xEvent *) event);
 				wait_for_first_event = 0;
 			}
-			else if(current_error && event->full_sequence == current_request)
+			else if(current_error && event_sequence == current_request)
 			{
 				/* This can only occur when called from
 				 * _XReply, which doesn't need a new event. */
@@ -152,18 +196,9 @@ static void process_responses(Display *dpy, int wait_for_first_event, xcb_generi
 			free(event);
 			event = wait_or_poll_for_event(dpy, wait_for_first_event);
 		}
-		else if(req && req->waiters != -1)
+		else if(req && req->sequence == current_request)
 		{
-			if(req->sequence == current_request)
-				break;
-			if(!current_request && !wait_for_first_event)
-				break;
-			dpy->xcb->next_event = event;
-			req->waiters++;
-			assert(req->waiters > 0);
-			condition_wait(dpy, &req->condition);
-			--req->waiters;
-			event = dpy->xcb->next_event;
+			break;
 		}
 		else if(req && xcb_poll_for_reply(dpy->xcb->connection, req->sequence, &reply, &error))
 		{
@@ -192,7 +227,7 @@ static void process_responses(Display *dpy, int wait_for_first_event, xcb_generi
 	if(xcb_connection_has_error(c))
 		_XIOError(dpy);
 
-	assert_sequence_less(dpy->last_request_read, dpy->request);
+	assert(XLIB_SEQUENCE_COMPARE(dpy->last_request_read, <=, dpy->request));
 	assert(!wait_for_first_event);
 }
 
@@ -234,32 +269,61 @@ void _XReadEvents(Display *dpy)
  */
 void _XSend(Display *dpy, const char *data, long size)
 {
+	static const xReq dummy_request;
+	static char const pad[3];
+	struct iovec vec[3];
+	uint64_t requests;
+	_XExtension *ext;
 	xcb_connection_t *c = dpy->xcb->connection;
 	if(dpy->flags & XlibDisplayIOError)
 		return;
 
-	assert(!dpy->xcb->request_extra);
-	dpy->xcb->request_extra = data;
-	dpy->xcb->request_extra_size = size;
+	if(dpy->bufptr == dpy->buffer && !size)
+		return;
+
+	/* iff we asked XCB to set aside errors, we must pick those up
+	 * eventually. iff there are async handlers, we may have just
+	 * issued requests that will generate replies. in either case,
+	 * we need to remember to check later. */
+	if(dpy->xcb->event_owner != XlibOwnsEventQueue || dpy->async_handlers)
+	{
+		unsigned int sequence;
+		for(sequence = dpy->xcb->last_flushed; sequence < dpy->request; ++sequence)
+		{
+			PendingRequest *req = malloc(sizeof(PendingRequest));
+			assert(req);
+			req->next = 0;
+			req->sequence = sequence;
+			*dpy->xcb->pending_requests_tail = req;
+			dpy->xcb->pending_requests_tail = &req->next;
+		}
+	}
+	requests = dpy->request - dpy->xcb->last_flushed;
+	dpy->xcb->last_flushed = dpy->request;
 
-	/* give dpy->buffer to XCB */
-	_XPutXCBBuffer(dpy);
+	vec[0].iov_base = dpy->buffer;
+	vec[0].iov_len = dpy->bufptr - dpy->buffer;
+	vec[1].iov_base = (caddr_t) data;
+	vec[1].iov_len = size;
+	vec[2].iov_base = (caddr_t) pad;
+	vec[2].iov_len = -size & 3;
 
-	if(xcb_flush(c) <= 0)
-		_XIOError(dpy);
+	for(ext = dpy->flushes; ext; ext = ext->next_flush)
+	{
+		int i;
+		for(i = 0; i < 3; ++i)
+			if(vec[i].iov_len)
+				ext->before_flush(dpy, &ext->codes, vec[i].iov_base, vec[i].iov_len);
+	}
 
-	/* get a new dpy->buffer */
-	_XGetXCBBuffer(dpy);
+	if(xcb_writev(c, vec, 3, requests) < 0)
+		_XIOError(dpy);
+	dpy->bufptr = dpy->buffer;
+	dpy->last_req = (char *) &dummy_request;
 
 	check_internal_connections(dpy);
 
-	/* A straight port of XlibInt.c would call _XSetSeqSyncFunction
-	 * here. However that does no good: unlike traditional Xlib,
-	 * Xlib/XCB almost never calls _XFlush because _XPutXCBBuffer
-	 * automatically pushes requests down into XCB, so Xlib's buffer
-	 * is empty most of the time. Since setting a synchandler has no
-	 * effect until after UnlockDisplay returns, we may as well do
-	 * the check in _XUnlockDisplay. */
+	_XSetSeqSyncFunction(dpy);
 }
 
 /*
@@ -268,6 +332,7 @@ void _XSend(Display *dpy, const char *data, long size)
  */
 void _XFlush(Display *dpy)
 {
+	require_socket(dpy);
 	_XSend(dpy, 0, 0);
 
 	_XEventsQueued(dpy, QueuedAfterReading);
@@ -308,10 +373,18 @@ XID _XAllocID(Display *dpy)
 void _XAllocIDs(Display *dpy, XID *ids, int count)
 {
 	int i;
-	_XPutXCBBuffer(dpy);
+#ifdef XTHREADS
+	if (dpy->lock)
+		(*dpy->lock->user_lock_display)(dpy);
+	UnlockDisplay(dpy);
+#endif
 	for (i = 0; i < count; i++)
 		ids[i] = xcb_generate_id(dpy->xcb->connection);
-	_XGetXCBBuffer(dpy);
+#ifdef XTHREADS
+	LockDisplay(dpy);
+	if (dpy->lock)
+		(*dpy->lock->user_unlock_display)(dpy);
+#endif
 }
 
 static void _XFreeReplyData(Display *dpy, Bool force)
@@ -325,16 +398,9 @@ static void _XFreeReplyData(Display *dpy, Bool force)
 static PendingRequest * insert_pending_request(Display *dpy)
 {
 	PendingRequest **cur = &dpy->xcb->pending_requests;
-	while(*cur && XCB_SEQUENCE_COMPARE((*cur)->sequence, <, dpy->request))
+	while(*cur && XLIB_SEQUENCE_COMPARE((*cur)->sequence, <, dpy->request))
 		cur = &((*cur)->next);
-	if(*cur && (*cur)->sequence == dpy->request)
-	{
-		/* Replacing an existing PendingRequest should only happen once,
-		   when calling _XReply, and the replaced PendingRequest must
-		   not have a condition set. */
-		assert((*cur)->waiters == -1);
-	}
-	else
+	if(!*cur || (*cur)->sequence != dpy->request)
 	{
 		PendingRequest *node = malloc(sizeof(PendingRequest));
 		assert(node);
@@ -344,8 +410,6 @@ static PendingRequest * insert_pending_request(Display *dpy)
 			dpy->xcb->pending_requests_tail = &(node->next);
 		*cur = node;
 	}
-	(*cur)->waiters = 0;
-	xcondition_init(&((*cur)->condition));
 	return *cur;
 }
 
@@ -367,27 +431,15 @@ Status _XReply(Display *dpy, xReply *rep, int extra, Bool discard)
 	if(dpy->flags & XlibDisplayIOError)
 		return 0;
 
-	/* Internals of UnlockDisplay done by hand here, so that we can
-	   insert_pending_request *after* we _XPutXCBBuffer, but before we
-	   unlock the display. */
-	_XPutXCBBuffer(dpy);
+	_XSend(dpy, 0, 0);
 	current = insert_pending_request(dpy);
-	if(!dpy->lock || dpy->lock->locking_level == 0)
-		xcb_xlib_unlock(dpy->xcb->connection);
-	if(dpy->xcb->lock_fns.unlock_display)
-		dpy->xcb->lock_fns.unlock_display(dpy);
+	/* FIXME: drop the Display lock while waiting?
+	 * Complicates process_responses. */
 	reply = xcb_wait_for_reply(c, current->sequence, &error);
-	LockDisplay(dpy);
 
 	check_internal_connections(dpy);
 	process_responses(dpy, 0, &error, current->sequence);
 
-	if(current->waiters)
-	{ /* The ConditionBroadcast macro contains an if; braces needed here. */
-		ConditionBroadcast(dpy, &current->condition);
-	}
-	--current->waiters;
-
 	if(error)
 	{
 		_XExtension *ext;
diff --git a/src/xcb_lock.c b/src/xcb_lock.c
deleted file mode 100644
index 71b2383..0000000
--- a/src/xcb_lock.c
+++ /dev/null
@@ -1,235 +0,0 @@
-/* Copyright (C) 2003-2006 Jamey Sharp, Josh Triplett
- * This file is licensed under the MIT license. See the file COPYING. */
-
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-
-#include "Xlibint.h"
-#include "locking.h"
-#include "Xxcbint.h"
-#include <xcb/xcbext.h>
-#include <xcb/xcbxlib.h>
-
-#include <pthread.h>
-
-static void _XCBLockDisplay(Display *dpy)
-{
-    if(dpy->xcb->lock_fns.lock_display)
-	dpy->xcb->lock_fns.lock_display(dpy);
-    if(!dpy->lock || dpy->lock->locking_level == 0)
-	xcb_xlib_lock(dpy->xcb->connection);
-    if(!(dpy->flags & XlibDisplayIOError))
-	_XGetXCBBuffer(dpy);
-}
-
-/* XXX: If you change this function, update _XReply's copy of its guts! */
-static void _XCBUnlockDisplay(Display *dpy)
-{
-    if(!(dpy->flags & XlibDisplayIOError))
-    {
-	_XPutXCBBuffer(dpy);
-	assert(dpy->xcb->partial_request == 0);
-	assert(xcb_get_request_sent(dpy->xcb->connection) == dpy->request);
-
-	/* Traditional Xlib does this in _XSend; see the Xlib/XCB version
-	 * of that function for why we do it here instead. */
-	_XSetSeqSyncFunction(dpy);
-    }
-
-    if(!dpy->lock || dpy->lock->locking_level == 0)
-	xcb_xlib_unlock(dpy->xcb->connection);
-    if(dpy->xcb->lock_fns.unlock_display)
-	dpy->xcb->lock_fns.unlock_display(dpy);
-}
-
-int _XCBInitDisplayLock(Display *dpy)
-{
-    if(!dpy->lock_fns && !(dpy->lock_fns = Xcalloc(1, sizeof(*dpy->lock_fns))))
-	return 0;
-    dpy->xcb->lock_fns.lock_display = dpy->lock_fns->lock_display;
-    dpy->lock_fns->lock_display = _XCBLockDisplay;
-    dpy->xcb->lock_fns.unlock_display = dpy->lock_fns->unlock_display;
-    dpy->lock_fns->unlock_display = _XCBUnlockDisplay;
-    return 1;
-}
-
-void _XCBShutdownDisplayLock(Display *dpy)
-{
-    if(dpy->lock_fns) {
-	Xfree((char *)dpy->lock_fns);
-	dpy->lock_fns = NULL;
-    }
-}
-
-void _XGetXCBBuffer(Display *dpy)
-{
-    static const xReq dummy_request;
-    unsigned int xcb_req = xcb_get_request_sent(dpy->xcb->connection);
-    if(xcb_connection_has_error(dpy->xcb->connection))
-	_XIOError(dpy);
-
-    /* if Xlib has a partial request pending then XCB doesn't know about
-     * the current request yet */
-    if(dpy->xcb->partial_request)
-	++xcb_req;
-
-    assert(XCB_SEQUENCE_COMPARE(xcb_req, >=, dpy->request));
-    dpy->request = xcb_req;
-
-    dpy->last_req = (char *) &dummy_request;
-}
-
-static size_t request_length(struct iovec *vec)
-{
-    /* we have at least part of a request. dig out the length field.
-     * note that length fields are always in vec[0]: Xlib doesn't split
-     * fixed-length request parts. */
-    size_t len;
-    assert(vec[0].iov_len >= 4);
-    len = ((uint16_t *) vec[0].iov_base)[1];
-    if(len == 0)
-    {
-	/* it's a bigrequest. dig out the *real* length field. */
-	assert(vec[0].iov_len >= 8);
-	len = ((uint32_t *) vec[0].iov_base)[1];
-    }
-    return len << 2;
-}
-
-static inline int issue_complete_request(Display *dpy, int veclen, struct iovec *vec)
-{
-    xcb_protocol_request_t xcb_req = { 0 };
-    unsigned int sequence;
-    int flags = XCB_REQUEST_RAW;
-    int i;
-    size_t len;
-
-    /* skip empty iovecs. if no iovecs remain, we're done. */
-    assert(veclen >= 0);
-    while(veclen > 0 && vec[0].iov_len == 0)
-	--veclen, ++vec;
-    if(!veclen)
-	return 0;
-
-    len = request_length(vec);
-
-    /* do we have enough data for a complete request? how many iovec
-     * elements does it span? */
-    for(i = 0; i < veclen; ++i)
-    {
-	size_t oldlen = len;
-	len -= vec[i].iov_len;
-	/* if len is now 0 or has wrapped, we have enough data. */
-	if((len - 1) > oldlen)
-	    break;
-    }
-    if(i == veclen)
-	return 0;
-
-    /* we have enough data to issue one complete request. the remaining
-     * code can't fail. */
-
-    /* len says how far we overshot our data needs. (it's "negative" if
-     * we actually overshot, or 0 if we're right on.) */
-    vec[i].iov_len += len;
-    xcb_req.count = i + 1;
-    xcb_req.opcode = ((uint8_t *) vec[0].iov_base)[0];
-
-    /* if we don't own the event queue, we have to ask XCB to set our
-     * errors aside for us. */
-    if(dpy->xcb->event_owner != XlibOwnsEventQueue)
-	flags |= XCB_REQUEST_CHECKED;
-
-    /* XCB will always skip request 0; account for that in the Xlib count */
-    if (xcb_get_request_sent(dpy->xcb->connection) == 0xffffffff)
-	dpy->request++;
-    /* send the accumulated request. */
-    sequence = xcb_send_request(dpy->xcb->connection, flags, vec, &xcb_req);
-    if(!sequence)
-	_XIOError(dpy);
-
-    /* update the iovecs to refer only to data not yet sent. */
-    vec[i].iov_len = -len;
-
-    /* iff we asked XCB to set aside errors, we must pick those up
-     * eventually. iff there are async handlers, we may have just
-     * issued requests that will generate replies. in either case,
-     * we need to remember to check later. */
-    if(flags & XCB_REQUEST_CHECKED || dpy->async_handlers)
-    {
-	PendingRequest *req = malloc(sizeof(PendingRequest));
-	assert(req);
-	req->next = 0;
-	req->waiters = -1;
-	req->sequence = sequence;
-	*dpy->xcb->pending_requests_tail = req;
-	dpy->xcb->pending_requests_tail = &req->next;
-    }
-    return 1;
-}
-
-void _XPutXCBBuffer(Display *dpy)
-{
-    static char const pad[3];
-    const int padsize = -dpy->xcb->request_extra_size & 3;
-    xcb_connection_t *c = dpy->xcb->connection;
-    _XExtension *ext;
-    struct iovec iov[6];
-
-    assert_sequence_less(dpy->last_request_read, dpy->request);
-    assert_sequence_less(xcb_get_request_sent(c), dpy->request);
-
-    for(ext = dpy->flushes; ext; ext = ext->next_flush)
-    {
-	ext->before_flush(dpy, &ext->codes, dpy->buffer, dpy->bufptr - dpy->buffer);
-	if(dpy->xcb->request_extra)
-	{
-	    ext->before_flush(dpy, &ext->codes, dpy->xcb->request_extra, dpy->xcb->request_extra_size);
-	    if(padsize)
-		ext->before_flush(dpy, &ext->codes, pad, padsize);
-	}
-    }
-
-    iov[2].iov_base = dpy->xcb->partial_request;
-    iov[2].iov_len = dpy->xcb->partial_request_offset;
-    iov[3].iov_base = dpy->buffer;
-    iov[3].iov_len = dpy->bufptr - dpy->buffer;
-    iov[4].iov_base = (caddr_t) dpy->xcb->request_extra;
-    iov[4].iov_len = dpy->xcb->request_extra_size;
-    iov[5].iov_base = (caddr_t) pad;
-    iov[5].iov_len = padsize;
-
-    while(issue_complete_request(dpy, 4, iov + 2))
-	/* empty */;
-
-    /* first discard any completed partial_request. */
-    if(iov[2].iov_len == 0 && dpy->xcb->partial_request)
-    {
-	free(dpy->xcb->partial_request);
-	dpy->xcb->partial_request = 0;
-	dpy->xcb->partial_request_offset = 0;
-    }
-
-    /* is there anything to copy into partial_request? */
-    if(iov[3].iov_len != 0 || iov[4].iov_len != 0 || iov[5].iov_len != 0)
-    {
-	int i;
-	if(!dpy->xcb->partial_request)
-	{
-	    size_t len = request_length(iov + 3);
-	    assert(!dpy->xcb->partial_request_offset);
-	    dpy->xcb->partial_request = malloc(len);
-	    assert(dpy->xcb->partial_request);
-	}
-	for(i = 3; i < sizeof(iov) / sizeof(*iov); ++i)
-	{
-	    memcpy(dpy->xcb->partial_request + dpy->xcb->partial_request_offset, iov[i].iov_base, iov[i].iov_len);
-	    dpy->xcb->partial_request_offset += iov[i].iov_len;
-	}
-    }
-
-    dpy->xcb->request_extra = 0;
-    dpy->xcb->request_extra_size = 0;
-    dpy->bufptr = dpy->buffer;
-}
-- 
1.6.0.2.307.gc427.dirty