leo hendrickson / Mbed OS example-Ethernet-mbed-Cloud-connect
Committer:
leothedragon
Date:
Tue May 04 08:55:12 2021 +0000
Revision:
0:8f0bb79ddd48
nmn

Who changed what in which revision?

UserRevisionLine numberNew contents of line
leothedragon 0:8f0bb79ddd48 1 // ----------------------------------------------------------------------------
leothedragon 0:8f0bb79ddd48 2 // Copyright 2016-2017 ARM Ltd.
leothedragon 0:8f0bb79ddd48 3 //
leothedragon 0:8f0bb79ddd48 4 // SPDX-License-Identifier: Apache-2.0
leothedragon 0:8f0bb79ddd48 5 //
leothedragon 0:8f0bb79ddd48 6 // Licensed under the Apache License, Version 2.0 (the "License");
leothedragon 0:8f0bb79ddd48 7 // you may not use this file except in compliance with the License.
leothedragon 0:8f0bb79ddd48 8 // You may obtain a copy of the License at
leothedragon 0:8f0bb79ddd48 9 //
leothedragon 0:8f0bb79ddd48 10 // http://www.apache.org/licenses/LICENSE-2.0
leothedragon 0:8f0bb79ddd48 11 //
leothedragon 0:8f0bb79ddd48 12 // Unless required by applicable law or agreed to in writing, software
leothedragon 0:8f0bb79ddd48 13 // distributed under the License is distributed on an "AS IS" BASIS,
leothedragon 0:8f0bb79ddd48 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
leothedragon 0:8f0bb79ddd48 15 // See the License for the specific language governing permissions and
leothedragon 0:8f0bb79ddd48 16 // limitations under the License.
leothedragon 0:8f0bb79ddd48 17 // ----------------------------------------------------------------------------
leothedragon 0:8f0bb79ddd48 18
leothedragon 0:8f0bb79ddd48 19 #include "ns_event_loop.h"
leothedragon 0:8f0bb79ddd48 20
leothedragon 0:8f0bb79ddd48 21 #include "pal.h"
leothedragon 0:8f0bb79ddd48 22 #include "ns_trace.h"
leothedragon 0:8f0bb79ddd48 23
leothedragon 0:8f0bb79ddd48 24 #include "eventOS_scheduler.h"
leothedragon 0:8f0bb79ddd48 25
leothedragon 0:8f0bb79ddd48 26 #include <assert.h>
leothedragon 0:8f0bb79ddd48 27
leothedragon 0:8f0bb79ddd48 28
leothedragon 0:8f0bb79ddd48 29 #define TRACE_GROUP "evlp"
leothedragon 0:8f0bb79ddd48 30
leothedragon 0:8f0bb79ddd48 31 static void event_loop_thread(const void *arg);
leothedragon 0:8f0bb79ddd48 32
leothedragon 0:8f0bb79ddd48 33 static palThreadID_t event_thread_id = 0;
leothedragon 0:8f0bb79ddd48 34 static palMutexID_t event_mutex_id = 0;
leothedragon 0:8f0bb79ddd48 35 static palSemaphoreID_t event_start_sema_id = 0;
leothedragon 0:8f0bb79ddd48 36 static palSemaphoreID_t event_signal_sema_id = 0;
leothedragon 0:8f0bb79ddd48 37 static palSemaphoreID_t event_stop_sema_id = 0;
leothedragon 0:8f0bb79ddd48 38 static volatile bool event_stop_loop;
leothedragon 0:8f0bb79ddd48 39
leothedragon 0:8f0bb79ddd48 40 void eventOS_scheduler_mutex_wait(void)
leothedragon 0:8f0bb79ddd48 41 {
leothedragon 0:8f0bb79ddd48 42 palStatus_t status;
leothedragon 0:8f0bb79ddd48 43 status = pal_osMutexWait(event_mutex_id, UINT32_MAX);
leothedragon 0:8f0bb79ddd48 44 assert(PAL_SUCCESS == status);
leothedragon 0:8f0bb79ddd48 45 }
leothedragon 0:8f0bb79ddd48 46
leothedragon 0:8f0bb79ddd48 47 void eventOS_scheduler_mutex_release(void)
leothedragon 0:8f0bb79ddd48 48 {
leothedragon 0:8f0bb79ddd48 49 palStatus_t status;
leothedragon 0:8f0bb79ddd48 50 status = pal_osMutexRelease(event_mutex_id);
leothedragon 0:8f0bb79ddd48 51 assert(PAL_SUCCESS == status);
leothedragon 0:8f0bb79ddd48 52 }
leothedragon 0:8f0bb79ddd48 53
leothedragon 0:8f0bb79ddd48 54 void eventOS_scheduler_signal(void)
leothedragon 0:8f0bb79ddd48 55 {
leothedragon 0:8f0bb79ddd48 56 palStatus_t status;
leothedragon 0:8f0bb79ddd48 57 status = pal_osSemaphoreRelease(event_signal_sema_id);
leothedragon 0:8f0bb79ddd48 58 assert(PAL_SUCCESS == status);
leothedragon 0:8f0bb79ddd48 59 }
leothedragon 0:8f0bb79ddd48 60
leothedragon 0:8f0bb79ddd48 61 void eventOS_scheduler_idle(void)
leothedragon 0:8f0bb79ddd48 62 {
leothedragon 0:8f0bb79ddd48 63 int32_t counters = 0;
leothedragon 0:8f0bb79ddd48 64 palStatus_t status;
leothedragon 0:8f0bb79ddd48 65
leothedragon 0:8f0bb79ddd48 66 eventOS_scheduler_mutex_release();
leothedragon 0:8f0bb79ddd48 67
leothedragon 0:8f0bb79ddd48 68 status = pal_osSemaphoreWait(event_signal_sema_id, UINT32_MAX, &counters);
leothedragon 0:8f0bb79ddd48 69 assert(PAL_SUCCESS == status);
leothedragon 0:8f0bb79ddd48 70
leothedragon 0:8f0bb79ddd48 71 eventOS_scheduler_mutex_wait();
leothedragon 0:8f0bb79ddd48 72 }
leothedragon 0:8f0bb79ddd48 73
leothedragon 0:8f0bb79ddd48 74 static void event_loop_thread(const void *arg)
leothedragon 0:8f0bb79ddd48 75 {
leothedragon 0:8f0bb79ddd48 76 int32_t counters = 0;
leothedragon 0:8f0bb79ddd48 77 palStatus_t status;
leothedragon 0:8f0bb79ddd48 78
leothedragon 0:8f0bb79ddd48 79 tr_debug("event_loop_thread create");
leothedragon 0:8f0bb79ddd48 80
leothedragon 0:8f0bb79ddd48 81 event_stop_loop = false;
leothedragon 0:8f0bb79ddd48 82
leothedragon 0:8f0bb79ddd48 83 status = pal_osSemaphoreWait(event_start_sema_id, UINT32_MAX, &counters);
leothedragon 0:8f0bb79ddd48 84 assert(PAL_SUCCESS == status);
leothedragon 0:8f0bb79ddd48 85
leothedragon 0:8f0bb79ddd48 86 // TODO: Delete start semaphore?
leothedragon 0:8f0bb79ddd48 87 eventOS_scheduler_mutex_wait();
leothedragon 0:8f0bb79ddd48 88 tr_debug("event_loop_thread loop start");
leothedragon 0:8f0bb79ddd48 89
leothedragon 0:8f0bb79ddd48 90 // A stoppable version of eventOS_scheduler_run(void)
leothedragon 0:8f0bb79ddd48 91 while (event_stop_loop == false) {
leothedragon 0:8f0bb79ddd48 92 if (!eventOS_scheduler_dispatch_event()) {
leothedragon 0:8f0bb79ddd48 93 eventOS_scheduler_idle();
leothedragon 0:8f0bb79ddd48 94 }
leothedragon 0:8f0bb79ddd48 95 }
leothedragon 0:8f0bb79ddd48 96 tr_debug("event_loop_thread loop end");
leothedragon 0:8f0bb79ddd48 97
leothedragon 0:8f0bb79ddd48 98 // cleanup the scheduler timer resources which are not needed anymore
leothedragon 0:8f0bb79ddd48 99 eventOS_scheduler_timer_stop();
leothedragon 0:8f0bb79ddd48 100
leothedragon 0:8f0bb79ddd48 101 // signal the ns_event_loop_thread_stop() that it can continue
leothedragon 0:8f0bb79ddd48 102 status = pal_osSemaphoreRelease(event_stop_sema_id);
leothedragon 0:8f0bb79ddd48 103 assert(PAL_SUCCESS == status);
leothedragon 0:8f0bb79ddd48 104 }
leothedragon 0:8f0bb79ddd48 105
leothedragon 0:8f0bb79ddd48 106 void ns_event_loop_thread_create(void)
leothedragon 0:8f0bb79ddd48 107 {
leothedragon 0:8f0bb79ddd48 108 int32_t counters = 0;
leothedragon 0:8f0bb79ddd48 109 palStatus_t status;
leothedragon 0:8f0bb79ddd48 110
leothedragon 0:8f0bb79ddd48 111 status = pal_osSemaphoreCreate(1, &event_start_sema_id);
leothedragon 0:8f0bb79ddd48 112 assert(PAL_SUCCESS == status);
leothedragon 0:8f0bb79ddd48 113
leothedragon 0:8f0bb79ddd48 114 status = pal_osSemaphoreWait(event_start_sema_id, UINT32_MAX, &counters);
leothedragon 0:8f0bb79ddd48 115 assert(PAL_SUCCESS == status);
leothedragon 0:8f0bb79ddd48 116
leothedragon 0:8f0bb79ddd48 117 status = pal_osSemaphoreCreate(0, &event_stop_sema_id);
leothedragon 0:8f0bb79ddd48 118 assert(PAL_SUCCESS == status);
leothedragon 0:8f0bb79ddd48 119
leothedragon 0:8f0bb79ddd48 120 status = pal_osSemaphoreCreate(1, &event_signal_sema_id);
leothedragon 0:8f0bb79ddd48 121 assert(PAL_SUCCESS == status);
leothedragon 0:8f0bb79ddd48 122
leothedragon 0:8f0bb79ddd48 123 status = pal_osMutexCreate(&event_mutex_id);
leothedragon 0:8f0bb79ddd48 124 assert(PAL_SUCCESS == status);
leothedragon 0:8f0bb79ddd48 125
leothedragon 0:8f0bb79ddd48 126 status = pal_osThreadCreateWithAlloc(event_loop_thread, NULL, PAL_osPriorityNormal, MBED_CONF_NS_HAL_PAL_EVENT_LOOP_THREAD_STACK_SIZE, NULL, &event_thread_id);
leothedragon 0:8f0bb79ddd48 127 assert(PAL_SUCCESS == status);
leothedragon 0:8f0bb79ddd48 128 }
leothedragon 0:8f0bb79ddd48 129
leothedragon 0:8f0bb79ddd48 130 void ns_event_loop_thread_start(void)
leothedragon 0:8f0bb79ddd48 131 {
leothedragon 0:8f0bb79ddd48 132 palStatus_t status;
leothedragon 0:8f0bb79ddd48 133 status = pal_osSemaphoreRelease(event_start_sema_id);
leothedragon 0:8f0bb79ddd48 134 assert(PAL_SUCCESS == status);
leothedragon 0:8f0bb79ddd48 135 }
leothedragon 0:8f0bb79ddd48 136
leothedragon 0:8f0bb79ddd48 137 void ns_event_loop_thread_stop(void)
leothedragon 0:8f0bb79ddd48 138 {
leothedragon 0:8f0bb79ddd48 139 palStatus_t status;
leothedragon 0:8f0bb79ddd48 140
leothedragon 0:8f0bb79ddd48 141 // request loop to stop
leothedragon 0:8f0bb79ddd48 142 event_stop_loop = true;
leothedragon 0:8f0bb79ddd48 143
leothedragon 0:8f0bb79ddd48 144 // Ping the even loop at least once more so it will notice the flag and
leothedragon 0:8f0bb79ddd48 145 // hopefully end the loop soon.
leothedragon 0:8f0bb79ddd48 146 eventOS_scheduler_signal();
leothedragon 0:8f0bb79ddd48 147
leothedragon 0:8f0bb79ddd48 148 // wait until the event loop has been stopped and the thread is shutting down.
leothedragon 0:8f0bb79ddd48 149 // Note: the PAL API does not have any better means to join with a thread termination.
leothedragon 0:8f0bb79ddd48 150 status = pal_osSemaphoreWait(event_stop_sema_id, UINT32_MAX, NULL);
leothedragon 0:8f0bb79ddd48 151 assert(PAL_SUCCESS == status);
leothedragon 0:8f0bb79ddd48 152
leothedragon 0:8f0bb79ddd48 153 pal_osSemaphoreDelete(&event_start_sema_id);
leothedragon 0:8f0bb79ddd48 154 pal_osSemaphoreDelete(&event_stop_sema_id);
leothedragon 0:8f0bb79ddd48 155 }