- Win32: WaitOnAddress vs. WaitForSingleObject - 13 Updates
| "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>: Jan 14 04:15PM -0800 On 1/13/2021 10:51 PM, Chris M. Thomasson wrote: > thing, vs one that uses a normal kernel object for the slow path. > You can use a benaphore or any other mutex you want vs futex as a test > vector. Can you run this Bonita? Fwiw, it would be my first time using a Windows Futex! lol: ______________________________________ #include <iostream> #include <thread> #include <functional> #define WIN32_LEAN_AND_MEAN #include <Windows.h> #define CT_L2_ALIGNMENT 128 #define CT_THREADS 32 #define CT_ITERS 6666666 struct ct_futex_mutex { ULONG alignas(CT_L2_ALIGNMENT) m_state; ct_futex_mutex() : m_state(0) { } void lock() { if (InterlockedExchange(&m_state, 1)) { while (InterlockedExchange(&m_state, 2)) { ULONG cmp = 2; WaitOnAddress(&m_state, &cmp, sizeof(ULONG), INFINITE); } } } void unlock() { if (InterlockedExchange(&m_state, 0) == 2) { WakeByAddressSingle(&m_state); } } }; struct ct_shared { ct_futex_mutex m_mtx; unsigned long m_count; ct_shared() : m_count(0) {} ~ct_shared() { if (m_count != 0) { std::cout << "counter is totally fubar!\n"; } } }; void ct_thread(ct_shared& shared) { for (unsigned long i = 0; i < CT_ITERS; ++i) { shared.m_mtx.lock(); ++shared.m_count; shared.m_mtx.unlock(); shared.m_mtx.lock(); --shared.m_count; shared.m_mtx.unlock(); } } int main() { std::thread threads[CT_THREADS]; std::cout << "Starting up...\n"; { ct_shared shared; for (unsigned long i = 0; i < CT_THREADS; ++i) { threads[i] = std::thread(ct_thread, std::ref(shared)); } std::cout << "Running...\n"; for (unsigned long i = 0; i < CT_THREADS; ++i) { threads[i].join(); } } std::cout << "Completed!\n"; return 0; } ______________________________________ Afaict, it should work. I have not tested this in Relacy yet. |
| Bonita Montero <Bonita.Montero@gmail.com>: Jan 15 08:56AM +0100 > Afaict, it should work. I have not tested this in Relacy yet. It works. And it's not necessary to test such simple code with Relacy or whatever. I yesterday wrote a portable semaphore-class: // fsemaphore.h #pragma once #if defined(_MSC_VER) #include <Windows.h> #elif defined(__gnu_linux__) #include <unistd.h> #include <linux/futex.h> #include <stdint.h> #include <sys/time.h> #include <sys/syscall.h>
Subscribe to:
Post Comments (Atom)
|
No comments:
Post a Comment