Annotation of OpenXM/src/ox_math/serv1.c, Revision 1.4
1.1 ohara 1: /* -*- mode: C; coding: euc-japan -*- */
1.4 ! ohara 2: /* $OpenXM: OpenXM/src/ox_math/serv1.c,v 1.3 1999/11/03 10:56:40 ohara Exp $ */
1.1 ohara 3:
4: #include <stdio.h>
5: #include <stdlib.h>
6: #include <unistd.h>
7: #include <signal.h>
8: #include <gmp.h>
9: #include <mathlink.h>
10: #include "ox.h"
11: #include "serv2.h"
12:
1.2 ohara 13: static int send_ox_sync_ball(int fd);
14:
1.1 ohara 15: extern MLINK lp;
16: static int sv_read = 3;
17: static int sv_write = 4;
18:
19: static int flag_sigusr1 = 0;
20: static int flag_sigusr2 = 0;
21:
22: /* 1 のとき割り込み禁止 */
23: static int in_critical = 0;
24:
25: static int set_critical()
26: {
27: in_critical = 1;
28: }
29:
30: static int unset_critical()
31: {
32: in_critical = 0;
33: }
34:
35: static int critical_p() {
36: return in_critical;
37: }
38:
39: static int already_send_ox_sync_ball = 0;
40:
41: /* SM_control_reset_connection */
42: static int handler_reset1()
43: {
44: fprintf(stderr, "signal received.\n");
45: signal(SIGUSR1, handler_reset1);
46: if (!flag_sigusr1) {
47: flag_sigusr1 = 1;
48: if(critical_p()) {
49: send_ox_sync_ball(sv_write);
50: already_send_ox_sync_ball = 1;
51: }
52: }
53: }
54:
55: static int handler_kill()
56: {
57: close(3);
58: close(4);
59: exit(1);
60: }
61:
62: static int send_ox_sync_ball(int fd)
63: {
64: fprintf(stderr, "sending a sync_ball.\n");
65: send_ox_tag(fd, OX_SYNC_BALL);
66: }
67:
68: static int exchange_ox_syncball(int fd)
69: {
70: int tag;
71:
72: while((tag = receive_ox_tag(fd)) != OX_SYNC_BALL) {
73: /* skipping a message. */
74: if (tag == OX_DATA) {
75: receive_cmo(fd);
76: }else {
77: receive_int32(fd);
78: }
79: }
80: fprintf(stderr, "received a sync_ball.\n");
81: }
82:
83: /* スタックマシン部分 */
84: int receive_ox(int fd_read, int fd_write)
85: {
86: int tag;
87: int code;
88:
89: tag = receive_ox_tag(fd_read);
90: switch(tag) {
91: case OX_DATA:
92: push(receive_cmo(fd_read));
93: break;
94: case OX_COMMAND:
95: code = receive_sm_command(fd_read);
96: set_critical();
97: execute_sm_command(fd_write, code);
98: unset_critical();
99: break;
100: default:
101: fprintf(stderr, "illeagal message? ox_tag = (%d)\n", tag);
102: return -1;
103: break;
104: }
105: return 0;
106: }
107:
1.4 ! ohara 108: int shutdown()
! 109: {
! 110: close(sv_read);
! 111: MATH_exit();
! 112: exit(0);
! 113: }
! 114:
1.1 ohara 115: int main()
116: {
117: MATH_init();
118: initialize_stack();
119:
120: signal(SIGUSR1, handler_reset1);
121: signal(SIGKILL, handler_kill);
122:
123: /* バイトオーダの決定 */
1.3 ohara 124: decideByteOrderServer(sv_read, 0);
1.1 ohara 125:
126: while(1) {
127: receive_ox(sv_read, sv_write);
128: if(flag_sigusr1) {
129: if (!already_send_ox_sync_ball) {
130: send_ox_sync_ball(sv_write);
131: already_send_ox_sync_ball = 1;
132: }
133: exchange_ox_syncball(sv_read);
134: flag_sigusr1 = 0;
135: already_send_ox_sync_ball = 0;
136: }
137: }
1.4 ! ohara 138: shutdown();
1.1 ohara 139: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>