mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mbed_poll.cpp Source File

mbed_poll.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2017 ARM Limited
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 #include "mbed_poll.h"
00018 #include "FileHandle.h"
00019 #if MBED_CONF_RTOS_PRESENT
00020 #include "rtos/Kernel.h"
00021 #include "rtos/ThisThread.h"
00022 using namespace rtos;
00023 #else
00024 #include "drivers/Timer.h"
00025 #include "drivers/LowPowerTimer.h"
00026 #endif
00027 
00028 namespace mbed {
00029 
00030 // timeout -1 forever, or milliseconds
00031 int poll(pollfh fhs[], unsigned nfhs, int timeout)
00032 {
00033     /*
00034      * TODO Proper wake-up mechanism.
00035      * In order to correctly detect availability of read/write a FileHandle, we needed
00036      * a select or poll mechanisms. We opted for poll as POSIX defines in
00037      * http://pubs.opengroup.org/onlinepubs/009695399/functions/poll.html Currently,
00038      * mbed::poll() just spins and scans filehandles looking for any events we are
00039      * interested in. In future, his spinning behaviour will be replaced with
00040      * condition variables.
00041      */
00042 #if MBED_CONF_RTOS_PRESENT
00043     uint64_t start_time = 0;
00044     if (timeout > 0) {
00045         start_time = Kernel::get_ms_count();
00046     }
00047 #define TIME_ELAPSED() int64_t(Kernel::get_ms_count() - start_time)
00048 #else
00049 #if MBED_CONF_PLATFORM_POLL_USE_LOWPOWER_TIMER
00050     LowPowerTimer timer;
00051 #else
00052     Timer timer;
00053 #endif
00054     if (timeout > 0) {
00055         timer.start();
00056     }
00057 #define TIME_ELAPSED() timer.read_ms()
00058 #endif // MBED_CONF_RTOS_PRESENT
00059 
00060     int count = 0;
00061     for (;;) {
00062         /* Scan the file handles */
00063         for (unsigned n = 0; n < nfhs; n++) {
00064             FileHandle *fh = fhs[n].fh;
00065             short mask = fhs[n].events | POLLERR | POLLHUP | POLLNVAL;
00066             if (fh) {
00067                 fhs[n].revents = fh->poll(mask) & mask;
00068             } else {
00069                 fhs[n].revents = POLLNVAL;
00070             }
00071             if (fhs[n].revents) {
00072                 count++;
00073             }
00074         }
00075 
00076         if (count) {
00077             break;
00078         }
00079 
00080         /* Nothing selected - this is where timeout handling would be needed */
00081         if (timeout == 0 || (timeout > 0 && TIME_ELAPSED() > timeout)) {
00082             break;
00083         }
00084 #ifdef MBED_CONF_RTOS_PRESENT
00085         // TODO - proper blocking
00086         // wait for condition variable, wait queue whatever here
00087         rtos::ThisThread::sleep_for(1);
00088 #endif
00089     }
00090     return count;
00091 }
00092 
00093 } // namespace mbed