mirror of https://github.com/ARMmbed/mbed-os.git
				
				
				
			RTOS_2 Semaphore resource locking test automation added to test suite
							parent
							
								
									c1c470bf6f
								
							
						
					
					
						commit
						773e0ce493
					
				| 
						 | 
					@ -1,24 +1,69 @@
 | 
				
			||||||
#include "mbed.h"
 | 
					#include "mbed.h"
 | 
				
			||||||
 | 
					#include "test_env.h"
 | 
				
			||||||
#include "rtos.h"
 | 
					#include "rtos.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Mutex stdio_mutex; 
 | 
					#define THREAD_DELAY     50
 | 
				
			||||||
 | 
					#define SIGNALS_TO_EMIT  100
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void notify(const char* name, int state) {
 | 
					void print_char(char c = '*')
 | 
				
			||||||
    stdio_mutex.lock();
 | 
					{
 | 
				
			||||||
    printf("%s: %d\n\r", name, state);
 | 
					    printf("%c", c);
 | 
				
			||||||
    stdio_mutex.unlock();
 | 
					    fflush(stdout);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Mutex stdio_mutex;
 | 
				
			||||||
 | 
					DigitalOut led(LED1);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					volatile int change_counter = 0;
 | 
				
			||||||
 | 
					volatile bool changing_counter = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					bool manipulate_protected_zone(const int thread_delay) {
 | 
				
			||||||
 | 
					    bool result = true;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    stdio_mutex.lock(); // LOCK
 | 
				
			||||||
 | 
					    if (changing_counter == true) {
 | 
				
			||||||
 | 
					        print_char('e');    // if changing_counter is true access is not exclusively
 | 
				
			||||||
 | 
					        result = false;
 | 
				
			||||||
 | 
					        notify_completion(false);
 | 
				
			||||||
 | 
					        exit(1);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    changing_counter = true;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Some action on protected
 | 
				
			||||||
 | 
					    led = !led;
 | 
				
			||||||
 | 
					    change_counter++;
 | 
				
			||||||
 | 
					    print_char('.');
 | 
				
			||||||
 | 
					    Thread::wait(thread_delay);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    changing_counter = false;
 | 
				
			||||||
 | 
					    stdio_mutex.unlock();   // UNLOCK
 | 
				
			||||||
 | 
					    return result;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void test_thread(void const *args) {
 | 
					void test_thread(void const *args) {
 | 
				
			||||||
 | 
					    const int thread_delay = int(args);
 | 
				
			||||||
    while (true) {
 | 
					    while (true) {
 | 
				
			||||||
        notify((const char*)args, 0); Thread::wait(1000);
 | 
					        manipulate_protected_zone(thread_delay);
 | 
				
			||||||
        notify((const char*)args, 1); Thread::wait(1000);
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int main() {
 | 
					int main() {
 | 
				
			||||||
    Thread t2(test_thread, (void *)"Th 2");
 | 
					    const int t1_delay = THREAD_DELAY * 1;
 | 
				
			||||||
    Thread t3(test_thread, (void *)"Th 3");
 | 
					    const int t2_delay = THREAD_DELAY * 2;
 | 
				
			||||||
    
 | 
					    const int t3_delay = THREAD_DELAY * 3;
 | 
				
			||||||
    test_thread((void *)"Th 1");
 | 
					    Thread t2(test_thread, (void *)t2_delay);
 | 
				
			||||||
 | 
					    Thread t3(test_thread, (void *)t3_delay);
 | 
				
			||||||
 | 
					    bool result = true;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    while (true) {
 | 
				
			||||||
 | 
					        // Thread 1 action
 | 
				
			||||||
 | 
					        Thread::wait(t1_delay);
 | 
				
			||||||
 | 
					        manipulate_protected_zone(t1_delay);
 | 
				
			||||||
 | 
					        if (change_counter >= SIGNALS_TO_EMIT) {
 | 
				
			||||||
 | 
					            break;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    notify_completion(result);
 | 
				
			||||||
 | 
					    return 0;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,20 +1,55 @@
 | 
				
			||||||
#include "mbed.h"
 | 
					#include "mbed.h"
 | 
				
			||||||
 | 
					#include "test_env.h"
 | 
				
			||||||
#include "rtos.h"
 | 
					#include "rtos.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Semaphore two_slots(2);
 | 
					#define THREAD_DELAY     100
 | 
				
			||||||
 | 
					#define SEMAPHORE_SLOTS  2
 | 
				
			||||||
 | 
					#define SEM_CHANGES      100
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void test_thread(void const *name) {
 | 
					void print_char(char c = '*')
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    printf("%c", c);
 | 
				
			||||||
 | 
					    fflush(stdout);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Semaphore two_slots(SEMAPHORE_SLOTS);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					volatile int change_counter = 0;
 | 
				
			||||||
 | 
					volatile int sem_counter = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void test_thread(void const *delay) {
 | 
				
			||||||
 | 
					    const int thread_delay = int(delay);
 | 
				
			||||||
    while (true) {
 | 
					    while (true) {
 | 
				
			||||||
        two_slots.wait();
 | 
					        two_slots.wait();
 | 
				
			||||||
        printf("%s\n\r", (const char*)name);
 | 
					        sem_counter++;
 | 
				
			||||||
        Thread::wait(1000);
 | 
					        const bool sem_lock_failed = sem_counter > SEMAPHORE_SLOTS;
 | 
				
			||||||
 | 
					        const char msg = sem_lock_failed ? 'e' : sem_counter + '0';
 | 
				
			||||||
 | 
					        print_char(msg);
 | 
				
			||||||
 | 
					        if (sem_lock_failed) {
 | 
				
			||||||
 | 
					            notify_completion(false);
 | 
				
			||||||
 | 
					            exit(1);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        Thread::wait(thread_delay);
 | 
				
			||||||
 | 
					        print_char('.');
 | 
				
			||||||
 | 
					        sem_counter--;
 | 
				
			||||||
 | 
					        change_counter++;
 | 
				
			||||||
        two_slots.release();
 | 
					        two_slots.release();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int main (void) {
 | 
					int main (void) {
 | 
				
			||||||
    Thread t2(test_thread, (void *)"Th 2");
 | 
					    const int t1_delay = THREAD_DELAY * 1;
 | 
				
			||||||
    Thread t3(test_thread, (void *)"Th 3");
 | 
					    const int t2_delay = THREAD_DELAY * 2;
 | 
				
			||||||
    
 | 
					    const int t3_delay = THREAD_DELAY * 3;
 | 
				
			||||||
    test_thread((void *)"Th 1");
 | 
					    Thread t1(test_thread, (void *)t1_delay);
 | 
				
			||||||
 | 
					    Thread t2(test_thread, (void *)t2_delay);
 | 
				
			||||||
 | 
					    Thread t3(test_thread, (void *)t3_delay);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    while (true) {
 | 
				
			||||||
 | 
					        if (change_counter >= SEM_CHANGES) {
 | 
				
			||||||
 | 
					            notify_completion(true);
 | 
				
			||||||
 | 
					            break;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return 0;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -507,15 +507,17 @@ TESTS = [
 | 
				
			||||||
        "host_test": "wait_us_auto"
 | 
					        "host_test": "wait_us_auto"
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        "id": "RTOS_2", "description": "Mutex",
 | 
					        "id": "RTOS_2", "description": "Mutex resource lock/1",
 | 
				
			||||||
        "source_dir": join(TEST_DIR, "rtos", "mbed", "mutex"),
 | 
					        "source_dir": join(TEST_DIR, "rtos", "mbed", "mutex"),
 | 
				
			||||||
        "dependencies": [MBED_LIBRARIES, RTOS_LIBRARIES],
 | 
					        "dependencies": [MBED_LIBRARIES, RTOS_LIBRARIES, TEST_MBED_LIB],
 | 
				
			||||||
        "duration": 20
 | 
					        "duration": 20,
 | 
				
			||||||
 | 
					        "automated": True,
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        "id": "RTOS_3", "description": "Semaphore",
 | 
					        "id": "RTOS_3", "description": "Semaphore",
 | 
				
			||||||
        "source_dir": join(TEST_DIR, "rtos", "mbed", "semaphore"),
 | 
					        "source_dir": join(TEST_DIR, "rtos", "mbed", "semaphore"),
 | 
				
			||||||
        "dependencies": [MBED_LIBRARIES, RTOS_LIBRARIES],
 | 
					        "dependencies": [MBED_LIBRARIES, RTOS_LIBRARIES, TEST_MBED_LIB],
 | 
				
			||||||
 | 
					        "automated": True,
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        "id": "RTOS_4", "description": "Signals",
 | 
					        "id": "RTOS_4", "description": "Signals",
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue