Skip to content

Commit 04cb39d

Browse files
committed
Minimal C thread API
1 parent 83b329c commit 04cb39d

File tree

5 files changed

+140
-4
lines changed

5 files changed

+140
-4
lines changed

mbed.h

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
#include "platform/LocalFileSystem.h"
8888
#include "drivers/InterruptIn.h"
8989
#include "platform/mbed_wait_api.h"
90+
#include "platform/mbed_thread.h"
9091
#include "hal/sleep_api.h"
9192
#include "platform/Atomic.h"
9293
#include "platform/mbed_power_mgmt.h"

platform/mbed_thread.cpp

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright (c) 2019, ARM Limited, All Rights Reserved
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
6+
* not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "platform/mbed_thread.h"
19+
#include "platform/mbed_critical.h"
20+
#include "platform/mbed_os_timer.h"
21+
22+
/* If the RTOS is present, we call the RTOS API to do the work */
23+
/* If the RTOS is not present, the RTOS API calls us to do the work */
24+
#if MBED_CONF_RTOS_PRESENT
25+
#include "rtos/Kernel.h"
26+
#include "rtos/ThisThread.h"
27+
#endif
28+
29+
extern "C" {
30+
31+
uint64_t get_ms_count(void)
32+
{
33+
#if MBED_CONF_RTOS_PRESENT
34+
return rtos::Kernel::get_ms_count();
35+
#else
36+
return mbed::internal::init_os_timer()->update_and_get_tick();
37+
#endif
38+
}
39+
40+
void thread_sleep_for(uint32_t millisec)
41+
{
42+
#if MBED_CONF_RTOS_PRESENT
43+
rtos::ThisThread::sleep_for(millisec);
44+
#else
45+
// Undocumented, but osDelay(UINT32_MAX) does actually sleep forever
46+
mbed::internal::do_timed_sleep_relative_or_forever(millisec);
47+
#endif
48+
}
49+
50+
void thread_sleep_until(uint64_t millisec)
51+
{
52+
#if MBED_CONF_RTOS_PRESENT
53+
rtos::ThisThread::sleep_until(millisec);
54+
#else
55+
mbed::internal::do_timed_sleep_absolute(millisec);
56+
#endif
57+
}
58+
59+
}

platform/mbed_thread.h

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2019 ARM Limited
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
#ifndef MBED_THREAD_H
18+
#define MBED_THREAD_H
19+
#include <stdint.h>
20+
21+
#ifdef __cplusplus
22+
extern "C" {
23+
#endif
24+
25+
/** Generic thread functions.
26+
*
27+
* These are C versions of functions provided in C++ via rtos::Thread and rtos::ThisThread
28+
*/
29+
30+
/** Read the current RTOS kernel millisecond tick count.
31+
The tick count corresponds to the tick count the RTOS uses for timing
32+
purposes. It increments monotonically from 0 at boot, so it effectively
33+
never wraps. If the underlying RTOS only provides a 32-bit tick count,
34+
this method expands it to 64 bits.
35+
@return RTOS kernel current tick count
36+
@note Mbed OS always uses millisecond RTOS ticks, and this could only wrap
37+
after half a billion years.
38+
@note In a non-RTOS build, this computes an equivalent time in milliseconds,
39+
based on a HAL timer. The time may be referenced as 0 on first call.
40+
@note You cannot call this function from ISR context.
41+
@note The equivalent functionality is accessible in C++ via rtos::Kernel::get_ms_count.
42+
*/
43+
uint64_t get_ms_count(void);
44+
45+
/** Sleep for a specified time period in millisec:
46+
@param millisec time delay value
47+
@note You cannot call this function from ISR context.
48+
@note The equivalent functionality is accessible in C++ via rtos::ThisThread::sleep_for.
49+
*/
50+
void thread_sleep_for(uint32_t millisec);
51+
52+
/** Sleep until a specified time in millisec
53+
The specified time is according to Kernel::get_ms_count().
54+
@param millisec absolute time in millisec
55+
@note You cannot call this function from ISR context.
56+
@note if millisec is equal to or lower than the current tick count, this
57+
returns immediately.
58+
@note The equivalent functionality is accessible in C++ via ThisThread::sleep_until.
59+
*/
60+
void thread_sleep_until(uint64_t millisec);
61+
62+
#ifdef __cplusplus
63+
}
64+
#endif
65+
66+
67+
#endif //MBED_THREAD_H

rtos/Kernel.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@
2626
#include "platform/mbed_critical.h"
2727
#include "platform/mbed_os_timer.h"
2828

29+
#if !MBED_CONF_RTOS_PRESENT
30+
/* If the RTOS is not present, we call mbed_thread.cpp to do the work */
31+
/* If the RTOS is present, mbed_thread.cpp calls us to do the work */
32+
#include "platform/mbed_thread.h"
33+
#endif
34+
2935
namespace rtos {
3036

3137
uint64_t Kernel::get_ms_count()
@@ -63,7 +69,7 @@ uint64_t Kernel::get_ms_count()
6369
return ret;
6470
}
6571
#else
66-
return mbed::internal::init_os_timer()->update_and_get_tick();
72+
return ::get_ms_count();
6773
#endif
6874
}
6975

rtos/ThisThread.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
#include "platform/mbed_os_timer.h"
3232

3333
#if !MBED_CONF_RTOS_PRESENT
34+
/* If the RTOS is not present, we call mbed_thread.cpp to do the work */
35+
/* If the RTOS is present, mbed_thread.cpp calls us to do the work */
36+
#include "platform/mbed_thread.h"
37+
3438
static uint32_t thread_flags;
3539

3640
/* For the flags to be useful, need a way of setting them, but there's only the main
@@ -188,8 +192,7 @@ void ThisThread::sleep_for(uint32_t millisec)
188192
osStatus_t status = osDelay(millisec);
189193
MBED_ASSERT(status == osOK);
190194
#else
191-
// Undocumented, but osDelay(UINT32_MAX) does actually sleep forever
192-
mbed::internal::do_timed_sleep_relative_or_forever(millisec);
195+
thread_sleep_for(millisec);
193196
#endif
194197
}
195198

@@ -213,7 +216,7 @@ void ThisThread::sleep_until(uint64_t millisec)
213216
}
214217
}
215218
#else
216-
mbed::internal::do_timed_sleep_absolute(millisec);
219+
thread_sleep_until(millisec);
217220
#endif
218221
}
219222

0 commit comments

Comments
 (0)