forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsys.c
276 lines (234 loc) · 5.95 KB
/
sys.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
269
270
271
272
273
274
275
276
/*
sys.c
I/O and operating system utility functions
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <setjmp.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <limits.h>
#include <errno.h>
#include <math.h>
#include <signal.h>
#include <libgen.h>
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>
#ifdef BOEHM_GC
#include <gc.h>
#endif
#include "llt.h"
#include "julia.h"
// --- system word size ---
int jl_word_size()
{
#ifdef BITS64
return 64;
#else
return 32;
#endif
}
// --- io and select ---
void jl__not__used__()
{
// force inclusion of lib/socket.o in executable
short p=0;
open_any_tcp_port(&p);
}
DLLEXPORT int jl_sizeof_fd_set() { return sizeof(fd_set); }
DLLEXPORT int jl_sizeof_timeval() { return sizeof(struct timeval); }
DLLEXPORT void jl_set_timeval(struct timeval *tv, double tout)
{
tv->tv_sec = (int)tout;
tv->tv_usec = (int)((tout-trunc(tout))*1.0e6);
}
DLLEXPORT void jl_fd_clr(fd_set *set, int fd)
{
FD_CLR(fd, set);
}
DLLEXPORT int jl_fd_isset(fd_set *set, int fd)
{
return FD_ISSET(fd, set);
}
DLLEXPORT void jl_fd_set(fd_set *set, int fd)
{
FD_SET(fd, set);
}
DLLEXPORT void jl_fd_zero(fd_set *set)
{
FD_ZERO(set);
}
DLLEXPORT
int jl_read_avail(ios_t *s)
{
int fd = s->fd;
fd_set fds;
struct timeval tout;
tout.tv_sec = 0;
tout.tv_usec = 0;
FD_ZERO(&fds);
FD_SET(fd, &fds);
select(fd+1, &fds, NULL, NULL, &tout);
if (FD_ISSET(fd, &fds))
return 1;
return 0;
}
DLLEXPORT
uint32_t jl_getutf8(ios_t *s)
{
uint32_t wc=0;
ios_getutf8(s, &wc);
return wc;
}
DLLEXPORT
size_t jl_ios_size(ios_t *s)
{
return s->size;
}
DLLEXPORT
int32_t jl_nb_available(ios_t *s)
{
return (int32_t)(s->size - s->bpos);
}
// --- io constructors ---
DLLEXPORT int jl_sizeof_ios_t() { return sizeof(ios_t); }
// hack to expose ios_stdout to julia. we could create a new iostream pointing
// to stdout, but then there would be two buffers for one descriptor, and
// ios_stdout is used before julia IOStream is available, creating a potential
// mess.
DLLEXPORT jl_value_t *jl_stdout_stream()
{
jl_array_t *a = jl_alloc_array_1d(jl_array_uint8_type, sizeof(ios_t));
a->data = (void*)ios_stdout;
return (jl_value_t*)a;
}
// --- current output stream ---
jl_value_t *jl_current_output_stream_obj()
{
return jl_current_task->state.ostream_obj;
}
ios_t *jl_current_output_stream()
{
return jl_current_task->state.current_output_stream;
}
void jl_set_current_output_stream_obj(jl_value_t *v)
{
jl_current_task->state.ostream_obj = v;
jl_value_t *ptr = jl_convert((jl_type_t*)jl_pointer_void_type, v);
jl_current_task->state.current_output_stream =
(ios_t*)jl_unbox_pointer(ptr);
// if current stream has never been set before, propagate to all
// outer contexts.
jl_savestate_t *ss = jl_current_task->state.prev;
while (ss != NULL && ss->ostream_obj == (jl_value_t*)jl_null) {
ss->ostream_obj = v;
ss->current_output_stream = (ios_t*)jl_unbox_pointer(ptr);
ss = ss->prev;
}
}
// --- buffer manipulation ---
// TODO: avoid the extra copy
jl_array_t *jl_takebuf_array(ios_t *s)
{
size_t n;
char *b = ios_takebuf(s, &n);
jl_array_t *a = jl_pchar_to_array(b, n-1);
LLT_FREE(b);
return a;
}
jl_value_t *jl_takebuf_string(ios_t *s)
{
size_t n;
char *b = ios_takebuf(s, &n);
jl_value_t *v = jl_pchar_to_string(b, n-1);
LLT_FREE(b);
return v;
}
// -- syscall utilities --
int jl_errno() { return errno; }
jl_value_t *jl_strerror(int errnum)
{
char *str = strerror(errnum);
return jl_pchar_to_string((char*)str, strlen(str));
}
// -- child process status --
int jl_process_exited(int status) { return WIFEXITED(status); }
int jl_process_signaled(int status) { return WIFSIGNALED(status); }
int jl_process_stopped(int status) { return WIFSTOPPED(status); }
int jl_process_exit_status(int status) { return WEXITSTATUS(status); }
int jl_process_term_signal(int status) { return WTERMSIG(status); }
int jl_process_stop_signal(int status) { return WSTOPSIG(status); }
// -- access to std filehandles --
int jl_stdin() { return STDIN_FILENO; }
int jl_stdout() { return STDOUT_FILENO; }
int jl_stderr() { return STDERR_FILENO; }
// I/O thread
static pthread_t io_thread;
static pthread_mutex_t q_mut;
static pthread_mutex_t wake_mut;
static pthread_cond_t wake_cond;
typedef struct _sendreq_t {
int fd;
void *buf;
size_t n;
struct _sendreq_t *next;
} sendreq_t;
static sendreq_t *ioq = NULL;
int _os_write_all(long fd, void *buf, size_t n, size_t *nwritten);
static void *run_io_thr(void *arg)
{
while (1) {
pthread_mutex_lock(&wake_mut);
pthread_cond_wait(&wake_cond, &wake_mut);
pthread_mutex_unlock(&wake_mut);
pthread_mutex_lock(&q_mut);
while (ioq != NULL) {
sendreq_t *r = ioq;
ioq = ioq->next;
pthread_mutex_unlock(&q_mut);
size_t nw;
_os_write_all(r->fd, r->buf, r->n, &nw);
LLT_FREE(r->buf);
free(r);
pthread_mutex_lock(&q_mut);
}
pthread_mutex_unlock(&q_mut);
}
return NULL;
}
DLLEXPORT
void jl_enq_send_req(ios_t *dest, ios_t *buf)
{
sendreq_t *req = (sendreq_t*)malloc(sizeof(sendreq_t));
req->fd = dest->fd;
size_t sz;
req->n = buf->size;
req->buf = ios_takebuf(buf, &sz);
req->next = NULL;
pthread_mutex_lock(&q_mut);
if (ioq == NULL) {
ioq = req;
}
else {
sendreq_t *r = ioq;
while (r->next != NULL) {
r = r->next;
}
r->next = req;
}
pthread_mutex_unlock(&q_mut);
pthread_cond_signal(&wake_cond);
}
DLLEXPORT
void jl_start_io_thread()
{
pthread_mutex_init(&q_mut, NULL);
pthread_mutex_init(&wake_mut, NULL);
pthread_cond_init(&wake_cond, NULL);
pthread_create(&io_thread, NULL, run_io_thr, NULL);
}