Skip to content

Commit

Permalink
Add more examples of locking.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidchisnall committed Dec 20, 2024
1 parent 04bb073 commit e1e81f1
Show file tree
Hide file tree
Showing 8 changed files with 512 additions and 20 deletions.
2 changes: 1 addition & 1 deletion rtos-source
36 changes: 36 additions & 0 deletions text/examples/barrier/barrier.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright Microsoft and CHERIoT
// Contributors.
// SPDX-License-Identifier: MIT

#include <atomic>
#include <stdio.h>
#include <thread.h>

// entry#begin
/// Thread entry point.
__cheri_compartment(
"barrier") void entry()
{
static std::atomic<uint32_t>
barrier = 2;
printf(
"Thread: %d arrived at barrier\n",
thread_id_get());
uint32_t value = --barrier;
if (value == 0)
{
barrier.notify_all();
}
else
{
while (value != 0)
{
barrier.wait(value);
value = barrier;
}
}
printf(
"Thread: %d passed barrier\n",
thread_id_get());
}
// entry#end
50 changes: 50 additions & 0 deletions text/examples/barrier/xmake.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
-- Copyright Microsoft and CHERIoT Contributors.
-- SPDX-License-Identifier: MIT

set_project("CHERIoT example")

sdkdir = os.getenv("CHERIOT_SDK") or
"../../../rtos-source/sdk/"
includes(sdkdir)

set_toolchains("cheriot-clang")

option("board")
set_default("sail")

-- An single compartment for this example.
compartment("barrier")
add_files("barrier.cc")


-- firmware#begin
-- Firmware image for the example.
firmware("barrier_thread")
-- RTOS-provided libraries
add_deps("freestanding", "stdio")
-- Our compartments
add_deps("barrier")
on_load(function(target)
-- The board to target
target:values_set("board", "$(board)")
-- threads#begin
-- Threads to select
target:values_set("threads", {
{
compartment = "barrier",
priority = 1,
entry_point = "entry",
stack_size = 0x400,
trusted_stack_frames = 2
},
{
compartment = "barrier",
priority = 2,
entry_point = "entry",
stack_size = 0x400,
trusted_stack_frames = 2
}
}, {expand = false})
-- threads#end
end)
-- firmware#end
72 changes: 72 additions & 0 deletions text/examples/locking/locking.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright Microsoft and CHERIoT
// Contributors.
// SPDX-License-Identifier: MIT

#include <locks.hh>
#include <stdio.h>
#include <thread.h>

// declare#begin
// Comment out this line and uncomment
// the next one to see how ticket locks
// behave.
FlagLock lock;
// TicketLock lock;
// declare#end

/**
* Function that serves as a placeholder
* for something that does work with the
* lock held.
*/
void do_useful_work()
{
Timeout t{MS_TO_TICKS(1000)};
thread_sleep(
&t, ThreadSleepNoEarlyWake);
}

// low#begin
__cheri_compartment(
"locking") void low()
{
while (true)
{
lock.lock();
printf("Low priority thread "
"acquired lock\n");
do_useful_work();
lock.unlock();
}
}
// low#end

// medium#begin
__cheri_compartment(
"locking") void medium()
{
while (true)
{
lock.lock();
printf("Medium priority thread "
"acquired lock\n");
do_useful_work();
lock.unlock();
}
}
// medium#end

// high#begin
__cheri_compartment(
"locking") void high()
{
while (true)
{
lock.lock();
printf("High priority thread "
"acquired lock\n");
do_useful_work();
lock.unlock();
}
}
// high#end
57 changes: 57 additions & 0 deletions text/examples/locking/xmake.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
-- Copyright Microsoft and CHERIoT Contributors.
-- SPDX-License-Identifier: MIT

set_project("CHERIoT example")

sdkdir = os.getenv("CHERIOT_SDK") or
"../../../rtos-source/sdk/"
includes(sdkdir)

set_toolchains("cheriot-clang")

option("board")
set_default("sail")

-- An single compartment for this example.
compartment("locking")
add_files("locking.cc")


-- firmware#begin
-- Firmware image for the example.
firmware("locking_thread")
-- RTOS-provided libraries
add_deps("freestanding", "stdio")
-- Our compartments
add_deps("locking")
on_load(function(target)
-- The board to target
target:values_set("board", "$(board)")
-- threads#begin
-- Threads to select
target:values_set("threads", {
{
compartment = "locking",
priority = 1,
entry_point = "low",
stack_size = 0x400,
trusted_stack_frames = 2
},
{
compartment = "locking",
priority = 2,
entry_point = "medium",
stack_size = 0x400,
trusted_stack_frames = 2
},
{
compartment = "locking",
priority = 3,
entry_point = "high",
stack_size = 0x400,
trusted_stack_frames = 2
}
}, {expand = false})
-- threads#end
end)
-- firmware#end
83 changes: 83 additions & 0 deletions text/examples/priority_inheritance/priority_inheritance.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright Microsoft and CHERIoT
// Contributors.
// SPDX-License-Identifier: MIT

#include <locks.hh>
#include <stdio.h>
#include <thread.h>

// Comment this line and uncomment the
// line below to fix the priority
// inversion in this example.
FlagLock lock;
//FlagLockPriorityInherited lock;

// high#begin
__cheri_compartment(
"priority_"
"inheritance") void high()
{
// Let the low and
// medium-priority threads start
Timeout t(MS_TO_TICKS(1000));
thread_sleep(&t);
while (true)
{
t = Timeout(MS_TO_TICKS(1000));
if (LockGuard g{lock, &t})
{
printf(
"High-priority thread "
"acquired the lock!\n");
}
else
{
printf(
"High-priority thread "
"failed to acquire the "
"lock!\n");
}
}
}
// high#end

std::atomic<int> x;

// medium#begin
__cheri_compartment(
"priority_"
"inheritance") void medium()
{
// Let the low-priority thread run
// until it yields
Timeout t(MS_TO_TICKS(1000));
thread_sleep(&t);
printf("Medium priority thread "
"entering infinite loop and "
"not yielding\n");
while (true)
{
x++;
}
}
// medium#end

// low#begin
__cheri_compartment(
"priority_"
"inheritance") void low()
{
while (true)
{
lock.lock();
printf("Low-priority thread "
"acquired the lock\n");
Timeout t(MS_TO_TICKS(500));
thread_sleep(
&t, ThreadSleepNoEarlyWake);
printf("Low-priority thread "
"releasing the lock\n");
lock.unlock();
}
}
// low#end
57 changes: 57 additions & 0 deletions text/examples/priority_inheritance/xmake.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
-- Copyright Microsoft and CHERIoT Contributors.
-- SPDX-License-Identifier: MIT

set_project("CHERIoT example")

sdkdir = os.getenv("CHERIOT_SDK") or
"../../../rtos-source/sdk/"
includes(sdkdir)

set_toolchains("cheriot-clang")

option("board")
set_default("sail")

-- An single compartment for this example.
compartment("priority_inheritance")
add_files("priority_inheritance.cc")


-- firmware#begin
-- Firmware image for the example.
firmware("priority_inheritance_thread")
-- RTOS-provided libraries
add_deps("freestanding", "stdio")
-- Our compartments
add_deps("priority_inheritance")
on_load(function(target)
-- The board to target
target:values_set("board", "$(board)")
-- threads#begin
-- Threads to select
target:values_set("threads", {
{
compartment = "priority_inheritance",
priority = 1,
entry_point = "low",
stack_size = 0x400,
trusted_stack_frames = 2
},
{
compartment = "priority_inheritance",
priority = 2,
entry_point = "medium",
stack_size = 0x400,
trusted_stack_frames = 2
},
{
compartment = "priority_inheritance",
priority = 3,
entry_point = "high",
stack_size = 0x400,
trusted_stack_frames = 2
}
}, {expand = false})
-- threads#end
end)
-- firmware#end
Loading

0 comments on commit e1e81f1

Please sign in to comment.