xwrust/xwos/lock/seqlock.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
//! XWOS RUST:顺序锁
//! ========
//!
//! 顺序锁是对自旋锁改良后的锁,顺序锁中包含一个自旋锁,并且带有一个顺序值。
//! 顺序锁将临界区分为三种:
//!
//! #### 独占写
//!
//! 任何对顺序锁临界区的 **写** 操作都是独占的,每次 **写** 操作时,会先上锁自旋锁,
//! 然后增加顺序值,离开临界区时也会再次增加顺序值。言外之意,顺序值增加两次,顺序值增加的次数是 **偶数** 。
//!
//! 锁的模式:
//!
//! + 当临界区只被多个线程访问时,需使用关闭抢占的模式: [`SeqlockMode::WriteLock`]
//! + 当临界区被中断底半部访问时,需使用关闭中断底半部的模式: [`SeqlockMode::WriteLockBh`]
//! + 当临界区被多个线程和单一中断访问时,需使用关闭中断的模式: [`SeqlockMode::WriteLockCpuirq`]
//! + 当临界区被多个中断上下文访问时,需使用保存中断标志的模式: [`SeqlockMode::WriteLockCpuirqSave(None)`]
//!
//! #### 非独占读
//!
//! 如果多个CPU进行的是 **只读** 操作,它们可以同时进入 **非独占读临界区** 。
//! CPU进入非独占读临界区无需获得自旋锁,但需要先测试顺序值是否为 **偶数**,并记录此时的顺序值。
//! 当退出读临界区时,需要再次读取顺序值,并与之前记录的值进行比较:
//!
//! + 如果相等,表示读的结果有效;
//! + 如果不相等,则表示读的过程中别的CPU进行了写操作,此次的读操作无效。
//!
//! #### 独占读
//!
//! 如果希望读临界区不会被写操作无效掉,可以使用独占读的方式,
//! 独占读会排斥其他CPU上的独占写和独占读操作,但不会排斥非独占读,
//! 其他CPU依然可以进入非独占读临界区。
//!
//! 锁的模式 :
//!
//! + 当临界区只被多个线程访问时,需使用关闭抢占的模式: [`SeqlockMode::ReadExclusiveLock`]
//! + 当临界区被中断底半部访问时,需使用关闭中断底半部的模式: [`SeqlockMode::ReadExclusiveLockBh`]
//! + 当临界区被多个线程和单一中断访问时,需使用关闭中断的模式: [`SeqlockMode::ReadExclusiveLockCpuirq`]
//! + 当临界区被多个中断上下文访问时,需使用保存中断标志的模式: [`SeqlockMode::ReadExclusiveLockCpuirqSave(None)`]
//!
//!
//! # 创建
//!
//! XWOS RUST的顺序锁可使用 [`Seqlock::new()`] 创建。
//!
//! + 可以创建具有静态生命周期 [`static`] 约束的全局变量:
//!
//! ```rust
//! use xwrust::xwos::lock::seqlock::*;
//!
//! static GLOBAL_SEQLOCK: Seqlock<u32> = Seqlock::new(0);
//! ```
//!
//! + 也可以使用 [`alloc::sync::Arc`] 在heap中创建:
//!
//! ```rust
//! extern crate alloc;
//! use alloc::sync::Arc;
//!
//! use xwrust::xwos::lock::seqlock::*;
//!
//! pub fn xwrust_example_seqlock() {
//! let seqlock: Arc<Seqlock<u32>> = Arc::new(Seqlock::new(0));
//! }
//! ```
//!
//!
//! # 解锁
//!
//! 上锁后返回的 [`SeqlockGuard`] 。 [`SeqlockGuard`] 的生命周期结束时,会自动解锁。
//! 也可调用 [`Seqlock::unlock()`] 主动消费掉 [`SeqlockGuard`] 来解锁。
//!
//!
//! # 上锁
//!
//! 上锁的方法 [`Seqlock::lock()`] 除了 **自旋** 等待,改变顺序值外,还会根据上锁模式增加额外操作:
//!
//! + 只关闭抢占的模式: [`SeqlockMode::WriteLock`] 以及 [`SeqlockMode::ReadExclusiveLock`]
//! + 关闭中断底半部的模式: [`SeqlockMode::WriteLockBh`] 以及 [`SeqlockMode::ReadExclusiveLockBh`]
//! + 关闭全局中断的模式: [`SeqlockMode::WriteLockCpuirq`] 以及 [`SeqlockMode::ReadExclusiveLockCpuirq`]
//! + 保存中断全局标志并关闭全局中断的模式: [`SeqlockMode::WriteLockCpuirqSave(None)`] 以及 [`SeqlockMode::ReadExclusiveLockCpuirqSave(None)`]
//!
//!
//! # 尝试上锁
//!
//! 尝试上锁的方法 [`Seqlock::trylock()`] 只会测试一下锁,不会 **自旋** 等待,上锁成功后还会根据上锁模式增加额外操作:
//!
//! + 只关闭抢占的模式: [`SeqlockMode::WriteLock`] 以及 [`SeqlockMode::ReadExclusiveLock`]
//! + 关闭中断底半部的模式: [`SeqlockMode::WriteLockBh`] 以及 [`SeqlockMode::ReadExclusiveLockBh`]
//! + 关闭全局中断的模式: [`SeqlockMode::WriteLockCpuirq`] 以及 [`SeqlockMode::ReadExclusiveLockCpuirq`]
//! + 保存中断全局标志并关闭全局中断的模式: [`SeqlockMode::WriteLockCpuirqSave(None)`] 以及 [`SeqlockMode::ReadExclusiveLockCpuirqSave(None)`]
//!
//!
//! # 示例
//!
//! [XWOS/xwam/xwrust-example/xwrust_example_seqlock](https://gitee.com/xwos/XWOS/blob/main/xwam/xwrust-example/xwrust_example_seqlock/src/lib.rs)
//!
//! [`SeqlockMode::WriteLockCpuirqSave(None)`]: SeqlockMode::WriteLockCpuirqSave
//! [`SeqlockMode::ReadExclusiveLockCpuirqSave(None)`]: SeqlockMode::ReadExclusiveLockCpuirqSave
//! [`static`]: https://doc.rust-lang.org/std/keyword.static.html
//! [`alloc::sync::Arc`]: <https://doc.rust-lang.org/alloc/sync/struct.Arc.html>
extern crate core;
use core::cell::UnsafeCell;
use core::result::Result;
use core::default::Default;
use core::option::Option;
use core::ops::Drop;
use core::ops::Deref;
use core::ops::DerefMut;
use core::ptr;
use crate::types::*;
use crate::errno::*;
use crate::xwos::sync::cond::*;
extern "C" {
fn xwrustffi_sqlk_init(sqlk: *mut XwosSqlk);
fn xwrustffi_sqlk_rd_begin(sqlk: *mut XwosSqlk) -> XwSq;
fn xwrustffi_sqlk_rd_retry(sqlk: *mut XwosSqlk, start: XwSq) -> bool;
fn xwrustffi_sqlk_rdex_unlock(sqlk: *mut XwosSqlk);
fn xwrustffi_sqlk_rdex_lock(sqlk: *mut XwosSqlk);
fn xwrustffi_sqlk_rdex_trylock(sqlk: *mut XwosSqlk) -> XwEr;
fn xwrustffi_sqlk_rdex_unlock_cpuirq(sqlk: *mut XwosSqlk);
fn xwrustffi_sqlk_rdex_lock_cpuirq(sqlk: *mut XwosSqlk);
fn xwrustffi_sqlk_rdex_trylock_cpuirq(sqlk: *mut XwosSqlk) -> XwEr;
fn xwrustffi_sqlk_rdex_unlock_cpuirqrs(sqlk: *mut XwosSqlk, cpuirq: XwReg);
fn xwrustffi_sqlk_rdex_lock_cpuirqsv(sqlk: *mut XwosSqlk, cpuirq: *mut XwReg);
fn xwrustffi_sqlk_rdex_trylock_cpuirqsv(sqlk: *mut XwosSqlk, cpuirq: *mut XwReg) -> XwEr;
fn xwrustffi_sqlk_rdex_unlock_bh(sqlk: *mut XwosSqlk);
fn xwrustffi_sqlk_rdex_lock_bh(sqlk: *mut XwosSqlk);
fn xwrustffi_sqlk_rdex_trylock_bh(sqlk: *mut XwosSqlk) -> XwEr;
fn xwrustffi_sqlk_wr_unlock(sqlk: *mut XwosSqlk);
fn xwrustffi_sqlk_wr_lock(sqlk: *mut XwosSqlk);
fn xwrustffi_sqlk_wr_trylock(sqlk: *mut XwosSqlk) -> XwEr;
fn xwrustffi_sqlk_wr_unlock_cpuirq(sqlk: *mut XwosSqlk);
fn xwrustffi_sqlk_wr_lock_cpuirq(sqlk: *mut XwosSqlk);
fn xwrustffi_sqlk_wr_trylock_cpuirq(sqlk: *mut XwosSqlk) -> XwEr;
fn xwrustffi_sqlk_wr_unlock_cpuirqrs(sqlk: *mut XwosSqlk, cpuirq: XwReg);
fn xwrustffi_sqlk_wr_lock_cpuirqsv(sqlk: *mut XwosSqlk, cpuirq: *mut XwReg);
fn xwrustffi_sqlk_wr_trylock_cpuirqsv(sqlk: *mut XwosSqlk, cpuirq: *mut XwReg) -> XwEr;
fn xwrustffi_sqlk_wr_unlock_bh(sqlk: *mut XwosSqlk);
fn xwrustffi_sqlk_wr_lock_bh(sqlk: *mut XwosSqlk);
fn xwrustffi_sqlk_wr_trylock_bh(sqlk: *mut XwosSqlk) -> XwEr;
}
/// 顺序锁的错误码
#[derive(Debug)]
pub enum SeqlockError {
/// 尝试上锁失败
Again(XwEr),
/// 未知错误
Unknown(XwEr),
}
impl SeqlockError {
/// 消费掉 `SeqlockError` 自身,返回内部的错误码。
pub fn unwrap(self) -> XwEr {
match self {
Self::Again(rc) => rc,
Self::Unknown(rc) => rc,
}
}
}
/// 顺序锁的上锁模式
pub enum SeqlockMode {
/// 写,关闭抢占
WriteLock,
/// 写,关闭抢占、中断底半部
WriteLockBh,
/// 写,关闭抢占、中断底半部和中断
WriteLockCpuirq,
/// 写,关闭抢占、中断底半部和中断,并保存之前的中断标志
WriteLockCpuirqSave(Option<XwReg>),
/// 独占读,关闭抢占
ReadExclusiveLock,
/// 独占读,关闭抢占、中断底半部
ReadExclusiveLockBh,
/// 独占读,关闭抢占、中断底半部和中断
ReadExclusiveLockCpuirq,
/// 独占读,关闭抢占、中断底半部和中断,并保存之前的中断标志
ReadExclusiveLockCpuirqSave(Option<XwReg>),
}
/// XWOS顺序锁占用的内存大小
#[cfg(target_pointer_width = "32")]
pub const SIZEOF_XWOS_SQLK: usize = 8;
/// XWOS顺序锁占用的内存大小
#[cfg(target_pointer_width = "64")]
pub const SIZEOF_XWOS_SQLK: usize = 16;
/// 用于构建顺序锁的内存数组类型
#[repr(C)]
#[cfg_attr(target_pointer_width = "32", repr(align(8)))]
#[cfg_attr(target_pointer_width = "64", repr(align(16)))]
pub(crate) struct XwosSqlk {
pub(crate) obj: [u8; SIZEOF_XWOS_SQLK],
}
/// 用于构建顺序锁的内存数组常量
///
/// 此常量的作用是告诉编译器顺序锁需要多大的内存。
pub(crate) const XWOS_SQLK_INITIALIZER: XwosSqlk = XwosSqlk {
obj: [0; SIZEOF_XWOS_SQLK],
};
/// 顺序锁结构体
pub struct Seqlock<T: ?Sized> {
/// XWOS顺序锁
pub(crate) sqlk: UnsafeCell<XwosSqlk>,
/// 上锁方式
pub(crate) mode: UnsafeCell<SeqlockMode>,
/// 用户数据
pub data: UnsafeCell<T>,
}
impl<T> Seqlock<T> {
/// 新建顺序锁
///
/// 此方法是编译期方法,可用于新建 [`static`] 约束的全局变量。
///
/// # 示例
///
/// ```rust
/// use xwrust::xwos::lock::seqlock::*;
///
/// static GLOBAL_SEQLOCK: Seqlock<u32> = Seqlock::new(0);
/// ```
///
/// [`static`]: https://doc.rust-lang.org/std/keyword.static.html
pub const fn new(t: T) -> Self {
Self {
sqlk: UnsafeCell::new(XWOS_SQLK_INITIALIZER),
mode: UnsafeCell::new(SeqlockMode::WriteLock),
data: UnsafeCell::new(t),
}
}
}
impl<T: ?Sized> Seqlock<T> {
/// 开启共享读临界区
///
/// 若其他CPU正在访问写临界区,此方法会一直 **自旋** 测试是否可进入 **共享读临界区** 。
///
/// # 示例
///
/// ```rust
/// loop {
/// let guard = sqlk.read_begin();
/// let val = *guard; // 读取
/// if !guard.read_retry() {
/// break;
/// }
/// }
/// ```
pub fn read_begin(&self) -> SeqlockReadGuard<'_, T> {
unsafe {
let start = xwrustffi_sqlk_rd_begin(self.sqlk.get());
SeqlockReadGuard::new(self, start)
}
}
fn read_retry(&self, start: XwSq) -> bool {
unsafe {
xwrustffi_sqlk_rd_retry(self.sqlk.get(), start)
}
}
/// 获取顺序锁,若线程无法获取顺序锁,就阻塞等待,直到能获得锁为止
///
/// + 若成功获取顺序锁,将返回 **RAII Guard** : [`SeqlockGuard`] ,用于提供 **Scoped Lock** 机制。
/// + [`SeqlockGuard`] 中包含 [`Seqlock`] 的引用, 当 [`SeqlockGuard`] 生命周期结束时,会在 [`drop()`] 方法中自动解锁顺序锁。
/// + 若失败,将返回错误码 [`SeqlockError`] 。
///
/// # 参数说明
///
/// + mode: 锁模式
/// + [`SeqlockMode::WriteLock`] 写,关闭抢占
/// + [`SeqlockMode::WriteLockBh`] 写,关闭抢占、中断底半部
/// + [`SeqlockMode::WriteLockCpuirq`] 写,关闭抢占、中断底半部和中断
/// + [`SeqlockMode::WriteLockCpuirqSave(None)`] 写,关闭抢占、中断底半部和中断,并保存之前的中断标志
/// + [`SeqlockMode::ReadExclusiveLock`] 独占读,关闭抢占
/// + [`SeqlockMode::ReadExclusiveLockBh`] 独占读,关闭抢占、中断底半部
/// + [`SeqlockMode::ReadExclusiveLockCpuirq`] 独占读,关闭抢占、中断底半部和中断
/// + [`SeqlockMode::ReadExclusiveLockCpuirqSave(None)`] 独占读,关闭抢占、中断底半部和中断,并保存之前的中断标志
///
/// # 示例
///
/// ```rust
/// use xwrust::xwos::lock::seqlock::*;
/// static GLOBAL_SEQLOCK: Seqlock<u32> = Seqlock::new(0);
///
/// pub fn xwrust_example_seqlock() {
/// // ...省略...
/// let mut guard GLOBAL_SEQLOCK.lock(SeqlockMode::ReadExclusiveLockCpuirqSave(None));
/// *guard = 1; // 写共享变量
/// drop(guard); // guard 生命周期结束,自动解锁,并恢复中断标志
/// }
/// ```
///
/// [`SeqlockMode::WriteLockCpuirqSave(None)`]: SeqlockMode::WriteLockCpuirqSave
/// [`SeqlockMode::ReadExclusiveLockCpuirqSave(None)`]: SeqlockMode::ReadExclusiveLockCpuirqSave
/// [`drop()`]: https://doc.rust-lang.org/std/mem/fn.drop.html
pub fn lock(&self, mode: SeqlockMode) -> SeqlockGuard<'_, T> {
unsafe {
match mode {
SeqlockMode::ReadExclusiveLock => {
xwrustffi_sqlk_rdex_lock(self.sqlk.get());
*self.mode.get() = SeqlockMode::ReadExclusiveLock;
},
SeqlockMode::ReadExclusiveLockCpuirq => {
xwrustffi_sqlk_rdex_lock_cpuirq(self.sqlk.get());
*self.mode.get() = SeqlockMode::ReadExclusiveLockCpuirq;
},
SeqlockMode::ReadExclusiveLockCpuirqSave(_) => {
let mut cpuirq: XwReg = 0;
xwrustffi_sqlk_rdex_lock_cpuirqsv(self.sqlk.get(), &mut cpuirq);
*self.mode.get() = SeqlockMode::ReadExclusiveLockCpuirqSave(Some(cpuirq));
},
SeqlockMode::ReadExclusiveLockBh => {
xwrustffi_sqlk_rdex_lock_bh(self.sqlk.get());
*self.mode.get() = SeqlockMode::ReadExclusiveLockBh;
},
SeqlockMode::WriteLock => {
xwrustffi_sqlk_wr_lock(self.sqlk.get());
*self.mode.get() = SeqlockMode::WriteLock;
},
SeqlockMode::WriteLockCpuirq => {
xwrustffi_sqlk_wr_lock_cpuirq(self.sqlk.get());
*self.mode.get() = SeqlockMode::WriteLockCpuirq;
},
SeqlockMode::WriteLockCpuirqSave(_) => {
let mut cpuirq: XwReg = 0;
xwrustffi_sqlk_wr_lock_cpuirqsv(self.sqlk.get(), &mut cpuirq);
*self.mode.get() = SeqlockMode::WriteLockCpuirqSave(Some(cpuirq));
},
SeqlockMode::WriteLockBh => {
xwrustffi_sqlk_wr_lock_bh(self.sqlk.get());
*self.mode.get() = SeqlockMode::WriteLockBh;
},
}
SeqlockGuard::new(self)
}
}
/// 尝试获取顺序锁,若线程无法获取顺序锁,立即返回错误
///
/// + 若成功获取顺序锁,将返回 **RAII Guard** : [`SeqlockGuard`] ,用于提供 **Scoped Lock** 机制。
/// + [`SeqlockGuard`] 中包含 [`Seqlock`] 的引用, 当 [`SeqlockGuard`] 生命周期结束时,会在 [`drop()`] 方法中自动解锁顺序锁。
/// + 若失败,将返回错误码 [`SeqlockError`] 。
///
///
/// # 参数说明
///
/// + mode: 锁模式
/// + [`SeqlockMode::WriteLock`] 写,关闭抢占
/// + [`SeqlockMode::WriteLockBh`] 写,关闭抢占、中断底半部
/// + [`SeqlockMode::WriteLockCpuirq`] 写,关闭抢占、中断底半部和中断
/// + [`SeqlockMode::WriteLockCpuirqSave(None)`] 写,关闭抢占、中断底半部和中断,并保存之前的中断标志
/// + [`SeqlockMode::ReadExclusiveLock`] 独占读,关闭抢占
/// + [`SeqlockMode::ReadExclusiveLockBh`] 独占读,关闭抢占、中断底半部
/// + [`SeqlockMode::ReadExclusiveLockCpuirq`] 独占读,关闭抢占、中断底半部和中断
/// + [`SeqlockMode::ReadExclusiveLockCpuirqSave(None)`] 独占读,关闭抢占、中断底半部和中断,并保存之前的中断标志
///
/// # 错误码
///
/// + [`SeqlockError::Again`] 尝试获取锁失败
///
/// # 示例
/// ```rust
/// use xwrust::xwos::lock::seqlock::*;
/// static GLOBAL_SEQLOCK: Seqlock<u32> = Seqlock::new(0);
///
/// pub fn xwrust_example_seqlock() {
/// // ...省略...
/// match GLOBAL_SEQLOCK.trylock(SeqlockMode::ReadExclusiveLockCpuirq) {
/// Ok(mut guard) => { // 上锁成功
/// *guard = 1; // 写共享变量
/// } // guard 生命周期结束,自动解锁
/// Err(e) => {
/// // 上锁失败
/// }
/// }
/// }
/// ```
///
/// [`SeqlockMode::WriteLockCpuirqSave(None)`]: SeqlockMode::WriteLockCpuirqSave
/// [`SeqlockMode::ReadExclusiveLockCpuirqSave(None)`]: SeqlockMode::ReadExclusiveLockCpuirqSave
/// [`drop()`]: https://doc.rust-lang.org/std/mem/fn.drop.html
pub fn trylock(&self, mode: SeqlockMode) -> Result<SeqlockGuard<'_, T>, SeqlockError> {
unsafe {
let rc: XwEr;
match mode {
SeqlockMode::ReadExclusiveLock => {
rc = xwrustffi_sqlk_rdex_trylock(self.sqlk.get());
if 0 == rc {
*self.mode.get() = SeqlockMode::ReadExclusiveLock;
Ok(SeqlockGuard::new(self))
} else if -EAGAIN == rc {
Err(SeqlockError::Again(rc))
} else {
Err(SeqlockError::Unknown(rc))
}
},
SeqlockMode::ReadExclusiveLockCpuirq => {
rc = xwrustffi_sqlk_rdex_trylock_cpuirq(self.sqlk.get());
if 0 == rc {
*self.mode.get() = SeqlockMode::ReadExclusiveLockCpuirq;
Ok(SeqlockGuard::new(self))
} else if -EAGAIN == rc {
Err(SeqlockError::Again(rc))
} else {
Err(SeqlockError::Unknown(rc))
}
},
SeqlockMode::ReadExclusiveLockCpuirqSave(_) => {
let mut cpuirq: XwReg = 0;
rc = xwrustffi_sqlk_rdex_trylock_cpuirqsv(self.sqlk.get(), &mut cpuirq);
if 0 == rc {
*self.mode.get() = SeqlockMode::ReadExclusiveLockCpuirqSave(Some(cpuirq));
Ok(SeqlockGuard::new(self))
} else if -EAGAIN == rc {
Err(SeqlockError::Again(rc))
} else {
Err(SeqlockError::Unknown(rc))
}
},
SeqlockMode::ReadExclusiveLockBh => {
rc = xwrustffi_sqlk_rdex_trylock_bh(self.sqlk.get());
if 0 == rc {
*self.mode.get() = SeqlockMode::ReadExclusiveLockBh;
Ok(SeqlockGuard::new(self))
} else if -EAGAIN == rc {
Err(SeqlockError::Again(rc))
} else {
Err(SeqlockError::Unknown(rc))
}
},
SeqlockMode::WriteLock => {
rc = xwrustffi_sqlk_wr_trylock(self.sqlk.get());
if 0 == rc {
*self.mode.get() = SeqlockMode::WriteLock;
Ok(SeqlockGuard::new(self))
} else if -EAGAIN == rc {
Err(SeqlockError::Again(rc))
} else {
Err(SeqlockError::Unknown(rc))
}
},
SeqlockMode::WriteLockCpuirq => {
rc = xwrustffi_sqlk_wr_trylock_cpuirq(self.sqlk.get());
if 0 == rc {
*self.mode.get() = SeqlockMode::WriteLockCpuirq;
Ok(SeqlockGuard::new(self))
} else if -EAGAIN == rc {
Err(SeqlockError::Again(rc))
} else {
Err(SeqlockError::Unknown(rc))
}
},
SeqlockMode::WriteLockCpuirqSave(_) => {
let mut cpuirq: XwReg = 0;
rc = xwrustffi_sqlk_wr_trylock_cpuirqsv(self.sqlk.get(), &mut cpuirq);
if 0 == rc {
*self.mode.get() = SeqlockMode::WriteLockCpuirqSave(Some(cpuirq));
Ok(SeqlockGuard::new(self))
} else if -EAGAIN == rc {
Err(SeqlockError::Again(rc))
} else {
Err(SeqlockError::Unknown(rc))
}
},
SeqlockMode::WriteLockBh => {
rc = xwrustffi_sqlk_wr_trylock_bh(self.sqlk.get());
if 0 == rc {
*self.mode.get() = SeqlockMode::WriteLockBh;
Ok(SeqlockGuard::new(self))
} else if -EAGAIN == rc {
Err(SeqlockError::Again(rc))
} else {
Err(SeqlockError::Unknown(rc))
}
},
}
}
}
/// 解锁顺序锁,并释放 [`SeqlockGuard`]
///
/// # 示例
///
/// ```rust
/// use xwrust::xwos::lock::seqlock::*;
/// static GLOBAL_SEQLOCK: Seqlock<u32> = Seqlock::new(0);
///
/// pub fn xwrust_example_seqlock() {
/// // ...省略...
/// match GLOBAL_SEQLOCK.lock() {
/// Ok(mut guard) => { // 上锁成功
/// *guard = 1; // 访问共享变量
/// Seqlock::unlock(guard); // 主动解锁
/// }
/// Err(e) => {
/// // 上锁失败
/// }
/// }
/// }
/// ```
///
/// [`drop()`]: https://doc.rust-lang.org/std/mem/fn.drop.html
pub fn unlock(guard: SeqlockGuard<'_, T>) {
drop(guard)
}
}
unsafe impl<T: ?Sized + Send> Send for Seqlock<T> {}
unsafe impl<T: ?Sized + Send> Sync for Seqlock<T> {}
impl<T> From<T> for Seqlock<T> {
/// 从数据新建顺序锁
///
/// 此方法会将数据所有权转移到顺序锁的内部
///
/// 等价于 [`Seqlock::new`]
fn from(t: T) -> Self {
Seqlock::new(t)
}
}
impl<T: ?Sized + Default> Default for Seqlock<T> {
fn default() -> Seqlock<T> {
Seqlock::new(Default::default())
}
}
impl<T: ?Sized> Drop for Seqlock<T> {
fn drop(&mut self) {
}
}
/// 顺序锁共享读临界区的守卫
///
/// 共享读临界区的守卫只提供解引用到只读引用。
///
pub struct SeqlockReadGuard<'a, T: ?Sized + 'a> {
/// 顺序锁的引用
lock: &'a Seqlock<T>,
start: XwSq,
}
impl<T: ?Sized> !Send for SeqlockReadGuard<'_, T> {}
unsafe impl<T: ?Sized + Sync> Sync for SeqlockReadGuard<'_, T> {}
impl<'a, T: ?Sized> SeqlockReadGuard<'a, T> {
fn new(lock: &'a Seqlock<T>, start: XwSq) -> SeqlockReadGuard<'a, T> {
SeqlockReadGuard {
lock: lock,
start: start,
}
}
/// 关闭共享读临界区,并检查是否需要重试
///
/// + 当顺序锁的顺序值没有发生变化,返回 [`false`] ;
/// + 当顺序锁的顺序值发生了变化,返回 [`true`] 。
///
/// # 示例
///
/// ```rust
/// loop {
/// let guard = sqlk.read_begin();
/// let val = *guard;
/// if !guard.read_retry() {
/// break;
/// }
/// }
/// ```
pub fn read_retry(&self) -> bool {
self.lock.read_retry(self.start)
}
}
impl<T: ?Sized> Deref for SeqlockReadGuard<'_, T> {
type Target = T;
fn deref(&self) -> &T {
unsafe { &*self.lock.data.get() }
}
}
/// 顺序锁的RAII Guard
///
/// **RAII Guard** 用于提供 **Scoped Lock** 机制。
///
/// + [`SeqlockGuard`] 中包含 [`Seqlock`] 的引用, 当 [`SeqlockGuard`] 生命周期结束时,会在 [`drop()`] 方法中自动解锁顺序锁。
/// + [`SeqlockGuard`] 不可在线程之间转移所有权,因为其 [`drop()`] 方法包含解锁的语义,上锁和解锁必须在同一线程;
/// + [`SeqlockGuard`] 虽然可以在多线程中传递引用( [`Sync`] 约束),但其实现中没有 **unlock()** 方法,意味着其他线程即便拿到引用也不能解锁。
///
/// [`drop()`]: https://doc.rust-lang.org/std/mem/fn.drop.html
/// [`Sync`]: https://doc.rust-lang.org/std/marker/trait.Send.html
pub struct SeqlockGuard<'a, T: ?Sized + 'a> {
/// 顺序锁的引用
lock: &'a Seqlock<T>,
}
impl<T: ?Sized> !Send for SeqlockGuard<'_, T> {}
unsafe impl<T: ?Sized + Sync> Sync for SeqlockGuard<'_, T> {}
impl<'a, T: ?Sized> SeqlockGuard<'a, T> {
fn new(lock: &'a Seqlock<T>) -> SeqlockGuard<'a, T> {
SeqlockGuard { lock: lock }
}
fn lock(&self) {
unsafe {
let mode = &*self.lock.mode.get();
match mode {
SeqlockMode::WriteLock |
SeqlockMode::WriteLockBh |
SeqlockMode::WriteLockCpuirq |
SeqlockMode::WriteLockCpuirqSave(_) => {
xwrustffi_sqlk_wr_lock(self.lock.sqlk.get());
},
SeqlockMode::ReadExclusiveLock |
SeqlockMode::ReadExclusiveLockBh |
SeqlockMode::ReadExclusiveLockCpuirq |
SeqlockMode::ReadExclusiveLockCpuirqSave(_) => {
xwrustffi_sqlk_rdex_lock(self.lock.sqlk.get());
},
}
}
}
fn unlock(&self) {
unsafe {
let mode = &*self.lock.mode.get();
match mode {
SeqlockMode::WriteLock |
SeqlockMode::WriteLockBh |
SeqlockMode::WriteLockCpuirq |
SeqlockMode::WriteLockCpuirqSave(_) => {
xwrustffi_sqlk_wr_unlock(self.lock.sqlk.get());
},
SeqlockMode::ReadExclusiveLock |
SeqlockMode::ReadExclusiveLockBh |
SeqlockMode::ReadExclusiveLockCpuirq |
SeqlockMode::ReadExclusiveLockCpuirqSave(_) => {
xwrustffi_sqlk_rdex_unlock(self.lock.sqlk.get());
},
}
}
}
/// 阻塞当前线程,直到被条件量唤醒
///
/// 此方法会消费顺序锁的守卫(Guard),并当线程阻塞时,在条件量内部释放顺序锁。
/// 当条件成立,线程被唤醒,会在条件量内部上锁顺序锁,并重新返回顺序锁的守卫(Guard)。
///
/// + 当返回顺序锁的守卫 [`SeqlockGuard`] 时,顺序锁已经被重新上锁;
/// + 当返回 [`Err`] 时,顺序锁未被上锁。
///
/// # 参数说明
///
/// + cond: 条件量的引用
///
/// # 错误码
///
/// + [`CondError::NotInit`] 条件量未被初始化
/// + [`CondError::Interrupt`] 等待被中断
/// + [`CondError::NotThreadContext`] 不在线程上下文中
///
/// # 示例
///
/// ```rust
/// use xwrust::xwos::thd;
/// use xwrust::xwos::lock::seqlock::*;
/// use xwrust::xwos::sync::cond::*;
/// extern crate alloc;
/// use alloc::sync::Arc;
///
/// pub fn xwrust_example_seqlock() {
/// let pair = Arc::new((Seqlock::new(true), Cond::new()));
/// pair.1.init();
/// let pair_c = pair.clone();
///
/// thd::Builder::new()
/// .name("child".into())
/// .spawn(move |_| { // 子线程闭包
/// cthd::sleep(xwtm::ms(500));
/// let (lock, cvar) = &*pair_c;
/// let mut guard = lock_child.lock(SeqlockMode::WriteLockCpuirqSave(None));
/// *guard = false; // 设置共享数据
/// drop(guard); // 先解锁再触发条件可提高效率
/// cvar.broadcast();
/// });
/// let (lock, cvar) = &*pair;
/// {
/// let mut guard = lock.lock(SeqlockMode::WriteLockCpuirqSave(None));
/// while *guard {
/// match guard.wait(cvar) {
/// Ok(g) => { // 唤醒
/// guard = g;
/// },
/// Err(e) => { // 等待条件量失败
/// break;
/// },
/// }
/// }
/// }
/// }
/// ```
///
/// [`Err`]: <https://doc.rust-lang.org/core/result/enum.Result.html#variant.Err>
pub fn wait(self, cond: &Cond) -> Result<SeqlockGuard<'a, T>, CondError> {
unsafe {
let mut rc = xwrustffi_cond_acquire(cond.cond.get(), *cond.tik.get());
if rc == 0 {
let lktype = match &*self.lock.mode.get() {
SeqlockMode::WriteLock |
SeqlockMode::WriteLockBh |
SeqlockMode::WriteLockCpuirq |
SeqlockMode::WriteLockCpuirqSave(_) => {
XWOS_LK_SQLK_WR
},
SeqlockMode::ReadExclusiveLock |
SeqlockMode::ReadExclusiveLockBh |
SeqlockMode::ReadExclusiveLockCpuirq |
SeqlockMode::ReadExclusiveLockCpuirqSave(_) => {
XWOS_LK_SQLK_RDEX
},
};
let mut lkst = 0;
rc = xwrustffi_cond_wait(cond.cond.get(),
self.lock.sqlk.get() as _, lktype, ptr::null_mut(),
&mut lkst);
xwrustffi_cond_put(cond.cond.get());
if 0 == rc {
Ok(self)
} else {
if XWOS_LKST_LOCKED == lkst {
drop(self);
} else {
self.lock();
drop(self);
}
if -EINTR == rc {
Err(CondError::Interrupt(rc))
} else if -ENOTTHDCTX == rc {
Err(CondError::NotThreadContext(rc))
} else {
Err(CondError::Unknown(rc))
}
}
} else {
drop(self);
Err(CondError::NotInit(rc))
}
}
}
/// 限时阻塞当前线程,直到被条件量唤醒
///
/// 此方法会消费顺序锁的守卫(Guard),并当线程阻塞时,在条件量内部释放顺序锁。
/// 当条件成立,线程被唤醒,会在条件量内部上锁顺序锁,并重新返回顺序锁的守卫(Guard)。
/// 当超时后,将返回错误。
///
/// + 当返回顺序锁的守卫 [`SeqlockGuard`] 时,顺序锁已经被重新上锁;
/// + 当返回 [`Err`] 时,顺序锁未被上锁。
///
/// # 参数说明
///
/// + cond: 条件量的引用
/// + to: 期望唤醒的时间点
///
/// # 错误码
///
/// + [`CondError::NotInit`] 条件量未被初始化
/// + [`CondError::Interrupt`] 等待被中断
/// + [`CondError::Timedout`] 等待超时
/// + [`CondError::NotThreadContext`] 不在线程上下文中
///
/// # 示例
///
/// ```rust
/// use xwrust::xwos::thd;
/// use xwrust::xwos::lock::seqlock::*;
/// use xwrust::xwos::sync::cond::*;
/// extern crate alloc;
/// use alloc::sync::Arc;
///
/// pub fn xwrust_example_seqlock() {
/// let pair = Arc::new((Seqlock::new(true), Cond::new()));
/// pair.1.init();
/// let pair_c = pair.clone();
///
/// thd::Builder::new()
/// .name("child".into())
/// .spawn(move |_| { // 子线程闭包
/// cthd::sleep(xwtm::ms(500));
/// let (lock, cvar) = &*pair_c;
/// let mut guard = lock_child.lock(SeqlockMode::WriteLockCpuirqSave(None));
/// *guard = false; // 设置共享数据
/// drop(guard); // 先解锁再触发条件可提高效率
/// cvar.broadcast();
/// });
/// let (lock, cvar) = &*pair;
/// {
/// let mut guard = lock.lock(SeqlockMode::WriteLockCpuirqSave(None));
/// while *guard {
/// match guard.wait_to(cvar, xwtm::ft(xwtm::s(2))) {
/// Ok(g) => { // 唤醒
/// guard = g;
/// },
/// Err(e) => { // 等待条件量失败
/// break;
/// },
/// }
/// }
/// }
/// }
/// ```
///
/// [`Err`]: <https://doc.rust-lang.org/core/result/enum.Result.html#variant.Err>
pub fn wait_to(self, cond: &Cond, to: XwTm) -> Result<SeqlockGuard<'a, T>, CondError> {
unsafe {
let mut rc = xwrustffi_cond_acquire(cond.cond.get(), *cond.tik.get());
if rc == 0 {
let lktype = match &*self.lock.mode.get() {
SeqlockMode::WriteLock |
SeqlockMode::WriteLockBh |
SeqlockMode::WriteLockCpuirq |
SeqlockMode::WriteLockCpuirqSave(_) => {
XWOS_LK_SQLK_WR
},
SeqlockMode::ReadExclusiveLock |
SeqlockMode::ReadExclusiveLockBh |
SeqlockMode::ReadExclusiveLockCpuirq |
SeqlockMode::ReadExclusiveLockCpuirqSave(_) => {
XWOS_LK_SQLK_RDEX
},
};
let mut lkst = 0;
rc = xwrustffi_cond_wait_to(cond.cond.get(),
self.lock.sqlk.get() as _, lktype, ptr::null_mut(),
to, &mut lkst);
xwrustffi_cond_put(cond.cond.get());
if 0 == rc {
Ok(self)
} else {
if XWOS_LKST_LOCKED == lkst {
drop(self);
} else {
self.lock();
drop(self);
}
if -EINTR == rc {
Err(CondError::Interrupt(rc))
} else if -ETIMEDOUT == rc {
Err(CondError::Timedout(rc))
} else if -ENOTTHDCTX == rc {
Err(CondError::NotThreadContext(rc))
} else {
Err(CondError::Unknown(rc))
}
}
} else {
drop(self);
Err(CondError::NotInit(rc))
}
}
}
/// 阻塞当前线程,直到被条件量唤醒,且阻塞不可被中断
///
/// 此方法会消费顺序锁的守卫(Guard),并当线程阻塞时,在条件量内部释放顺序锁。
/// 当条件成立,线程被唤醒,会在条件量内部上锁顺序锁,并重新返回顺序锁的守卫(Guard)。
///
/// + 当返回顺序锁的守卫 [`SeqlockGuard`] 时,顺序锁已经被重新上锁;
/// + 当返回 [`Err`] 时,顺序锁未被上锁。
///
/// # 参数说明
///
/// + cond: 条件量的引用
///
/// # 错误码
///
/// + [`CondError::NotInit`] 条件量未被初始化
/// + [`CondError::NotThreadContext`] 不在线程上下文中
///
/// # 示例
///
/// ```rust
/// use xwrust::xwos::thd;
/// use xwrust::xwos::lock::seqlock::*;
/// use xwrust::xwos::sync::cond::*;
/// extern crate alloc;
/// use alloc::sync::Arc;
///
/// pub fn xwrust_example_seqlock() {
/// let pair = Arc::new((Seqlock::new(true), Cond::new()));
/// pair.1.init();
/// let pair_c = pair.clone();
///
/// thd::Builder::new()
/// .name("child".into())
/// .spawn(move |_| { // 子线程闭包
/// cthd::sleep(xwtm::ms(500));
/// let (lock, cvar) = &*pair_c;
/// let mut guard = lock_child.lock(SeqlockMode::ReadExclusiveLockCpuirqSave(None));
/// *guard = false; // 设置共享数据
/// drop(guard); // 先解锁再触发条件可提高效率
/// cvar.broadcast();
/// });
/// let (lock, cvar) = &*pair;
/// {
/// let mut guard = lock.lock(SeqlockMode::ReadExclusiveLockCpuirqSave(None));
/// while *guard {
/// match guard.wait_unintr(cvar) {
/// Ok(g) => { // 唤醒
/// guard = g;
/// },
/// Err(e) => { // 等待条件量失败
/// break;
/// },
/// }
/// }
/// }
/// }
/// ```
///
/// [`Err`]: <https://doc.rust-lang.org/core/result/enum.Result.html#variant.Err>
pub fn wait_unintr(self, cond: &Cond) -> Result<SeqlockGuard<'a, T>, CondError> {
unsafe {
let mut rc = xwrustffi_cond_acquire(cond.cond.get(), *cond.tik.get());
if rc == 0 {
let lktype = match &*self.lock.mode.get() {
SeqlockMode::WriteLock |
SeqlockMode::WriteLockBh |
SeqlockMode::WriteLockCpuirq |
SeqlockMode::WriteLockCpuirqSave(_) => {
XWOS_LK_SQLK_WR
},
SeqlockMode::ReadExclusiveLock |
SeqlockMode::ReadExclusiveLockBh |
SeqlockMode::ReadExclusiveLockCpuirq |
SeqlockMode::ReadExclusiveLockCpuirqSave(_) => {
XWOS_LK_SQLK_RDEX
},
};
let mut lkst = 0;
rc = xwrustffi_cond_wait_unintr(cond.cond.get(),
self.lock.sqlk.get() as _, lktype, ptr::null_mut(),
&mut lkst);
xwrustffi_cond_put(cond.cond.get());
if 0 == rc {
Ok(self)
} else {
if XWOS_LKST_LOCKED == lkst {
drop(self);
} else {
self.lock();
drop(self);
}
if -EINTR == rc {
Err(CondError::Interrupt(rc))
} else if -ENOTTHDCTX == rc {
Err(CondError::NotThreadContext(rc))
} else {
Err(CondError::Unknown(rc))
}
}
} else {
drop(self);
Err(CondError::NotInit(rc))
}
}
}
}
impl<T: ?Sized> Deref for SeqlockGuard<'_, T> {
type Target = T;
fn deref(&self) -> &T {
unsafe { &*self.lock.data.get() }
}
}
impl<T: ?Sized> DerefMut for SeqlockGuard<'_, T> {
fn deref_mut(&mut self) -> &mut T {
unsafe { &mut *self.lock.data.get() }
}
}
impl<T: ?Sized> Drop for SeqlockGuard<'_, T> {
fn drop(&mut self) {
unsafe {
match *self.lock.mode.get() {
SeqlockMode::ReadExclusiveLock => {
xwrustffi_sqlk_rdex_unlock(self.lock.sqlk.get());
},
SeqlockMode::ReadExclusiveLockCpuirq => {
xwrustffi_sqlk_rdex_unlock_cpuirq(self.lock.sqlk.get());
},
SeqlockMode::ReadExclusiveLockCpuirqSave(cpuirq) => {
xwrustffi_sqlk_rdex_unlock_cpuirqrs(self.lock.sqlk.get(), cpuirq.unwrap_unchecked());
},
SeqlockMode::ReadExclusiveLockBh => {
xwrustffi_sqlk_rdex_unlock_bh(self.lock.sqlk.get());
},
SeqlockMode::WriteLock => {
xwrustffi_sqlk_wr_unlock(self.lock.sqlk.get());
},
SeqlockMode::WriteLockCpuirq => {
xwrustffi_sqlk_wr_unlock_cpuirq(self.lock.sqlk.get());
},
SeqlockMode::WriteLockCpuirqSave(cpuirq) => {
xwrustffi_sqlk_wr_unlock_cpuirqrs(self.lock.sqlk.get(), cpuirq.unwrap_unchecked());
},
SeqlockMode::WriteLockBh => {
xwrustffi_sqlk_wr_unlock_bh(self.lock.sqlk.get());
},
}
}
}
}