Sets the current thread status, but only if it was in the given expected state before. This is used to allow high-level control flow "override" the thread status before low-level (futex wait) operations set it.
| 30 | // state before. This is used to allow high-level control flow "override" the |
| 31 | // thread status before low-level (futex wait) operations set it. |
| 32 | static void set_status_conditional(int expectedStatus, int newStatus) { |
| 33 | if (!enabled) { |
| 34 | return; |
| 35 | } |
| 36 | pthread_t thread = pthread_self(); |
| 37 | if (!thread) return; // AudioWorklets do not have a pthread block, but if user calls emscripten_futex_wait() in an AudioWorklet, it will call here via emscripten_set_current_thread_status(). |
| 38 | int prevStatus = thread->profilerBlock->threadStatus; |
| 39 | |
| 40 | if (prevStatus != newStatus && (prevStatus == expectedStatus || expectedStatus == -1)) { |
| 41 | double now = emscripten_get_now(); |
| 42 | double startState = thread->profilerBlock->currentStatusStartTime; |
| 43 | double duration = now - startState; |
| 44 | |
| 45 | thread->profilerBlock->timeSpentInStatus[prevStatus] += duration; |
| 46 | thread->profilerBlock->threadStatus = newStatus; |
| 47 | thread->profilerBlock->currentStatusStartTime = now; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | void emscripten_conditional_set_current_thread_status(int expectedStatus, int newStatus) { |
| 52 | set_status_conditional(expectedStatus, newStatus); |
no test coverage detected