Concurrent Operations
Relevant source files
The following files were used as context for generating this wiki page:
Concurrent operations in the tauri-plugin-serialplugin project refer to the ability of the system to handle multiple, simultaneous requests to a single serial port or across multiple ports. This includes simultaneous read/write actions, overlapping configuration changes, and asynchronous event listening.
The plugin architecture leverages Tauri's IPC mechanism, allowing the frontend to dispatch multiple asynchronous commands that are processed by the Rust backend. On desktop platforms, serial port access is managed via a thread-safe Arc<Mutex<HashMap<...>>> structure to ensure data integrity during concurrent access.
Sources: src/lib.rs:165-175, tests/concurrent-operations.test.ts:21-35
Architecture and State Management
The backend maintains a centralized state to manage serial port instances. For desktop platforms, this state is an Arc<Mutex<HashMap<String, SerialPort>>>, which allows safe concurrent access across different asynchronous Tauri commands.
The diagram shows how frontend requests pass through the Tauri bridge to a Mutex-protected state in Rust, ensuring that concurrent operations on the same port map are serialized at the lock level.
Sources: src/lib.rs:165-175, build.rs:5-45
Simultaneous Data Operations
The plugin supports overlapping read and write operations. The JavaScript API returns Promises for these operations, which can be awaited individually or processed using Promise.all().
Concurrent Reads and Writes
The system is tested to handle multiple concurrent read requests and write requests simultaneously. When multiple reads are initiated, the backend processes them according to the available data and timeouts defined in the port configuration.
| Operation Type | Behavior | Expected Result |
|---|---|---|
| Multiple Reads | Simultaneous calls to read() | Each call returns its respective data segment |
| Multiple Writes | Simultaneous calls to write() | Bytes are queued and written to the hardware |
| Mixed I/O | Interleaved read() and write() | Full duplex-like behavior simulated via async tasks |
Sources: tests/concurrent-operations.test.ts:24-65
Sequence of Concurrent I/O
The following sequence diagram illustrates mixed concurrent read/write operations:
The diagram highlights how the JavaScript environment can initiate multiple parallel requests which the plugin manages asynchronously.
Sources: tests/concurrent-operations.test.ts:46-65
Concurrent Configuration Changes
The plugin allows changing port settings (Baud Rate, Data Bits, Parity, etc.) while other operations are pending. These settings are applied via independent IPC calls.
Parameter Safety
Concurrent updates to settings like setBaudRate, setDataBits, and setParity are handled through the command handler. Since the backend protects the port instance with a Mutex, settings are updated safely without corrupting the port state.
// Example of concurrent settings application
await Promise.all([
serialPort.setBaudRate(115200),
serialPort.setDataBits('Seven'),
serialPort.setParity('Even')
]);
Sources: tests/concurrent-operations.test.ts:78-93
Control Signals and Buffer Management
Control signals (RTS, DTR, CTS, etc.) and buffer operations can also be performed concurrently with data transmission.
- Signal Operations:
setRequestToSendandsetDataTerminalReadycan be toggled while data is being read or written. - Buffer Clearing:
clearBuffercan be invoked to reset input or output buffers during active sessions.
Signal Handling Flow
This demonstrates that signal toggling can occur independently of the data transmission flow.
Sources: tests/concurrent-operations.test.ts:95-111, examples/serialport-test/src/components/SerialPortComponent.svelte:145-165
Error Handling in Concurrent Contexts
When operating concurrently, errors in one operation do not necessarily terminate others. The plugin provides granular error reporting for each specific IPC call.
- Port Not Found: If a port is disconnected during concurrent operations, all pending Promises will reject with a "Port not found" or "Device busy" error.
- Permission Denied: Concurrent attempts to open a port that is already in use or restricted will result in individual rejected Promises for those specific calls.
- Recovery: The system is designed to allow recovery; after a port-level error, the port can be re-opened, and operations can resume.
Sources: tests/error-handling.test.ts:40-85, tests/error-handling.test.ts:115-155
Conclusion
Concurrent operations within the tauri-plugin-serialplugin are facilitated by a combination of JavaScript asynchronous Promises and a thread-safe Rust backend. The architecture ensures that multiple data transfers, signal toggles, and configuration updates can be initiated simultaneously without compromising the stability of the serial communication or the plugin's internal state.