00

Committer:
ganlikun
Date:
Sun Jun 12 14:02:44 2022 +0000
Revision:
0:13413ea9a877
00

Who changed what in which revision?

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