home *** CD-ROM | disk | FTP | other *** search
- /*
- * DDEMO.C Version 2.1
- *
- * DOSdemo module. Contains code to demonstrate use of smx with MS-DOS,
- * or equivalents.
- *
- * Copyright (c) 1990-1993 Micro Digital Associates, Inc.
- * All Rights Reserved.
- *
- * Author: Ralph Moore
- * Adapted for smxWindows Demo by David Moore
- *
- ***********************************************************************/
-
- #include "ddemo.h"
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
-
- #define MSG_LEN 25
- #define Ctrl_D 4
- #define Tab 9
-
-
- #if __cplusplus
- extern "C" {
- void win_init(...);
- void welcome(...);
- void disk_demo_main(...);
- void win_task_init(...);
- void win_task_main(...);
- void slice_init(...);
- void ts_control_main(...);
- void slice_a_main(...);
- void slice_b_main(...);
- void slice_c_main(...);
- void sleeper_main(...);
- void msg_init(...);
- void msg_task_main(...);
- void msg_a_task_main(...);
- void msg_b_task_main(...);
- void msg_c_task_main(...);
- void msg_d_task_main(...);
- void msg_e_task_main(...);
- void clear_msg_wins_main(...);
- void suspender_main(...);
- void opcon_main(...);
- void democon_main(...);
- void errgen_main(...);
- void swap_screen(...);
- }
- #else
- void win_init(void);
- void welcome(void);
- void disk_demo_main(void);
- void win_task_init(void);
- void win_task_main(void);
- void slice_init(void);
- void ts_control_main(void);
- void slice_a_main(void);
- void slice_b_main(void);
- void slice_c_main(void);
- void sleeper_main(void);
- void msg_init(void);
- void msg_task_main(void);
- void msg_a_task_main(void);
- void msg_b_task_main(void);
- void msg_c_task_main(void);
- void msg_d_task_main(void);
- void msg_e_task_main(void);
- void clear_msg_wins_main(void);
- void suspender_main(void);
- void opcon_main(void);
- void democon_main(void);
- void errgen_main(void);
- void swap_screen(void);
- #endif
-
- char buffer[10]; /* output buffer */
- long near disk_reads; /* disk read counter */
- byte near fbuf[10000]; /* file buffer */
-
- int welcome_win; /* opening window */
-
- /* windows for window manipulation demo */
- int win_intro_win; /* to display introductory message */
- int status_win; /* handle of status window */
- int windows[5]; /* array of window handles */
-
- /* windows for time slicing demo */
- int ts_intro_win; /* to display introductory message */
- int ts_win_a;
- int ts_win_b;
- int ts_win_c;
-
- /* windows for message sending demo */
- int msg_intro_win;
- int msg_source_win;
- int msg_win_a;
- int msg_win_b;
- int msg_win_c;
- int msg_win_d;
- int msg_win_e;
-
- PXCB_PTR democon_pipe;
- extern PXCB_PTR Wn_KbdPipe;
- extern SCB_PTR Wn_SingleSem;
-
- TCB_PTR near disk_demo;
- TCB_PTR near win_task;
-
- TCB_PTR near ts_control;
- TCB_PTR near slice_a;
- TCB_PTR near slice_b;
- TCB_PTR near slice_c;
-
- TCB_PTR near msg_task;
- TCB_PTR near msg_a_task;
- TCB_PTR near msg_b_task;
- TCB_PTR near msg_c_task;
- TCB_PTR near msg_d_task;
- TCB_PTR near msg_e_task;
- TCB_PTR near clear_msg_wins_task;
-
- TCB_PTR near sleeper;
- TCB_PTR near suspender;
- TCB_PTR near errgen;
- TCB_PTR opcon;
- TCB_PTR democon;
-
- XCB_PTR free_msgs;
- XCB_PTR msg_a_xchg;
- XCB_PTR msg_b_xchg;
- XCB_PTR msg_c_xchg;
- XCB_PTR msg_d_xchg;
- XCB_PTR msg_e_xchg;
-
- int msg_delay = 18; /* length of pause (ticks) between each send of message */
-
-
- void _cdecl demo_init()
- {
- MCB_PTR democon_buf; /* buffer to hold characters sent to democon_pipe */
-
- win_init(); /* initialize system for using smxWindows */
- Wn_RawDisplay (22, 0, "Hit Esc to quit.", WN_BCKBLK | WN_FGNYEL);
- Wn_RawDisplay (22, 49, "^D = Done (go on to next demo)", WN_BCKBLK | WN_FGNBLU |WN_INTEN);
- democon_pipe = create_cx(PIPE);
- democon_buf = create_nmsg(NULL, 20);
- put_msg(democon_buf, 80, democon_pipe);
- BUILD_HT(democon_pipe, "democon_pipe");
-
- opcon = create_task(opcon_main, NORM, 0);
- BUILD_HT(opcon, "opcon");
- start(opcon);
-
- democon = create_task(democon_main, NORM, 0);
- BUILD_HT(democon, "democon");
- BUILD_HT(Wn_SingleSem, "Wn_SingleSem");
-
- errgen = create_task(errgen_main, HI, 0);
-
- BUILD_HT(errgen, "errgen");
- BUILD_HT((VOID_PTR)keep_time, "keep_time");
-
- start(democon);
- }
-
- void _cdecl win_init() /* performs initialization specific to smxWindows */
- {
- MCB_PTR Wn_KbdPipeBuffer;
-
- Wn_Init();
- Wn_KbdPipe = create_cx(PIPE);
- Wn_KbdPipeBuffer = create_nmsg(NULL, 80);
- put_msg(Wn_KbdPipeBuffer, 80, Wn_KbdPipe);
- BUILD_HT(Wn_KbdPipe, "Wn_KbdPipe");
- }
-
-
- void _cdecl opcon_main(void)
- {
- char key;
-
- while(key = (char)pget_char(op_pipe, INF))
- {
- if(key == Esc)
- {
- Wn_Exit();
- start(exitx);
- }
- else if(key == Ctrl_S)
- {
- swap_screen();
- kbd_pipe = op_pipe;
- }
- else
- pput_char(key, democon_pipe, INF); /* pass key on to democon */
- }
- }
-
-
- /********* Demo Control *********/
- /*~D*/
-
- void _cdecl democon_main(void)
- {
- int i;
- char key;
-
- unlock();
-
- /* display welcome window */
- welcome();
-
- /* start first action */
- win_task_init();
-
- while(key = (char)pget_char(democon_pipe, INF))
- {
- if (key == Ctrl_D) /* if Ctrl D, go on to next demo (action) */
- {
- test (Wn_SingleSem, INF); /* wait until current smxWindows call finishes */
- signal (Wn_SingleSem); /* nec. since test resets sem counter */
- // count (2, ticks, INF); /* allows windows functions to execute return statement */
-
- if (win_task) /* if win_task exists */
- {
- /* delete windows demo task */
- LOCK();
- if (delete_task(win_task)) win_task = NULL;
- unlock();
-
- /* delete windows (if they exist) */
- for (i = 4; i >= 0; i--)
- {
- if (windows[i])
- {
- Wn_Delete (windows[i]);
- windows[i] = 0;
- }
- }
- if (status_win) Wn_Delete (status_win);
- if (win_intro_win) Wn_Delete (win_intro_win);
- status_win = 0;
- win_intro_win = 0;
-
- /* start time slicing demo */
- slice_init();
- }
- else if (ts_control) /* if time slice tasks exist */
- {
- /* delete all time slicing tasks: */
- LOCK();
- if (delete_task(ts_control)) ts_control = NULL;
- if (delete_task(slice_a)) slice_a = NULL;
- if (delete_task(slice_b)) slice_b = NULL;
- if (delete_task(slice_c)) slice_c = NULL;
- unlock();
-
- /* delete windows (if they exist) */
- if (ts_win_c) Wn_Delete (ts_win_c);
- if (ts_win_b) Wn_Delete (ts_win_b);
- if (ts_win_a) Wn_Delete (ts_win_a);
- if (ts_intro_win) Wn_Delete (ts_intro_win);
- ts_win_c = 0;
- ts_win_b = 0;
- ts_win_a = 0;
- ts_intro_win = 0;
-
- /* start the message sending demo */
- msg_init();
- }
- else if (msg_task) /* if message sending tasks exist */
- {
- /* delete all message sending tasks */
- LOCK();
- if (delete_task(msg_task)) msg_task = NULL;
- if (delete_task(msg_a_task)) msg_a_task = NULL;
- if (delete_task(msg_b_task)) msg_b_task = NULL;
- if (delete_task(msg_c_task)) msg_c_task = NULL;
- if (delete_task(msg_d_task)) msg_d_task = NULL;
- if (delete_task(msg_e_task)) msg_e_task = NULL;
- if (delete_task(clear_msg_wins_task)) clear_msg_wins_task = NULL;
- unlock();
-
- /* delete windows (if they exist) */
- if (msg_source_win) Wn_Delete (msg_source_win);
- if (msg_win_a) Wn_Delete (msg_win_a);
- if (msg_win_b) Wn_Delete (msg_win_b);
- if (msg_win_c) Wn_Delete (msg_win_c);
- if (msg_win_d) Wn_Delete (msg_win_d);
- if (msg_win_e) Wn_Delete (msg_win_e);
- if (msg_intro_win) Wn_Delete (msg_intro_win);
- msg_source_win = 0;
- msg_win_a = 0;
- msg_win_b = 0;
- msg_win_c = 0;
- msg_win_d = 0;
- msg_win_e = 0;
- msg_intro_win = 0;
-
- /* start windows demo task again */
- win_task_init();
- }
- }
- else if (key == Tab)
- {
- /* put code to select next task and highlight selected task's window */
- }
- else if (msg_task)
- pput_char(key, Wn_KbdPipe, INF); /* pass key on to windows functions */
- }
- }
-
-
- void _cdecl welcome(void)
- {
- UCHAR color;
-
- Wn_BorderAtt = 0;
- Wn_BorderAtt = WN_BCKBLK | WN_FGNWHT | WN_INTEN;
- welcome_win = Wn_Create (2, 26, 3, 27, 1, 0, 0, 0);
- Wn_Display (welcome_win, 1, 2, "Welcome to the smx Demo", WN_BCKBLK | WN_FGNWHT);
- for (color = 0x09; color <= 0x0F; color++)
- {
- count(9, ticks, INF);
- Wn_SetAttr (welcome_win, 1, 1, 25, (UCHAR)((UCHAR)WN_BCKBLK | color));
- }
- count(36, ticks, INF);
- Wn_Delete (welcome_win);
- }
-
- /********* smxWindows Demo *********/
- /*~W*/
-
- void _cdecl win_task_init(void)
- {
- win_task = create_task(win_task_main, LO, 0);
- BUILD_HT(win_task, "win_task");
- start(win_task);
- }
-
-
- void _cdecl win_task_main(void)
- {
- int i; /* index into array */
- UCHAR status_bord = WN_BCKBLK | WN_FGNWHT | WN_INTEN;
- UCHAR status_text = WN_BCKBLK | WN_FGNMAG | WN_INTEN;
- UCHAR status_text_dim = WN_BCKBLK | WN_FGNMAG;
- char *status_str;
-
- unlock();
-
- /* Display an introductory window */
-
- LOCK();
- Wn_BorderAtt = 0;
- Wn_BorderAtt = WN_BCKBLK | WN_FGNBRN;
- win_intro_win = Wn_Create (0, 24, 3, 33, 1, 0, 0, 0);
- Wn_Display (win_intro_win, 1, 2, "Here's What smxWindows Can Do", WN_BCKBLK | WN_FGNGRN);
- Wn_BorderAtt = 0;
- unlock();
-
- count(36, ticks, INF);
-
- LOCK();
- Wn_BorderAtt = status_bord;
- status_win = Wn_Create (4, 56, 8, 17, 2, 1, 0, "Status Window");
- Wn_Display (status_win, 4, 0, "> <", status_text);
- unlock();
-
- while(TRUE)
- {
- LOCK();
- Wn_BorderAtt = 0; /* clear the attributes */
- Wn_BorderAtt = WN_BCKBLK | WN_FGNYEL; /* set new attributes */
- Wn_Display (status_win, 4, 1, " Wn_Create(A) ", status_text);
- windows[0] = Wn_Create (9, 20, 10, 32, 1, 0, "A", 0);
- unlock();
-
- count(18, ticks, INF);
- Wn_BorderAtt = 0;
- Wn_BorderAtt = WN_BCKBLK | WN_FGNRED | WN_INTEN;
- Wn_SetAttr(status_win, 4, 1, 15, status_text_dim);
- Wn_Scroll (status_win, 1, 1, 4, 15, 0, 1);
- count(18, ticks, INF);
-
- LOCK();
- Wn_Display (status_win, 4, 1, " Wn_Create(B) ", status_text);
- windows[1] = Wn_Create (8, 17, 10, 32, 1, 0, "B", 0);
- unlock();
-
- count(18, ticks, INF);
- Wn_BorderAtt = 0;
- Wn_BorderAtt = WN_BCKBLK | WN_FGNMAG | WN_INTEN;
- Wn_SetAttr(status_win, 4, 1, 15, status_text_dim);
- Wn_Scroll (status_win, 1, 1, 4, 15, 0, 1);
- count(18, ticks, INF);
-
- LOCK();
- Wn_Display (status_win, 4, 1, " Wn_Create(C) ", status_text);
- windows[2] = Wn_Create (7, 14, 10, 32, 1, 0, "C", 0);
- unlock();
-
- count(18, ticks, INF);
- Wn_BorderAtt = 0;
- Wn_BorderAtt = WN_BCKBLK | WN_FGNWHT | WN_INTEN;
- Wn_SetAttr(status_win, 4, 1, 15, status_text_dim);
- Wn_Scroll (status_win, 1, 1, 4, 15, 0, 1);
- count(18, ticks, INF);
-
- LOCK();
- Wn_Display (status_win, 4, 1, " Wn_Create(D) ", status_text);
- windows[3] = Wn_Create (6, 11, 10, 32, 1, 0, "D", 0);
- unlock();
-
- count(18, ticks, INF);
- Wn_BorderAtt = 0;
- Wn_BorderAtt = WN_BCKBLK | WN_FGNBLU | WN_INTEN;
- Wn_SetAttr(status_win, 4, 1, 15, status_text_dim);
- Wn_Scroll (status_win, 1, 1, 4, 15, 0, 1);
- count(18, ticks, INF);
-
- LOCK();
- Wn_Display (status_win, 4, 1, " Wn_Create(E) ", status_text);
- windows[4] = Wn_Create (5, 8, 10, 32, 1, 0, "E", 0);
- unlock();
-
- for (i = 3; i >= 0; i--)
- {
- count(36, ticks, INF);
- Wn_SetAttr(status_win, 4, 1, 15, status_text_dim);
- Wn_Scroll (status_win, 1, 1, 4, 15, 0, 1);
- count(18, ticks, INF);
- switch (i)
- {
- case 0 : status_str = " Wn_Top(A) "; break;
- case 1 : status_str = " Wn_Top(B) "; break;
- case 2 : status_str = " Wn_Top(C) "; break;
- case 3 : status_str = " Wn_Top(D) ";
- }
- Wn_Display (status_win, 4, 1, status_str, status_text);
- Wn_Top(windows[i]);
- }
-
- count(36, ticks, INF);
- Wn_SetAttr(status_win, 4, 1, 15, status_text_dim);
- Wn_Scroll (status_win, 1, 1, 4, 15, 0, 1);
- count(18, ticks, INF);
- Wn_Display (status_win, 4, 1, " Wn_Top(E) ", status_text);
- Wn_Top(windows[4]);
-
- count(36, ticks, INF);
- Wn_SetAttr(status_win, 4, 1, 15, status_text_dim);
- Wn_Scroll (status_win, 1, 1, 4, 15, 0, 1);
- count(18, ticks, INF);
- Wn_Display (status_win, 4, 1, " Wn_Bottom(E) ", status_text);
- Wn_Bottom(windows[4]);
- Wn_Bottom(status_win);
- Wn_Bottom(win_intro_win);
-
- for (i = 0; i < 4; i++)
- {
- count(18, ticks, INF);
- Wn_SetAttr(status_win, 4, 1, 15, status_text_dim);
- Wn_Scroll (status_win, 1, 1, 4, 15, 0, 1);
- count(18, ticks, INF);
- Wn_Display (status_win, 4, 1, " Wn_Pop(E) ", status_text);
- Wn_Pop(windows[4]);
- }
-
- for (i = 0; i < 4; i++)
- {
- count(18, ticks, INF);
- Wn_SetAttr(status_win, 4, 1, 15, status_text_dim);
- Wn_Scroll (status_win, 1, 1, 4, 15, 0, 1);
- count(18, ticks, INF);
- Wn_Display (status_win, 4, 1, " Wn_Push(E) ", status_text);
- Wn_Push(windows[4]);
- }
-
- count(36, ticks, INF);
- Wn_SetAttr(status_win, 4, 1, 15, status_text_dim);
- Wn_Scroll (status_win, 1, 1, 4, 15, 0, 1);
- count(18, ticks, INF);
- Wn_Display (status_win, 4, 1, " Wn_Delete(C) ", status_text);
- Wn_Delete(windows[2]);
-
- count(36, ticks, INF);
- Wn_SetAttr(status_win, 4, 1, 15, status_text_dim);
- Wn_Scroll (status_win, 1, 1, 4, 15, 0, 1);
- count(18, ticks, INF);
- Wn_Display (status_win, 4, 1, " Wn_Delete(A) ", status_text);
- Wn_Delete(windows[0]);
-
- count(54, ticks, INF);
- Wn_SetAttr(status_win, 4, 1, 15, status_text_dim);
- Wn_Scroll (status_win, 1, 1, 4, 15, 0, 1);
- count(18, ticks, INF);
- Wn_Display (status_win, 4, 1, " Wn_Delete(E) ", status_text);
- Wn_Delete(windows[4]);
-
- count(18, ticks, INF);
- Wn_SetAttr(status_win, 4, 1, 15, status_text_dim);
- Wn_Scroll (status_win, 1, 1, 4, 15, 0, 1);
- count(18, ticks, INF);
- Wn_Display (status_win, 4, 1, " Wn_Delete(B) ", status_text);
- Wn_Delete(windows[1]);
-
- count(18, ticks, INF);
- Wn_SetAttr(status_win, 4, 1, 15, status_text_dim);
- Wn_Scroll (status_win, 1, 1, 4, 15, 0, 1);
- count(18, ticks, INF);
- Wn_Display (status_win, 4, 1, " Wn_Delete(D) ", status_text);
- Wn_Delete(windows[3]);
-
- count(36, ticks, INF);
- Wn_SetAttr(status_win, 4, 1, 15, status_text_dim);
- Wn_Scroll (status_win, 1, 1, 4, 15, 0, 1);
- Wn_Display (status_win, 4, 1, " start again ", status_text);
- count(18, ticks, INF);
- }
- }
-
-
-
- /******* Time Slicing Demo ********/
- /*~T*/
-
- void slice_init()
- {
-
- ts_control = create_task(ts_control_main, HI_SLICE, 0);
- slice_a = create_task(slice_a_main, LO_SLICE, 0);
- slice_b = create_task(slice_b_main, LO_SLICE, 0);
- slice_c = create_task(slice_c_main, LO_SLICE, 0);
-
- BUILD_HT(ts_control,"ts_control");
- BUILD_HT(slice_a,"slice_a");
- BUILD_HT(slice_b,"slice_b");
- BUILD_HT(slice_c,"slice_c");
-
- /* Display an introductory window */
- Wn_BorderAtt = 0;
- Wn_BorderAtt = WN_BCKBLU | WN_FGNWHT | WN_INTEN;
- ts_intro_win = Wn_Create (2, 26, 3, 27, 1, 0, 0, 0);
- Wn_Display (ts_intro_win, 1, 2, "smx can do time slicing", WN_BCKBLU | WN_FGNYEL);
-
- /* create slice windows */
- Wn_BorderAtt = 0;
- Wn_BorderAtt = WN_BCKBLK | WN_FGNBLU;
- ts_win_c = Wn_Create (8, 15, 6, 14, 1, 0, "slice_c", 0);
- Wn_BorderAtt = 0;
- Wn_BorderAtt = WN_BCKBLK | WN_FGNGRN;
- ts_win_b = Wn_Create (10, 18, 6, 14, 1, 0, "slice_b", 0);
- Wn_BorderAtt = 0;
- Wn_BorderAtt = WN_BCKBLK | WN_FGNRED;
- ts_win_a = Wn_Create (12, 21, 6, 14, 1, 0, "slice_a", 0);
-
- start(ts_control);
- }
-
-
- void ts_control_main()
- {
- int slice_dur = 36; /* time slice duration in ticks */
-
- start(slice_a);
- start(slice_b);
- start(slice_c);
-
- unlock();
- while(TRUE)
- {
- count(slice_dur, ticks, INF);
- bump_task(slice_a, slice_a->priority);
- Wn_Top (ts_win_b);
- count(slice_dur, ticks, INF);
- bump_task(slice_b, slice_b->priority);
- Wn_Top (ts_win_c);
- count(slice_dur, ticks, INF);
- bump_task(slice_c, slice_c->priority);
- Wn_Top (ts_win_a);
-
- /* put front task at LO_SLICE level of rq at end of level: */
- // bump_task((*(rq + LO_SLICE)).fl, (*(rq + LO_SLICE)).fl->priority);
- }
- }
-
-
- void slice_a_main()
- {
- static long counter = 0;
- static char buffer[12];
-
- unlock();
- while(TRUE)
- {
- ltoa(++counter, buffer, 10);
- Wn_Display (ts_win_a, 4, 1, buffer, WN_BCKBLK | WN_FGNRED | WN_INTEN);
- #ifdef DEBUG
- while(pcticks)
- {
- pcticks--; /* decrement before task switch due to
- time-slice */
- (*tick)();
- }
- #endif
- }
- }
-
- void slice_b_main()
- {
- static long counter = 0;
- static char buffer[12];
-
- unlock();
- while(TRUE)
- {
- ltoa(++counter, buffer, 10);
- Wn_Display (ts_win_b, 4, 1, buffer, WN_BCKBLK | WN_FGNGRN | WN_INTEN);
- #ifdef DEBUG
- while(pcticks)
- {
- pcticks--; /* decrement before task switch due to
- time-slice */
- (*tick)();
- }
- #endif
- }
- }
-
- void slice_c_main()
- {
- static long counter = 0;
- static char buffer[12];
-
- unlock();
- while(TRUE)
- {
- ltoa(++counter, buffer, 10);
- Wn_Display (ts_win_c, 4, 1, buffer, WN_BCKBLK | WN_FGNBLU | WN_INTEN);
- #ifdef DEBUG
- while(pcticks)
- {
- pcticks--; /* decrement before task switch due to
- time-slice */
- (*tick)();
- }
- #endif
- }
- }
-
-
- /*********** Message Sending Demo **********/
- /*~M*/
-
- void _cdecl msg_init(void)
- {
-
- msg_task = create_task(msg_task_main, LO, 0);
- msg_a_task = create_task(msg_a_task_main, LO + 1, 0);
- msg_b_task = create_task(msg_b_task_main, LO + 1, 0);
- msg_c_task = create_task(msg_c_task_main, LO + 1, 0);
- msg_d_task = create_task(msg_d_task_main, LO + 1, 0);
- msg_e_task = create_task(msg_e_task_main, LO + 1, 0);
- clear_msg_wins_task = create_task(clear_msg_wins_main, LO - 1, 0);
-
- BUILD_HT(msg_task,"msg_task");
- BUILD_HT(msg_a_task,"msg_a_task");
- BUILD_HT(msg_b_task,"msg_b_task");
- BUILD_HT(msg_c_task,"msg_c_task");
- BUILD_HT(msg_d_task,"msg_d_task");
- BUILD_HT(msg_e_task,"msg_e_task");
- BUILD_HT(clear_msg_wins_task,"clear_msg_wins_task");
-
- /* create free message pool if it doesn't already exist */
- if (!free_msgs)
- {
- free_msgs = create_xchg(RXCHG, 0, 0);
- BUILD_HT(free_msgs, "free_msgs");
- create_pool(free_msgs, 5, MSG_LEN + 1, &ndar);
- }
-
- /* create message exchanges if they don't already exist. only checks
- one exchange -- basically, this just monitors whether or not this
- (message sending) demo has been run */
- if (!msg_a_xchg)
- {
- msg_a_xchg = create_xchg(NXCHG, 0, 0);
- msg_b_xchg = create_xchg(NXCHG, 0, 0);
- msg_c_xchg = create_xchg(NXCHG, 0, 0);
- msg_d_xchg = create_xchg(NXCHG, 0, 0);
- msg_e_xchg = create_xchg(NXCHG, 0, 0);
- BUILD_HT(msg_a_xchg, "msg_a_xchg");
- BUILD_HT(msg_b_xchg, "msg_b_xchg");
- BUILD_HT(msg_c_xchg, "msg_c_xchg");
- BUILD_HT(msg_d_xchg, "msg_d_xchg");
- BUILD_HT(msg_e_xchg, "msg_e_xchg");
- }
-
- /* Display an introductory window */
- Wn_BorderAtt = 0;
- Wn_BorderAtt = WN_BCKRED | WN_FGNWHT | WN_INTEN;
- msg_intro_win = Wn_Create (2, 26, 3, 27, 1, 0, 0, 0);
- Wn_Display (msg_intro_win, 1, 2, "Intertask Communication", WN_BCKRED | WN_FGNWHT);
-
- /* create send/receive task windows */
- Wn_BorderAtt = 0;
- Wn_BorderAtt = WN_BCKBLK | WN_FGNBRN;
- msg_win_e = Wn_Create (8, 12, 4, MSG_LEN + 2, 1, 0, "msg_e_task", 0);
-
- Wn_BorderAtt = 0;
- Wn_BorderAtt = WN_BCKBLK | WN_FGNBLU | WN_INTEN;
- msg_win_d = Wn_Create (10, 16, 4, MSG_LEN + 2, 1, 0, "msg_d_task", 0);
-
- Wn_BorderAtt = 0;
- Wn_BorderAtt = WN_BCKBLK | WN_FGNRED | WN_INTEN;
- msg_win_c = Wn_Create (12, 20, 4, MSG_LEN + 2, 1, 0, "msg_c_task", 0);
-
- Wn_BorderAtt = 0;
- Wn_BorderAtt = WN_BCKBLK | WN_FGNMAG;
- msg_win_b = Wn_Create (14, 24, 4, MSG_LEN + 2, 1, 0, "msg_b_task", 0);
-
- Wn_BorderAtt = 0;
- Wn_BorderAtt = WN_BCKBLK | WN_FGNGRN;
- msg_win_a = Wn_Create (16, 28, 4, MSG_LEN + 2, 1, 0, "msg_a_task", 0);
-
- Wn_BorderAtt = 0;
- Wn_BorderAtt = WN_BCKWHT | WN_FGNMAG;
- msg_source_win = Wn_Create(8, 52, 5, MSG_LEN + 2, 1, 1, "msg_task",
- "Enter a Message to Send");
-
- start(msg_a_task);
- start(msg_b_task);
- start(msg_c_task);
- start(msg_d_task);
- start(msg_e_task);
- start(msg_task);
- }
-
-
- void _cdecl msg_task_main(void)
- {
- static char msg[MSG_LEN + 1];
- static char *terminator;
- int char_ctr; /* count of the number of characters in the string entered */
- int i;
- MCB_PTR message_handle;
- char *cp;
- int no_msgs_win; /* displays warning: no more messages */
- UCHAR warn_att = WN_BCKRED | WN_FGNWHT | WN_INTEN;
-
- unlock();
- no_msgs_win = 0;
-
- while(TRUE)
- {
- /* clears windows if no message entered after a certain amount of time */
- start(clear_msg_wins_task);
-
- char_ctr = Wn_KbdInput(msg_source_win, 1, 1, MSG_LEN, msg, WN_BCKMAG | WN_FGNWHT,
- WN_BCKWHT | WN_FGNMAG, terminator, INF);
- while ((message_handle = receive (free_msgs, NO_WAIT)) == NULL)
- {
- /* if the warning window hasn't been created, do so */
- if (!no_msgs_win)
- {
- Wn_BorderAtt = 0;
- Wn_BorderAtt = warn_att;
- no_msgs_win = Wn_Create(8, 52, 5, MSG_LEN + 2, 2, 0, 0, 0);
- Wn_Display(no_msgs_win, 1, 10, "Hold On!!", warn_att);
- Wn_Display(no_msgs_win, 2, 4, "Waiting for a Free", warn_att);
- Wn_Display(no_msgs_win, 3, 7, "Message Block", warn_att);
- }
- #ifdef DEBUG
- while(pcticks)
- {
- pcticks--;
- (*tick)();
- }
- #endif
- }
-
- /* if the warning window was created, delete it */
- if (no_msgs_win)
- {
- Wn_Delete(no_msgs_win);
- no_msgs_win = 0;
- }
- /* Copy each character in the message entered into the block.
- Start at first byte of message block. */
- #if LARGE_DATA
- cp = message_handle->mp;
- #else
- cp = (char *)FP_OFF(message_handle->mp);
- #endif
- for (i = 0; i < char_ctr; i++)
- *cp++ = msg[i];
-
- /* write spaces to fill remaining spaces (to nicen screen display) */
- for (; i < MSG_LEN; i++)
- *cp++ = ' ';
- *cp = '\0'; /* tack on the terminator */
-
- send(message_handle, msg_a_xchg);
- }
- }
-
-
- void _cdecl msg_a_task_main(void)
- {
- MCB_PTR message_handle;
- char *cp;
- int i;
- static char message[MSG_LEN + 1];
-
- unlock();
- while(TRUE)
- {
- message_handle = receive(msg_a_xchg, INF);
-
- Wn_Display(msg_source_win, 1, 1, " ", WN_BCKWHT | WN_FGNMAG);
-
- /* copy message to a string */
- #if LARGE_DATA
- cp = message_handle->mp;
- #else
- cp = (char *)FP_OFF(message_handle->mp);
- #endif
- for (i = 0; i <= MSG_LEN; i++)
- message[i] = *cp++;
-
- Wn_Display (msg_win_a, 1, 1, message, WN_BCKBLK | WN_FGNGRN);
-
- count(msg_delay, ticks, INF); /* delay */
-
- send(message_handle, msg_b_xchg); /* send message to next task */
- bump_task(msg_a_task, msg_a_task->priority); /* bump this task to end of level */
- }
- }
-
- void _cdecl msg_b_task_main(void)
- {
- MCB_PTR message_handle;
- char *cp;
- int i;
- static char message[MSG_LEN + 1];
-
- unlock();
- while(TRUE)
- {
- message_handle = receive(msg_b_xchg, INF);
-
- /* copy message to a string */
- #if LARGE_DATA
- cp = message_handle->mp;
- #else
- cp = (char *)FP_OFF(message_handle->mp);
- #endif
- for (i = 0; i <= MSG_LEN; i++)
- message[i] = *cp++;
-
- Wn_Display (msg_win_b, 1, 1, message, WN_BCKBLK | WN_FGNMAG);
-
- count(msg_delay, ticks, INF); /* delay */
-
- send(message_handle, msg_c_xchg); /* send message to next task */
- bump_task(msg_b_task, msg_b_task->priority); /* bump this task to end of level */
- }
- }
-
- void _cdecl msg_c_task_main(void)
- {
- MCB_PTR message_handle;
- char *cp;
- int i;
- static char message[MSG_LEN + 1];
-
- unlock();
- while(TRUE)
- {
- message_handle = receive(msg_c_xchg, INF);
-
- /* copy message to a string */
- #if LARGE_DATA
- cp = message_handle->mp;
- #else
- cp = (char *)FP_OFF(message_handle->mp);
- #endif
- for (i = 0; i <= MSG_LEN; i++)
- message[i] = *cp++;
-
- Wn_Display (msg_win_c, 1, 1, message, WN_BCKBLK | WN_FGNRED | WN_INTEN);
-
- count(msg_delay, ticks, INF); /* delay */
-
- send(message_handle, msg_d_xchg); /* send message to next task */
- bump_task(msg_c_task, msg_c_task->priority); /* bump this task to end of level */
- }
- }
-
- void _cdecl msg_d_task_main(void)
- {
- MCB_PTR message_handle;
- char *cp;
- int i;
- static char message[MSG_LEN + 1];
-
- unlock();
- while(TRUE)
- {
- message_handle = receive(msg_d_xchg, INF);
-
- /* copy message to a string */
- #if LARGE_DATA
- cp = message_handle->mp;
- #else
- cp = (char *)FP_OFF(message_handle->mp);
- #endif
- for (i = 0; i <= MSG_LEN; i++)
- message[i] = *cp++;
-
- Wn_Display (msg_win_d, 1, 1, message, WN_BCKBLK | WN_FGNBLU | WN_INTEN);
-
- count(msg_delay, ticks, INF); /* delay */
-
- send(message_handle, msg_e_xchg); /* send message to next task */
- bump_task(msg_d_task, msg_d_task->priority); /* bump this task to end of level */
- }
- }
-
- void _cdecl msg_e_task_main(void)
- {
- MCB_PTR message_handle;
- char *cp;
- int i;
- static char message[MSG_LEN + 1];
-
- unlock();
- while(TRUE)
- {
- message_handle = receive(msg_e_xchg, INF);
-
- /* copy message to a string */
- #if LARGE_DATA
- cp = message_handle->mp;
- #else
- cp = (char *)FP_OFF(message_handle->mp);
- #endif
- for (i = 0; i <= MSG_LEN; i++)
- message[i] = *cp++;
-
- Wn_Display (msg_win_e, 1, 1, message, WN_BCKBLK | WN_FGNBRN);
-
- count(msg_delay, ticks, INF); /* delay */
-
- send(message_handle, free_msgs); /* send message back to free message xchg */
- bump_task(msg_e_task, msg_e_task->priority); /* bump this task to end of level */
- }
- }
-
-
- void _cdecl clear_msg_wins_main(void)
-
- /* This task is kind of clever. It clears the windows if, after a specified
- duration, the user doesn't enter a message to send. The way it works:
- - it is started by the message-getting task just before it waits for
- a message to be entered
- - this task has lower priority than the message-getting task, so when
- that task is suspended (waiting for the message to be input), this
- task will run. (The receive tasks are all waiting on their exchanges
- so they don't run.)
- - if a message is input before the count in this task finishes, this
- task is preempted and the message is sent (the windows are not
- cleared). Otherwise, this task clears the screen and then stops.
- - this task is then restarted on every pass through the message-getting
- task to start the counter all over again. If this task is preempted,
- it never gets a chance to resume because of its lower priority. */
-
- {
- int window;
-
- unlock();
-
- count(7 * msg_delay, ticks, INF);
- for (window = msg_win_e; window <= msg_win_a; window++)
- Wn_Display(window, 1, 1, " ", WN_BCKBLK);
- }
-
-
- void _cdecl sleeper_main()
- {
- unlock();
- while(sleep(get_stime() + 5))
- {
- test(in_clib, INF);
- sprintf (buffer, "WAKEUP");
- signal(in_clib);
- wr_string(24,11,LIGHTCYAN,BLACK,!BLINK,buffer);
- }
- }
-
- void _cdecl suspender_main()
- {
- int control = 0;
-
- unlock();
- while(count (36, ticks, INF))
- {
- if (control++ % 2 == 0)
- {
- wr_string(24,7,LIGHTBLUE,BLACK,!BLINK,"suspender");
- }
- else
- {
- wr_string(24,7,YELLOW,BLACK,!BLINK,"suspender");
- }
- }
- }
-
- void _cdecl quit_demo()
- {
- }
-
- /* error generator. The purpose of this task is to simulate errors
- to test the error system. */
-
- void _cdecl errgen_main(void)
- {
- while(count(10,ticks,INF))
- {
- start(0);
- stop(0,0);
- send(0,0);
- receive(0,0);
- create_task(0,0,0);
- signal(0);
- test(0,0);
- pput_char(0,0,0);
- pget_char(0,0);
- }
- /* Must make sure returns from catastrophic errors are ok before
- putting the following improvement in
-
- word errno;
- errno = 0;
- while(count(5,ticks,INF))
- {
- errno++;
- if(errno == NUM_XERRORS) errno = 1;
- (*xesrt[errno])(errno);
- }
- */
- }