Created
January 6, 2020 02:06
-
-
Save thrimbor/7c4fe2207f5354e0966d3dcfb5df1a35 to your computer and use it in GitHub Desktop.
SRW locks test for nxdk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <hal/debug.h> | |
#include <hal/video.h> | |
#include <windows.h> | |
// expected behavior: | |
// threads 1, 3, 4, 5 lock, threads 3, 4, 5 unlock (in any order) | |
// 5 second pause | |
// thread 1 unlocks | |
// thread 2 locks | |
// 5 second pause | |
// thread 2 unlocks | |
// thread 2_2 locks | |
// thread 2_2 unlocks | |
SRWLOCK srwlock = SRWLOCK_INIT; | |
DWORD WINAPI threadFn1 (LPVOID lpParam) | |
{ | |
AcquireSRWLockShared(&srwlock); | |
debugPrint("thread 1 locked\n"); | |
Sleep(5000); | |
ReleaseSRWLockShared(&srwlock); | |
debugPrint("thread 1 unlocked\n"); | |
return 0; | |
} | |
DWORD WINAPI threadFn2_2 (LPVOID lpParam) | |
{ | |
AcquireSRWLockShared(&srwlock); | |
debugPrint("thread 2_2 locked\n"); | |
ReleaseSRWLockShared(&srwlock); | |
debugPrint("thread 2_2 unlocked\n"); | |
return 0; | |
} | |
DWORD WINAPI threadFn2 (LPVOID lpParam) | |
{ | |
AcquireSRWLockExclusive(&srwlock); | |
debugPrint("thread 2 locked\n"); | |
CreateThread(NULL, 0, threadFn2_2, NULL, 0, NULL); | |
Sleep(5000); | |
ReleaseSRWLockExclusive(&srwlock); | |
debugPrint("thread 2 unlocked\n"); | |
return 0; | |
} | |
DWORD WINAPI threadFn3 (LPVOID lpParam) | |
{ | |
AcquireSRWLockShared(&srwlock); | |
debugPrint("thread 3 locked\n"); | |
ReleaseSRWLockShared(&srwlock); | |
debugPrint("thread 3 unlocked\n"); | |
return 0; | |
} | |
DWORD WINAPI threadFn4 (LPVOID lpParam) | |
{ | |
AcquireSRWLockShared(&srwlock); | |
debugPrint("thread 4 locked\n"); | |
ReleaseSRWLockShared(&srwlock); | |
debugPrint("thread 4 unlocked\n"); | |
return 0; | |
} | |
DWORD WINAPI threadFn5 (LPVOID lpParam) | |
{ | |
AcquireSRWLockShared(&srwlock); | |
debugPrint("thread 5 locked\n"); | |
ReleaseSRWLockShared(&srwlock); | |
debugPrint("thread 5 unlocked\n"); | |
return 0; | |
} | |
int main(void) | |
{ | |
XVideoSetMode(640, 480, 32, REFRESH_DEFAULT); | |
CreateThread(NULL, 0, threadFn1, NULL, 0, NULL); | |
CreateThread(NULL, 0, threadFn2, NULL, 0, NULL); | |
CreateThread(NULL, 0, threadFn3, NULL, 0, NULL); | |
CreateThread(NULL, 0, threadFn4, NULL, 0, NULL); | |
CreateThread(NULL, 0, threadFn5, NULL, 0, NULL); | |
while(1) { | |
Sleep(2000); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment