home *** CD-ROM | disk | FTP | other *** search
- /*
- * DDEMO.CPP Version 1.0
- *
- * 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
- * Update: Rick Evans
- ***********************************************************************/
-
- #include <core.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <ddemo.hpp>
- #include <DProcess.hpp>
-
- /* function prototypes */
-
-
- /* variables */
- long disk_reads; /* disk read counter */
- byte fbuf[10000]; /* file buffer */
- long preempts; /* preempt task run counter */
- FILE *test_file;
-
- DProcess* DPP;
-
-
- /***** APPLICATION INITIALIZATION
- * This is a typcial application initialization function. Create
- * something equivalent.
- **********************************************************************/
-
- void _cdecl appl_init()
- {
-
- /* Create DProcess */
-
- DPP = new DProcess( disk_demo_main, errgen_main, opcon_main,
- preempter_main, sleeper_main);
-
- /* Start the process */
-
- DPP->Start();
-
- /*
- BUILD_HT(opcon, "opcon");
- BUILD_HT(disk_demo, "disk_demo");
- BUILD_HT(sleeper, "sleeper");
- BUILD_HT(preempter, "preempter");
- BUILD_HT(errgen, "errgen");
- */
-
- wr_string(0,21,YELLOW,BLACK,!BLINK,"Hit Esc to quit.");
- wr_string(0,7,LIGHTMAGENTA,BLACK,!BLINK,"DISK_DEMO TASK reads = ");
- wr_string(0,9,LIGHTBLUE,BLACK,!BLINK,"PREEMPTER TASK runs = ");
- wr_string(0,11,LIGHTGREEN,BLACK,!BLINK,"TIMEOUT TASK ticks = ");
-
- wr_string(0,15,YELLOW,BLACK,!BLINK,"% IDLE = ");
- wr_string(0,16,YELLOW,BLACK,!BLINK,"% BUSY = ");
- wr_string(0,17,YELLOW,BLACK,!BLINK,"% OVH = ");
-
- #ifndef DEBUG
- if((test_file = fopen("..\\test.dat","r+")) == NULL)
- {
- wr_string(0,21,LIGHTRED,BLACK,!BLINK,"Cannot find test.dat");
- ExitxTp->Start();
- }
- #endif
- }
-
-
- /***** OPERATION CONTROL
- * This task can be enlarged to interpret more keys and to interpret
- * user commands.
- **********************************************************************/
-
- void _cdecl opcon_main(void)
- {
- char key;
-
- while(key = (char)pget_char(op_pipe, INF))
- {
- if(key == Esc)
- ExitxTp->Start();
- else if(key == Ctrl_S)
- {
- if(sp_scrn_sel)
- swap_screen();
- kbd_pipe = op_pipe;
- }
- }
- }
-
-
- /***** DISK DEMO
- * This task performs continual disk reads. The amount read can be
- * changed by changing the third fread parameter. DO NOT EXCEED THE SIZE
- * OF fbuf.
- **********************************************************************/
-
- void _cdecl disk_demo_main(void)
- {
- static char buffer[10]; /* output buffer for this task */
-
- DPP->DiskDemo.UnLock(); /* permit this task to be preempted */
-
- while(TRUE)
- {
- #ifndef DEBUG /* file reads */
- rewind(test_file);
- fread(fbuf, 1, 1100, test_file);
- #endif
-
- InClibSp->Test();
- sprintf(buffer, "%.6ld", ++disk_reads);
- InClibSp->Signal();
-
- wr_string(24,7,LIGHTMAGENTA,BLACK,!BLINK,buffer);
-
- #ifdef DEBUG
- if(pcticks)
- {
- pcticks--; /* decrement before task switch if timeslicing */
- #ifdef __ZTC__
- tick_int();
- #else
- (*tick)();
- #endif
- }
- #endif
- }
- }
-
-
- /***** ERROR GENERATOR
- * The purpose of this task is to simulate errors to test the error
- * system.
- **********************************************************************/
-
- void _cdecl errgen_main(void)
- {
- while(1)
- {
- start(0);
- send(0,0);
- receive(0,0);
- create_task(0,0,0);
- pput_char(0,0,0);
- count(10,ticks,INF);
- }
- /* 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);
- }
- */
- }
-
-
- /***** PREEMPTER
- * The purpose of this task is to test that disk i/o operations can
- * be preempted by tasks of various lengths. To alter the length of
- * operation, change the dctr initialization value.
- **********************************************************************/
-
- void _cdecl preempter_main()
- {
- static char buffer[10]; /* output buffer for this task */
- long dctr;
-
- DPP->Preempter.UnLock();
- preempts = 0;
- while(count(2, ticks, INF))
- {
- for(dctr = 10000L; dctr; dctr--); /* delay */
- InClibSp->Test();
- sprintf(buffer, "%.6ld", ++preempts);
- InClibSp->Signal();
- wr_string(24,9,LIGHTBLUE,BLACK,!BLINK,buffer);
- }
- }
-
-
- /***** SLEEPER
- * This task demonstrates how to make a task periodically wake up.
- **********************************************************************/
-
- void _cdecl sleeper_main()
- {
- DPP->Sleeper.UnLock();
- while(sleep(get_stime() + 5))
- wr_string(24,11,LIGHTCYAN,BLACK,!BLINK,"WAKEUP");
- }
-
-
- /***** SUBROUTINES
- *
- **********************************************************************/
-
- void _cdecl quit_appl()
- {
- if(test_file) fclose(test_file);
- }
-
-
- void _cdecl show_etime()
- {
- static char buffer[10]; /* output buffer for this task */
- int i, j;
-
- ltoa(get_etime(), buffer, 10);
-
- for(i = 0; buffer[i] && i < 6; i++); /* find end of string */
- for(; i < 6; i++) /* right align */
- {
- for(j = i+1; j > 0; j--)
- buffer[j] = buffer[j-1];
- buffer[j] = '0';
- }
- wr_string(24,11,LIGHTGREEN,BLACK,!BLINK,buffer);
- }