Mercurial > ecos
comparison packages/io/serial/current/tests/timeout.inl @ 2:443894e2e912 ecos-v1_2_1-release
Block commit of eCos version 1.2.1
| author | jlarmour |
|---|---|
| date | Tue, 11 May 1999 12:24:34 +0000 |
| parents | |
| children | 1d7f19c9e4d1 |
comparison
equal
deleted
inserted
replaced
| 1:72f549f0d891 | 2:443894e2e912 |
|---|---|
| 1 #ifndef CYGONCE_IO_SERIAL_MISC_TIMEOUT_INL | |
| 2 #define CYGONCE_IO_SERIAL_MISC_TIMEOUT_INL | |
| 3 //========================================================================== | |
| 4 // | |
| 5 // timeout.inl | |
| 6 // | |
| 7 // Simple timeout support for serial I/O testing. | |
| 8 // | |
| 9 //========================================================================== | |
| 10 //####COPYRIGHTBEGIN#### | |
| 11 // | |
| 12 // ------------------------------------------- | |
| 13 // The contents of this file are subject to the Cygnus eCos Public License | |
| 14 // Version 1.0 (the "License"); you may not use this file except in | |
| 15 // compliance with the License. You may obtain a copy of the License at | |
| 16 // http://sourceware.cygnus.com/ecos | |
| 17 // | |
| 18 // Software distributed under the License is distributed on an "AS IS" | |
| 19 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the | |
| 20 // License for the specific language governing rights and limitations under | |
| 21 // the License. | |
| 22 // | |
| 23 // The Original Code is eCos - Embedded Cygnus Operating System, released | |
| 24 // September 30, 1998. | |
| 25 // | |
| 26 // The Initial Developer of the Original Code is Cygnus. Portions created | |
| 27 // by Cygnus are Copyright (C) 1998,1999 Cygnus Solutions. All Rights Reserved. | |
| 28 // ------------------------------------------- | |
| 29 // | |
| 30 //####COPYRIGHTEND#### | |
| 31 //========================================================================== | |
| 32 //#####DESCRIPTIONBEGIN#### | |
| 33 // | |
| 34 // Author(s): gthomas | |
| 35 // Contributors: gthomas | |
| 36 // Date: 1999-02-05 | |
| 37 // Description: Simple timeout functions | |
| 38 //####DESCRIPTIONEND#### | |
| 39 | |
| 40 // Timeout support | |
| 41 | |
| 42 typedef void (timeout_fun)(void *); | |
| 43 #ifndef NTIMEOUTS | |
| 44 #define NTIMEOUTS 8 | |
| 45 #endif | |
| 46 typedef struct { | |
| 47 cyg_int32 delta; // Number of "ticks" in the future for this timeout | |
| 48 timeout_fun *fun; // Function to execute when it expires | |
| 49 void *arg; // Argument to pass when it does | |
| 50 } timeout_entry; | |
| 51 static timeout_entry timeouts[NTIMEOUTS]; | |
| 52 static cyg_handle_t timeout_alarm_handle; | |
| 53 static cyg_alarm timeout_alarm; | |
| 54 static cyg_int32 last_delta; | |
| 55 | |
| 56 static void | |
| 57 do_timeout(cyg_handle_t alarm, cyg_addrword_t data) | |
| 58 { | |
| 59 int i; | |
| 60 cyg_int32 min_delta; | |
| 61 timeout_entry *e = timeouts; | |
| 62 min_delta = 0x7FFFFFFF; // Maxint | |
| 63 for (i = 0; i < NTIMEOUTS; i++, e++) { | |
| 64 if (e->delta) { | |
| 65 e->delta -= last_delta; | |
| 66 if (e->delta == 0) { | |
| 67 // Time for this item to 'fire' | |
| 68 (e->fun)(e->arg); | |
| 69 } else { | |
| 70 if (e->delta < min_delta) min_delta = e->delta; | |
| 71 } | |
| 72 } | |
| 73 } | |
| 74 if (min_delta != 0x7FFFFFFF) { | |
| 75 // Still something to do, schedule it | |
| 76 cyg_alarm_initialize(timeout_alarm_handle, min_delta, 0); | |
| 77 last_delta = min_delta; | |
| 78 } | |
| 79 } | |
| 80 | |
| 81 static cyg_uint32 | |
| 82 timeout(cyg_int32 delta, timeout_fun *fun, void *arg) | |
| 83 { | |
| 84 int i; | |
| 85 cyg_int32 min_delta; | |
| 86 static bool init = false; | |
| 87 timeout_entry *e = timeouts; | |
| 88 cyg_uint32 stamp; | |
| 89 if (!init) { | |
| 90 cyg_handle_t h; | |
| 91 cyg_clock_to_counter(cyg_real_time_clock(), &h); | |
| 92 cyg_alarm_create(h, do_timeout, 0, &timeout_alarm_handle, &timeout_alarm); | |
| 93 init = true; | |
| 94 } | |
| 95 stamp = 0; // Assume no slots available | |
| 96 for (i = 0; i < NTIMEOUTS; i++, e++) { | |
| 97 if ((e->delta == 0) && (e->fun == 0)) { | |
| 98 // Free entry | |
| 99 e->delta = delta; | |
| 100 e->fun = fun; | |
| 101 e->arg = arg; | |
| 102 stamp = (cyg_uint32)e; | |
| 103 break; | |
| 104 } | |
| 105 } | |
| 106 e = timeouts; | |
| 107 min_delta = 0x7FFFFFFF; | |
| 108 for (i = 0; i < NTIMEOUTS; i++, e++) { | |
| 109 if (e->delta && (e->delta < min_delta)) min_delta = e->delta; | |
| 110 } | |
| 111 if (min_delta != 0x7FFFFFFF) { | |
| 112 // Still something to do, schedule it | |
| 113 cyg_alarm_initialize(timeout_alarm_handle, min_delta, 0); | |
| 114 last_delta = min_delta; | |
| 115 } | |
| 116 return stamp; | |
| 117 } | |
| 118 | |
| 119 static void | |
| 120 untimeout(cyg_uint32 stamp) | |
| 121 { | |
| 122 if (stamp != 0) { | |
| 123 timeout_entry *e = (timeout_entry *)stamp; | |
| 124 if (e->fun != 0) { | |
| 125 e->delta = 0; | |
| 126 e->fun = 0; | |
| 127 e->arg = 0; | |
| 128 } | |
| 129 } | |
| 130 } | |
| 131 | |
| 132 #endif // CYGONCE_IO_SERIAL_MISC_TIMEOUT_INL |
