XQuartz: Use dispatch_async to handoff the FD

Signed-off-by: Jeremy Huddleston <jeremyhu@apple.com>
This commit is contained in:
Jeremy Huddleston 2011-04-21 16:31:58 -07:00
parent ca7b9e6c81
commit 034538ea9b

View File

@ -40,10 +40,15 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <pthread.h>
#include <stdbool.h> #include <stdbool.h>
#include <signal.h> #include <signal.h>
#ifdef HAVE_LIBDISPATCH
#include <dispatch/dispatch.h>
#else
#include <pthread.h>
#endif
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/un.h> #include <sys/un.h>
@ -94,6 +99,7 @@ int server_main(int argc, char **argv, char **envp);
static int execute(const char *command); static int execute(const char *command);
static char *command_from_prefs(const char *key, const char *default_value); static char *command_from_prefs(const char *key, const char *default_value);
#ifndef HAVE_LIBDISPATCH
/*** Pthread Magics ***/ /*** Pthread Magics ***/
static pthread_t create_thread(void *(*func)(void *), void *arg) { static pthread_t create_thread(void *(*func)(void *), void *arg) {
pthread_attr_t attr; pthread_attr_t attr;
@ -107,6 +113,7 @@ static pthread_t create_thread(void *(*func)(void *), void *arg) {
return tid; return tid;
} }
#endif
/*** Mach-O IPC Stuffs ***/ /*** Mach-O IPC Stuffs ***/
@ -199,8 +206,13 @@ typedef struct {
/* This thread accepts an incoming connection and hands off the file /* This thread accepts an incoming connection and hands off the file
* descriptor for the new connection to accept_fd_handoff() * descriptor for the new connection to accept_fd_handoff()
*/ */
#ifdef HAVE_LIBDISPATCH
static void socket_handoff(socket_handoff_t *handoff_data) {
#else
static void *socket_handoff_thread(void *arg) { static void *socket_handoff_thread(void *arg) {
socket_handoff_t *handoff_data = (socket_handoff_t *)arg; socket_handoff_t *handoff_data = (socket_handoff_t *)arg;
#endif
int launchd_fd = -1; int launchd_fd = -1;
int connected_fd; int connected_fd;
@ -229,7 +241,9 @@ static void *socket_handoff_thread(void *arg) {
fprintf(stderr, "X11.app Handing off fd to server thread via DarwinListenOnOpenFD(%d)\n", launchd_fd); fprintf(stderr, "X11.app Handing off fd to server thread via DarwinListenOnOpenFD(%d)\n", launchd_fd);
DarwinListenOnOpenFD(launchd_fd); DarwinListenOnOpenFD(launchd_fd);
#ifndef HAVE_LIBDISPATCH
return NULL; return NULL;
#endif
} }
static int create_socket(char *filename_out) { static int create_socket(char *filename_out) {
@ -298,8 +312,14 @@ kern_return_t do_request_fd_handoff_socket(mach_port_t port, string_t filename)
} }
strlcpy(filename, handoff_data->filename, STRING_T_SIZE); strlcpy(filename, handoff_data->filename, STRING_T_SIZE);
#ifdef HAVE_LIBDISPATCH
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
socket_handoff(handoff_data);
});
#else
create_thread(socket_handoff_thread, handoff_data); create_thread(socket_handoff_thread, handoff_data);
#endif
#ifdef DEBUG #ifdef DEBUG
fprintf(stderr, "X11.app: Thread created for handoff. Returning success to tell caller to connect and push the fd.\n"); fprintf(stderr, "X11.app: Thread created for handoff. Returning success to tell caller to connect and push the fd.\n");