|
| 1 | +# os.notify - File Descriptor Event Notification |
| 2 | + |
| 3 | +The `os.notify` module provides cross-platform file descriptor event notification using the best available mechanism for each platform: |
| 4 | +- **Linux**: epoll |
| 5 | +- **macOS/BSD**: kqueue |
| 6 | +- **Other platforms**: Not yet supported |
| 7 | + |
| 8 | +## Usage |
| 9 | + |
| 10 | +```v |
| 11 | +import os |
| 12 | +import os.notify |
| 13 | +
|
| 14 | +mut notifier := notify.new()! |
| 15 | +defer { |
| 16 | + notifier.close() or {} |
| 17 | +} |
| 18 | +
|
| 19 | +// Add a file descriptor to watch for read events |
| 20 | +notifier.add(fd, .read)! |
| 21 | +
|
| 22 | +// Wait for events (with timeout) |
| 23 | +events := notifier.wait(100 * time.millisecond) |
| 24 | +for event in events { |
| 25 | + if event.kind.has(.read) { |
| 26 | + // Handle read event |
| 27 | + } |
| 28 | +} |
| 29 | +``` |
| 30 | + |
| 31 | +## Event Types |
| 32 | + |
| 33 | +- `.read` - Data is available to read |
| 34 | +- `.write` - File descriptor is ready for writing |
| 35 | +- `.peer_hangup` - Peer closed the connection (Linux only) |
| 36 | +- `.exception` - Exceptional condition on file descriptor |
| 37 | +- `.error` - Error occurred (Linux only) |
| 38 | +- `.hangup` - Hangup occurred (Linux only) |
| 39 | + |
| 40 | +## Configuration Flags |
| 41 | + |
| 42 | +- `.edge_trigger` - Edge-triggered notifications (note: behavior differs between Linux and macOS) |
| 43 | +- `.one_shot` - Event is disabled after first notification |
| 44 | +- `.wake_up` - System wake-up event (Linux only) |
| 45 | +- `.exclusive` - Exclusive wake-up (Linux only) |
| 46 | + |
| 47 | +## Platform Differences |
| 48 | + |
| 49 | +### macOS/kqueue vs Linux/epoll |
| 50 | + |
| 51 | +**Event Type Support:** |
| 52 | +- macOS kqueue does **not** support: `.peer_hangup`, `.error`, `.hangup` |
| 53 | +- Linux epoll supports all event types |
| 54 | + |
| 55 | +**Configuration Flags:** |
| 56 | +- macOS kqueue does **not** support: `.wake_up`, `.exclusive` |
| 57 | +- Linux epoll supports all configuration flags |
| 58 | + |
| 59 | +**Edge Trigger Behavior:** |
| 60 | +- Linux epoll (`EPOLLET`): Triggers once when state changes from unreadable to readable |
| 61 | +- macOS kqueue (`EV_CLEAR`): May trigger multiple times even if data is not completely read |
| 62 | + |
| 63 | +When using unsupported features on macOS, the methods will return an error instead of panicking, allowing for graceful degradation. |
| 64 | + |
| 65 | +## Error Handling |
| 66 | + |
| 67 | +All methods that can fail return a `Result` type. Handle errors appropriately: |
| 68 | + |
| 69 | +```v |
| 70 | +notifier.add(fd, .read) or { |
| 71 | + eprintln('Failed to add fd: ${err}') |
| 72 | + return |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +On macOS, attempting to use unsupported features will return an error: |
| 77 | + |
| 78 | +```v |
| 79 | +// This will fail on macOS with an error message |
| 80 | +notifier.add(fd, .hangup) or { |
| 81 | + eprintln('Error: ${err}') // "kqueue does not support 'hangup' event type" |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +## Thread Safety |
| 86 | + |
| 87 | +Both `EpollNotifier` (Linux) and `KqueueNotifier` (macOS) use fixed-size arrays for event storage, making them thread-safe for concurrent use. |
0 commit comments