Skip to content

Commit ced7be6

Browse files
authored
Allow to specify per-subsystem signal channel size (#29)
1 parent 4db6c12 commit ced7be6

File tree

3 files changed

+66
-19
lines changed

3 files changed

+66
-19
lines changed

orchestra/examples/duo.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl<Context> Fortified {
6363

6464
#[orchestra(signal=SigSigSig, event=EvX, error=Yikes, gen=AllMessages)]
6565
struct Duo<T, U, V, W> {
66-
#[subsystem(consumes: MsgStrukt, sends: [Plinko], message_capacity: 32768)]
66+
#[subsystem(consumes: MsgStrukt, sends: [Plinko], message_capacity: 32768, signal_capacity: 128)]
6767
sub0: Awesome,
6868

6969
#[subsystem(blocking, consumes: Plinko, sends: [MsgStrukt])]

orchestra/proc-macro/src/impl_builder.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ pub(crate) fn impl_builder(info: &OrchestraInfo) -> proc_macro2::TokenStream {
4242
let consumes = &info.consumes_without_wip();
4343
let channel_name = &info.channel_names_without_wip(None);
4444
let channel_name_unbounded = &info.channel_names_without_wip("_unbounded");
45-
let channel_capacity = &info.channel_capacities_without_wip(info.message_channel_capacity);
45+
let message_channel_capacity =
46+
&info.message_channel_capacities_without_wip(info.message_channel_capacity);
47+
let signal_channel_capacity =
48+
&info.signal_channel_capacities_without_wip(info.signal_channel_capacity);
4649

4750
let channel_name_tx = &info.channel_names_without_wip("_tx");
4851
let channel_name_unbounded_tx = &info.channel_names_without_wip("_unbounded_tx");
@@ -520,6 +523,8 @@ pub(crate) fn impl_builder(info: &OrchestraInfo) -> proc_macro2::TokenStream {
520523
#spawner_where_clause,
521524
{
522525
/// Set the interconnecting signal channel capacity.
526+
/// This will override both static overseer default, e.g. `overseer(signal_capacity=123,...)`,
527+
/// **and** subsystem specific capacities, e.g. `subsystem(signal_capacity: 123,...)`.
523528
pub fn signal_channel_capacity(mut self, capacity: usize) -> Self
524529
{
525530
self.signal_capacity = Some(capacity);
@@ -528,7 +533,7 @@ pub(crate) fn impl_builder(info: &OrchestraInfo) -> proc_macro2::TokenStream {
528533

529534
/// Set the interconnecting message channel capacities.
530535
/// This will override both static overseer default, e.g. `overseer(message_capacity=123,...)`,
531-
/// **and** subsystem specific capacities, e.g. `subsystem(message_capacity=123,...)`.
536+
/// **and** subsystem specific capacities, e.g. `subsystem(message_capacity: 123,...)`.
532537
pub fn message_channel_capacity(mut self, capacity: usize) -> Self
533538
{
534539
self.channel_capacity = Some(capacity);
@@ -581,7 +586,7 @@ pub(crate) fn impl_builder(info: &OrchestraInfo) -> proc_macro2::TokenStream {
581586
#support_crate ::metered::channel::<
582587
MessagePacket< #consumes >
583588
>(
584-
self.channel_capacity.unwrap_or(#channel_capacity)
589+
self.channel_capacity.unwrap_or(#message_channel_capacity)
585590
);
586591
)*
587592

@@ -624,7 +629,7 @@ pub(crate) fn impl_builder(info: &OrchestraInfo) -> proc_macro2::TokenStream {
624629
#support_crate ::select_message_channel_strategy
625630
);
626631
let (signal_tx, signal_rx) = #support_crate ::metered::channel(
627-
self.signal_capacity.unwrap_or(SIGNAL_CHANNEL_CAPACITY)
632+
self.signal_capacity.unwrap_or(#signal_channel_capacity)
628633
);
629634

630635
let ctx = #subsystem_ctx_name::< #consumes >::new(

orchestra/proc-macro/src/parse/parse_orchestra_struct.rs

Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use syn::{
2121
parse::{Parse, ParseStream},
2222
punctuated::Punctuated,
2323
spanned::Spanned,
24+
token,
2425
token::Bracket,
2526
AttrStyle, Error, Field, FieldsNamed, GenericParam, Ident, ItemStruct, LitInt, Path,
2627
PathSegment, Result, Token, Type, Visibility,
@@ -34,6 +35,7 @@ mod kw {
3435
syn::custom_keyword!(consumes);
3536
syn::custom_keyword!(sends);
3637
syn::custom_keyword!(message_capacity);
38+
syn::custom_keyword!(signal_capacity);
3739
}
3840

3941
#[derive(Clone, Debug)]
@@ -49,7 +51,9 @@ pub(crate) enum SubSysAttrItem {
4951
/// Message to be consumed by this subsystem.
5052
Consumes(Consumes),
5153
/// Custom message channels capacity for this subsystem
52-
MessageChannelCapacity(MessageCapacity),
54+
MessageChannelCapacity(ChannelCapacity<kw::message_capacity>),
55+
/// Custom signal channels capacity for this subsystem
56+
SignalChannelCapacity(ChannelCapacity<kw::signal_capacity>),
5357
}
5458

5559
impl Parse for SubSysAttrItem {
@@ -62,7 +66,9 @@ impl Parse for SubSysAttrItem {
6266
} else if lookahead.peek(kw::sends) {
6367
Self::Sends(input.parse::<Sends>()?)
6468
} else if lookahead.peek(kw::message_capacity) {
65-
Self::MessageChannelCapacity(input.parse::<MessageCapacity>()?)
69+
Self::MessageChannelCapacity(input.parse::<ChannelCapacity<kw::message_capacity>>()?)
70+
} else if lookahead.peek(kw::signal_capacity) {
71+
Self::SignalChannelCapacity(input.parse::<ChannelCapacity<kw::signal_capacity>>()?)
6672
} else {
6773
Self::Consumes(input.parse::<Consumes>()?)
6874
})
@@ -87,6 +93,9 @@ impl ToTokens for SubSysAttrItem {
8793
Self::MessageChannelCapacity(_) => {
8894
quote! {}
8995
},
96+
Self::SignalChannelCapacity(_) => {
97+
quote! {}
98+
},
9099
};
91100
tokens.extend(ts.into_iter());
92101
}
@@ -113,8 +122,10 @@ pub(crate) struct SubSysField {
113122
/// Avoids dispatching `Wrapper` type messages, but generates the variants.
114123
/// Does not require the subsystem to be instantiated with the builder pattern.
115124
pub(crate) wip: bool,
116-
/// Custom message capacity
125+
/// Custom message channel capacity
117126
pub(crate) message_capacity: Option<usize>,
127+
/// Custom signal channel capacity
128+
pub(crate) signal_capacity: Option<usize>,
118129
}
119130

120131
impl SubSysField {
@@ -300,18 +311,18 @@ impl Parse for Consumes {
300311
}
301312

302313
#[derive(Debug, Clone)]
303-
pub(crate) struct MessageCapacity {
314+
pub(crate) struct ChannelCapacity<T: Parse> {
304315
#[allow(dead_code)]
305-
tag: kw::message_capacity,
316+
tag: T,
306317
#[allow(dead_code)]
307-
colon_token: Token![:],
318+
colon_token: token::Colon,
308319
value: usize,
309320
}
310321

311-
impl Parse for MessageCapacity {
322+
impl<T: Parse> Parse for ChannelCapacity<T> {
312323
fn parse(input: syn::parse::ParseStream) -> Result<Self> {
313324
Ok(Self {
314-
tag: input.parse::<kw::message_capacity>()?,
325+
tag: input.parse::<T>()?,
315326
colon_token: input.parse()?,
316327
value: input.parse::<LitInt>()?.base10_parse::<usize>()?,
317328
})
@@ -331,8 +342,10 @@ pub(crate) struct SubSystemAttrItems {
331342
/// The message type being consumed by the subsystem.
332343
pub(crate) consumes: Option<Consumes>,
333344
pub(crate) sends: Option<Sends>,
334-
/// Custom channel capacity
335-
pub(crate) message_capacity: Option<MessageCapacity>,
345+
/// Custom message channel capacity
346+
pub(crate) message_capacity: Option<ChannelCapacity<kw::message_capacity>>,
347+
/// Custom signal channel capacity
348+
pub(crate) signal_capacity: Option<ChannelCapacity<kw::signal_capacity>>,
336349
}
337350

338351
impl Parse for SubSystemAttrItems {
@@ -373,8 +386,9 @@ impl Parse for SubSystemAttrItems {
373386
let blocking = extract_variant!(unique, Blocking; default = false);
374387
let wip = extract_variant!(unique, Wip; default = false);
375388
let message_capacity = extract_variant!(unique, MessageChannelCapacity take );
389+
let signal_capacity = extract_variant!(unique, SignalChannelCapacity take );
376390

377-
Ok(Self { blocking, wip, sends, consumes, message_capacity })
391+
Ok(Self { blocking, wip, sends, consumes, message_capacity, signal_capacity })
378392
}
379393
}
380394

@@ -507,7 +521,10 @@ impl OrchestraInfo {
507521
.collect::<Vec<_>>()
508522
}
509523

510-
pub(crate) fn channel_capacities_without_wip(&self, default_capacity: usize) -> Vec<LitInt> {
524+
pub(crate) fn message_channel_capacities_without_wip(
525+
&self,
526+
default_capacity: usize,
527+
) -> Vec<LitInt> {
511528
self.subsystems
512529
.iter()
513530
.filter(|ssf| !ssf.wip)
@@ -520,6 +537,22 @@ impl OrchestraInfo {
520537
.collect::<Vec<_>>()
521538
}
522539

540+
pub(crate) fn signal_channel_capacities_without_wip(
541+
&self,
542+
default_capacity: usize,
543+
) -> Vec<LitInt> {
544+
self.subsystems
545+
.iter()
546+
.filter(|ssf| !ssf.wip)
547+
.map(|ssf| {
548+
LitInt::new(
549+
&(ssf.signal_capacity.unwrap_or(default_capacity).to_string()),
550+
ssf.signal_capacity.span(),
551+
)
552+
})
553+
.collect::<Vec<_>>()
554+
}
555+
523556
pub(crate) fn consumes_without_wip(&self) -> Vec<Path> {
524557
self.subsystems
525558
.iter()
@@ -601,8 +634,15 @@ impl OrchestraGuts {
601634
}
602635
unique_subsystem_idents.insert(generic.clone());
603636

604-
let SubSystemAttrItems { wip, blocking, consumes, sends, message_capacity, .. } =
605-
subsystem_attrs;
637+
let SubSystemAttrItems {
638+
wip,
639+
blocking,
640+
consumes,
641+
sends,
642+
message_capacity,
643+
signal_capacity,
644+
..
645+
} = subsystem_attrs;
606646

607647
// messages to be sent
608648
let sends = if let Some(sends) = sends {
@@ -612,6 +652,7 @@ impl OrchestraGuts {
612652
};
613653
let consumes = consumes.map(|consumes| consumes.consumes);
614654
let message_capacity = message_capacity.map(|capacity| capacity.value);
655+
let signal_capacity = signal_capacity.map(|capacity| capacity.value);
615656

616657
subsystems.push(SubSysField {
617658
name: ident,
@@ -621,6 +662,7 @@ impl OrchestraGuts {
621662
wip,
622663
blocking,
623664
message_capacity,
665+
signal_capacity,
624666
});
625667
} else {
626668
// collect the "baggage"

0 commit comments

Comments
 (0)