xcb_conn.c revision 1c7386f4
1602e473dSmrg/* Copyright (C) 2001-2004 Bart Massey and Jamey Sharp. 2602e473dSmrg * 3602e473dSmrg * Permission is hereby granted, free of charge, to any person obtaining a 4602e473dSmrg * copy of this software and associated documentation files (the "Software"), 5602e473dSmrg * to deal in the Software without restriction, including without limitation 6602e473dSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 7602e473dSmrg * and/or sell copies of the Software, and to permit persons to whom the 8602e473dSmrg * Software is furnished to do so, subject to the following conditions: 9602e473dSmrg * 10602e473dSmrg * The above copyright notice and this permission notice shall be included in 11602e473dSmrg * all copies or substantial portions of the Software. 12602e473dSmrg * 13602e473dSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14602e473dSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15602e473dSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16602e473dSmrg * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 17602e473dSmrg * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 18602e473dSmrg * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 19602e473dSmrg * 20602e473dSmrg * Except as contained in this notice, the names of the authors or their 21602e473dSmrg * institutions shall not be used in advertising or otherwise to promote the 22602e473dSmrg * sale, use or other dealings in this Software without prior written 23602e473dSmrg * authorization from the authors. 24602e473dSmrg */ 25602e473dSmrg 26602e473dSmrg/* Connection management: the core of XCB. */ 27602e473dSmrg 2821298544Smrg#ifdef HAVE_CONFIG_H 2921298544Smrg#include "config.h" 3021298544Smrg#endif 3121298544Smrg 32602e473dSmrg#include <assert.h> 33602e473dSmrg#include <string.h> 34602e473dSmrg#include <stdio.h> 35602e473dSmrg#include <unistd.h> 36602e473dSmrg#include <stdlib.h> 37602e473dSmrg#include <fcntl.h> 38602e473dSmrg#include <errno.h> 3921298544Smrg#include <limits.h> 40602e473dSmrg 41602e473dSmrg#include "xcb.h" 42602e473dSmrg#include "xcbint.h" 43602e473dSmrg#if USE_POLL 44602e473dSmrg#include <poll.h> 4521298544Smrg#elif !defined _WIN32 46602e473dSmrg#include <sys/select.h> 47602e473dSmrg#endif 48602e473dSmrg 4921298544Smrg#ifdef _WIN32 5021298544Smrg#include "xcb_windefs.h" 5121298544Smrg#else 5221298544Smrg#include <sys/socket.h> 5321298544Smrg#include <netinet/in.h> 5421298544Smrg#endif /* _WIN32 */ 5521298544Smrg 5621298544Smrg/* SHUT_RDWR is fairly recent and is not available on all platforms */ 5721298544Smrg#if !defined(SHUT_RDWR) 5821298544Smrg#define SHUT_RDWR 2 5921298544Smrg#endif 6021298544Smrg 61602e473dSmrgtypedef struct { 62602e473dSmrg uint8_t status; 63602e473dSmrg uint8_t pad0[5]; 64602e473dSmrg uint16_t length; 65602e473dSmrg} xcb_setup_generic_t; 66602e473dSmrg 671c7386f4Smrg/* Keep this list in sync with is_static_error_conn()! */ 6821298544Smrgstatic const int xcb_con_error = XCB_CONN_ERROR; 6921298544Smrgstatic const int xcb_con_closed_mem_er = XCB_CONN_CLOSED_MEM_INSUFFICIENT; 7021298544Smrgstatic const int xcb_con_closed_parse_er = XCB_CONN_CLOSED_PARSE_ERR; 7121298544Smrgstatic const int xcb_con_closed_screen_er = XCB_CONN_CLOSED_INVALID_SCREEN; 72602e473dSmrg 731c7386f4Smrgstatic int is_static_error_conn(xcb_connection_t *c) 741c7386f4Smrg{ 751c7386f4Smrg return c == (xcb_connection_t *) &xcb_con_error || 761c7386f4Smrg c == (xcb_connection_t *) &xcb_con_closed_mem_er || 771c7386f4Smrg c == (xcb_connection_t *) &xcb_con_closed_parse_er || 781c7386f4Smrg c == (xcb_connection_t *) &xcb_con_closed_screen_er; 791c7386f4Smrg} 801c7386f4Smrg 81602e473dSmrgstatic int set_fd_flags(const int fd) 82602e473dSmrg{ 8321298544Smrg/* Win32 doesn't have file descriptors and the fcntl function. This block sets the socket in non-blocking mode */ 8421298544Smrg 8521298544Smrg#ifdef _WIN32 861c7386f4Smrg u_long iMode = 1; /* non-zero puts it in non-blocking mode, 0 in blocking mode */ 8721298544Smrg int ret = 0; 8821298544Smrg 8921298544Smrg ret = ioctlsocket(fd, FIONBIO, &iMode); 901c7386f4Smrg if(ret != 0) 9121298544Smrg return 0; 9221298544Smrg return 1; 9321298544Smrg#else 94602e473dSmrg int flags = fcntl(fd, F_GETFL, 0); 95602e473dSmrg if(flags == -1) 96602e473dSmrg return 0; 97602e473dSmrg flags |= O_NONBLOCK; 98602e473dSmrg if(fcntl(fd, F_SETFL, flags) == -1) 99602e473dSmrg return 0; 100602e473dSmrg if(fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) 101602e473dSmrg return 0; 102602e473dSmrg return 1; 10321298544Smrg#endif /* _WIN32 */ 104602e473dSmrg} 105602e473dSmrg 106602e473dSmrgstatic int write_setup(xcb_connection_t *c, xcb_auth_info_t *auth_info) 107602e473dSmrg{ 108602e473dSmrg static const char pad[3]; 109602e473dSmrg xcb_setup_request_t out; 110602e473dSmrg struct iovec parts[6]; 111602e473dSmrg int count = 0; 112602e473dSmrg static const uint32_t endian = 0x01020304; 113602e473dSmrg int ret; 114602e473dSmrg 115602e473dSmrg memset(&out, 0, sizeof(out)); 116602e473dSmrg 117602e473dSmrg /* B = 0x42 = MSB first, l = 0x6c = LSB first */ 118602e473dSmrg if(htonl(endian) == endian) 119602e473dSmrg out.byte_order = 0x42; 120602e473dSmrg else 121602e473dSmrg out.byte_order = 0x6c; 122602e473dSmrg out.protocol_major_version = X_PROTOCOL; 123602e473dSmrg out.protocol_minor_version = X_PROTOCOL_REVISION; 124602e473dSmrg out.authorization_protocol_name_len = 0; 125602e473dSmrg out.authorization_protocol_data_len = 0; 126602e473dSmrg parts[count].iov_len = sizeof(xcb_setup_request_t); 127602e473dSmrg parts[count++].iov_base = &out; 128602e473dSmrg parts[count].iov_len = XCB_PAD(sizeof(xcb_setup_request_t)); 129602e473dSmrg parts[count++].iov_base = (char *) pad; 130602e473dSmrg 131602e473dSmrg if(auth_info) 132602e473dSmrg { 133602e473dSmrg parts[count].iov_len = out.authorization_protocol_name_len = auth_info->namelen; 134602e473dSmrg parts[count++].iov_base = auth_info->name; 135602e473dSmrg parts[count].iov_len = XCB_PAD(out.authorization_protocol_name_len); 136602e473dSmrg parts[count++].iov_base = (char *) pad; 137602e473dSmrg parts[count].iov_len = out.authorization_protocol_data_len = auth_info->datalen; 138602e473dSmrg parts[count++].iov_base = auth_info->data; 139602e473dSmrg parts[count].iov_len = XCB_PAD(out.authorization_protocol_data_len); 140602e473dSmrg parts[count++].iov_base = (char *) pad; 141602e473dSmrg } 142602e473dSmrg assert(count <= (int) (sizeof(parts) / sizeof(*parts))); 143602e473dSmrg 144602e473dSmrg pthread_mutex_lock(&c->iolock); 145b20a2039Smrg ret = _xcb_out_send(c, parts, count); 146602e473dSmrg pthread_mutex_unlock(&c->iolock); 147602e473dSmrg return ret; 148602e473dSmrg} 149602e473dSmrg 150602e473dSmrgstatic int read_setup(xcb_connection_t *c) 151602e473dSmrg{ 152602e473dSmrg /* Read the server response */ 153602e473dSmrg c->setup = malloc(sizeof(xcb_setup_generic_t)); 154602e473dSmrg if(!c->setup) 155602e473dSmrg return 0; 156602e473dSmrg 157602e473dSmrg if(_xcb_in_read_block(c, c->setup, sizeof(xcb_setup_generic_t)) != sizeof(xcb_setup_generic_t)) 158602e473dSmrg return 0; 159602e473dSmrg 160602e473dSmrg { 161602e473dSmrg void *tmp = realloc(c->setup, c->setup->length * 4 + sizeof(xcb_setup_generic_t)); 162602e473dSmrg if(!tmp) 163602e473dSmrg return 0; 164602e473dSmrg c->setup = tmp; 165602e473dSmrg } 166602e473dSmrg 167602e473dSmrg if(_xcb_in_read_block(c, (char *) c->setup + sizeof(xcb_setup_generic_t), c->setup->length * 4) <= 0) 168602e473dSmrg return 0; 169602e473dSmrg 170602e473dSmrg /* 0 = failed, 2 = authenticate, 1 = success */ 171602e473dSmrg switch(c->setup->status) 172602e473dSmrg { 173602e473dSmrg case 0: /* failed */ 174602e473dSmrg { 175602e473dSmrg xcb_setup_failed_t *setup = (xcb_setup_failed_t *) c->setup; 176602e473dSmrg write(STDERR_FILENO, xcb_setup_failed_reason(setup), xcb_setup_failed_reason_length(setup)); 177602e473dSmrg return 0; 178602e473dSmrg } 179602e473dSmrg 180602e473dSmrg case 2: /* authenticate */ 181602e473dSmrg { 182602e473dSmrg xcb_setup_authenticate_t *setup = (xcb_setup_authenticate_t *) c->setup; 183602e473dSmrg write(STDERR_FILENO, xcb_setup_authenticate_reason(setup), xcb_setup_authenticate_reason_length(setup)); 184602e473dSmrg return 0; 185602e473dSmrg } 186602e473dSmrg } 187602e473dSmrg 188602e473dSmrg return 1; 189602e473dSmrg} 190602e473dSmrg 191602e473dSmrg/* precondition: there must be something for us to write. */ 192602e473dSmrgstatic int write_vec(xcb_connection_t *c, struct iovec **vector, int *count) 193602e473dSmrg{ 194602e473dSmrg int n; 195602e473dSmrg assert(!c->out.queue_len); 19621298544Smrg 19721298544Smrg#ifdef _WIN32 19821298544Smrg int i = 0; 19921298544Smrg int ret = 0,err = 0; 20021298544Smrg struct iovec *vec; 20121298544Smrg n = 0; 20221298544Smrg 20321298544Smrg /* Could use the WSASend win32 function for scatter/gather i/o but setting up the WSABUF struct from 20421298544Smrg an iovec would require more work and I'm not sure of the benefit....works for now */ 20521298544Smrg vec = *vector; 20621298544Smrg while(i < *count) 2071c7386f4Smrg { 2081c7386f4Smrg ret = send(c->fd,vec->iov_base,vec->iov_len,0); 20921298544Smrg if(ret == SOCKET_ERROR) 21021298544Smrg { 21121298544Smrg err = WSAGetLastError(); 21221298544Smrg if(err == WSAEWOULDBLOCK) 21321298544Smrg { 21421298544Smrg return 1; 21521298544Smrg } 21621298544Smrg } 21721298544Smrg n += ret; 21821298544Smrg *vec++; 21921298544Smrg i++; 22021298544Smrg } 22121298544Smrg#else 22221298544Smrg n = *count; 22321298544Smrg if (n > IOV_MAX) 2241c7386f4Smrg n = IOV_MAX; 22521298544Smrg 2261016ad83Smrg#if HAVE_SENDMSG 2271016ad83Smrg if (c->out.out_fd.nfd) { 2281016ad83Smrg union { 2291016ad83Smrg struct cmsghdr cmsghdr; 2301016ad83Smrg char buf[CMSG_SPACE(XCB_MAX_PASS_FD * sizeof(int))]; 2311016ad83Smrg } cmsgbuf; 2321016ad83Smrg struct msghdr msg = { 2331016ad83Smrg .msg_name = NULL, 2341016ad83Smrg .msg_namelen = 0, 2351016ad83Smrg .msg_iov = *vector, 2361016ad83Smrg .msg_iovlen = n, 2371016ad83Smrg .msg_control = cmsgbuf.buf, 2381016ad83Smrg .msg_controllen = CMSG_LEN(c->out.out_fd.nfd * sizeof (int)), 2391016ad83Smrg }; 2401016ad83Smrg int i; 2411016ad83Smrg struct cmsghdr *hdr = CMSG_FIRSTHDR(&msg); 2421016ad83Smrg 2431016ad83Smrg hdr->cmsg_len = msg.msg_controllen; 2441016ad83Smrg hdr->cmsg_level = SOL_SOCKET; 2451016ad83Smrg hdr->cmsg_type = SCM_RIGHTS; 2461016ad83Smrg memcpy(CMSG_DATA(hdr), c->out.out_fd.fd, c->out.out_fd.nfd * sizeof (int)); 2471016ad83Smrg 2481016ad83Smrg n = sendmsg(c->fd, &msg, 0); 2491016ad83Smrg if(n < 0 && errno == EAGAIN) 2501016ad83Smrg return 1; 2511016ad83Smrg for (i = 0; i < c->out.out_fd.nfd; i++) 2521016ad83Smrg close(c->out.out_fd.fd[i]); 2531016ad83Smrg c->out.out_fd.nfd = 0; 2541016ad83Smrg } else 2551016ad83Smrg#endif 2561016ad83Smrg { 2571016ad83Smrg n = writev(c->fd, *vector, n); 2581016ad83Smrg if(n < 0 && errno == EAGAIN) 2591016ad83Smrg return 1; 2601016ad83Smrg } 2611016ad83Smrg 2621c7386f4Smrg#endif /* _WIN32 */ 26321298544Smrg 264602e473dSmrg if(n <= 0) 265602e473dSmrg { 26621298544Smrg _xcb_conn_shutdown(c, XCB_CONN_ERROR); 267602e473dSmrg return 0; 268602e473dSmrg } 269602e473dSmrg 270602e473dSmrg for(; *count; --*count, ++*vector) 271602e473dSmrg { 272602e473dSmrg int cur = (*vector)->iov_len; 273602e473dSmrg if(cur > n) 274602e473dSmrg cur = n; 275602e473dSmrg (*vector)->iov_len -= cur; 276602e473dSmrg (*vector)->iov_base = (char *) (*vector)->iov_base + cur; 277602e473dSmrg n -= cur; 278602e473dSmrg if((*vector)->iov_len) 279602e473dSmrg break; 280602e473dSmrg } 281602e473dSmrg if(!*count) 282602e473dSmrg *vector = 0; 283602e473dSmrg assert(n == 0); 284602e473dSmrg return 1; 285602e473dSmrg} 286602e473dSmrg 287602e473dSmrg/* Public interface */ 288602e473dSmrg 289602e473dSmrgconst xcb_setup_t *xcb_get_setup(xcb_connection_t *c) 290602e473dSmrg{ 291602e473dSmrg if(c->has_error) 292602e473dSmrg return 0; 293602e473dSmrg /* doesn't need locking because it's never written to. */ 294602e473dSmrg return c->setup; 295602e473dSmrg} 296602e473dSmrg 297602e473dSmrgint xcb_get_file_descriptor(xcb_connection_t *c) 298602e473dSmrg{ 299602e473dSmrg if(c->has_error) 300602e473dSmrg return -1; 301602e473dSmrg /* doesn't need locking because it's never written to. */ 302602e473dSmrg return c->fd; 303602e473dSmrg} 304602e473dSmrg 305602e473dSmrgint xcb_connection_has_error(xcb_connection_t *c) 306602e473dSmrg{ 307602e473dSmrg /* doesn't need locking because it's read and written atomically. */ 308602e473dSmrg return c->has_error; 309602e473dSmrg} 310602e473dSmrg 311602e473dSmrgxcb_connection_t *xcb_connect_to_fd(int fd, xcb_auth_info_t *auth_info) 312602e473dSmrg{ 313602e473dSmrg xcb_connection_t* c; 314602e473dSmrg 31521298544Smrg#ifndef _WIN32 316602e473dSmrg#ifndef USE_POLL 317602e473dSmrg if(fd >= FD_SETSIZE) /* would overflow in FD_SET */ 318602e473dSmrg { 319602e473dSmrg close(fd); 32021298544Smrg return _xcb_conn_ret_error(XCB_CONN_ERROR); 321602e473dSmrg } 322602e473dSmrg#endif 32321298544Smrg#endif /* !_WIN32*/ 324602e473dSmrg 325602e473dSmrg c = calloc(1, sizeof(xcb_connection_t)); 326602e473dSmrg if(!c) { 327602e473dSmrg close(fd); 32821298544Smrg return _xcb_conn_ret_error(XCB_CONN_CLOSED_MEM_INSUFFICIENT) ; 329602e473dSmrg } 330602e473dSmrg 331602e473dSmrg c->fd = fd; 332602e473dSmrg 333602e473dSmrg if(!( 334602e473dSmrg set_fd_flags(fd) && 335602e473dSmrg pthread_mutex_init(&c->iolock, 0) == 0 && 336602e473dSmrg _xcb_in_init(&c->in) && 337602e473dSmrg _xcb_out_init(&c->out) && 338602e473dSmrg write_setup(c, auth_info) && 339602e473dSmrg read_setup(c) && 340602e473dSmrg _xcb_ext_init(c) && 341602e473dSmrg _xcb_xid_init(c) 342602e473dSmrg )) 343602e473dSmrg { 344602e473dSmrg xcb_disconnect(c); 34521298544Smrg return _xcb_conn_ret_error(XCB_CONN_ERROR); 346602e473dSmrg } 347602e473dSmrg 348602e473dSmrg return c; 349602e473dSmrg} 350602e473dSmrg 351602e473dSmrgvoid xcb_disconnect(xcb_connection_t *c) 352602e473dSmrg{ 3531c7386f4Smrg if(c == NULL || is_static_error_conn(c)) 354602e473dSmrg return; 355602e473dSmrg 356602e473dSmrg free(c->setup); 35721298544Smrg 35821298544Smrg /* disallow further sends and receives */ 35921298544Smrg shutdown(c->fd, SHUT_RDWR); 360602e473dSmrg close(c->fd); 361602e473dSmrg 362602e473dSmrg pthread_mutex_destroy(&c->iolock); 363602e473dSmrg _xcb_in_destroy(&c->in); 364602e473dSmrg _xcb_out_destroy(&c->out); 365602e473dSmrg 366602e473dSmrg _xcb_ext_destroy(c); 367602e473dSmrg _xcb_xid_destroy(c); 368602e473dSmrg 369602e473dSmrg free(c); 37021298544Smrg 37121298544Smrg#ifdef _WIN32 37221298544Smrg WSACleanup(); 37321298544Smrg#endif 374602e473dSmrg} 375602e473dSmrg 376602e473dSmrg/* Private interface */ 377602e473dSmrg 37821298544Smrgvoid _xcb_conn_shutdown(xcb_connection_t *c, int err) 379602e473dSmrg{ 38021298544Smrg c->has_error = err; 38121298544Smrg} 38221298544Smrg 38321298544Smrg/* Return connection error state. 38421298544Smrg * To make thread-safe, I need a seperate static 38521298544Smrg * variable for every possible error. 3861c7386f4Smrg * has_error is the first field in xcb_connection_t, so just 3871c7386f4Smrg * return a casted int here; checking has_error (and only 3881c7386f4Smrg * has_error) will be safe. 38921298544Smrg */ 39021298544Smrgxcb_connection_t *_xcb_conn_ret_error(int err) 39121298544Smrg{ 39221298544Smrg 39321298544Smrg switch(err) 39421298544Smrg { 39521298544Smrg case XCB_CONN_CLOSED_MEM_INSUFFICIENT: 39621298544Smrg { 39721298544Smrg return (xcb_connection_t *) &xcb_con_closed_mem_er; 39821298544Smrg } 39921298544Smrg case XCB_CONN_CLOSED_PARSE_ERR: 40021298544Smrg { 40121298544Smrg return (xcb_connection_t *) &xcb_con_closed_parse_er; 40221298544Smrg } 40321298544Smrg case XCB_CONN_CLOSED_INVALID_SCREEN: 40421298544Smrg { 40521298544Smrg return (xcb_connection_t *) &xcb_con_closed_screen_er; 40621298544Smrg } 40721298544Smrg case XCB_CONN_ERROR: 40821298544Smrg default: 40921298544Smrg { 41021298544Smrg return (xcb_connection_t *) &xcb_con_error; 41121298544Smrg } 41221298544Smrg } 413602e473dSmrg} 414602e473dSmrg 415602e473dSmrgint _xcb_conn_wait(xcb_connection_t *c, pthread_cond_t *cond, struct iovec **vector, int *count) 416602e473dSmrg{ 417602e473dSmrg int ret; 418602e473dSmrg#if USE_POLL 419602e473dSmrg struct pollfd fd; 420602e473dSmrg#else 421602e473dSmrg fd_set rfds, wfds; 422602e473dSmrg#endif 423602e473dSmrg 424602e473dSmrg /* If the thing I should be doing is already being done, wait for it. */ 425602e473dSmrg if(count ? c->out.writing : c->in.reading) 426602e473dSmrg { 427602e473dSmrg pthread_cond_wait(cond, &c->iolock); 428602e473dSmrg return 1; 429602e473dSmrg } 430602e473dSmrg 431602e473dSmrg#if USE_POLL 432602e473dSmrg memset(&fd, 0, sizeof(fd)); 433602e473dSmrg fd.fd = c->fd; 434602e473dSmrg fd.events = POLLIN; 435602e473dSmrg#else 436602e473dSmrg FD_ZERO(&rfds); 437602e473dSmrg FD_SET(c->fd, &rfds); 438602e473dSmrg#endif 439602e473dSmrg ++c->in.reading; 440602e473dSmrg 441602e473dSmrg#if USE_POLL 442602e473dSmrg if(count) 443602e473dSmrg { 444602e473dSmrg fd.events |= POLLOUT; 445602e473dSmrg ++c->out.writing; 446602e473dSmrg } 447602e473dSmrg#else 448602e473dSmrg FD_ZERO(&wfds); 449602e473dSmrg if(count) 450602e473dSmrg { 451602e473dSmrg FD_SET(c->fd, &wfds); 452602e473dSmrg ++c->out.writing; 453602e473dSmrg } 454602e473dSmrg#endif 455602e473dSmrg 456602e473dSmrg pthread_mutex_unlock(&c->iolock); 457602e473dSmrg do { 458602e473dSmrg#if USE_POLL 459b20a2039Smrg ret = poll(&fd, 1, -1); 46021298544Smrg /* If poll() returns an event we didn't expect, such as POLLNVAL, treat 46121298544Smrg * it as if it failed. */ 46221298544Smrg if(ret >= 0 && (fd.revents & ~fd.events)) 46321298544Smrg { 46421298544Smrg ret = -1; 46521298544Smrg break; 46621298544Smrg } 467602e473dSmrg#else 468b20a2039Smrg ret = select(c->fd + 1, &rfds, &wfds, 0, 0); 469602e473dSmrg#endif 470602e473dSmrg } while (ret == -1 && errno == EINTR); 471b20a2039Smrg if(ret < 0) 472602e473dSmrg { 47321298544Smrg _xcb_conn_shutdown(c, XCB_CONN_ERROR); 474b20a2039Smrg ret = 0; 475602e473dSmrg } 476602e473dSmrg pthread_mutex_lock(&c->iolock); 477602e473dSmrg 478602e473dSmrg if(ret) 479602e473dSmrg { 48021298544Smrg /* The code allows two threads to call select()/poll() at the same time. 48121298544Smrg * First thread just wants to read, a second thread wants to write, too. 48221298544Smrg * We have to make sure that we don't steal the reading thread's reply 48321298544Smrg * and let it get stuck in select()/poll(). 48421298544Smrg * So a thread may read if either: 48521298544Smrg * - There is no other thread that wants to read (the above situation 48621298544Smrg * did not occur). 48721298544Smrg * - It is the reading thread (above situation occurred). 48821298544Smrg */ 48921298544Smrg int may_read = c->in.reading == 1 || !count; 490602e473dSmrg#if USE_POLL 4911016ad83Smrg if(may_read && (fd.revents & POLLIN) != 0) 492602e473dSmrg#else 49321298544Smrg if(may_read && FD_ISSET(c->fd, &rfds)) 494602e473dSmrg#endif 495602e473dSmrg ret = ret && _xcb_in_read(c); 496602e473dSmrg 497602e473dSmrg#if USE_POLL 4981016ad83Smrg if((fd.revents & POLLOUT) != 0) 499602e473dSmrg#else 500602e473dSmrg if(FD_ISSET(c->fd, &wfds)) 501602e473dSmrg#endif 502602e473dSmrg ret = ret && write_vec(c, vector, count); 503602e473dSmrg } 504602e473dSmrg 505602e473dSmrg if(count) 506602e473dSmrg --c->out.writing; 507602e473dSmrg --c->in.reading; 508602e473dSmrg 509602e473dSmrg return ret; 510602e473dSmrg} 511