VxWorks

Using Rust With VxWorks-7

by | Dec 3, 2020 | VxWorks

1 Introduction

VxWorks is a Real Time Operating System built by Wind River, and Rust is a programming language designed
for performance and safety-critical type applications.

Rust : https://www.rust-lang.org/

Wind River supports Rust 1.39 in user space on 32/64-bit ARM and Intel architecture devices. Rust is automatically included
into the VxWorks source build (VSB) projects on those CPU architectures.

This blog describes how to build and deploy a multi-threaded Dining Philosophers Rust application on VxWorks 7 from a Windows
workstation. Rust applications run on VxWorks as real-time processes (RTPs).

 

2 Prerequisites

These instructions assume that you are using:

Wind River VxWorks 7, SR0650

 

3 Related Documentation

For more information on these topics, refer to:

Wind River documentation:
VxWorks Support for Third Party Software

Non-Wind River Documentation:
The Rust Programming Language, https://doc.rust-lang.org/book/

 

4 Create and Build the VxWorks Source Build (VSB) Project

Open a DOS shell, configure the build environment and then build the project.

cd <WIND_HOME>              // your installation directory
wrenv -p vxworks-7
cd <YOUR_WORKSPACE>         // your workspace
vxprj vsb create rust_vsb -bsp vxsim_windows -smp -force -S
cd rust_vsb                 // your workspace
make -j 32                  // build the VSB

Once your VxWorks source build (VSB) has built, check that it contains the Rust layer. Verify that the following directory exists:

<YOUR_WORKSPACE>\rust_vsb\usr\rust\

 

5 Create and Build the Basic VxWorks Image Project (VIP)

Create the basic VxWorks image project (VIP) as follows.

cd ..
vxprj create -smp vxsim_windows rust_vip -profile PROFILE_DEVELOPMENT -vsb rust_vsb
cd rust_vip
vxprj vip bundle add BUNDLE_RUST
                            // set VxWorks process pthread scheduling to the POSIX 1003.1 standard
vxprj vip component add INCLUDE_POSIX_PTHREAD_SCHEDULER
vxprj build

6 Create a Dining Philosophers Example Rust Application

Rust applications are managed and built from the Cargo utility.

cd ..
cd rust_vsb\usr\rust
rustenv.bat
cargo new dining_phil
cd dining_phil\src

Edit the dining_phil project source file main.rs. Replace the contents with the following Rust source code:

use std::sync::{Arc, Mutex};
use std::thread;

struct Philosopher {
    name: String,
    left: usize,
    right: usize,
}

impl Philosopher {
    fn new(name: &str, left: usize, right: usize) -> Philosopher {
        Philosopher {
            name: name.to_string(),
            left: left,
            right: right,
        }
    }

    fn eat(&self, table: &Table) {
        let _left = table.forks[self.left].lock().unwrap();
        let _right = table.forks[self.right].lock().unwrap();

        println!("{} is eating.", self.name);

        thread::sleep_ms(1000);

        println!("{} is done eating.", self.name);
    }
}

struct Table {
    forks: Vec<Mutex<()>>,
}

fn main() {
    let table = Arc::new(Table {
        forks: vec![
            Mutex::new(()),
            Mutex::new(()),
            Mutex::new(()),
            Mutex::new(()),
            Mutex::new(()),
        ],
    });

    let philosophers = vec![
        Philosopher::new("Donald", 0, 1),
        Philosopher::new("Larry", 1, 2),
        Philosopher::new("Mark", 2, 3),
        Philosopher::new("John", 3, 4),
        Philosopher::new("Bruce", 0, 4),
    ];

    let handles: Vec<_> = philosophers
        .into_iter()
        .map(|p| {
            let table = table.clone();

            thread::spawn(move || {
                p.eat(&table);
            })
        })
        .collect();

    for h in handles {
        h.join().unwrap();
    }
}

7 Build Dining Philosophers Example Rust Application

cd ..
cargo build
Compiling dining_phil v0.1.0 (C:\NoScan\WindRiverPRT\workspace\rust_vsb\usr\rust\dining_phil)
warning: use of deprecated item 'std::thread::sleep_ms': replaced by `std::thread::sleep`
  --> src\main.rs:25:9
   |
25 |         thread::sleep_ms(1000);
   |         ^^^^^^^^^^^^^^^^
   |
   = note: `#[warn(deprecated)]` on by default

    Finished dev [unoptimized + debuginfo] target(s) in 2.45s

8 Boot VxWorks and Run the Rust Application

8.1 Boot the VxWorks Simulator Target

cd <YOUR_WORKSPACE>
cd rust_vip\default
vxsim

8.2 Locate the Dining Philosophers Application on the VxWorks Simulator

From the VxWorks Simulator kernel shell:

-> cmd
[vxWorks *]# cd ../../rust_vsb/usr/rust/dining_phil/target/x86_64-wrs-vxworks/debug

8.3 Run the Dining Philosophers Application

[vxWorks *]# ./dining_phil.vxe
Launching process './dining_phil.vxe' ...
Process './dining_phil.vxe' (process Id = 0x4001d6c30) launched.
Donald is eating.
Mark is eating.
Donald is done eating.
Mark is done eating.
Larry is eating.
Bruce is eating.
Larry is done eating.
Bruce is done eating.
John is eating.
John is done eating.
[vxWorks *]#

Download Our VxWorks Cheat Sheet

Download our VxWorks Cheat Sheet; a handy little tool to improve your workflow.

Got A Project In Mind?

Our levels of professionalism and commitment to our clients results in a long waiting list for projects. Let us know in advance if you might need us.