test

Committer:
elijahsj
Date:
Mon Nov 09 00:33:19 2020 -0500
Revision:
2:4364577b5ad8
Parent:
1:8a094db1347f
copied mbed library

Who changed what in which revision?

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