File tree Expand file tree Collapse file tree 2 files changed +27
-0
lines changed Expand file tree Collapse file tree 2 files changed +27
-0
lines changed Original file line number Diff line number Diff line change 20
20
* SOFTWARE.
21
21
*/
22
22
#include " rtos/Semaphore.h"
23
+ #include " rtos/Kernel.h"
23
24
#include " platform/mbed_assert.h"
24
25
25
26
#include < string.h>
@@ -57,6 +58,20 @@ int32_t Semaphore::wait(uint32_t millisec) {
57
58
}
58
59
}
59
60
61
+ int32_t Semaphore::wait_until (uint64_t millisec) {
62
+ uint64_t now = Kernel::get_ms_count ();
63
+ uint32_t timeout;
64
+
65
+ if (now >= millisec) {
66
+ return wait (0 );
67
+ } else if (millisec - now >= osWaitForever) {
68
+ // API permits early return
69
+ return wait (osWaitForever - 1 );
70
+ } else {
71
+ return wait (millisec - now);
72
+ }
73
+ }
74
+
60
75
osStatus Semaphore::release (void ) {
61
76
return osSemaphoreRelease (_id);
62
77
}
Original file line number Diff line number Diff line change @@ -67,6 +67,18 @@ class Semaphore : private mbed::NonCopyable<Semaphore> {
67
67
*/
68
68
int32_t wait (uint32_t millisec=osWaitForever);
69
69
70
+ /* * Wait until a Semaphore resource becomes available.
71
+ @param millisec absolute timeout time, referenced to Kernel::get_ms_count()
72
+ @return number of available tokens, before taking one; or -1 in case of incorrect parameters
73
+ @note the underlying RTOS may have a limit to the maximum wait time
74
+ due to internal 32-bit computations, but this is guaranteed to work if the
75
+ wait is <= 0x7fffffff milliseconds (~24 days). If the limit is exceeded,
76
+ the acquire attempt will time out earlier than specified.
77
+
78
+ @note You cannot call this function from ISR context.
79
+ */
80
+ int32_t wait_until (uint64_t millisec);
81
+
70
82
/* * Release a Semaphore resource that was obtain with Semaphore::wait.
71
83
@return status code that indicates the execution status of the function:
72
84
@a osOK the token has been correctly released.
You can’t perform that action at this time.
0 commit comments