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