2012-10-01 06:58:59 +04:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <winpr/crt.h>
|
|
|
|
#include <winpr/windows.h>
|
|
|
|
#include <winpr/interlocked.h>
|
|
|
|
|
|
|
|
typedef struct _PROGRAM_ITEM
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WINPR_SLIST_ENTRY ItemEntry;
|
2012-10-03 05:52:27 +04:00
|
|
|
ULONG Signature;
|
2012-10-01 06:58:59 +04:00
|
|
|
} PROGRAM_ITEM, *PPROGRAM_ITEM;
|
|
|
|
|
|
|
|
int TestInterlockedSList(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
ULONG Count;
|
2016-08-10 13:39:26 +03:00
|
|
|
WINPR_PSLIST_ENTRY pFirstEntry;
|
|
|
|
WINPR_PSLIST_HEADER pListHead;
|
2012-10-01 06:58:59 +04:00
|
|
|
|
|
|
|
/* Initialize the list header to a MEMORY_ALLOCATION_ALIGNMENT boundary. */
|
2019-11-06 17:24:51 +03:00
|
|
|
pListHead = (WINPR_PSLIST_HEADER)_aligned_malloc(sizeof(WINPR_SLIST_HEADER),
|
|
|
|
MEMORY_ALLOCATION_ALIGNMENT);
|
2012-10-01 06:58:59 +04:00
|
|
|
|
2012-10-02 05:58:24 +04:00
|
|
|
if (!pListHead)
|
2012-10-01 06:58:59 +04:00
|
|
|
{
|
|
|
|
printf("Memory allocation failed.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2012-10-02 05:58:24 +04:00
|
|
|
|
2012-10-01 06:58:59 +04:00
|
|
|
InitializeSListHead(pListHead);
|
|
|
|
|
|
|
|
/* Insert 10 items into the list. */
|
|
|
|
for (Count = 1; Count <= 10; Count += 1)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
PPROGRAM_ITEM pProgramItem =
|
|
|
|
(PPROGRAM_ITEM)_aligned_malloc(sizeof(PROGRAM_ITEM), MEMORY_ALLOCATION_ALIGNMENT);
|
2012-10-01 06:58:59 +04:00
|
|
|
|
|
|
|
if (!pProgramItem)
|
|
|
|
{
|
|
|
|
printf("Memory allocation failed.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
pProgramItem->Signature = Count;
|
2012-10-02 05:58:24 +04:00
|
|
|
pFirstEntry = InterlockedPushEntrySList(pListHead, &(pProgramItem->ItemEntry));
|
2012-10-01 06:58:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove 10 items from the list and display the signature. */
|
|
|
|
for (Count = 10; Count >= 1; Count -= 1)
|
|
|
|
{
|
2017-03-28 17:18:00 +03:00
|
|
|
PPROGRAM_ITEM pProgramItem;
|
|
|
|
WINPR_PSLIST_ENTRY pListEntry = InterlockedPopEntrySList(pListHead);
|
2012-10-01 06:58:59 +04:00
|
|
|
|
|
|
|
if (!pListEntry)
|
|
|
|
{
|
|
|
|
printf("List is empty.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2017-03-28 17:18:00 +03:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
pProgramItem = (PPROGRAM_ITEM)pListEntry;
|
|
|
|
printf("Signature is %" PRIu32 "\n", pProgramItem->Signature);
|
2012-10-01 06:58:59 +04:00
|
|
|
|
2017-03-28 17:18:00 +03:00
|
|
|
/*
|
|
|
|
* This example assumes that the SLIST_ENTRY structure is the
|
|
|
|
* first member of the structure. If your structure does not
|
|
|
|
* follow this convention, you must compute the starting address
|
2012-10-01 06:58:59 +04:00
|
|
|
* of the structure before calling the free function.
|
|
|
|
*/
|
|
|
|
|
|
|
|
_aligned_free(pListEntry);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Flush the list and verify that the items are gone. */
|
|
|
|
pFirstEntry = InterlockedPopEntrySList(pListHead);
|
|
|
|
|
|
|
|
if (pFirstEntry)
|
|
|
|
{
|
|
|
|
printf("Error: List is not empty.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
_aligned_free(pListHead);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|