diff --git a/README.md b/README.md index 20c4390..8e13aed 100644 --- a/README.md +++ b/README.md @@ -124,6 +124,45 @@ func Main() -> int { } ``` +### Channels (Producer/Consumer) +```bux +import Std::Io::{PrintLine, PrintInt}; +import Std::Task::{Task_Join, TaskHandle}; +import Std::Channel::{Channel, Channel_New, Channel_SendInt, Channel_RecvInt, Channel_Close}; + +func Producer(chPtr: *Channel) { + var i: int = 1; + while i <= 5 { + Channel_SendInt(chPtr, i * 10); + i = i + 1; + } + Channel_Close(chPtr); +} + +func Consumer(chPtr: *Channel) { + var total: int = 0; + while true { + let val: int = Channel_RecvInt(chPtr); + if val == 0 { break; } + total = total + val; + PrintInt(val); + PrintLine(""); + } + PrintLine("Total:"); + PrintInt(total); + PrintLine(""); +} + +func Main() -> int { + let ch: Channel = Channel_New(3); + let p: *void = spawn Producer(&ch); + let c: *void = spawn Consumer(&ch); + Task_Join(TaskHandle { handle: p }); + Task_Join(TaskHandle { handle: c }); + return 0; +} +``` + ### Hash Map ```bux import Std::Map::{Map, Map_New, Map_Set, Map_Get}; diff --git a/examples/channel.bux b/examples/channel.bux new file mode 100644 index 0000000..4723fa1 --- /dev/null +++ b/examples/channel.bux @@ -0,0 +1,37 @@ +import Std::Io::{PrintLine, PrintInt}; +import Std::Task::{Task_Join, TaskHandle}; +import Std::Channel::{Channel, Channel_New, Channel_SendInt, Channel_RecvInt, Channel_Close}; + +func Producer(chPtr: *Channel) { + var i: int = 1; + while i <= 5 { + Channel_SendInt(chPtr, i * 10); + i = i + 1; + } + Channel_Close(chPtr); +} + +func Consumer(chPtr: *Channel) { + var total: int = 0; + while true { + let val: int = Channel_RecvInt(chPtr); + if val == 0 { + break; + } + total = total + val; + PrintInt(val); + PrintLine(""); + } + PrintLine("Total:"); + PrintInt(total); + PrintLine(""); +} + +func Main() -> int { + let ch: Channel = Channel_New(3); + let p: *void = spawn Producer(&ch); + let c: *void = spawn Consumer(&ch); + Task_Join(TaskHandle { handle: p }); + Task_Join(TaskHandle { handle: c }); + return 0; +} diff --git a/library/std/Channel.bux b/library/std/Channel.bux index e179e79..727ad84 100644 --- a/library/std/Channel.bux +++ b/library/std/Channel.bux @@ -15,12 +15,12 @@ func Channel_New(capacity: int64) -> Channel { } func Channel_Send(ch: *Channel, value: T) { - bux_channel_send(ch.handle, &value); + bux_channel_send(ch.handle, (&value) as *void); } func Channel_Recv(ch: *Channel) -> T { var result: T; - bux_channel_recv(ch.handle, &result); + bux_channel_recv(ch.handle, (&result) as *void); return result; } @@ -32,4 +32,25 @@ func Channel_Free(ch: *Channel) { bux_channel_free(ch.handle); } +/* Non-generic wrappers for common types (avoid monomorphization issues) */ +func Channel_SendInt(ch: *Channel, value: int) { + bux_channel_send(ch.handle, (&value) as *void); +} + +func Channel_RecvInt(ch: *Channel) -> int { + var result: int = 0; + bux_channel_recv(ch.handle, (&result) as *void); + return result; +} + +func Channel_SendFloat64(ch: *Channel, value: float64) { + bux_channel_send(ch.handle, (&value) as *void); +} + +func Channel_RecvFloat64(ch: *Channel) -> float64 { + var result: float64 = 0.0; + bux_channel_recv(ch.handle, (&result) as *void); + return result; +} + }