| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2011 The Native Client Authors. All rights reserved. | 2 * Copyright (c) 2011 The Native Client Authors. All rights reserved. |
| 3 * Use of this source code is governed by a BSD-style license that can be | 3 * Use of this source code is governed by a BSD-style license that can be |
| 4 * found in the LICENSE file. | 4 * found in the LICENSE file. |
| 5 */ | 5 */ |
| 6 | 6 |
| 7 #include <pthread.h> | 7 #include <pthread.h> |
| 8 #include <stdlib.h> | 8 #include <stdlib.h> |
| 9 #include <unistd.h> | 9 #include <unistd.h> |
| 10 | 10 |
| 11 #include "native_client/src/public/imc_syscalls.h" | 11 #include "native_client/src/public/imc_syscalls.h" |
| 12 #include "native_client/src/shared/srpc/nacl_srpc.h" | 12 #include "native_client/src/shared/srpc/nacl_srpc.h" |
| 13 #include "native_client/src/shared/srpc/nacl_srpc_ppapi_plugin_internal.h" | |
| 14 | 13 |
| 15 #define BOUND_SOCKET 3 | 14 #define BOUND_SOCKET 3 |
| 16 | 15 |
| 17 struct WorkerData { | 16 struct WorkerData { |
| 18 int sock_fd; | 17 int sock_fd; |
| 19 const struct NaClSrpcHandlerDesc *methods; | 18 const struct NaClSrpcHandlerDesc *methods; |
| 20 }; | 19 }; |
| 21 | 20 |
| 22 static void *Worker(void *arg) { | 21 static void *Worker(void *arg) { |
| 23 struct WorkerData *worker_data = (struct WorkerData *) arg; | 22 struct WorkerData *worker_data = (struct WorkerData *) arg; |
| 24 NaClSrpcServerLoop(worker_data->sock_fd, worker_data->methods, NULL); | 23 NaClSrpcServerLoop(worker_data->sock_fd, worker_data->methods, NULL); |
| 25 close(worker_data->sock_fd); | 24 close(worker_data->sock_fd); |
| 26 free(worker_data); | 25 free(worker_data); |
| 27 return NULL; | 26 return NULL; |
| 28 } | 27 } |
| 29 | 28 |
| 30 int NaClSrpcAcceptClientOnThread(const struct NaClSrpcHandlerDesc* methods) { | 29 int NaClSrpcAcceptClientOnThread(const struct NaClSrpcHandlerDesc* methods) { |
| 31 int sock_fd = -1; | 30 int sock_fd = -1; |
| 32 int result = 0; | 31 int result = 0; |
| 33 struct WorkerData *worker_data = NULL; | 32 struct WorkerData *worker_data = NULL; |
| 34 pthread_t worker_tid; | 33 pthread_t worker_tid; |
| 35 | 34 |
| 36 NaClPluginLowLevelInitializationComplete(); | |
| 37 sock_fd = imc_accept(BOUND_SOCKET); | 35 sock_fd = imc_accept(BOUND_SOCKET); |
| 38 if (sock_fd == -1) { | 36 if (sock_fd == -1) { |
| 39 goto done; | 37 goto done; |
| 40 } | 38 } |
| 41 worker_data = (struct WorkerData*) malloc(sizeof *worker_data); | 39 worker_data = (struct WorkerData*) malloc(sizeof *worker_data); |
| 42 if (worker_data == NULL) { | 40 if (worker_data == NULL) { |
| 43 goto done; | 41 goto done; |
| 44 } | 42 } |
| 45 worker_data->sock_fd = sock_fd; | 43 worker_data->sock_fd = sock_fd; |
| 46 worker_data->methods = methods; | 44 worker_data->methods = methods; |
| 47 if (pthread_create(&worker_tid, NULL, Worker, (void *) worker_data) != 0) { | 45 if (pthread_create(&worker_tid, NULL, Worker, (void *) worker_data) != 0) { |
| 48 goto done; | 46 goto done; |
| 49 } | 47 } |
| 50 /* On successful thread start the worker takes ownership. */ | 48 /* On successful thread start the worker takes ownership. */ |
| 51 worker_data = NULL; | 49 worker_data = NULL; |
| 52 sock_fd = -1; | 50 sock_fd = -1; |
| 53 result = 1; | 51 result = 1; |
| 54 done: | 52 done: |
| 55 free(worker_data); | 53 free(worker_data); |
| 56 if (sock_fd != -1) { | 54 if (sock_fd != -1) { |
| 57 close(sock_fd); | 55 close(sock_fd); |
| 58 } | 56 } |
| 59 return result; | 57 return result; |
| 60 } | 58 } |
| OLD | NEW |