Path: utzoo!utgpu!water!watmath!clyde!rutgers!cmcl2!nrl-cmf!ames!ucbcad!pasteur!ucbvax!CORY.BERKELEY.EDU!dillon From: dillon@CORY.BERKELEY.EDU (Matt Dillon) Newsgroups: comp.sys.amiga Subject: IOEXAMPLE ... Example source/executable use of the timer.device Message-ID: <8801200803.AA12935@cory.Berkeley.EDU> Date: 20 Jan 88 08:03:54 GMT Sender: daemon@ucbvax.BERKELEY.EDU Lines: 394 Here is a neat little example I put together which demonstrates how simple it is to use the timer.device ... It also provides a contrast to other examples in that it uses GetMsg() and related calls to handle the IO as well as specific IO calls like WaitIO() etc... -Matt #! /bin/sh # This is a shell archive, meaning: # 1. Remove everything above the #! /bin/sh line. # 2. Save the resulting text in a file. # 3. Execute the file with /bin/sh (not csh) to create: # ioexample.c # ioexample.uue # This archive created: Wed Jan 20 00:00:54 1988 export PATH; PATH=/bin:/usr/bin:$PATH echo shar: "extracting 'ioexample.c'" '(5621 characters)' if test -f 'ioexample.c' then echo shar: "will not over-write existing file 'ioexample.c'" else cat << \!Funky!Stuff! > 'ioexample.c' /* * IOEXAMPLE.C * * Example use of the timer device... using GetMsg() to handle returned * IO requests. AZTEC COMPILATION. Should work with Lattice with * slight modifications. 32 BIT INTEGERS!!!!!! (+L for Aztec). I * probably missed some #include's as well... * * First "run ioexample on", then send timeout requests to the running * program.. example "ioexample 2000" for a 2 second delay. A public * message port is used to communicate to the master. * * ioexample off or sending a CTRL-C (BREAK) to the master will turn * it off. * * run ioexample on * ioexample 8000 * ioexample 2000 * ioexample 8000 * ioexample 3000 * ioexample 10000 * * (wait for the messages to display at the appropriate time) * * ioexample off (aborts any remaining requests and kills it) */ #include #include #include #include typedef struct MinList MINLIST; typedef struct MinNode MINNODE; typedef struct MsgPort PORT; typedef struct { struct IORequest tr_node; /* standard timerequest */ struct timeval tr_time; /* standard timerequest */ MINNODE node; /* for our own tracking */ } IOT; extern APTR GetMsg(); extern APTR AllocMem(); extern APTR RemHead(); extern PORT *FindPort(); extern PORT *CreatePort(); #define NTOBASE(ptr) (IOT *)((char *)ptr-(sizeof(IOT)-sizeof(MINNODE))) extern int Enable_Abort; main(ac,av) char *av[]; { Enable_Abort = 0; /* disable break */ if (ac != 2) { puts("run ioexample on"); puts("ioexample "); puts("ioexample off"); av[1] = "off"; } if (strcmp(av[1], "on") == 0) { /* run server */ if (do_on() == 0) puts("Failed"); exit(0); } if (strcmp(av[1], "off") == 0) { /* kill server */ PORT *port; Forbid(); /* FindPort/Signal combo */ if (port = FindPort("IOEXAMPLE")) { Signal(port->mp_SigTask, SIGBREAKF_CTRL_C); puts("IOEXAMPLE Killed"); } else { puts("IOEXAMPLE not currently running"); } Permit(); exit(0); } /* * Queue a timeout event to the server. The server picks it up * immediately. The Forbid() is simply to ensure the server does * not go away between finding the port and sending the message. * The server will fill in the remaining fields of the request. */ { long timeout = atoi(av[1]); IOT *iot = (IOT *)AllocMem(sizeof(IOT), MEMF_PUBLIC|MEMF_CLEAR); PORT *port = NULL; if (iot) { Forbid(); iot->tr_time.tv_secs = timeout / 1000; iot->tr_time.tv_micro= (timeout % 1000) * 1000; if (port = FindPort("IOEXAMPLE")) PutMsg(port, iot); else FreeMem(iot, sizeof(IOT)); Permit(); } if (port == NULL) puts("Unable to post message, IOEXAMPLE not running"); exit(0); } } /* * The SERVER! * * -Accept new requests from the public port * -SendIO() them to the timer device * -Accept the BREAK command (BREAKF_CTRL_C) to exit */ do_on() { register IOT *iot; IOT IOTimer; /* timer request template */ PORT *PubPort; /* the public port */ PORT *IOPort; /* replies from the device */ MINLIST LBase; long pub_mask, io_mask, mask; long reqnumber = 0; NewList(&LBase); IOPort = CreatePort(NULL, 0); /* Assume it works... */ PubPort= CreatePort("IOEXAMPLE", 0); /* * The timer.device is easy... no initialization of the request * is required before the OpenDevice(). Since the node in the * io request itself is used by the device, and we need some way * of aborting pending requests, I added another node to the * structure and we use that to keep track of pending requests. */ if (OpenDevice("timer.device", UNIT_MICROHZ, &IOTimer, 0) == 0) { pub_mask = 1 << PubPort->mp_SigBit; io_mask = 1 << IOPort->mp_SigBit; /* * Loops forever, until broken with a CTRL-C. */ for (;mask = Wait(pub_mask|io_mask|SIGBREAKF_CTRL_C);) { if (mask & SIGBREAKF_CTRL_C) { /* * Abort pending requests. Works even if the request * has come back since WaitIO() will remove it from * the reply port (PubPort). */ while (iot = (IOT *)RemHead(&LBase)) { iot = NTOBASE(iot); /* the actual IOT */ AbortIO(iot); /* Abort it */ WaitIO(iot); /* Wait for it */ printf("ABORTED: %ld\n", iot->tr_node.io_Message.mn_Node.ln_Name); FreeMem(iot, sizeof(IOT)); /* Free it */ } Forbid(); /* Remove the port */ while (iot = (IOT *)GetMsg(PubPort)) FreeMem(iot, sizeof(IOT)); DeletePort(PubPort); PubPort = NULL; Permit(); break; } if (mask & io_mask) { while (iot = (IOT *)GetMsg(IOPort)) { printf("Timeout #%ld\n", iot->tr_node.io_Message.mn_Node.ln_Name); Remove(&iot->node); FreeMem(iot, sizeof(IOT)); } } if (mask & pub_mask) { while (iot = (IOT *)GetMsg(PubPort)) { iot->tr_node.io_Device = IOTimer.tr_node.io_Device; iot->tr_node.io_Unit = IOTimer.tr_node.io_Unit; iot->tr_node.io_Command = TR_ADDREQUEST; iot->tr_node.io_Message.mn_ReplyPort = IOPort; iot->tr_node.io_Message.mn_Node.ln_Type = NT_MESSAGE; iot->tr_node.io_Message.mn_Node.ln_Name = (char *)reqnumber++; AddTail(&LBase, &iot->node); SendIO(iot); } } } CloseDevice(&IOTimer); } else { puts("Unable to open timer.device"); } if (IOPort) DeletePort(IOPort); if (PubPort) DeletePort(PubPort); puts("exiting"); return(1); } !Funky!Stuff! fi # end of overwriting check echo shar: "extracting 'ioexample.uue'" '(9512 characters)' if test -f 'ioexample.uue' then echo shar: "will not over-write existing file 'ioexample.uue'" else cat << \!Funky!Stuff! > 'ioexample.uue' begin 644 ioexample M```#\P`````````#``````````(```8/````IP````$```/I```&#T[Z!0I. M5?_T0JR"5@RM`````@`(9RI(>@%N3KH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+J\````#&<0NKP````-9PBZO`````IF!%*+8,P,$P`@ M;0``C`P3`")F,E*+($M2BQ`02(!(P"H`9R`@2E**$(6ZO````")F$`P3`")F M!%*+8`9"*O__8`)@TF!$($M2BQ`02(!(P"H`9S"ZO````"!G*+J\````"6<@ MNKP````,9QBZO`````UG$+J\````"F<(($I2BA"%8,(@2E**0A!*A68"4XM2 MK()V8`#_/$(20J<@+()V4H#E@"\`3KH.L%!/*4""`/M+PM.N@O@4$\L`&R``E.*%+`(`"(M``P@!$ZZ`\HH`&;>2JT`%&<&4XH4 MO``M(`I,WP003EU.=4Y5_Q1(YP@P)&T`""9M``Q"K?_X*VT`$/_\($M2BQ`0 M2(!(P"@`9P`#,+B\````)68``PI"+?\B*WP````!__0K?````"#_\"M\```G M$/_L($M2BQ`02(!(P"@`L+P````M9A!"K?_T($M2BQ`02(!(P"@`N+P````P M9A0K?````##_\"!+4HL0$$B`2,`H`+B\````*F8:(&W__%BM__PK4/_H($M2 MBQ`02(!(P"@`8#1"K?_H8")R"B`M_^A.N@G`T(20O````#`K0/_H($M2BQ`0 M2(!(P"@`0>R`%0@P``)(`&;2N+P````N9F(@2U*+$!!(@$C`*`"PO````"IF M&B!M__Q8K?_\*U#_["!+4HL0$$B`2,`H`&`T0JW_[&`B<@H@+?_L3KH)5M"$ MD+P````P*T#_["!+4HL0$$B`2,`H`$'L@!4(,``"2`!FTBM\````!/_DN+P` M``!L9A8@2U*+$!!(@$C`*``K?`````3_Y&`4N+P```!H9@P@2U*+$!!(@$C` M*``@!&!^*WP````(_^!@'"M\````"O_@8!(K?````!#_X&`(*WS____V_^`O M+?_D2&W_(B\M_^`O+?_\3KK]M$_O`!`K0/_<("W_Y-&M__Q@6B!M__Q8K?_\ M*U#_W"\M_]Q.N@(<6$\K0/_D8$H@;?_\6*W__"@00>W_(2M(_]P0A&`HD+P` M``!C9^)3@&>4D+P````+9P#_;EF`9[15@&<`_VY7@&<`_W)@S$'M_R*1[?_< M*TC_Y"`M_^2PK?_L;P8K;?_L_^1*K?_T9W`@;?_<#!``+6<*(FW_W`P1`"MF M-`RM````,/_P9BI3K?_H(&W_W%*M_]P0$$B`2,`O`$Z26$^PO/____]F"G#_ M3-\,$$Y=3G5@&"\M__!.DEA/L+S_____9@1P_V#B4JW_^"`M_^A3K?_HL*W_ MY&[:0JW_X&`D(&W_W%*M_]P0$$B`2,`O`$Z26$^PO/____]F!'#_8*I2K?_@ M(&W_W$H09PH@+?_@L*W_[&W*("W_X-&M__A*K?_T9BI@&DAX`"!.DEA/L+S_ M____9@9P_V``_W!2K?_X("W_Z%.M_^BPK?_D;MA@&"\$3I)83["\_____V8& M/__+RT`#$ZZ`/Y03R@?3EU.=6#X3E4``"\*)&T`#"!2L>H` M!&4:("T`","\````_R\`+PI.N@#04$\D7TY=3G4@4E*2$"T`"Q"`2(!(P,"\ M````_V#D3E4``"\*0>R`EB1(($K5_````!8O"&$06$]![().M``!2&W__Q`J``U(@$C`+P!.N@(R3^\`#+"\ M`````6:8("T`#&``_V`DJ@`(,"H`$$C`T*H`""5```0(Z@`"``P@4E*2$"T` M#Q"`2(!(P,"\````_V``_S!.50``+PI![("6)$A**@`,9QC5_````!9![(). MM`0`3KH` MPEA/*T#__&88-7P``0`0(`K0O`````XE0``()%].74YU-7P$```0".H``0`, M)6W__``($"H`#4B`2,`O`$ZZ`-Y83TJ`9P8`*@"```Q@S$Y5``!(YP`P)&R" M4F`4)E(@*@`$4(`O`"\*3KH%R"9DJM``AM$C`L M@DY(P"(M``BR@&P$2I)F$"E\`````H**%!/8`Y.N@)D+RR";DZZ`P983R`M__PN;():3G4H M'TY=3G5.50``2.<.("@M``AR!B`$3KH`1"1`U>R"9DJ$;0XP+().2,"X@&P$ M2I)F$BE\`````H**3N[_"DCG`01,[R"```PL;()>3J[_ ME$S?((!.=2)O``0L;()>3N[^/D[Z``(B;P`$+&R"7D[N_F).50``2.<(($AX M__].N@#06$\H`+"\_____V8*<`!,WP003EU.=4AY``$``4AX`").N@"X4$\D M0$J`9@PO!$ZZ`/A83W``8-8E;0`(``H5;0`/``D5?``$``A"*@`.%40`#T*G M3KH`HEA/)4``$$JM``AG"B\*3KH`6EA/8`I(:@`43KH`T%A/(`I@DDY5```O M"B1M``A*J@`*9P@O"DZZ`1I83Q5\`/\`""5\_____P`4<``0*@`/+P!.N@!\ M6$](>``B+PI.N@!<4$\D7TY=3G4B;P`$+&R"7D[N_IX@+P`$+&R"7D[N_K9. M^@`"3.\``P`$+&R"7D[N_SHB;P`$+&R"7D[N_GI.^@`"(F\`!"QL@EY.[O[: M3OH``BQL@EY.[O]\3OH``B)O``0@+P`(+&R"7D[N_RX@+P`$+&R"7D[N_K!. M^@`"(&\`!"QL@EY.[OZ,(&\`!""(6)!"J``$(4@`"$YU(&\`!$SO`@$`""(O M`!`L;()>3N[^1"QL@EXB;P`$("\`"$[N_=@L;()>3N[_=DSO`P``!"QL@EY. M[OZ2(&\`!"QL@EY.[O[^(F\`!"QL@EY.[O\$(F\`!"QL@EY.[OZ8(F\`!"QL M@EY.[OZ&(F\`!"QL@EY.[OXR3.\``P`$+&R"7D[N_LXB;P`$("\`""QL@EY. M[OZ\("\`!"QL@EY.[O["(F\`!"QL@EY.[OXF(&\`!"QL@EY.[OZ`3.\#```$ M+&R"FD[N_Z`@;P`$+&R"FD[N_Z8@;P`$+&R"FD[N_[(``````^P````!```` M`0``!8`````````#\@```^H```"4,#$R,S0U-C