-
Notifications
You must be signed in to change notification settings - Fork 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
Thread is always fully running. #497
Comments
What I like about webui is it's very lightweight, and uses very little CPU, so the issue you see is not normal! The loop does what it says in the comment Example from Civetweb: This is using a mutex check, but webui Example: Line 3062 in 3b0736f
|
Maybe it need to use a semaphore not a mutex for wait a job. threadsema.init(0); // semaphore must be initialized to zero
thread() {
sema.down(); // wait for up
mutex.lock();
... // do post-processing
mutex.release();
}
command() {
mutex.lock();
... // do something...
sema.up();
mutex.release();
} It will be a good reference for this. |
What is a |
If you're asking about semaphore behavior... init(n) {
sema_count = n;
}
up() {
// mutex guarding this function
sema_count++;
}
down() {
// mutex guarding this function
int backup_sema_count = sema_count;
if ((--sema_count) < 0) wait for until (sema_count >= backup_sema_count);
} |
I still don't know how semaphore can be better than mutex atomic variable webui is already using. mutex_atomic_initializer(); // Mutexs and Atomic variables must be initialized
thread() {
mutex.lock();
... // check atomic variable is true
mutex.release();
}
command() {
mutex.lock();
... // Set atomic variable to true
mutex.release();
} |
Mutex can prevents concurrent use of the same resource. (apply critical section.) |
I see, is semaphore still better in this case than mutex condition wait? |
In my experience, yes. |
thread() {
mutex.condition_wait();
}
command() {
mutex.fire_condition_wait();
} |
actually mutex is semaphore that count is initialized to one. static void _webui_mutex_lock(webui_mutex_t* mutex) {
#ifdef _WIN32
EnterCriticalSection(mutex);
#else
pthread_mutex_lock(mutex);
#endif
} It seems that mutex is designed that lock/free is performed in the same thread. |
In my personal opinion:
The problem with mutex condition is if it's used by more than one thread, then not guaranteed all thread will receive the condition fire event. In our case, since we use multiple threads, we should use mutex lock/unlock system (we already do). Now you suggest using mutex lock/unlock system semaphore, and think you are right 👍 |
I will see how to implement it, I never done this before. |
Fine, I will check out how to solve this issue too. |
Fixed (3084ff3). Thank you @testdrive-profiling-master for reporting this. Can you please retest to confirm? |
I've noticed that recent webui operations are taking up a lot of CPU resource on Windows.
Even a very simple
examples/C/minimal
project shows high CPU resource consumption.Maybe the mutex inside the thread is behaving weirdly, is it ok on other OS?
I found following code is fully running without any hangs.
webui/src/webui.c
Lines 8514 to 8526 in cf0c54e
This loop just keeps spinning without doing anything.
Is this normal behavior?
I haven't analyzed it in detail, but is it possible that
_webui_mutex_is_exit_now
&_webui_mutex_is_connected
functions aren't working properly? Or need some other mutex/semaphore for wait?The text was updated successfully, but these errors were encountered: