Guest User

Untitled

a guest
Jul 10th, 2026
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. structures:
  2. ```c
  3. /* per-domain SMR state */
  4. struct smr {
  5. seq_t rd_seq;
  6. seq_t wr_seq;
  7. };
  8.  
  9. struct smr_cpu {
  10. seq_t oldest_seq; /* oldest observed sequence */
  11. size_t nest; /* nesting depth */
  12. lock_t stall_lock;
  13. list_t stalled; /* sorted list of trackers by seq */
  14. seq_t stall_min; /* quick cache of stalled head sequence */
  15. };
  16.  
  17. /* trackers are on the thread stacks (or ig could have a limit of like 3 per thread), they follow a thread's smr state */
  18. struct smr_tracker {
  19. seq_t seq; /* tracker sequence */
  20. thread_t *td;
  21. struct smr_cpu *cpu; /* stall queue we're on */
  22. struct list_entry link;
  23. };
  24. ```
  25.  
  26. then read side:
  27.  
  28. ```c
  29. smr_enter(smr, t):
  30. preempt_disable();
  31. cpu = percpu(smr);
  32. if (cpu->nest++ == 0) {
  33. // first section
  34. s = atomic_load(&smr->wr_seq);
  35. atomic_store(cpu->oldest_seq, s);
  36. } else s = cpu->oldest_seq;
  37.  
  38. t->seq = s;
  39. t->cpu = NULL;
  40. t->td = curthread;
  41. list_push(&curthread->smr_sections, t);
  42. preempt_enable();
  43. ```
  44.  
  45. ```c
  46. smr_exit(smr, t):
  47. preempt_disable();
  48. list_remove(&curthread->smr_sections, t);
  49. cpu = percpu(smr);
  50. if (t->cpu == NULL) {
  51. // never got preempted
  52. if (--cpu->nest == 0)
  53. atomic_store(&cpu->oldest_seq, INVALID);
  54. } else {
  55. // we did get preempted
  56. lock(&t->cpu->stall_lock);
  57. list_remove(&t->cpu->stalled, t);
  58. t->cpu->stall_min = get_new_min(t->cpu);
  59. unlock(&t->cpu->stall_lock);
  60. }
  61. preempt_enable();
  62. ```
  63.  
  64. then on preemption:
  65. ```c
  66. if (!list_empty(td->smr_sections)) {
  67. for each tracker t on td->smr_sections with t->cpu == NULL:
  68. lock(&cpu->stall_lock);
  69. sorted_insert(&cpu->stalled, t);
  70. cpu->stall_min = head of stalled
  71. unlock(&cpu->stall_lock);
  72. t->cpu = cpu;
  73. cpu->nest = 0;
  74. atomic_store(cpu->oldest_seq, INVALID);
  75. }
  76. ```
  77.  
  78.  
  79.  
Advertisement
Add Comment
Please, Sign In to add comment