Committer:
ganlikun
Date:
Mon Oct 24 15:19:39 2022 +0000
Revision:
0:06036f8bee2d
11

Who changed what in which revision?

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