mirror of
https://github.com/aseprite/aseprite.git
synced 2025-01-30 15:32:38 +00:00
Fix problems with 100% CPU usage, thanks to Tobias Hansen (issue #47).
Based on the Gernot Frisch idea.
This commit is contained in:
parent
25884b0fb7
commit
2ae5fc613b
@ -15,6 +15,8 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <allegro.h>
|
#include <allegro.h>
|
||||||
|
|
||||||
|
#include "base/chrono.h"
|
||||||
|
#include "base/thread.h"
|
||||||
#include "gui/gui.h"
|
#include "gui/gui.h"
|
||||||
#include "gui/intern.h"
|
#include "gui/intern.h"
|
||||||
|
|
||||||
@ -255,13 +257,26 @@ void jmanager_free(JWidget widget)
|
|||||||
|
|
||||||
void jmanager_run(JWidget widget)
|
void jmanager_run(JWidget widget)
|
||||||
{
|
{
|
||||||
|
base::Chrono chrono;
|
||||||
|
|
||||||
while (!jlist_empty(widget->children)) {
|
while (!jlist_empty(widget->children)) {
|
||||||
|
chrono.reset();
|
||||||
|
|
||||||
if (jmanager_generate_messages(widget)) {
|
if (jmanager_generate_messages(widget)) {
|
||||||
jmanager_dispatch_messages(widget);
|
jmanager_dispatch_messages(widget);
|
||||||
}
|
}
|
||||||
else if (!garbage.empty()) {
|
else if (!garbage.empty()) {
|
||||||
collect_garbage();
|
collect_garbage();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If the dispatching of messages was faster than 10 milliseconds,
|
||||||
|
// it means that the process is not using a lot of CPU, so we can
|
||||||
|
// wait the difference to cover those 10 milliseconds
|
||||||
|
// sleeping. With this code we can avoid 100% CPU usage (a
|
||||||
|
// property of Allegro 4 polling nature).
|
||||||
|
double elapsedMSecs = chrono.elapsed() * 1000.0;
|
||||||
|
if (elapsedMSecs > 0.0 && elapsedMSecs < 10.0)
|
||||||
|
base::this_thread::sleep_for((10.0 - elapsedMSecs) / 1000.0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user