-
Notifications
You must be signed in to change notification settings - Fork 58
Reduce busy-waiting CPU load in waitEvents() #201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -229,15 +229,23 @@ public static String getLibraryVersion() { | |
| */ | ||
| public native int getEventsMask(long handle); | ||
|
|
||
| /** | ||
| * Convenience overload for {@link #waitEvents(long, int)}, NOT using | ||
| * the {@link SerialPort#setWaitEventsTimeoutMs(int)} feature. | ||
| */ | ||
| public int[][] waitEvents(long handle){ return waitEvents(handle, -1); } | ||
|
|
||
|
Comment on lines
+232
to
+237
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd rather remove this overload. First, it is unused in SerialPort class, which is generally what the library users use to interact with native code, not SerialNativeInterface ( Secondly it's unnecessary if we have an option to reset There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Agreed, this is more of a
|
||
| /** | ||
| * Wait events | ||
| * | ||
| * @param handle handle of opened port | ||
| * | ||
| * @param waitEventsTimeoutMs See {@link SerialPort#setWaitEventsTimeoutMs(int)}. | ||
| * | ||
| * @return Method returns two-dimensional array containing event types and their values | ||
| * (<b>events[i][0] - event type</b>, <b>events[i][1] - event value</b>). | ||
| */ | ||
| public native int[][] waitEvents(long handle); | ||
| public native int[][] waitEvents(long handle, int waitEventsTimeoutMs); | ||
|
|
||
| /** | ||
| * Change RTS line state | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,6 +41,7 @@ public class SerialPort { | |
| private final String portName; | ||
| private volatile boolean portOpened = false; | ||
| private boolean maskAssigned = false; | ||
| private int waitEventsTimeoutMs = -1; | ||
|
|
||
| //since 2.2.0 -> | ||
| private volatile Method methodErrorOccurred = null; | ||
|
|
@@ -920,6 +921,34 @@ public boolean setFlowControlMode(int mask) throws SerialPortException { | |
| return serialInterface.setFlowControlMode(portHandle, mask); | ||
| } | ||
|
|
||
| /** | ||
| * Reduce busy-waiting CPU load in {@link #waitEvents()}. | ||
| * | ||
| * (This is irrelevant for windows) | ||
| * | ||
| * The {@link #waitEvents()} implementation on non-windows systems | ||
| * usually returns immediately. This is unfortunate for the callers | ||
| * which want to await events in an infinite-loop. As doing so would | ||
| * burn lot of CPU time. | ||
| * | ||
| * This setting can be used to reduce that load. For regular | ||
| * incoming data events this does not cause any further delays. | ||
| * {@link #waitEvents()} still will reports most of the events as | ||
| * soon they become available, even before the specified timeout got | ||
| * reached. | ||
| * | ||
| * BUT BE AWARE: Enabling this might delay delivery of some special | ||
| * serial-events (like 'DCD line changed' or 'RI line changed') by | ||
| * the amount of time specified. So you have to decide yourself if | ||
| * you can/will afford this trade. | ||
| */ | ||
| public void setWaitEventsTimeoutMs(int waitEventsTimeoutMs) { | ||
| if (waitEventsTimeoutMs <= 0) { | ||
| throw new IllegalArgumentException(String.valueOf(waitEventsTimeoutMs)); | ||
| } | ||
| this.waitEventsTimeoutMs = waitEventsTimeoutMs; | ||
| } | ||
|
Comment on lines
+945
to
+950
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This bothers me. This method fails on |
||
|
|
||
| /** | ||
| * Get flow control mode | ||
| * | ||
|
|
@@ -951,7 +980,7 @@ public boolean sendBreak(int duration)throws SerialPortException { | |
| } | ||
|
|
||
| private int[][] waitEvents() { | ||
| return serialInterface.waitEvents(portHandle); | ||
| return serialInterface.waitEvents(portHandle, waitEventsTimeoutMs); | ||
| } | ||
|
|
||
| /** | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.