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