version 1.5, 1999/11/18 22:07:50 |
version 1.20, 2003/01/13 12:04:53 |
|
|
/* -*- mode: C; coding: euc-japan -*- */ |
/* -*- mode: C; coding: euc-japan -*- */ |
/* $OpenXM: OpenXM/src/ox_math/serv1.c,v 1.4 1999/11/18 21:56:43 ohara Exp $ */ |
/* $OpenXM: OpenXM/src/ox_math/serv1.c,v 1.19 2003/01/11 12:38:57 ohara Exp $ */ |
|
|
|
/* |
|
Copyright (C) Katsuyoshi OHARA, 2000. |
|
Portions copyright 1999 Wolfram Research, Inc. |
|
|
|
You must see OpenXM/Copyright/Copyright.generic. |
|
The MathLink Library is licensed from Wolfram Research Inc.. |
|
See OpenXM/Copyright/Copyright.mathlink for detail. |
|
*/ |
|
|
#include <stdio.h> |
#include <stdio.h> |
#include <stdlib.h> |
#include <stdlib.h> |
#include <unistd.h> |
#include <unistd.h> |
#include <signal.h> |
#include <signal.h> |
#include <gmp.h> |
|
#include <mathlink.h> |
#include <mathlink.h> |
#include "ox.h" |
#include <ox_toolkit.h> |
#include "serv2.h" |
#include "sm.h" |
|
|
static int send_ox_sync_ball(int fd); |
extern OXFILE *stack_oxfp; |
|
|
extern MLINK lp; |
/* SM_control_reset_connection */ |
static int sv_read = 3; |
static void handler() |
static int sv_write = 4; |
|
|
|
static int flag_sigusr1 = 0; |
|
static int flag_sigusr2 = 0; |
|
|
|
/* 1 のとき割り込み禁止 */ |
|
static int in_critical = 0; |
|
|
|
static int set_critical() |
|
{ |
{ |
in_critical = 1; |
sigset_t newmask, oldmask; |
|
sigemptyset(&newmask); |
|
sigaddset(&newmask, SIGUSR1); |
|
sigprocmask(SIG_SETMASK, &newmask, &oldmask); |
|
ox_printf("signal received.\n"); |
|
exchange_ox_sync_ball(stack_oxfp); |
|
sigprocmask(SIG_SETMASK, &oldmask, NULL); /* unmasked. */ |
} |
} |
|
|
static int unset_critical() |
static int exchange_ox_sync_ball(OXFILE *oxfp) |
{ |
{ |
in_critical = 0; |
int tag; |
} |
send_ox_tag(oxfp, OX_SYNC_BALL); |
|
while((tag = receive_ox_tag(oxfp)) != OX_SYNC_BALL) { |
static int critical_p() { |
if (tag == OX_DATA) { |
return in_critical; |
receive_cmo(oxfp); |
} |
}else if (tag == OX_COMMAND) { |
|
receive_int32(oxfp); |
static int already_send_ox_sync_ball = 0; |
|
|
|
/* SM_control_reset_connection */ |
|
static int handler_reset1() |
|
{ |
|
fprintf(stderr, "signal received.\n"); |
|
signal(SIGUSR1, handler_reset1); |
|
if (!flag_sigusr1) { |
|
flag_sigusr1 = 1; |
|
if(critical_p()) { |
|
send_ox_sync_ball(sv_write); |
|
already_send_ox_sync_ball = 1; |
|
} |
} |
} |
} |
} |
} |
|
|
static int handler_kill() |
int shutdown() |
{ |
{ |
close(3); |
oxf_close(stack_oxfp); |
close(4); |
ml_exit(); |
exit(1); |
exit(0); |
} |
} |
|
|
static int send_ox_sync_ball(int fd) |
#define VERSION 0x11121400 |
{ |
#define ID_STRING "2000/11/29" |
fprintf(stderr, "sending a sync_ball.\n"); |
|
send_ox_tag(fd, OX_SYNC_BALL); |
|
} |
|
|
|
static int exchange_ox_syncball(int fd) |
int main() |
{ |
{ |
int tag; |
OXFILE* sv; |
|
|
while((tag = receive_ox_tag(fd)) != OX_SYNC_BALL) { |
ox_stderr_init(NULL); |
/* skipping a message. */ |
ml_init(); |
if (tag == OX_DATA) { |
mathcap_init(VERSION, ID_STRING, "ox_math", NULL, NULL); |
receive_cmo(fd); |
|
}else { |
sv = oxf_open(3); |
receive_int32(fd); |
oxf_determine_byteorder_server(sv); |
} |
sm(sv); |
} |
shutdown(); |
fprintf(stderr, "received a sync_ball.\n"); |
|
} |
} |
|
|
/* スタックマシン部分 */ |
/* a part of stack machine. */ |
int receive_ox(int fd_read, int fd_write) |
int sm_receive_ox() |
{ |
{ |
int tag; |
int tag; |
int code; |
int code; |
|
|
tag = receive_ox_tag(fd_read); |
tag = receive_ox_tag(stack_oxfp); |
|
if (oxf_error(stack_oxfp)) { |
|
return 0; |
|
} |
switch(tag) { |
switch(tag) { |
case OX_DATA: |
case OX_DATA: |
push(receive_cmo(fd_read)); |
push(receive_cmo(stack_oxfp)); |
break; |
break; |
case OX_COMMAND: |
case OX_COMMAND: |
code = receive_sm_command(fd_read); |
code = receive_sm_command(stack_oxfp); |
set_critical(); |
sm_run(code); |
execute_sm_command(fd_write, code); |
|
unset_critical(); |
|
break; |
break; |
default: |
default: |
fprintf(stderr, "illeagal message? ox_tag = (%d)\n", tag); |
ox_printf("illeagal message? ox_tag = (%d)\n", tag); |
return -1; |
|
break; |
break; |
} |
} |
return 0; |
return 1; |
} |
} |
|
|
int shutdown() |
int sm(OXFILE *oxfp) |
{ |
{ |
close(sv_read); |
fd_set fdmask; |
close(sv_write); |
stack_oxfp = oxfp; |
MATH_exit(); |
stack_extend(); |
exit(0); |
signal(SIGUSR1, handler); |
} |
|
|
|
int main() |
FD_ZERO(&fdmask); |
{ |
FD_SET(oxf_fileno(oxfp), &fdmask); |
MATH_init(); |
|
initialize_stack(); |
|
|
|
signal(SIGUSR1, handler_reset1); |
|
signal(SIGKILL, handler_kill); |
|
|
|
/* バイトオーダの決定 */ |
|
decideByteOrderServer(sv_read, 0); |
|
|
|
while(1) { |
while(1) { |
receive_ox(sv_read, sv_write); |
if (select(5, &fdmask, NULL, NULL, NULL) > 0) { |
if(flag_sigusr1) { |
sigset_t newmask, oldmask; |
if (!already_send_ox_sync_ball) { |
sigemptyset(&newmask); |
send_ox_sync_ball(sv_write); |
sigaddset(&newmask, SIGUSR1); |
already_send_ox_sync_ball = 1; |
sigprocmask(SIG_SETMASK, &newmask, &oldmask); |
} |
sm_receive_ox(); |
exchange_ox_syncball(sv_read); |
sigprocmask(SIG_SETMASK, &oldmask, NULL); /* unmasked. */ |
flag_sigusr1 = 0; |
|
already_send_ox_sync_ball = 0; |
|
} |
} |
} |
} |
shutdown(); |
ox_printf("SM: socket(%d) is closed.\n", stack_oxfp->fd); |
} |
} |