Example to demonstrate race condition and how CriticalSectionLock API helps in resolving that.

Committer:
deepikabhavnani
Date:
Fri Oct 20 19:50:07 2017 +0000
Revision:
0:36e8acd8e607
CriticalSectionLock example

Who changed what in which revision?

UserRevisionLine numberNew contents of line
deepikabhavnani 0:36e8acd8e607 1 /*
deepikabhavnani 0:36e8acd8e607 2 * Copyright (c) 2016-2016, ARM Limited, All Rights Reserved
deepikabhavnani 0:36e8acd8e607 3 * SPDX-License-Identifier: Apache-2.0
deepikabhavnani 0:36e8acd8e607 4 *
deepikabhavnani 0:36e8acd8e607 5 * Licensed under the Apache License, Version 2.0 (the "License"); you may
deepikabhavnani 0:36e8acd8e607 6 * not use this file except in compliance with the License.
deepikabhavnani 0:36e8acd8e607 7 * You may obtain a copy of the License at
deepikabhavnani 0:36e8acd8e607 8 *
deepikabhavnani 0:36e8acd8e607 9 * http://www.apache.org/licenses/LICENSE-2.0
deepikabhavnani 0:36e8acd8e607 10 *
deepikabhavnani 0:36e8acd8e607 11 * Unless required by applicable law or agreed to in writing, software
deepikabhavnani 0:36e8acd8e607 12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
deepikabhavnani 0:36e8acd8e607 13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
deepikabhavnani 0:36e8acd8e607 14 * See the License for the specific language governing permissions and
deepikabhavnani 0:36e8acd8e607 15 * limitations under the License.
deepikabhavnani 0:36e8acd8e607 16 */
deepikabhavnani 0:36e8acd8e607 17 #include "rtos/Thread.h"
deepikabhavnani 0:36e8acd8e607 18 #include "mbed.h"
deepikabhavnani 0:36e8acd8e607 19 #include "rtos/rtos_idle.h"
deepikabhavnani 0:36e8acd8e607 20 #include "platform/mbed_critical.h"
deepikabhavnani 0:36e8acd8e607 21
deepikabhavnani 0:36e8acd8e607 22 #define USE_CRITICAL_SECTION_LOCK 1 // Set 0 to see race condition
deepikabhavnani 0:36e8acd8e607 23 // Note: Might require few runs to see race condition
deepikabhavnani 0:36e8acd8e607 24
deepikabhavnani 0:36e8acd8e607 25 #define THREAD_CNT 8
deepikabhavnani 0:36e8acd8e607 26
deepikabhavnani 0:36e8acd8e607 27 int32_t value = 100000;
deepikabhavnani 0:36e8acd8e607 28 volatile int32_t count = 0;
deepikabhavnani 0:36e8acd8e607 29
deepikabhavnani 0:36e8acd8e607 30 void increment(void) {
deepikabhavnani 0:36e8acd8e607 31 for (int i = 0; i < value; i++) {
deepikabhavnani 0:36e8acd8e607 32 #if (USE_CRITICAL_SECTION_LOCK == 1)
deepikabhavnani 0:36e8acd8e607 33 CriticalSectionLock lock;
deepikabhavnani 0:36e8acd8e607 34 #endif
deepikabhavnani 0:36e8acd8e607 35 count += 1;
deepikabhavnani 0:36e8acd8e607 36 }
deepikabhavnani 0:36e8acd8e607 37 }
deepikabhavnani 0:36e8acd8e607 38
deepikabhavnani 0:36e8acd8e607 39 int get_count(void) {
deepikabhavnani 0:36e8acd8e607 40 if (count == (value * THREAD_CNT)) {
deepikabhavnani 0:36e8acd8e607 41 printf("No Race condition\n");
deepikabhavnani 0:36e8acd8e607 42 } else {
deepikabhavnani 0:36e8acd8e607 43 printf("Race condition\n");
deepikabhavnani 0:36e8acd8e607 44 }
deepikabhavnani 0:36e8acd8e607 45 return count;
deepikabhavnani 0:36e8acd8e607 46 }
deepikabhavnani 0:36e8acd8e607 47
deepikabhavnani 0:36e8acd8e607 48 int main()
deepikabhavnani 0:36e8acd8e607 49 {
deepikabhavnani 0:36e8acd8e607 50 Thread counter_thread[THREAD_CNT];
deepikabhavnani 0:36e8acd8e607 51
deepikabhavnani 0:36e8acd8e607 52 for (int i = 0; i < THREAD_CNT; i++) {
deepikabhavnani 0:36e8acd8e607 53 counter_thread[i].start(callback(increment));
deepikabhavnani 0:36e8acd8e607 54 }
deepikabhavnani 0:36e8acd8e607 55
deepikabhavnani 0:36e8acd8e607 56 // Wait for the threads to finish
deepikabhavnani 0:36e8acd8e607 57 for (int i = 0; i < THREAD_CNT; i++) {
deepikabhavnani 0:36e8acd8e607 58 counter_thread[i].join();
deepikabhavnani 0:36e8acd8e607 59 }
deepikabhavnani 0:36e8acd8e607 60 printf ("Counter = %d\n", get_count());
deepikabhavnani 0:36e8acd8e607 61 }