Marco Zecchini
/
Example_RTOS
Rtos API example
mbed-os/platform/mbed_poll.cpp@0:9fca2b23d0ba, 2019-02-23 (annotated)
- Committer:
- marcozecchini
- Date:
- Sat Feb 23 12:13:36 2019 +0000
- Revision:
- 0:9fca2b23d0ba
final commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
marcozecchini | 0:9fca2b23d0ba | 1 | /* mbed Microcontroller Library |
marcozecchini | 0:9fca2b23d0ba | 2 | * Copyright (c) 2017 ARM Limited |
marcozecchini | 0:9fca2b23d0ba | 3 | * |
marcozecchini | 0:9fca2b23d0ba | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
marcozecchini | 0:9fca2b23d0ba | 5 | * you may not use this file except in compliance with the License. |
marcozecchini | 0:9fca2b23d0ba | 6 | * You may obtain a copy of the License at |
marcozecchini | 0:9fca2b23d0ba | 7 | * |
marcozecchini | 0:9fca2b23d0ba | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
marcozecchini | 0:9fca2b23d0ba | 9 | * |
marcozecchini | 0:9fca2b23d0ba | 10 | * Unless required by applicable law or agreed to in writing, software |
marcozecchini | 0:9fca2b23d0ba | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
marcozecchini | 0:9fca2b23d0ba | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
marcozecchini | 0:9fca2b23d0ba | 13 | * See the License for the specific language governing permissions and |
marcozecchini | 0:9fca2b23d0ba | 14 | * limitations under the License. |
marcozecchini | 0:9fca2b23d0ba | 15 | */ |
marcozecchini | 0:9fca2b23d0ba | 16 | #include "mbed_poll.h" |
marcozecchini | 0:9fca2b23d0ba | 17 | #include "FileHandle.h" |
marcozecchini | 0:9fca2b23d0ba | 18 | #include "Timer.h" |
marcozecchini | 0:9fca2b23d0ba | 19 | #ifdef MBED_CONF_RTOS_PRESENT |
marcozecchini | 0:9fca2b23d0ba | 20 | #include "rtos/Thread.h" |
marcozecchini | 0:9fca2b23d0ba | 21 | #endif |
marcozecchini | 0:9fca2b23d0ba | 22 | |
marcozecchini | 0:9fca2b23d0ba | 23 | namespace mbed { |
marcozecchini | 0:9fca2b23d0ba | 24 | |
marcozecchini | 0:9fca2b23d0ba | 25 | // timeout -1 forever, or milliseconds |
marcozecchini | 0:9fca2b23d0ba | 26 | int poll(pollfh fhs[], unsigned nfhs, int timeout) |
marcozecchini | 0:9fca2b23d0ba | 27 | { |
marcozecchini | 0:9fca2b23d0ba | 28 | /** |
marcozecchini | 0:9fca2b23d0ba | 29 | * TODO Proper wake-up mechanism. |
marcozecchini | 0:9fca2b23d0ba | 30 | * In order to correctly detect availability of read/write a FileHandle, we needed |
marcozecchini | 0:9fca2b23d0ba | 31 | * a select or poll mechanisms. We opted for poll as POSIX defines in |
marcozecchini | 0:9fca2b23d0ba | 32 | * http://pubs.opengroup.org/onlinepubs/009695399/functions/poll.html Currently, |
marcozecchini | 0:9fca2b23d0ba | 33 | * mbed::poll() just spins and scans filehandles looking for any events we are |
marcozecchini | 0:9fca2b23d0ba | 34 | * interested in. In future, his spinning behaviour will be replaced with |
marcozecchini | 0:9fca2b23d0ba | 35 | * condition variables. |
marcozecchini | 0:9fca2b23d0ba | 36 | */ |
marcozecchini | 0:9fca2b23d0ba | 37 | Timer timer; |
marcozecchini | 0:9fca2b23d0ba | 38 | if (timeout > 0) { |
marcozecchini | 0:9fca2b23d0ba | 39 | timer.start(); |
marcozecchini | 0:9fca2b23d0ba | 40 | } |
marcozecchini | 0:9fca2b23d0ba | 41 | |
marcozecchini | 0:9fca2b23d0ba | 42 | int count = 0; |
marcozecchini | 0:9fca2b23d0ba | 43 | for (;;) { |
marcozecchini | 0:9fca2b23d0ba | 44 | /* Scan the file handles */ |
marcozecchini | 0:9fca2b23d0ba | 45 | for (unsigned n = 0; n < nfhs; n++) { |
marcozecchini | 0:9fca2b23d0ba | 46 | FileHandle *fh = fhs[n].fh; |
marcozecchini | 0:9fca2b23d0ba | 47 | short mask = fhs[n].events | POLLERR | POLLHUP | POLLNVAL; |
marcozecchini | 0:9fca2b23d0ba | 48 | if (fh) { |
marcozecchini | 0:9fca2b23d0ba | 49 | fhs[n].revents = fh->poll(mask) & mask; |
marcozecchini | 0:9fca2b23d0ba | 50 | } else { |
marcozecchini | 0:9fca2b23d0ba | 51 | fhs[n].revents = POLLNVAL; |
marcozecchini | 0:9fca2b23d0ba | 52 | } |
marcozecchini | 0:9fca2b23d0ba | 53 | if (fhs[n].revents) { |
marcozecchini | 0:9fca2b23d0ba | 54 | count++; |
marcozecchini | 0:9fca2b23d0ba | 55 | } |
marcozecchini | 0:9fca2b23d0ba | 56 | } |
marcozecchini | 0:9fca2b23d0ba | 57 | |
marcozecchini | 0:9fca2b23d0ba | 58 | if (count) { |
marcozecchini | 0:9fca2b23d0ba | 59 | break; |
marcozecchini | 0:9fca2b23d0ba | 60 | } |
marcozecchini | 0:9fca2b23d0ba | 61 | |
marcozecchini | 0:9fca2b23d0ba | 62 | /* Nothing selected - this is where timeout handling would be needed */ |
marcozecchini | 0:9fca2b23d0ba | 63 | if (timeout == 0 || (timeout > 0 && timer.read_ms() > timeout)) { |
marcozecchini | 0:9fca2b23d0ba | 64 | break; |
marcozecchini | 0:9fca2b23d0ba | 65 | } |
marcozecchini | 0:9fca2b23d0ba | 66 | #ifdef MBED_CONF_RTOS_PRESENT |
marcozecchini | 0:9fca2b23d0ba | 67 | // TODO - proper blocking |
marcozecchini | 0:9fca2b23d0ba | 68 | // wait for condition variable, wait queue whatever here |
marcozecchini | 0:9fca2b23d0ba | 69 | rtos::Thread::wait(1); |
marcozecchini | 0:9fca2b23d0ba | 70 | #endif |
marcozecchini | 0:9fca2b23d0ba | 71 | } |
marcozecchini | 0:9fca2b23d0ba | 72 | return count; |
marcozecchini | 0:9fca2b23d0ba | 73 | } |
marcozecchini | 0:9fca2b23d0ba | 74 | |
marcozecchini | 0:9fca2b23d0ba | 75 | } // namespace mbed |