-
Notifications
You must be signed in to change notification settings - Fork 237
/
lin_xs.c
268 lines (251 loc) · 6.97 KB
/
lin_xs.c
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
/*
* Copyright (c) 2016-2017 Moddable Tech, Inc.
*
* This file is part of the Moddable SDK Runtime.
*
* The Moddable SDK Runtime is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Moddable SDK Runtime is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with the Moddable SDK Runtime. If not, see <http://www.gnu.org/licenses/>.
*
* This file incorporates work covered by the following copyright and
* permission notice:
*
* Copyright (C) 2010-2016 Marvell International Ltd.
* Copyright (C) 2002-2010 Kinoma, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "xsAll.h"
#include "xsScript.h"
#include <fcntl.h>
#include <glib.h>
#include <netdb.h>
#include <signal.h>
static gboolean fxQueuePromiseJobsCallback(void *it);
static gboolean fxQueueWorkerJobsCallback(void *it);
void fxCreateMachinePlatform(txMachine* the)
{
the->workerContext = g_main_context_get_thread_default();
g_mutex_init(&(the->workerMutex));
}
void fxDeleteMachinePlatform(txMachine* the)
{
g_mutex_lock(&(the->workerMutex));
{
txWorkerJob* job = the->workerQueue;
while (job) {
txWorkerJob* next = job->next;
c_free(job);
job = next;
}
the->workerQueue = NULL;
}
g_mutex_unlock(&(the->workerMutex));
g_mutex_clear(&(the->workerMutex));
the->workerContext = NULL;
}
void fxQueuePromiseJobs(txMachine* the)
{
GSource* idle_source = g_idle_source_new();
g_source_set_callback(idle_source, fxQueuePromiseJobsCallback, the, NULL);
g_source_set_priority(idle_source, G_PRIORITY_DEFAULT);
g_source_attach(idle_source, g_main_context_get_thread_default());
g_source_unref(idle_source);
}
gboolean fxQueuePromiseJobsCallback(void *it)
{
txMachine* the = it;
fxRunPromiseJobs(the);
return G_SOURCE_REMOVE;
}
void fxQueueWorkerJob(void* machine, void* job)
{
txMachine* the = machine;
g_mutex_lock(&(the->workerMutex));
{
txWorkerJob** address = &(the->workerQueue);
txWorkerJob* former;
while ((former = *address))
address = &(former->next);
*address = job;
}
g_mutex_unlock(&(the->workerMutex));
GSource* idle_source = g_idle_source_new();
g_source_set_callback(idle_source, fxQueueWorkerJobsCallback, the, NULL);
g_source_set_priority(idle_source, G_PRIORITY_DEFAULT);
g_source_attach(idle_source, the->workerContext);
g_source_unref(idle_source);
}
gboolean fxQueueWorkerJobsCallback(void *it)
{
txMachine* the = it;
txWorkerJob* job;
g_mutex_lock(&(the->workerMutex));
job = the->workerQueue;
the->workerQueue = NULL;
g_mutex_unlock(&(the->workerMutex));
while (job) {
txWorkerJob* next = job->next;
(*job->callback)(the, job);
c_free(job);
job = next;
}
return G_SOURCE_REMOVE;
}
#ifdef mxDebug
extern char *program_invocation_name;
gboolean fxReadableCallback(GSocket *socket, GIOCondition condition, gpointer user_data)
{
txMachine* the = user_data;
if (fxIsReadable(the)) {
fxDebugCommand(the);
if (the->breakOnStartFlag) {
fxBeginHost(the);
fxDebugLoop(the, NULL, 0, "C: xsDebugger");
fxEndHost(the);
}
}
return TRUE;
}
void fxConnect(txMachine* the)
{
struct hostent *host;
struct sockaddr_in address;
int fd = -1;
int flag;
host = gethostbyname("localhost");
if (!host)
goto bail;
memset(&address, 0, sizeof(address));
address.sin_family = AF_INET;
memcpy(&(address.sin_addr), host->h_addr, host->h_length);
if (strstr(program_invocation_name, "xsbug"))
address.sin_port = htons(5003);
else
address.sin_port = htons(5002);
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0)
return;
signal(SIGPIPE, SIG_IGN);
flag = fcntl(fd, F_GETFL, 0);
fcntl(fd, F_SETFL, flag | O_NONBLOCK);
if (connect(fd, (struct sockaddr*)&address, sizeof(address)) < 0) {
if (errno == EINPROGRESS) {
fd_set fds;
struct timeval timeout = { 2, 0 }; // 2 seconds, 0 micro-seconds
int error = 0;
unsigned int length = sizeof(error);
FD_ZERO(&fds);
FD_SET(fd, &fds);
if (select(fd + 1, NULL, &fds, NULL, &timeout) == 0)
goto bail;
if (!FD_ISSET(fd, &fds))
goto bail;
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &length) < 0)
goto bail;
if (error)
goto bail;
}
else
goto bail;
}
fcntl(fd, F_SETFL, flag);
signal(SIGPIPE, SIG_DFL);
the->socket = g_socket_new_from_fd(fd, NULL);
if (!the->socket)
goto bail;
g_socket_set_blocking(the->socket, FALSE);
the->source = g_socket_create_source(the->socket, G_IO_IN, NULL);
g_source_set_callback(the->source, (void*)fxReadableCallback, the, NULL);
g_source_set_priority(the->source, G_PRIORITY_DEFAULT);
g_source_attach(the->source, g_main_context_get_thread_default());
return;
bail:
if (fd >= 0)
close(fd);
}
void fxDisconnect(txMachine* the)
{
if (the->source) {
g_source_destroy(the->source);
the->source = NULL;
}
if (the->socket) {
g_socket_close(the->socket, NULL);
g_object_unref(the->socket);
the->socket = NULL;
}
}
txBoolean fxIsConnected(txMachine* the)
{
return (the->socket) ? 1 : 0;
}
txBoolean fxIsReadable(txMachine* the)
{
if (the->socket) {
gssize count = g_socket_receive(the->socket, the->debugBuffer, sizeof(the->debugBuffer) - 1, NULL, NULL);
if (count > 0) {
the->debugOffset = count;
return 1;
}
}
return 0;
}
void fxReceive(txMachine* the)
{
if (the->socket) {
GError* error = NULL;
gssize count;
again:
count = g_socket_receive(the->socket, the->debugBuffer + the->debugOffset, sizeof(the->debugBuffer) - the->debugOffset - 1, NULL, &error);
if (count < 0) {
if (error->code == G_IO_ERROR_WOULD_BLOCK) {
g_clear_error(&error);
if (the->debugOffset == 0)
goto again;
}
else
fxDisconnect(the);
}
else {
the->debugOffset += count;
}
the->debugBuffer[the->debugOffset] = 0;
}
}
void fxSend(txMachine* the, txBoolean more)
{
if (the->socket) {
GError* error = NULL;
gssize count;
again:
count = g_socket_send(the->socket, the->echoBuffer, the->echoOffset, NULL, &error);
if (count < 0) {
if (error->code == G_IO_ERROR_WOULD_BLOCK) {
g_clear_error(&error);
goto again;
}
fxDisconnect(the);
}
}
}
#endif