Rust with VxWorks7

在VxWorks7下使用Rust

1 简介

VxWorks是由Wind River提供的实时操作系统,而Python是由Python Software Foundation管理的开源解释型编程语言和运行时解释器。

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

风河在32/64位ARM和Intel架构设备上的用户空间中支持Rust 1.39,在这些CPU架构上,Rust会被VxWorks源代码编译(VSB)项目自动包含。

本文描述了如何通在Windows上为VxWorks 7编译和部署多线程的Dining Philosophers Rust应用程序,Rust应用程序是作为实时过程(RTP)在VxWorks上运行的。

2 前提

本文的说明假定你正在使用:

3 相关文档

关于这些主题的更多信息,参考如下文档:

  • Wind River documentation:
  • VxWorks Support for Third Party Software

Non-Wind River Documentation:

  • The Rust Programming Language, https://doc.rust-lang.org/book/

4 创建/编译VxWorks Source Build(VSB)工程

打开一个DOS shell,配置编译环境,然后编译工程。


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

在你的VxWorks Source Build(VSB)编译成功后,检查其是否包含了Rust层。验证下面这个目录是存在的:

"YOUR_WORKSPACE"\rust_vsb\usr\rust\

5 创建/编译基本VxWorks Image Project(VIP)

像下面一样创建基本的VxWorks镜像工程


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 创建一个Dining Philosophers例子Rust应用程序

Rust应用程序是通过Cargo实用程序管理和编译的。


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

编辑dining_phil项目的源文件main.rs,将内容替换为以下Rust源代码:


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>,
}

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 编译Dining Philosophers例子Rust应用程序


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 启动VxWorks并运行Rust应用程序

8.1 启动VxWorks仿真目标机(Simulator Target)


cd "YOUR_WORKSPACE"
cd rust_vip\default
vxsim

8.2 在VxWorks仿真目标机上找到Dining Philosophers应用程序

从VxWorks的Simulator kernel shell上运行如下命令:


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

8.3 运行Dining Philosophers应用程序


[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 *]#