Skip to content

Commit

Permalink
Merge pull request #838 from spark/feature/threading_blocks
Browse files Browse the repository at this point in the history
locking API redux
  • Loading branch information
m-mcgowan committed Jan 26, 2016
2 parents f706de6 + 5a457b1 commit 5cf2c7e
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 20 deletions.
13 changes: 7 additions & 6 deletions system/src/system_setup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,13 @@ template <typename Config> SystemSetupConsole<Config>::SystemSetupConsole(Config

template<typename Config> void SystemSetupConsole<Config>::loop(void)
{
WITH_LOCK(serial);
if (serial.available()) {
int c = serial.read();
if (c>=0)
handle((char)c);
}
TRY_LOCK(serial) {
if (serial.available()) {
int c = serial.read();
if (c>=0)
handle((char)c);
}
}
}

template<typename Config> void SystemSetupConsole<Config>::handle(char c)
Expand Down
15 changes: 8 additions & 7 deletions user/tests/wiring/no_fixture/interrupts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ test(interrupts_atomic_section)
uint32_t start_micros = micros();
uint32_t start_millis, end_millis;
{
ATOMIC_SECTION();
start_millis = millis();
while (micros()-start_micros<2000)
{
// validates that atomic sections can be nested and interrupts restored when the outermost one exits
ATOMIC_SECTION();
ATOMIC_BLOCK() {
start_millis = millis();
while (micros()-start_micros<2000)
{
// validates that atomic sections can be nested and interrupts restored when the outermost one exits
ATOMIC_BLOCK();
}
end_millis = millis();
}
end_millis = millis();
}
assertEqual(start_millis, end_millis);

Expand Down
28 changes: 28 additions & 0 deletions user/tests/wiring/no_fixture/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,34 @@ test(Thread_creation)
assertTrue((bool)threadRan);
}

test(thread_SingleThreadedBlock)
{
SINGLE_THREADED_BLOCK() {

}
SINGLE_THREADED_BLOCK() {

}
}

test(thread_with_lock)
{
WITH_LOCK(Serial) {

}

WITH_LOCK(Serial) {

}

}

test(thread_try_lock)
{
TRY_LOCK(Serial) {

}
}

// todo - test for SingleThreadedSection

Expand Down
2 changes: 1 addition & 1 deletion wiring/inc/spark_wiring_interrupts.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class AtomicSection {
}
};

#define ATOMIC_SECTION() AtomicSection __as;
#define ATOMIC_BLOCK() for (bool __todo=true; __todo;) for (AtomicSection __as; __todo; __todo=false)



Expand Down
12 changes: 6 additions & 6 deletions wiring/inc/spark_wiring_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,17 +179,17 @@ class SingleThreadedSection {
}
};

#define SINGLE_THREADED_SECTION() SingleThreadedSection __cs;
#define WITH_LOCK(lock) std::lock_guard<decltype(lock)> __lock##lock((lock));
#define TRY_LOCK(lock) std::unique_lock<std::remove_reference<decltype(lock)>::type> __lock##lock((lock), std::try_to_lock);
#define IS_LOCKED(lock) (__lock##lock)
#define SINGLE_THREADED_SECTION() SingleThreadedSection __cs;

#else
#define SINGLE_THREADED_BLOCK() for (bool __todo = true; __todo; ) for (SingleThreadedSection __cs; __todo; __todo=0)
#define WITH_LOCK(lock) for (bool __todo = true; __todo;) for (std::lock_guard<decltype(lock)> __lock##lock((lock)); __todo; __todo=0)
#define TRY_LOCK(lock) for (bool __todo = true; __todo; ) for (std::unique_lock<typename std::remove_reference<decltype(lock)>::type> __lock##lock((lock), std::try_to_lock); __todo &= bool(__lock##lock); __todo=0)

#else
#define SINGLE_THREADED_SECTION()
#define SINGLE_THREADED_BLOCK()
#define WITH_LOCK(x)
#define TRY_LOCK(x)
#define IS_LOCKED(lock) (true)

#endif

Expand Down

0 comments on commit 5cf2c7e

Please sign in to comment.