Commit 9c80fb07 authored by Robert Schmidt's avatar Robert Schmidt

Merge remote-tracking branch 'origin/isolate-lfds' into integration_2022_wk10

parents 5907117c e477b28c
This diff is collapsed.
building liblfds
================
Windows (user-mode)
===================
1. Use Microsoft Visual Studio 2008 or Visual C++ 2008 Express Edition (or
later versions) to load "liblfds.sln". The "Win32" platform is x86,
the "x64" platform is x64.
2. Use Microsoft Windows SDK and GNUmake to run makefile.windows (obviously
you'll need to have run setenv.bat or the appropriate vcvars*.bat first;
you can build for x64/64-bit and x86/32-bit - just run the correct batch
file).
Targets are "librel", "libdbg", "dllrel", "dlldbg" and "clean". You need
to clean between switching targets.
Windows (kernel)
================
Use the Windows Driver Kit "build" command. Prior to running "build",
if you wish to build a static library, run the batch file
"runme_before_win_kernel_static_lib_build.bat"; if you wish to
build a dynamic library, instead run "runme_before_win_kernel_dynamic_lib_build.bat".
The Windows kernel build system is rather limited and rather than
really rather messing up the directory/file structure just for the
Windows kernel platform, I've instead arranged it that these batch
files do the necessary work so that "build" will work.
The batch files are idempotent; you can run them as often as you
like, in any order, at any time (before or after builds), and they'll
do the right thing. You need to clean between switching targets.
Linux
=====
Use GNUmake to run "makefile.linux". Targets are "arrel", "ardbg",
"sorel", "sodbg" and "clean". You need to clean between switching
targets.
EXPORTS
lfds611_liblfds_abstraction_test_helper_increment_non_atomic = lfds611_liblfds_abstraction_test_helper_increment_non_atomic @1
lfds611_liblfds_abstraction_test_helper_increment_atomic = lfds611_liblfds_abstraction_test_helper_increment_atomic @2
lfds611_liblfds_abstraction_test_helper_cas = lfds611_liblfds_abstraction_test_helper_cas @3
lfds611_liblfds_abstraction_test_helper_dcas = lfds611_liblfds_abstraction_test_helper_dcas @4
lfds611_freelist_delete = lfds611_freelist_delete @5
lfds611_freelist_get_user_data_from_element = lfds611_freelist_get_user_data_from_element @6
lfds611_freelist_guaranteed_pop = lfds611_freelist_guaranteed_pop @7
lfds611_freelist_new = lfds611_freelist_new @8
lfds611_freelist_new_elements = lfds611_freelist_new_elements @9
lfds611_freelist_pop = lfds611_freelist_pop @10
lfds611_freelist_push = lfds611_freelist_push @11
lfds611_freelist_query = lfds611_freelist_query @12
lfds611_freelist_set_user_data_in_element = lfds611_freelist_set_user_data_in_element @13
lfds611_freelist_use = lfds611_freelist_use @14
lfds611_queue_delete = lfds611_queue_delete @15
lfds611_queue_dequeue = lfds611_queue_dequeue @16
lfds611_queue_enqueue = lfds611_queue_enqueue @17
lfds611_queue_guaranteed_enqueue = lfds611_queue_guaranteed_enqueue @18
lfds611_queue_new = lfds611_queue_new @19
lfds611_queue_query = lfds611_queue_query @20
lfds611_queue_use = lfds611_queue_use @21
lfds611_ringbuffer_delete = lfds611_ringbuffer_delete @22
lfds611_ringbuffer_get_read_element = lfds611_ringbuffer_get_read_element @23
lfds611_ringbuffer_get_write_element = lfds611_ringbuffer_get_write_element @24
lfds611_ringbuffer_new = lfds611_ringbuffer_new @25
lfds611_ringbuffer_put_read_element = lfds611_ringbuffer_put_read_element @26
lfds611_ringbuffer_put_write_element = lfds611_ringbuffer_put_write_element @27
lfds611_ringbuffer_query = lfds611_ringbuffer_query @28
lfds611_ringbuffer_use = lfds611_ringbuffer_use @29
lfds611_slist_delete = lfds611_slist_delete @30
lfds611_slist_get_head = lfds611_slist_get_head @31
lfds611_slist_get_head_and_then_next = lfds611_slist_get_head_and_then_next @32
lfds611_slist_get_next = lfds611_slist_get_next @33
lfds611_slist_get_user_data_from_element = lfds611_slist_get_user_data_from_element @34
lfds611_slist_logically_delete_element = lfds611_slist_logically_delete_element @35
lfds611_slist_new = lfds611_slist_new @36
lfds611_slist_new_head = lfds611_slist_new_head @37
lfds611_slist_new_next = lfds611_slist_new_next @38
lfds611_slist_set_user_data_in_element = lfds611_slist_set_user_data_in_element @39
lfds611_slist_single_threaded_physically_delete_all_elements = lfds611_slist_single_threaded_physically_delete_all_elements @40
lfds611_slist_use = lfds611_slist_use @41
lfds611_stack_clear = lfds611_stack_clear @42
lfds611_stack_delete = lfds611_stack_delete @43
lfds611_stack_guaranteed_push = lfds611_stack_guaranteed_push @44
lfds611_stack_new = lfds611_stack_new @45
lfds611_stack_pop = lfds611_stack_pop @46
lfds611_stack_push = lfds611_stack_push @47
lfds611_stack_query = lfds611_stack_query @48
lfds611_stack_use = lfds611_stack_use @49

Microsoft Visual Studio Solution File, Format Version 10.00
# Visual Studio 2008
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "liblfds611", "liblfds611.vcproj", "{F73AE755-F6D8-4C3A-977D-FBB40DC0ED05}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug DLL|Win32 = Debug DLL|Win32
Debug DLL|x64 = Debug DLL|x64
Debug Lib|Win32 = Debug Lib|Win32
Debug Lib|x64 = Debug Lib|x64
Release DLL|Win32 = Release DLL|Win32
Release DLL|x64 = Release DLL|x64
Release Lib|Win32 = Release Lib|Win32
Release Lib|x64 = Release Lib|x64
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{F73AE755-F6D8-4C3A-977D-FBB40DC0ED05}.Debug DLL|Win32.ActiveCfg = Debug DLL|Win32
{F73AE755-F6D8-4C3A-977D-FBB40DC0ED05}.Debug DLL|Win32.Build.0 = Debug DLL|Win32
{F73AE755-F6D8-4C3A-977D-FBB40DC0ED05}.Debug DLL|x64.ActiveCfg = Debug DLL|x64
{F73AE755-F6D8-4C3A-977D-FBB40DC0ED05}.Debug DLL|x64.Build.0 = Debug DLL|x64
{F73AE755-F6D8-4C3A-977D-FBB40DC0ED05}.Debug Lib|Win32.ActiveCfg = Debug Lib|Win32
{F73AE755-F6D8-4C3A-977D-FBB40DC0ED05}.Debug Lib|Win32.Build.0 = Debug Lib|Win32
{F73AE755-F6D8-4C3A-977D-FBB40DC0ED05}.Debug Lib|x64.ActiveCfg = Debug Lib|x64
{F73AE755-F6D8-4C3A-977D-FBB40DC0ED05}.Debug Lib|x64.Build.0 = Debug Lib|x64
{F73AE755-F6D8-4C3A-977D-FBB40DC0ED05}.Release DLL|Win32.ActiveCfg = Release DLL|Win32
{F73AE755-F6D8-4C3A-977D-FBB40DC0ED05}.Release DLL|Win32.Build.0 = Release DLL|Win32
{F73AE755-F6D8-4C3A-977D-FBB40DC0ED05}.Release DLL|x64.ActiveCfg = Release DLL|x64
{F73AE755-F6D8-4C3A-977D-FBB40DC0ED05}.Release DLL|x64.Build.0 = Release DLL|x64
{F73AE755-F6D8-4C3A-977D-FBB40DC0ED05}.Release Lib|Win32.ActiveCfg = Release Lib|Win32
{F73AE755-F6D8-4C3A-977D-FBB40DC0ED05}.Release Lib|Win32.Build.0 = Release Lib|Win32
{F73AE755-F6D8-4C3A-977D-FBB40DC0ED05}.Release Lib|x64.ActiveCfg = Release Lib|x64
{F73AE755-F6D8-4C3A-977D-FBB40DC0ED05}.Release Lib|x64.Build.0 = Release Lib|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
##### paths #####
OUTDIR := .
BINDIR = $(OUTDIR)/bin
OBJDIR = $(OUTDIR)/obj
INCDIR = inc
SRCDIR = src
##### misc #####
QUIETLY = 1>/dev/null 2>/dev/null
##### sources, objects and libraries #####
BINNAME = liblfds611
AR_BINARY = $(BINDIR)/$(BINNAME).a
$(info $(AR_BINARY))
SO_BINARY = $(BINDIR)/$(BINNAME).so
SRCDIRS = lfds611_abstraction lfds611_freelist lfds611_liblfds lfds611_queue lfds611_ringbuffer lfds611_slist lfds611_stack
# TRD : be aware - in the linux makefile, with the one-pass linking behaviour of the GNU linker, the order
# of source files matters! this is because it leads to the ordering of objects in the library and
# that in turn, since the data structures all use the freelist API and the abstraction API, has to be
# correct
# TRD : lfds611_abstraction_cas.c lfds611_abstraction_dcas.c lfds611_abstraction_increment.c are inlined and are compiled by every C file
SOURCES = lfds611_queue_delete.c lfds611_queue_new.c lfds611_queue_query.c lfds611_queue_queue.c \
lfds611_ringbuffer_delete.c lfds611_ringbuffer_get_and_put.c lfds611_ringbuffer_new.c lfds611_ringbuffer_query.c \
lfds611_slist_delete.c lfds611_slist_get_and_set.c lfds611_slist_link.c lfds611_slist_new.c \
lfds611_stack_delete.c lfds611_stack_new.c lfds611_stack_push_pop.c lfds611_stack_query.c \
lfds611_freelist_delete.c lfds611_freelist_get_and_set.c lfds611_freelist_new.c lfds611_freelist_query.c lfds611_freelist_pop_push.c \
lfds611_liblfds_abstraction_test_helpers.c lfds611_liblfds_aligned_free.c lfds611_liblfds_aligned_malloc.c \
lfds611_abstraction_free.c lfds611_abstraction_malloc.c
OBJECTS = $(patsubst %.c,$(OBJDIR)/%.o,$(notdir $(SOURCES)))
##### CPU variants #####
UNAME = $(shell uname -m)
GCCARCH = -march=$(UNAME)
ifeq ($(UNAME),x86_64)
GCCARCH = -march=core2
endif
ifeq ($(findstring arm,$(UNAME)),arm)
GCCARCH = -march=armv6k -marm
endif
##### tools #####
MAKE = make
MFLAGS =
DG = gcc
DGFLAGS = -MM -std=c99 -I"$(SRCDIR)" -I"$(INCDIR)"
CC = gcc
CBASE = -Wall -Wno-unknown-pragmas -std=c99 $(GCCARCH) -pthread -c -I"$(SRCDIR)" -I"$(INCDIR)"
CFREL = -O2 -finline-functions -Wno-strict-aliasing
CFDBG = -O0 -g
AR = ar
AFLAGS = -rcs
LD = gcc
LFBASE = -Wall -std=c99 -shared
LFREL = -O2 -s
LFDBG = -O0 -g
##### rel/dbg .a/.so variants #####
ifeq ($(findstring so,$(MAKECMDGOALS)),so)
CBASE := $(CBASE) -fpic
endif
CFLAGS = $(CBASE) $(CFDBG)
LFLAGS = $(LFBASE) $(LFDBG)
ifeq ($(findstring rel,$(MAKECMDGOALS)),rel)
CFLAGS = $(CBASE) $(CFREL)
LFLAGS = $(LFBASE) $(LFREL)
endif
##### search paths #####
vpath %.c $(patsubst %,$(SRCDIR)/%:,$(SRCDIRS))
##### implicit rules #####
$(OBJDIR)/%.o : %.c
@echo "Compiling $<"
@$(DG) $(DGFLAGS) $< >$(OBJDIR)/$*.d
@$(CC) $(CFLAGS) -o $@ $<
##### explicit rules #####
$(AR_BINARY) : $(OBJECTS)
@echo "Creating $(AR_BINARY)"
@$(AR) $(AFLAGS) $(AR_BINARY) $(OBJECTS)
$(SO_BINARY) : $(OBJECTS)
@echo "Creating $(SO_BINARY)"
$(LD) $(LFLAGS) $(SYSLIBS) $(OBJECTS) -o $(SO_BINARY)
##### phony #####
.PHONY : clean arrel ardbg sorel sodbg
clean :
@rm -f $(BINDIR)/$(BINNAME).* $(OBJDIR)/*.o $(OBJDIR)/*.d
arrel : $(AR_BINARY)
ardbg : $(AR_BINARY)
sorel : $(SO_BINARY)
sodbg : $(SO_BINARY)
##### dependencies #####
-include $(DEPENDS)
##### notes #####
# TRD : we use -std=c99 purely to permit C++ style comments
##### paths #####
BINDIR = bin
INCDIR = inc
OBJDIR = obj
SRCDIR = src
##### misc #####
QUIETLY = 1>nul 2>nul
##### sources, objects and libraries #####
BINNAME = liblfds611
LIB_BINARY = $(BINDIR)\$(BINNAME).lib
DLL_BINARY = $(BINDIR)\$(BINNAME).dll
SRCDIRS = lfds611_abstraction lfds611_freelist lfds611_liblfds lfds611_queue lfds611_ringbuffer lfds611_slist lfds611_stack
# TRD : lfds611_abstraction_cas.c lfds611_abstraction_dcas.c lfds611_abstraction_increment.c are inlined and are compiled by every C file
SOURCES = lfds611_abstraction_free.c lfds611_abstraction_malloc.c \
lfds611_freelist_delete.c lfds611_freelist_get_and_set.c lfds611_freelist_new.c lfds611_freelist_query.c lfds611_freelist_pop_push.c \
lfds611_liblfds_abstraction_test_helpers.c lfds611_liblfds_aligned_free.c lfds611_liblfds_aligned_malloc.c \
lfds611_queue_delete.c lfds611_queue_new.c lfds611_queue_query.c lfds611_queue_queue.c \
lfds611_ringbuffer_delete.c lfds611_ringbuffer_get_and_put.c lfds611_ringbuffer_new.c lfds611_ringbuffer_query.c \
lfds611_slist_delete.c lfds611_slist_get_and_set.c lfds611_slist_link.c lfds611_slist_new.c \
lfds611_stack_delete.c lfds611_stack_new.c lfds611_stack_push_pop.c lfds611_stack_query.c
OBJECTS = $(patsubst %.c,$(OBJDIR)/%.obj,$(notdir $(SOURCES)))
SYSLIBS = kernel32.lib
##### tools #####
MAKE = make
MFLAGS =
CC = cl
CBASE = /nologo /W4 /WX /c "-I$(SRCDIR)" "-I$(INCDIR)" "/Fd$(BINDIR)\$(BINNAME).pdb" /DUNICODE /D_UNICODE /DWIN32_LEAN_AND_MEAN
CFREL = /Ox /DNDEBUG
CFDBG = /Od /Gm /Zi /D_DEBUG
AR = lib
AFLAGS = /nologo /subsystem:console /wx /verbose
LD = link
LFBASE = /dll /def:$(BINNAME).def /nologo /subsystem:console /wx /nodefaultlib /nxcompat
LFREL = /incremental:no
LFDBG = /debug "/pdb:$(BINDIR)\$(BINNAME).pdb"
##### variants #####
CFLAGS = $(CBASE) $(CFDBG) /MTd
LFLAGS = $(LFBASE) $(LFDBG)
CLIB = libcmtd.lib
ifeq ($(MAKECMDGOALS),librel)
CFLAGS = $(CBASE) $(CFREL) /MT
LFLAGS = $(LFBASE) $(LFREL)
CLIB = libcmt.lib
endif
ifeq ($(MAKECMDGOALS),libdbg)
CFLAGS = $(CBASE) $(CFDBG) /MTd
LFLAGS = $(LFBASE) $(LFDBG)
CLIB = libcmtd.lib
endif
ifeq ($(MAKECMDGOALS),dllrel)
CFLAGS = $(CBASE) $(CFREL) /MD
LFLAGS = $(LFBASE) $(LFREL)
CLIB = msvcrt.lib
endif
ifeq ($(MAKECMDGOALS),dlldbg)
CFLAGS = $(CBASE) $(CFDBG) /MDd
LFLAGS = $(LFBASE) $(LFDBG)
CLIB = msvcrtd.lib
endif
##### search paths #####
vpath %.c $(patsubst %,$(SRCDIR)/%;,$(SRCDIRS))
##### implicit rules #####
$(OBJDIR)/%.obj : %.c
$(CC) $(CFLAGS) "/Fo$@" $<
##### explicit rules #####
$(LIB_BINARY) : $(OBJECTS)
$(AR) $(AFLAGS) $(OBJECTS) /out:$(LIB_BINARY)
$(DLL_BINARY) : $(OBJECTS)
$(LD) $(LFLAGS) $(CLIB) $(SYSLIBS) $(OBJECTS) /out:$(DLL_BINARY)
##### phony #####
.PHONY : clean librel libdbg dllrel dlldbg
clean :
@erase /Q $(BINDIR)\$(BINNAME).* $(OBJDIR)\*.obj $(QUIETLY)
librel : $(LIB_BINARY)
libdbg : $(LIB_BINARY)
dllrel : $(DLL_BINARY)
dlldbg : $(DLL_BINARY)
introduction
============
Welcome to liblfds, a portable, license-free, lock-free data structure library
written in C.
supported platforms
===================
Out-of-the-box ports are provided for;
Operating System CPU Toolchain Choices
================ ============= =================
Windows 64-bit x64 1. Microsoft Visual Studio
2. Microsoft Windows SDK and GNUmake
Windows 32-bit x64, x86 1. Microsoft Visual Studio
2. Visual C++ Express Edition
3. Microsoft Windows SDK and GNUmake
Windows Kernel x64, x86 1. Windows Driver Kit
Linux 64-bit x64 1. GCC and GNUmake
Linux 32-bit ARM, x64, x86 1. GCC and GNUmake
For more information including version requirements, see the building guide (lfds).
data structures
===============
This release of liblfds provides the following;
* Freelist
* Queue
* Ringbuffer (each element read by a single reader)
* Singly-linked list (logical delete only)
* Stack
These are all many-readers, many-writers.
liblfds on-line
===============
On the liblfds home page, you will find the blog, a bugzilla, a forum, a
mediawiki and the current and all historical releases.
The mediawiki contains comprehensive documentation for development, building,
testing and porting.
http://www.liblfds.org
license
=======
There is no license. You are free to use this code in any way.
using
=====
Once built, there is a single header file, /inc/liblfds.h, which you must include
in your source code, and a single library file /bin/liblfds.*, where the suffix
depends on your platform and your build choice (static or dynamic), to which,
if statically built, you must link directly or, if dynamically built, you must
arrange your system such that the library can be found by the loader at run-time.
testing
=======
The library comes with a command line test and benchmark program. This program
requires threads. As such, it is only suitable for platforms which can execute
a command line binary and provide thread support. Currently this means the test
and benchmark program works for all platforms except the Windows Kernel.
For documentation, see the testing and benchmarking guide in the mediawiki.
porting
=======
Both the test program and liblfds provide an abstraction layer which acts to
mask platform differences. Porting is the act of implementing on your platform
the functions which make up the abstraction layers. You do not need to port
the test program to port liblfds, but obviously it is recommended, so you can
test your port.
To support liblfds, your platform MUST support;
* atomic single-word* increment
* atomic single-word compare-and-swap
* atomic contiguous double-word compare-and-swap*
* malloc and free
* compiler directive for alignment of variables declared on the stack
* compiler directives for compiler barriers and processor barriers
* A ''word'' here means a type equal in length to the platform pointer size.
* This requirement excludes the Alpha, IA64, MIPS, PowerPC and SPARC platforms.
Also, your platform MAY support;
* compiler keyword for function inlining
To support the test programme, your platform MUST support;
* determining the number of logical cores
* threads (starting and waiting on for completion)
For documentation, see the porting guide (lfds) in the mediawiki.
release history
===============
release 1, 25th September 2009, svn revision 1574.
- initial release
release 2, 5th October 2009, svn revision 1599.
- added abstraction layer for Windows kernel
- minor code tidyups/fixes
release 3, 25th October 2009, svn revision 1652.
- added singly linked list (logical delete only)
- minor code tidyups/fixes
release 4, 7th December 2009, svn revision 1716.
- added ARM support
- added benchmarking functionality to the test program
- fixed a profound and pervasive pointer
declaration bug; earlier releases of liblfds
*should not be used*
release 5, 19th December 2009, svn revision 1738.
- fixed subtle queue bug, which also affected ringbuffer
and caused data re-ordering under high load
- added benchmarks for freelist, ringbuffer and stack
release 6, 29th December 2009, svn revision 1746.
- fixed two implementation errors, which reduced performance,
spotted by Codeplug from "http://cboard.cprogramming.com".
release 6.0.0, 18th December 2012, svn revision 2537
- introduction of namespaces, e.g. the "lfds600_" prefix
code otherwise COMPLETELY AND WHOLLY UNCHANGED
this release is a stepping-stone to 6.1.0
release 6.0.1, 2nd January 2013, svn revision 3296
- bug fix where an enum wasn't moved into the new namespacing policy
release 6.1.0, 31th December 2012, svn revision 2600
- fixed all existing non-enhancement bugs
- discovered some new bugs and fixed them too
- a very few minor changes/enhancements
release 6.1.1, 2nd January 2013, svn revision 3297
- crucial bug fix where compiler barriers for atomic operations
were not brought over from 7.0.0 during backporting
- minor fix for abstraction tests, two missing store barriers
The Windows kernel build environment is primitive and has a number
of severe limitations; in particular, all source files must be in
one directory and it is not possible to choose the output binary type
(static or dynamic library) from the build command line; rather,
a string has to be modified in a text file used by the build (!)
To deal with these limitations, it is necessary for a Windows kernel
build to run a batch file prior to building.
There are two batch files, one for static library builds and the other
for dynamic library builds.
They are both idempotent; you can run them as often as you like and
switch between them as often as you want. It's all fine; whenever
you run one of them, it will take you from whatever state you were
previously in, into the state you want to be in.
Both batch files copy all the sources file into a single directory,
"/src/single_dir_for_windows_kernel/".
The static library batch file will then copy "/sources.static" into
"/src/single_dir_for_windows_kernel/", which will cause a static
library to be built.
The dynamic library batch file will then copy "/sources.dynamic" into
"/src/single_dir_for_windows_kernel/", which will cause a dynamic
library to be built. It will also copy "src/driver_entry.c" into
"/src/single_dir_for_windows_kernel/", since the linker requires
the DriverEntry function to exist for dynamic libraries, even
though it's not used.
@echo off
rmdir /q /s src\single_dir_for_windows_kernel 1>nul 2>nul
mkdir src\single_dir_for_windows_kernel 1>nul 2>nul
copy /y src\lfds611_abstraction\* src\single_dir_for_windows_kernel 1>nul 2>nul
copy /y src\lfds611_freelist\* src\single_dir_for_windows_kernel 1>nul 2>nul
copy /y src\lfds611_liblfds\* src\single_dir_for_windows_kernel 1>nul 2>nul
copy /y src\lfds611_queue\* src\single_dir_for_windows_kernel 1>nul 2>nul
copy /y src\lfds611_ringbuffer\* src\single_dir_for_windows_kernel 1>nul 2>nul
copy /y src\lfds611_slist\* src\single_dir_for_windows_kernel 1>nul 2>nul
copy /y src\lfds611_stack\* src\single_dir_for_windows_kernel 1>nul 2>nul
copy /y sources.dynamic src\single_dir_for_windows_kernel\sources 1>nul 2>nul
copy /y src\driver_entry.c src\single_dir_for_windows_kernel 1>nul 2>nul
echo Windows kernel dynamic library build directory structure created.
echo (Note the effects of this batch file are idempotent).
@echo off
rmdir /q /s src\single_dir_for_windows_kernel 1>nul 2>nul
mkdir src\single_dir_for_windows_kernel 1>nul 2>nul
copy /y src\lfds611_abstraction\* src\single_dir_for_windows_kernel 1>nul 2>nul
copy /y src\lfds611_freelist\* src\single_dir_for_windows_kernel 1>nul 2>nul
copy /y src\lfds611_liblfds\* src\single_dir_for_windows_kernel 1>nul 2>nul
copy /y src\lfds611_queue\* src\single_dir_for_windows_kernel 1>nul 2>nul
copy /y src\lfds611_ringbuffer\* src\single_dir_for_windows_kernel 1>nul 2>nul
copy /y src\lfds611_slist\* src\single_dir_for_windows_kernel 1>nul 2>nul
copy /y src\lfds611_stack\* src\single_dir_for_windows_kernel 1>nul 2>nul
copy /y sources.static src\single_dir_for_windows_kernel\sources 1>nul 2>nul
erase /f src\single_dir_for_windows_kernel\driver_entry.c 1>nul 2>nul
echo Windows kernel static library build directory structure created.
echo (Note the effects of this batch file are idempotent).
MSC_WARNING_LEVEL = /WX /W4
DLLDEF = ../../liblfds611.def
TARGETNAME = liblfds611
TARGETPATH = ../../bin/
TARGETTYPE = EXPORT_DRIVER
UMTYPE = nt
USER_C_FLAGS = /DWIN_KERNEL_BUILD
INCLUDES = ..;../../inc/
SOURCES = lfds611_abstraction_free.c \
lfds611_abstraction_malloc.c \
lfds611_freelist_delete.c \
lfds611_freelist_get_and_set.c \
lfds611_freelist_new.c \
lfds611_freelist_pop_push.c \
lfds611_freelist_query.c \
lfds611_liblfds_abstraction_test_helpers.c \
lfds611_liblfds_aligned_free.c \
lfds611_liblfds_aligned_malloc.c \
lfds611_queue_delete.c \
lfds611_queue_new.c \
lfds611_queue_query.c \
lfds611_queue_queue.c \
lfds611_ringbuffer_delete.c \
lfds611_ringbuffer_get_and_put.c \
lfds611_ringbuffer_new.c \
lfds611_ringbuffer_query.c \
lfds611_slist_delete.c \
lfds611_slist_get_and_set.c \
lfds611_slist_link.c \
lfds611_slist_new.c \
lfds611_stack_delete.c \
lfds611_stack_new.c \
lfds611_stack_push_pop.c \
lfds611_stack_query.c \
driver_entry.c
MSC_WARNING_LEVEL = /WX /W4
TARGETNAME = liblfds611
TARGETPATH = ../../bin/
TARGETTYPE = DRIVER_LIBRARY
UMTYPE = nt
USER_C_FLAGS = /DWIN_KERNEL_BUILD
INCLUDES = ..;../../inc/
SOURCES = lfds611_abstraction_free.c \
lfds611_abstraction_malloc.c \
lfds611_freelist_delete.c \
lfds611_freelist_get_and_set.c \
lfds611_freelist_new.c \
lfds611_freelist_pop_push.c \
lfds611_freelist_query.c \
lfds611_liblfds_abstraction_test_helpers.c \
lfds611_liblfds_aligned_free.c \
lfds611_liblfds_aligned_malloc.c \
lfds611_queue_delete.c \
lfds611_queue_new.c \
lfds611_queue_query.c \
lfds611_queue_queue.c \
lfds611_ringbuffer_delete.c \
lfds611_ringbuffer_get_and_put.c \
lfds611_ringbuffer_new.c \
lfds611_ringbuffer_query.c \
lfds611_slist_delete.c \
lfds611_slist_get_and_set.c \
lfds611_slist_link.c \
lfds611_slist_new.c \
lfds611_stack_delete.c \
lfds611_stack_new.c \
lfds611_stack_push_pop.c \
lfds611_stack_query.c
#include "liblfds611_internal.h"
/****************************************************************************/
#pragma warning( disable : 4100 )
NTSTATUS DriverEntry( struct _DRIVER_OBJECT *DriverObject, PUNICODE_STRING RegistryPath )
{
return( STATUS_SUCCESS );
}
#pragma warning( default : 4100 )
This C file (driver_entry.c) is used when building a dynamic library for
the Windows kernel. It exists to work around one of the limitations of
that build environment. It is not used by any other build; just ignore it.
#include "lfds611_abstraction_internal_body.h"
/****************************************************************************/
#if (defined _WIN32 && defined _MSC_VER)
/* TRD : 64 bit and 32 bit Windows (user-mode or kernel) on any CPU with the Microsoft C compiler
_WIN32 indicates 64-bit or 32-bit Windows
_MSC_VER indicates Microsoft C compiler
*/
static LFDS611_INLINE lfds611_atom_t lfds611_abstraction_cas( volatile lfds611_atom_t *destination, lfds611_atom_t exchange, lfds611_atom_t compare )
{
lfds611_atom_t
rv;
assert( destination != NULL );
// TRD : exchange can be any value in its range
// TRD : compare can be any value in its range
LFDS611_BARRIER_COMPILER_FULL;
rv = (lfds611_atom_t) _InterlockedCompareExchangePointer( (void * volatile *) destination, (void *) exchange, (void *) compare );
LFDS611_BARRIER_COMPILER_FULL;
return( rv );
}
#endif
/****************************************************************************/
#if (__GNUC__ >= 4 && __GNUC_MINOR__ >= 1 && __GNUC_PATCHLEVEL__ >= 0)
/* TRD : any OS on any CPU with GCC 4.1.0 or better
GCC 4.1.0 introduced the __sync_*() atomic intrinsics
__GNUC__ / __GNUC_MINOR__ / __GNUC_PATCHLEVEL__ indicates GCC and which version
*/
static LFDS611_INLINE lfds611_atom_t lfds611_abstraction_cas( volatile lfds611_atom_t *destination, lfds611_atom_t exchange, lfds611_atom_t compare )
{
lfds611_atom_t
rv;
assert( destination != NULL );
// TRD : exchange can be any value in its range
// TRD : compare can be any value in its range
// TRD : note the different argument order for the GCC instrinsic to the MSVC instrinsic
LFDS611_BARRIER_COMPILER_FULL;
rv = (lfds611_atom_t) __sync_val_compare_and_swap( destination, compare, exchange );
LFDS611_BARRIER_COMPILER_FULL;
return( rv );
}
#endif
#include "lfds611_abstraction_internal_body.h"
/****************************************************************************/
#if (defined _WIN64 && defined _MSC_VER)
/* TRD : 64 bit Windows (user-mode or kernel) on any CPU with the Microsoft C compiler
_WIN64 indicates 64 bit Windows
_MSC_VER indicates Microsoft C compiler
*/
static LFDS611_INLINE unsigned char lfds611_abstraction_dcas( volatile lfds611_atom_t *destination, lfds611_atom_t *exchange, lfds611_atom_t *compare )
{
unsigned char
cas_result;
assert( destination != NULL );
assert( exchange != NULL );
assert( compare != NULL );
LFDS611_BARRIER_COMPILER_FULL;
cas_result = _InterlockedCompareExchange128( (volatile __int64 *) destination, (__int64) *(exchange+1), (__int64) *exchange, (__int64 *) compare );
LFDS611_BARRIER_COMPILER_FULL;
return( cas_result ) ;
}
#endif
/****************************************************************************/
#if (!defined _WIN64 && defined _WIN32 && defined _MSC_VER)
/* TRD : 32 bit Windows (user-mode or kernel) on any CPU with the Microsoft C compiler
(!defined _WIN64 && defined _WIN32) indicates 32 bit Windows
_MSC_VER indicates Microsoft C compiler
*/
static LFDS611_INLINE unsigned char lfds611_abstraction_dcas( volatile lfds611_atom_t *destination, lfds611_atom_t *exchange, lfds611_atom_t *compare )
{
__int64
original_compare;
assert( destination != NULL );
assert( exchange != NULL );
assert( compare != NULL );
*(__int64 *) &original_compare = *(__int64 *) compare;
LFDS611_BARRIER_COMPILER_FULL;
*(__int64 *) compare = _InterlockedCompareExchange64( (volatile __int64 *) destination, *(__int64 *) exchange, *(__int64 *) compare );
LFDS611_BARRIER_COMPILER_FULL;
return( (unsigned char) (*(__int64 *) compare == *(__int64 *) &original_compare) );
}
#endif
/****************************************************************************/
#if (defined __x86_64__ && defined __GNUC__)
/* TRD : any OS on x64 with GCC
__x86_64__ indicates x64
__GNUC__ indicates GCC
*/
static LFDS611_INLINE unsigned char lfds611_abstraction_dcas( volatile lfds611_atom_t *destination, lfds611_atom_t *exchange, lfds611_atom_t *compare )
{
unsigned char
cas_result;
assert( destination != NULL );
assert( exchange != NULL );
assert( compare != NULL );
// TRD : __asm__ with "memory" in the clobber list is for GCC a full compiler barrier
__asm__ __volatile__
(
"lock;" // make cmpxchg16b atomic
"cmpxchg16b %0;" // cmpxchg16b sets ZF on success
"setz %3;" // if ZF set, set cas_result to 1
// output
: "+m" (*(volatile lfds611_atom_t (*)[2]) destination), "+a" (*compare), "+d" (*(compare+1)), "=q" (cas_result)
// input
: "b" (*exchange), "c" (*(exchange+1))
// clobbered
: "cc", "memory"
);
return( cas_result );
}
#endif
/****************************************************************************/
#if ((defined __i686__ || defined __arm__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 1 && __GNUC_PATCHLEVEL__ >= 0)
/* TRD : any OS on x86 or ARM with GCC 4.1.0 or better
GCC 4.1.0 introduced the __sync_*() atomic intrinsics
__GNUC__ / __GNUC_MINOR__ / __GNUC_PATCHLEVEL__ indicates GCC and which version
*/
static LFDS611_INLINE unsigned char lfds611_abstraction_dcas( volatile lfds611_atom_t *destination, lfds611_atom_t *exchange, lfds611_atom_t *compare )
{
unsigned char
cas_result = 0;
unsigned long long int
original_destination;
assert( destination != NULL );
assert( exchange != NULL );
assert( compare != NULL );
LFDS611_BARRIER_COMPILER_FULL;
original_destination = __sync_val_compare_and_swap( (volatile unsigned long long int *) destination, *(unsigned long long int *) compare, *(unsigned long long int *) exchange );
LFDS611_BARRIER_COMPILER_FULL;
if( original_destination == *(unsigned long long int *) compare )
cas_result = 1;
*(unsigned long long int *) compare = original_destination;
return( cas_result );
}
#endif
#include "lfds611_abstraction_internal_wrapper.h"
/****************************************************************************/
#if (!defined WIN_KERNEL_BUILD)
/* TRD : any OS except Windows kernel on any CPU with any compiler
!WIN_KERNEL_BUILD indicates not Windows kernel
*/
void lfds611_abstraction_free( void *memory )
{
free( memory );
return;
}
#endif
/****************************************************************************/
#if (defined WIN_KERNEL_BUILD)
/* TRD : any Windows (kernel) on any CPU with the Microsoft C compiler
WIN_KERNEL_BUILD indicates Windows kernel
*/
void lfds611_abstraction_free( void *memory )
{
ExFreePoolWithTag( memory, 'sdfl' );
return;
}
#endif
#include "lfds611_abstraction_internal_body.h"
/****************************************************************************/
#if (defined _WIN64 && defined _MSC_VER)
/* TRD : 64 bit Windows (user-mode or kernel) on any CPU with the Microsoft C compiler
_WIN64 indicates 64 bit Windows
_MSC_VER indicates Microsoft C compiler
*/
static LFDS611_INLINE lfds611_atom_t lfds611_abstraction_increment( volatile lfds611_atom_t *value )
{
lfds611_atom_t
rv;
assert( value != NULL );
LFDS611_BARRIER_COMPILER_FULL;
rv = (lfds611_atom_t) _InterlockedIncrement64( (__int64 *) value );
LFDS611_BARRIER_COMPILER_FULL;
return( rv );
}
#endif
/****************************************************************************/
#if (!defined _WIN64 && defined _WIN32 && defined _MSC_VER)
/* TRD : 32 bit Windows (user-mode or kernel) on any CPU with the Microsoft C compiler
(!defined _WIN64 && defined _WIN32) indicates 32 bit Windows
_MSC_VER indicates Microsoft C compiler
*/
static LFDS611_INLINE lfds611_atom_t lfds611_abstraction_increment( volatile lfds611_atom_t *value )
{
lfds611_atom_t
rv;
assert( value != NULL );
LFDS611_BARRIER_COMPILER_FULL;
rv = (lfds611_atom_t) _InterlockedIncrement( (long int *) value );
LFDS611_BARRIER_COMPILER_FULL;
return( rv );
}
#endif
/****************************************************************************/
#if (__GNUC__ >= 4 && __GNUC_MINOR__ >= 1 && __GNUC_PATCHLEVEL__ >= 0)
/* TRD : any OS on any CPU with GCC 4.1.0 or better
GCC 4.1.0 introduced the __sync_*() atomic intrinsics
__GNUC__ / __GNUC_MINOR__ / __GNUC_PATCHLEVEL__ indicates GCC and which version
*/
static LFDS611_INLINE lfds611_atom_t lfds611_abstraction_increment( volatile lfds611_atom_t *value )
{
lfds611_atom_t
rv;
assert( value != NULL );
// TRD : no need for casting here, GCC has a __sync_add_and_fetch() for all native types
LFDS611_BARRIER_COMPILER_FULL;
rv = (lfds611_atom_t) __sync_add_and_fetch( value, 1 );
LFDS611_BARRIER_COMPILER_FULL;
return( rv );
}
#endif
/***** the library wide include file *****/
#include "liblfds611_internal.h"
/***** the internal header body *****/
#include "lfds611_abstraction_internal_body.h"
#include "lfds611_abstraction_internal_wrapper.h"
/****************************************************************************/
#if (!defined WIN_KERNEL_BUILD)
/* TRD : any OS except Windows kernel on any CPU with any compiler
!WIN_KERNEL_BUILD indicates not Windows kernel
*/
void *lfds611_abstraction_malloc( size_t size )
{
return( malloc(size) );
}
#endif
/****************************************************************************/
#if (defined WIN_KERNEL_BUILD)
/* TRD : any Windows (kernel) on any CPU with the Microsoft C compiler
WIN_KERNEL_BUILD indicates Windows kernel
*/
void *lfds611_abstraction_malloc( size_t size )
{
return( ExAllocatePoolWithTag(NonPagedPool, size, 'sdfl') );
}
#endif
#include "lfds611_freelist_internal.h"
/****************************************************************************/
void lfds611_freelist_delete( struct lfds611_freelist_state *fs, void (*user_data_delete_function)(void *user_data, void *user_state), void *user_state )
{
struct lfds611_freelist_element
*fe;
void
*user_data;
assert( fs != NULL );
// TRD : user_data_delete_function can be NULL
// TRD : user_state can be NULL
// TRD : leading load barrier not required as it will be performed by the pop
while( lfds611_freelist_pop(fs, &fe) ) {
if( user_data_delete_function != NULL ) {
lfds611_freelist_get_user_data_from_element( fe, &user_data );
user_data_delete_function( user_data, user_state );
}
lfds611_liblfds_aligned_free( fe );
}
lfds611_liblfds_aligned_free( fs );
return;
}
#include "lfds611_freelist_internal.h"
/****************************************************************************/
void *lfds611_freelist_get_user_data_from_element( struct lfds611_freelist_element *fe, void **user_data )
{
assert( fe != NULL );
// TRD : user_data can be NULL
LFDS611_BARRIER_LOAD;
if( user_data != NULL )
*user_data = fe->user_data;
return( fe->user_data );
}
/****************************************************************************/
void lfds611_freelist_set_user_data_in_element( struct lfds611_freelist_element *fe, void *user_data )
{
assert( fe != NULL );
// TRD : user_data can be NULL
fe->user_data = user_data;
LFDS611_BARRIER_STORE;
return;
}
/***** the library wide include file *****/
#include "liblfds611_internal.h"
/***** defines *****/
#define LFDS611_FREELIST_POINTER 0
#define LFDS611_FREELIST_COUNTER 1
#define LFDS611_FREELIST_PAC_SIZE 2
/***** structures *****/
#pragma pack( push, LFDS611_ALIGN_DOUBLE_POINTER )
struct lfds611_freelist_state {
struct lfds611_freelist_element
*volatile top[LFDS611_FREELIST_PAC_SIZE];
int
(*user_data_init_function)( void **user_data, void *user_state );
void
*user_state;
lfds611_atom_t
aba_counter,
element_count;
};
struct lfds611_freelist_element {
struct lfds611_freelist_element
*next[LFDS611_FREELIST_PAC_SIZE];
void
*user_data;
};
#pragma pack( pop )
/***** private prototypes *****/
lfds611_atom_t lfds611_freelist_internal_new_element( struct lfds611_freelist_state *fs, struct lfds611_freelist_element **fe );
void lfds611_freelist_internal_validate( struct lfds611_freelist_state *fs, struct lfds611_validation_info *vi, enum lfds611_data_structure_validity *lfds611_freelist_validity );
#include "lfds611_freelist_internal.h"
/****************************************************************************/
int lfds611_freelist_new( struct lfds611_freelist_state **fs, lfds611_atom_t number_elements, int (*user_data_init_function)(void **user_data, void *user_state), void *user_state )
{
int
rv = 0;
lfds611_atom_t
element_count;
assert( fs != NULL );
// TRD : number_elements can be any value in its range
// TRD : user_data_init_function can be NULL
*fs = (struct lfds611_freelist_state *) lfds611_liblfds_aligned_malloc( sizeof(struct lfds611_freelist_state), LFDS611_ALIGN_DOUBLE_POINTER );
if( (*fs) != NULL ) {
(*fs)->top[LFDS611_FREELIST_POINTER] = NULL;
(*fs)->top[LFDS611_FREELIST_COUNTER] = 0;
(*fs)->user_data_init_function = user_data_init_function;
(*fs)->user_state = user_state;
(*fs)->aba_counter = 0;
(*fs)->element_count = 0;
element_count = lfds611_freelist_new_elements( *fs, number_elements );
if( element_count == number_elements )
rv = 1;
if( element_count != number_elements ) {
lfds611_liblfds_aligned_free( (*fs) );
*fs = NULL;
}
}
LFDS611_BARRIER_STORE;
return( rv );
}
/****************************************************************************/
//#pragma warning( disable : 4100 )
void lfds611_freelist_use( struct lfds611_freelist_state *fs )
{
assert( fs != NULL );
LFDS611_BARRIER_LOAD;
return;
}
//#pragma warning( default : 4100 )
/****************************************************************************/
lfds611_atom_t lfds611_freelist_new_elements( struct lfds611_freelist_state *fs, lfds611_atom_t number_elements )
{
struct lfds611_freelist_element
*fe;
lfds611_atom_t
loop,
count = 0;
assert( fs != NULL );
// TRD : number_elements can be any value in its range
// TRD : user_data_init_function can be NULL
for( loop = 0 ; loop < number_elements ; loop++ )
if( lfds611_freelist_internal_new_element(fs, &fe) ) {
lfds611_freelist_push( fs, fe );
count++;
}
return( count );
}
/****************************************************************************/
lfds611_atom_t lfds611_freelist_internal_new_element( struct lfds611_freelist_state *fs, struct lfds611_freelist_element **fe )
{
lfds611_atom_t
rv = 0;
assert( fs != NULL );
assert( fe != NULL );
/* TRD : basically, does what you'd expect;
allocates an element
calls the user init function
if anything fails, cleans up,
sets *fe to NULL
and returns 0
*/
*fe = (struct lfds611_freelist_element *) lfds611_liblfds_aligned_malloc( sizeof(struct lfds611_freelist_element), LFDS611_ALIGN_DOUBLE_POINTER );
if( *fe != NULL ) {
if( fs->user_data_init_function == NULL ) {
(*fe)->user_data = NULL;
rv = 1;
}
if( fs->user_data_init_function != NULL ) {
rv = fs->user_data_init_function( &(*fe)->user_data, fs->user_state );
if( rv == 0 ) {
lfds611_liblfds_aligned_free( *fe );
*fe = NULL;
}
}
}
if( rv == 1 )
lfds611_abstraction_increment( (lfds611_atom_t *) &fs->element_count );
return( rv );
}
#include "lfds611_freelist_internal.h"
/****************************************************************************/
struct lfds611_freelist_element *lfds611_freelist_pop( struct lfds611_freelist_state *fs, struct lfds611_freelist_element **fe )
{
LFDS611_ALIGN(LFDS611_ALIGN_DOUBLE_POINTER) struct lfds611_freelist_element
*fe_local[LFDS611_FREELIST_PAC_SIZE];
assert( fs != NULL );
assert( fe != NULL );
LFDS611_BARRIER_LOAD;
fe_local[LFDS611_FREELIST_COUNTER] = fs->top[LFDS611_FREELIST_COUNTER];
fe_local[LFDS611_FREELIST_POINTER] = fs->top[LFDS611_FREELIST_POINTER];
/* TRD : note that lfds611_abstraction_dcas loads the original value of the destination (fs->top) into the compare (fe_local)
(this happens of course after the CAS itself has occurred inside lfds611_abstraction_dcas)
*/
do {
if( fe_local[LFDS611_FREELIST_POINTER] == NULL ) {
*fe = NULL;
return( *fe );
}
} while( 0 == lfds611_abstraction_dcas((volatile lfds611_atom_t *) fs->top, (lfds611_atom_t *) fe_local[LFDS611_FREELIST_POINTER]->next, (lfds611_atom_t *) fe_local) );
*fe = (struct lfds611_freelist_element *) fe_local[LFDS611_FREELIST_POINTER];
return( *fe );
}
/****************************************************************************/
struct lfds611_freelist_element *lfds611_freelist_guaranteed_pop( struct lfds611_freelist_state *fs, struct lfds611_freelist_element **fe )
{
assert( fs != NULL );
assert( fe != NULL );
lfds611_freelist_internal_new_element( fs, fe );
return( *fe );
}
/****************************************************************************/
void lfds611_freelist_push( struct lfds611_freelist_state *fs, struct lfds611_freelist_element *fe )
{
LFDS611_ALIGN(LFDS611_ALIGN_DOUBLE_POINTER) struct lfds611_freelist_element
*fe_local[LFDS611_FREELIST_PAC_SIZE],
*original_fe_next[LFDS611_FREELIST_PAC_SIZE];
assert( fs != NULL );
assert( fe != NULL );
LFDS611_BARRIER_LOAD;
fe_local[LFDS611_FREELIST_POINTER] = fe;
fe_local[LFDS611_FREELIST_COUNTER] = (struct lfds611_freelist_element *) lfds611_abstraction_increment( (lfds611_atom_t *) &fs->aba_counter );
original_fe_next[LFDS611_FREELIST_POINTER] = fs->top[LFDS611_FREELIST_POINTER];
original_fe_next[LFDS611_FREELIST_COUNTER] = fs->top[LFDS611_FREELIST_COUNTER];
/* TRD : note that lfds611_abstraction_dcas loads the original value of the destination (fs->top) into the compare (original_fe_next)
(this happens of course after the CAS itself has occurred inside lfds611_abstraction_dcas)
this then causes us in our loop, should we repeat it, to update fe_local->next to a more
up-to-date version of the head of the lfds611_freelist
*/
do {
fe_local[LFDS611_FREELIST_POINTER]->next[LFDS611_FREELIST_POINTER] = original_fe_next[LFDS611_FREELIST_POINTER];
fe_local[LFDS611_FREELIST_POINTER]->next[LFDS611_FREELIST_COUNTER] = original_fe_next[LFDS611_FREELIST_COUNTER];
} while( 0 == lfds611_abstraction_dcas((volatile lfds611_atom_t *) fs->top, (lfds611_atom_t *) fe_local, (lfds611_atom_t *) original_fe_next) );
return;
}
#include "lfds611_freelist_internal.h"
/****************************************************************************/
void lfds611_freelist_query( struct lfds611_freelist_state *fs, enum lfds611_freelist_query_type query_type, void *query_input, void *query_output )
{
assert( fs != NULL );
// TRD : query type can be any value in its range
// TRD : query_input can be NULL in some cases
assert( query_output != NULL );
LFDS611_BARRIER_LOAD;
switch( query_type ) {
case LFDS611_FREELIST_QUERY_ELEMENT_COUNT:
assert( query_input == NULL );
*(lfds611_atom_t *) query_output = fs->element_count;
break;
case LFDS611_FREELIST_QUERY_VALIDATE:
// TRD : query_input can be NULL
lfds611_freelist_internal_validate( fs, (struct lfds611_validation_info *) query_input, (enum lfds611_data_structure_validity *) query_output );
break;
}
return;
}
/****************************************************************************/
void lfds611_freelist_internal_validate( struct lfds611_freelist_state *fs, struct lfds611_validation_info *vi, enum lfds611_data_structure_validity *lfds611_freelist_validity )
{
struct lfds611_freelist_element
*fe,
*fe_slow,
*fe_fast;
lfds611_atom_t
element_count = 0;
assert( fs != NULL );
// TRD : vi can be NULL
assert( lfds611_freelist_validity != NULL );
*lfds611_freelist_validity = LFDS611_VALIDITY_VALID;
fe_slow = fe_fast = (struct lfds611_freelist_element *) fs->top[LFDS611_FREELIST_POINTER];
/* TRD : first, check for a loop
we have two pointers
both of which start at the top of the lfds611_freelist
we enter a loop
and on each iteration
we advance one pointer by one element
and the other by two
we exit the loop when both pointers are NULL
(have reached the end of the lfds611_freelist)
or
if we fast pointer 'sees' the slow pointer
which means we have a loop
*/
if( fe_slow != NULL )
do {
fe_slow = fe_slow->next[LFDS611_FREELIST_POINTER];
if( fe_fast != NULL )
fe_fast = fe_fast->next[LFDS611_FREELIST_POINTER];
if( fe_fast != NULL )
fe_fast = fe_fast->next[LFDS611_FREELIST_POINTER];
} while( fe_slow != NULL and fe_fast != fe_slow );
if( fe_fast != NULL and fe_slow != NULL and fe_fast == fe_slow )
*lfds611_freelist_validity = LFDS611_VALIDITY_INVALID_LOOP;
/* TRD : now check for expected number of elements
vi can be NULL, in which case we do not check
we know we don't have a loop from our earlier check
*/
if( *lfds611_freelist_validity == LFDS611_VALIDITY_VALID and vi != NULL ) {
fe = (struct lfds611_freelist_element *) fs->top[LFDS611_FREELIST_POINTER];
while( fe != NULL ) {
element_count++;
fe = (struct lfds611_freelist_element *) fe->next[LFDS611_FREELIST_POINTER];
}
if( element_count < vi->min_elements )
*lfds611_freelist_validity = LFDS611_VALIDITY_INVALID_MISSING_ELEMENTS;
if( element_count > vi->max_elements )
*lfds611_freelist_validity = LFDS611_VALIDITY_INVALID_ADDITIONAL_ELEMENTS;
}
return;
}
#include "lfds611_liblfds_internal.h"
/****************************************************************************/
void lfds611_liblfds_abstraction_test_helper_increment_non_atomic( lfds611_atom_t *shared_counter )
{
/* TRD : lfds611_atom_t must be volatile or the compiler
optimizes it away into a single store
*/
volatile lfds611_atom_t
count = 0;
assert( shared_counter != NULL );
while( count++ < 10000000 )
(*(lfds611_atom_t *) shared_counter)++;
return;
}
/****************************************************************************/
void lfds611_liblfds_abstraction_test_helper_increment_atomic( volatile lfds611_atom_t *shared_counter )
{
lfds611_atom_t
count = 0;
assert( shared_counter != NULL );
while( count++ < 10000000 )
lfds611_abstraction_increment( shared_counter );
return;
}
/****************************************************************************/
void lfds611_liblfds_abstraction_test_helper_cas( volatile lfds611_atom_t *shared_counter, lfds611_atom_t *local_counter )
{
lfds611_atom_t
loop = 0,
original_destination;
LFDS611_ALIGN(LFDS611_ALIGN_SINGLE_POINTER) lfds611_atom_t
exchange,
compare;
assert( shared_counter != NULL );
assert( local_counter != NULL );
while( loop++ < 1000000 ) {
do {
compare = *shared_counter;
exchange = compare + 1;
original_destination = lfds611_abstraction_cas( shared_counter, exchange, compare );
} while( original_destination != compare );
(*local_counter)++;
}
return;
}
/****************************************************************************/
void lfds611_liblfds_abstraction_test_helper_dcas( volatile lfds611_atom_t *shared_counter, lfds611_atom_t *local_counter )
{
lfds611_atom_t
loop = 0;
LFDS611_ALIGN(LFDS611_ALIGN_DOUBLE_POINTER) lfds611_atom_t
exchange[2],
compare[2];
assert( shared_counter != NULL );
assert( local_counter != NULL );
while( loop++ < 1000000 ) {
compare[0] = *shared_counter;
compare[1] = *(shared_counter+1);
do {
exchange[0] = compare[0] + 1;
exchange[1] = compare[1];
} while( 0 == lfds611_abstraction_dcas(shared_counter, exchange, compare) );
(*local_counter)++;
}
return;
}
#include "lfds611_liblfds_internal.h"
/****************************************************************************/
void lfds611_liblfds_aligned_free( void *memory )
{
assert( memory != NULL );
// TRD : the "void *" stored above memory points to the root of the allocation
lfds611_abstraction_free( *( (void **) memory - 1 ) );
return;
}
#include "lfds611_liblfds_internal.h"
/****************************************************************************/
void *lfds611_liblfds_aligned_malloc( size_t size, size_t align_in_bytes )
{
void
*original_memory,
*memory;
size_t
offset;
// TRD : size can be any value in its range
// TRD : align_in_bytes can be any value in its range
original_memory = memory = lfds611_abstraction_malloc( size + sizeof(void *) + align_in_bytes );
if( memory != NULL ) {
memory = (void **) memory + 1;
offset = align_in_bytes - (size_t) memory % align_in_bytes;
memory = (unsigned char *) memory + offset;
*( (void **) memory - 1 ) = original_memory;
}
return( memory );
}
/***** the library wide include file *****/
#include "liblfds611_internal.h"
This is not a data structure but rather functions internal to the library.
#include "lfds611_queue_internal.h"
/****************************************************************************/
void lfds611_queue_delete( struct lfds611_queue_state *qs, void (*user_data_delete_function)(void *user_data, void *user_state), void *user_state )
{
void
*user_data;
assert( qs != NULL );
// TRD : user_data_delete_function can be NULL
// TRD : user_state can be NULL
// TRD : leading load barrier not required as it will be performed by the dequeue
while( lfds611_queue_dequeue(qs, &user_data) )
if( user_data_delete_function != NULL )
user_data_delete_function( user_data, user_state );
/* TRD : fully dequeuing will leave us
with a single dummy element
which both qs->enqueue and qs->dequeue point at
we push this back onto the lfds611_freelist
before we delete the lfds611_freelist
*/
lfds611_freelist_push( qs->fs, qs->enqueue[LFDS611_QUEUE_POINTER]->fe );
lfds611_freelist_delete( qs->fs, lfds611_queue_internal_freelist_delete_function, NULL );
lfds611_liblfds_aligned_free( qs );
return;
}
/****************************************************************************/
//#pragma warning( disable : 4100 )
void lfds611_queue_internal_freelist_delete_function( void *user_data, void *user_state )
{
assert( user_data != NULL );
assert( user_state == NULL );
lfds611_liblfds_aligned_free( user_data );
return;
}
//#pragma warning( default : 4100 )
/***** the library wide include file *****/
#include "liblfds611_internal.h"
/***** pragmas *****/
/***** defines *****/
#define LFDS611_QUEUE_STATE_UNKNOWN -1
#define LFDS611_QUEUE_STATE_EMPTY 0
#define LFDS611_QUEUE_STATE_ENQUEUE_OUT_OF_PLACE 1
#define LFDS611_QUEUE_STATE_ATTEMPT_DELFDS611_QUEUE 2
#define LFDS611_QUEUE_POINTER 0
#define LFDS611_QUEUE_COUNTER 1
#define LFDS611_QUEUE_PAC_SIZE 2
/***** structures *****/
#pragma pack( push, LFDS611_ALIGN_DOUBLE_POINTER )
struct lfds611_queue_state {
struct lfds611_queue_element
*volatile enqueue[LFDS611_QUEUE_PAC_SIZE],
*volatile dequeue[LFDS611_QUEUE_PAC_SIZE];
lfds611_atom_t
aba_counter;
struct lfds611_freelist_state
*fs;
};
struct lfds611_queue_element {
// TRD : next in a lfds611_queue requires volatile as it is target of CAS
struct lfds611_queue_element
*volatile next[LFDS611_QUEUE_PAC_SIZE];
struct lfds611_freelist_element
*fe;
void
*user_data;
};
#pragma pack( pop )
/***** externs *****/
/***** private prototypes *****/
int lfds611_queue_internal_freelist_init_function( void **user_data, void *user_state );
void lfds611_queue_internal_freelist_delete_function( void *user_data, void *user_state );
void lfds611_queue_internal_new_element_from_freelist( struct lfds611_queue_state *qs, struct lfds611_queue_element *qe[LFDS611_QUEUE_PAC_SIZE], void *user_data );
void lfds611_queue_internal_guaranteed_new_element_from_freelist( struct lfds611_queue_state *qs, struct lfds611_queue_element * qe[LFDS611_QUEUE_PAC_SIZE], void *user_data );
void lfds611_queue_internal_init_element( struct lfds611_queue_state *qs, struct lfds611_queue_element *qe[LFDS611_QUEUE_PAC_SIZE], struct lfds611_freelist_element *fe, void *user_data );
void lfds611_queue_internal_queue( struct lfds611_queue_state *qs, struct lfds611_queue_element *qe[LFDS611_QUEUE_PAC_SIZE] );
void lfds611_queue_internal_validate( struct lfds611_queue_state *qs, struct lfds611_validation_info *vi, enum lfds611_data_structure_validity *lfds611_queue_validity,
enum lfds611_data_structure_validity *lfds611_freelist_validity );
#include "lfds611_queue_internal.h"
/****************************************************************************/
int lfds611_queue_new( struct lfds611_queue_state **qs, lfds611_atom_t number_elements )
{
int
rv = 0;
struct lfds611_queue_element
*qe[LFDS611_QUEUE_PAC_SIZE];
assert( qs != NULL );
// TRD : number_elements can be any value in its range
*qs = (struct lfds611_queue_state *) lfds611_liblfds_aligned_malloc( sizeof(struct lfds611_queue_state), LFDS611_ALIGN_DOUBLE_POINTER );
if( *qs != NULL ) {
// TRD : the size of the lfds611_freelist is the size of the lfds611_queue (+1 for the leading dummy element, which is hidden from the caller)
lfds611_freelist_new( &(*qs)->fs, number_elements+1, lfds611_queue_internal_freelist_init_function, NULL );
if( (*qs)->fs != NULL ) {
lfds611_queue_internal_new_element_from_freelist( *qs, qe, NULL );
(*qs)->enqueue[LFDS611_QUEUE_POINTER] = (*qs)->dequeue[LFDS611_QUEUE_POINTER] = qe[LFDS611_QUEUE_POINTER];
(*qs)->enqueue[LFDS611_QUEUE_COUNTER] = (*qs)->dequeue[LFDS611_QUEUE_COUNTER] = 0;
(*qs)->aba_counter = 0;
rv = 1;
}
if( (*qs)->fs == NULL ) {
lfds611_liblfds_aligned_free( *qs );
*qs = NULL;
}
}
LFDS611_BARRIER_STORE;
return( rv );
}
/****************************************************************************/
//#pragma warning( disable : 4100 )
void lfds611_queue_use( struct lfds611_queue_state *qs )
{
assert( qs != NULL );
LFDS611_BARRIER_LOAD;
return;
}
//#pragma warning( default : 4100 )
/****************************************************************************/
//#pragma warning( disable : 4100 )
int lfds611_queue_internal_freelist_init_function( void **user_data, void *user_state )
{
int
rv = 0;
assert( user_data != NULL );
assert( user_state == NULL );
*user_data = lfds611_liblfds_aligned_malloc( sizeof(struct lfds611_queue_element), LFDS611_ALIGN_DOUBLE_POINTER );
if( *user_data != NULL )
rv = 1;
return( rv );
}
//#pragma warning( default : 4100 )
/****************************************************************************/
void lfds611_queue_internal_new_element_from_freelist( struct lfds611_queue_state *qs, struct lfds611_queue_element *qe[LFDS611_QUEUE_PAC_SIZE], void *user_data )
{
struct lfds611_freelist_element
*fe;
assert( qs != NULL );
assert( qe != NULL );
// TRD : user_data can be any value in its range
qe[LFDS611_QUEUE_POINTER] = NULL;
lfds611_freelist_pop( qs->fs, &fe );
if( fe != NULL )
lfds611_queue_internal_init_element( qs, qe, fe, user_data );
return;
}
/****************************************************************************/
void lfds611_queue_internal_guaranteed_new_element_from_freelist( struct lfds611_queue_state *qs, struct lfds611_queue_element *qe[LFDS611_QUEUE_PAC_SIZE], void *user_data )
{
struct lfds611_freelist_element
*fe;
assert( qs != NULL );
assert( qe != NULL );
// TRD : user_data can be any value in its range
qe[LFDS611_QUEUE_POINTER] = NULL;
lfds611_freelist_guaranteed_pop( qs->fs, &fe );
if( fe != NULL )
lfds611_queue_internal_init_element( qs, qe, fe, user_data );
return;
}
/****************************************************************************/
void lfds611_queue_internal_init_element( struct lfds611_queue_state *qs, struct lfds611_queue_element *qe[LFDS611_QUEUE_PAC_SIZE], struct lfds611_freelist_element *fe, void *user_data )
{
assert( qs != NULL );
assert( qe != NULL );
assert( fe != NULL );
// TRD : user_data can be any value in its range
lfds611_freelist_get_user_data_from_element( fe, (void **) &qe[LFDS611_QUEUE_POINTER] );
qe[LFDS611_QUEUE_COUNTER] = (struct lfds611_queue_element *) lfds611_abstraction_increment( (lfds611_atom_t *) &qs->aba_counter );
qe[LFDS611_QUEUE_POINTER]->next[LFDS611_QUEUE_POINTER] = NULL;
qe[LFDS611_QUEUE_POINTER]->next[LFDS611_QUEUE_COUNTER] = (struct lfds611_queue_element *) lfds611_abstraction_increment( (lfds611_atom_t *) &qs->aba_counter );
qe[LFDS611_QUEUE_POINTER]->fe = fe;
qe[LFDS611_QUEUE_POINTER]->user_data = user_data;
return;
}
#include "lfds611_queue_internal.h"
/****************************************************************************/
//#pragma warning( disable : 4100 )
void lfds611_queue_query( struct lfds611_queue_state *qs, enum lfds611_queue_query_type query_type, void *query_input, void *query_output )
{
assert( qs != NULL );
// TRD : query_type can be any value in its range
// TRD : query_input can be NULL
assert( query_output != NULL );
switch( query_type ) {
case LFDS611_QUEUE_QUERY_ELEMENT_COUNT:
assert( query_input == NULL );
lfds611_freelist_query( qs->fs, LFDS611_FREELIST_QUERY_ELEMENT_COUNT, NULL, query_output );
break;
case LFDS611_QUEUE_QUERY_VALIDATE:
// TRD : query_input can be NULL
lfds611_queue_internal_validate( qs, (struct lfds611_validation_info *) query_input, (enum lfds611_data_structure_validity *) query_output, ((enum lfds611_data_structure_validity *) query_output)+1 );
break;
}
return;
}
//#pragma warning( default : 4100 )
/****************************************************************************/
void lfds611_queue_internal_validate( struct lfds611_queue_state *qs, struct lfds611_validation_info *vi, enum lfds611_data_structure_validity *lfds611_queue_validity,
enum lfds611_data_structure_validity *lfds611_freelist_validity )
{
struct lfds611_queue_element
*qe,
*qe_slow,
*qe_fast;
lfds611_atom_t
element_count = 0,
total_elements;
struct lfds611_validation_info
lfds611_freelist_vi;
assert( qs != NULL );
// TRD : vi can be NULL
assert( lfds611_queue_validity != NULL );
assert( lfds611_freelist_validity != NULL );
*lfds611_queue_validity = LFDS611_VALIDITY_VALID;
LFDS611_BARRIER_LOAD;
qe_slow = qe_fast = (struct lfds611_queue_element *) qs->dequeue[LFDS611_QUEUE_POINTER];
/* TRD : first, check for a loop
we have two pointers
both of which start at the dequeue end of the lfds611_queue
we enter a loop
and on each iteration
we advance one pointer by one element
and the other by two
we exit the loop when both pointers are NULL
(have reached the end of the lfds611_queue)
or
if we fast pointer 'sees' the slow pointer
which means we have a loop
*/
if( qe_slow != NULL )
do {
qe_slow = qe_slow->next[LFDS611_QUEUE_POINTER];
if( qe_fast != NULL )
qe_fast = qe_fast->next[LFDS611_QUEUE_POINTER];
if( qe_fast != NULL )
qe_fast = qe_fast->next[LFDS611_QUEUE_POINTER];
} while( qe_slow != NULL and qe_fast != qe_slow );
if( qe_fast != NULL and qe_slow != NULL and qe_fast == qe_slow )
*lfds611_queue_validity = LFDS611_VALIDITY_INVALID_LOOP;
/* TRD : now check for expected number of elements
vi can be NULL, in which case we do not check
we know we don't have a loop from our earlier check
*/
if( *lfds611_queue_validity == LFDS611_VALIDITY_VALID and vi != NULL ) {
qe = (struct lfds611_queue_element *) qs->dequeue[LFDS611_QUEUE_POINTER];
while( qe != NULL ) {
element_count++;
qe = (struct lfds611_queue_element *) qe->next[LFDS611_QUEUE_POINTER];
}
/* TRD : remember there is a dummy element in the lfds611_queue */
element_count--;
if( element_count < vi->min_elements )
*lfds611_queue_validity = LFDS611_VALIDITY_INVALID_MISSING_ELEMENTS;
if( element_count > vi->max_elements )
*lfds611_queue_validity = LFDS611_VALIDITY_INVALID_ADDITIONAL_ELEMENTS;
}
/* TRD : now we validate the lfds611_freelist
we may be able to check for the expected number of
elements in the lfds611_freelist
if the caller has given us an expected min and max
number of elements in the lfds611_queue, then the total number
of elements in the lfds611_freelist, minus that min and max,
gives us the expected number of elements in the
lfds611_freelist
*/
if( vi != NULL ) {
lfds611_freelist_query( qs->fs, LFDS611_FREELIST_QUERY_ELEMENT_COUNT, NULL, (void *) &total_elements );
/* TRD : remember there is a dummy element in the lfds611_queue */
total_elements--;
lfds611_freelist_vi.min_elements = total_elements - vi->max_elements;
lfds611_freelist_vi.max_elements = total_elements - vi->min_elements;
lfds611_freelist_query( qs->fs, LFDS611_FREELIST_QUERY_VALIDATE, (void *) &lfds611_freelist_vi, (void *) lfds611_freelist_validity );
}
if( vi == NULL )
lfds611_freelist_query( qs->fs, LFDS611_FREELIST_QUERY_VALIDATE, NULL, (void *) lfds611_freelist_validity );
return;
}
#include "lfds611_queue_internal.h"
/****************************************************************************/
int lfds611_queue_enqueue( struct lfds611_queue_state *qs, void *user_data )
{
LFDS611_ALIGN(LFDS611_ALIGN_DOUBLE_POINTER) struct lfds611_queue_element
*qe[LFDS611_QUEUE_PAC_SIZE];
assert( qs != NULL );
// TRD : user_data can be NULL
lfds611_queue_internal_new_element_from_freelist( qs, qe, user_data );
if( qe[LFDS611_QUEUE_POINTER] == NULL )
return( 0 );
lfds611_queue_internal_queue( qs, qe );
return( 1 );
}
/****************************************************************************/
int lfds611_queue_guaranteed_enqueue( struct lfds611_queue_state *qs, void *user_data )
{
LFDS611_ALIGN(LFDS611_ALIGN_DOUBLE_POINTER) struct lfds611_queue_element
*qe[LFDS611_QUEUE_PAC_SIZE];
assert( qs != NULL );
// TRD : user_data can be NULL
lfds611_queue_internal_guaranteed_new_element_from_freelist( qs, qe, user_data );
if( qe[LFDS611_QUEUE_POINTER] == NULL )
return( 0 );
lfds611_queue_internal_queue( qs, qe );
return( 1 );
}
/****************************************************************************/
void lfds611_queue_internal_queue( struct lfds611_queue_state *qs, struct lfds611_queue_element *qe[LFDS611_QUEUE_PAC_SIZE] )
{
LFDS611_ALIGN(LFDS611_ALIGN_DOUBLE_POINTER) struct lfds611_queue_element
*enqueue[LFDS611_QUEUE_PAC_SIZE],
*next[LFDS611_QUEUE_PAC_SIZE];
unsigned char
cas_result = 0;
assert( qs != NULL );
assert( qe != NULL );
// TRD : the DCAS operation issues a read and write barrier, so we don't need a read barrier in the do() loop
LFDS611_BARRIER_LOAD;
do {
enqueue[LFDS611_QUEUE_POINTER] = qs->enqueue[LFDS611_QUEUE_POINTER];
enqueue[LFDS611_QUEUE_COUNTER] = qs->enqueue[LFDS611_QUEUE_COUNTER];
next[LFDS611_QUEUE_POINTER] = enqueue[LFDS611_QUEUE_POINTER]->next[LFDS611_QUEUE_POINTER];
next[LFDS611_QUEUE_COUNTER] = enqueue[LFDS611_QUEUE_POINTER]->next[LFDS611_QUEUE_COUNTER];
/* TRD : this if() ensures that the next we read, just above,
really is from qs->enqueue (which we copied into enqueue)
*/
LFDS611_BARRIER_LOAD;
if( qs->enqueue[LFDS611_QUEUE_POINTER] == enqueue[LFDS611_QUEUE_POINTER] and qs->enqueue[LFDS611_QUEUE_COUNTER] == enqueue[LFDS611_QUEUE_COUNTER] ) {
if( next[LFDS611_QUEUE_POINTER] == NULL ) {
qe[LFDS611_QUEUE_COUNTER] = next[LFDS611_QUEUE_COUNTER] + 1;
cas_result = lfds611_abstraction_dcas( (volatile lfds611_atom_t *) enqueue[LFDS611_QUEUE_POINTER]->next, (lfds611_atom_t *) qe, (lfds611_atom_t *) next );
} else {
next[LFDS611_QUEUE_COUNTER] = enqueue[LFDS611_QUEUE_COUNTER] + 1;
lfds611_abstraction_dcas( (volatile lfds611_atom_t *) qs->enqueue, (lfds611_atom_t *) next, (lfds611_atom_t *) enqueue );
}
}
} while( cas_result == 0 );
qe[LFDS611_QUEUE_COUNTER] = enqueue[LFDS611_QUEUE_COUNTER] + 1;
lfds611_abstraction_dcas( (volatile lfds611_atom_t *) qs->enqueue, (lfds611_atom_t *) qe, (lfds611_atom_t *) enqueue );
return;
}
/****************************************************************************/
int lfds611_queue_dequeue( struct lfds611_queue_state *qs, void **user_data )
{
LFDS611_ALIGN(LFDS611_ALIGN_DOUBLE_POINTER) struct lfds611_queue_element
*enqueue[LFDS611_QUEUE_PAC_SIZE],
*dequeue[LFDS611_QUEUE_PAC_SIZE],
*next[LFDS611_QUEUE_PAC_SIZE];
unsigned char
cas_result = 0;
int
rv = 1,
state = LFDS611_QUEUE_STATE_UNKNOWN,
finished_flag = LOWERED;
assert( qs != NULL );
assert( user_data != NULL );
// TRD : the DCAS operation issues a read and write barrier, so we don't need a read barrier in the do() loop
LFDS611_BARRIER_LOAD;
do {
dequeue[LFDS611_QUEUE_POINTER] = qs->dequeue[LFDS611_QUEUE_POINTER];
dequeue[LFDS611_QUEUE_COUNTER] = qs->dequeue[LFDS611_QUEUE_COUNTER];
enqueue[LFDS611_QUEUE_POINTER] = qs->enqueue[LFDS611_QUEUE_POINTER];
enqueue[LFDS611_QUEUE_COUNTER] = qs->enqueue[LFDS611_QUEUE_COUNTER];
next[LFDS611_QUEUE_POINTER] = dequeue[LFDS611_QUEUE_POINTER]->next[LFDS611_QUEUE_POINTER];
next[LFDS611_QUEUE_COUNTER] = dequeue[LFDS611_QUEUE_POINTER]->next[LFDS611_QUEUE_COUNTER];
/* TRD : confirm that dequeue didn't move between reading it
and reading its next pointer
*/
LFDS611_BARRIER_LOAD;
if( dequeue[LFDS611_QUEUE_POINTER] == qs->dequeue[LFDS611_QUEUE_POINTER] and dequeue[LFDS611_QUEUE_COUNTER] == qs->dequeue[LFDS611_QUEUE_COUNTER] ) {
if( enqueue[LFDS611_QUEUE_POINTER] == dequeue[LFDS611_QUEUE_POINTER] and next[LFDS611_QUEUE_POINTER] == NULL )
state = LFDS611_QUEUE_STATE_EMPTY;
if( enqueue[LFDS611_QUEUE_POINTER] == dequeue[LFDS611_QUEUE_POINTER] and next[LFDS611_QUEUE_POINTER] != NULL )
state = LFDS611_QUEUE_STATE_ENQUEUE_OUT_OF_PLACE;
if( enqueue[LFDS611_QUEUE_POINTER] != dequeue[LFDS611_QUEUE_POINTER] )
state = LFDS611_QUEUE_STATE_ATTEMPT_DELFDS611_QUEUE;
switch( state ) {
case LFDS611_QUEUE_STATE_EMPTY:
*user_data = NULL;
rv = 0;
finished_flag = RAISED;
break;
case LFDS611_QUEUE_STATE_ENQUEUE_OUT_OF_PLACE:
next[LFDS611_QUEUE_COUNTER] = enqueue[LFDS611_QUEUE_COUNTER] + 1;
lfds611_abstraction_dcas( (volatile lfds611_atom_t *) qs->enqueue, (lfds611_atom_t *) next, (lfds611_atom_t *) enqueue );
break;
case LFDS611_QUEUE_STATE_ATTEMPT_DELFDS611_QUEUE:
*user_data = next[LFDS611_QUEUE_POINTER]->user_data;
next[LFDS611_QUEUE_COUNTER] = dequeue[LFDS611_QUEUE_COUNTER] + 1;
cas_result = lfds611_abstraction_dcas( (volatile lfds611_atom_t *) qs->dequeue, (lfds611_atom_t *) next, (lfds611_atom_t *) dequeue );
if( cas_result == 1 )
finished_flag = RAISED;
break;
}
}
} while( finished_flag == LOWERED );
if( cas_result == 1 )
lfds611_freelist_push( qs->fs, dequeue[LFDS611_QUEUE_POINTER]->fe );
return( rv );
}
#include "lfds611_ringbuffer_internal.h"
/****************************************************************************/
void lfds611_ringbuffer_delete( struct lfds611_ringbuffer_state *rs, void (*user_data_delete_function)(void *user_data, void *user_state), void *user_state )
{
assert( rs != NULL );
// TRD : user_data_delete_function can be NULL
// TRD : user_state can be NULL
lfds611_queue_delete( rs->qs, NULL, NULL );
lfds611_freelist_delete( rs->fs, user_data_delete_function, user_state );
lfds611_liblfds_aligned_free( rs );
return;
}
#include "lfds611_ringbuffer_internal.h"
/****************************************************************************/
struct lfds611_freelist_element *lfds611_ringbuffer_get_read_element( struct lfds611_ringbuffer_state *rs, struct lfds611_freelist_element **fe )
{
assert( rs != NULL );
assert( fe != NULL );
lfds611_queue_dequeue( rs->qs, (void **) fe );
return( *fe );
}
/****************************************************************************/
struct lfds611_freelist_element *lfds611_ringbuffer_get_write_element( struct lfds611_ringbuffer_state *rs, struct lfds611_freelist_element **fe, int *overwrite_flag )
{
assert( rs != NULL );
assert( fe != NULL );
// TRD : overwrite_flag can be NULL
/* TRD : we try to obtain an element from the lfds611_freelist
if we can, we populate it and add it to the lfds611_queue
if we cannot, then the lfds611_ringbuffer is full
so instead we grab the current read element and
use that instead
dequeue may fail since the lfds611_queue may be emptied
during our dequeue attempt
so what we actually do here is a loop, attempting
the lfds611_freelist and if it fails then a dequeue, until
we obtain an element
once we have an element, we lfds611_queue it
you may be wondering why this operation is in a loop
remember - these operations are lock-free; anything
can happen in between
so for example the pop could fail because the lfds611_freelist
is empty; but by the time we go to get an element from
the lfds611_queue, the whole lfds611_queue has been emptied back into
the lfds611_freelist!
if overwrite_flag is provided, we set it to 0 if we
obtained a new element from the lfds611_freelist, 1 if we
stole an element from the lfds611_queue
*/
do {
if( overwrite_flag != NULL )
*overwrite_flag = 0;
lfds611_freelist_pop( rs->fs, fe );
if( *fe == NULL ) {
lfds611_ringbuffer_get_read_element( rs, fe );
if( overwrite_flag != NULL and *fe != NULL )
*overwrite_flag = 1;
}
} while( *fe == NULL );
return( *fe );
}
/****************************************************************************/
void lfds611_ringbuffer_put_read_element( struct lfds611_ringbuffer_state *rs, struct lfds611_freelist_element *fe )
{
assert( rs != NULL );
assert( fe != NULL );
lfds611_freelist_push( rs->fs, fe );
return;
}
/****************************************************************************/
void lfds611_ringbuffer_put_write_element( struct lfds611_ringbuffer_state *rs, struct lfds611_freelist_element *fe )
{
assert( rs != NULL );
assert( fe != NULL );
lfds611_queue_enqueue( rs->qs, fe );
return;
}
/***** the library wide include file *****/
#include "liblfds611_internal.h"
/***** defines *****/
/***** structures *****/
#pragma pack( push, LFDS611_ALIGN_DOUBLE_POINTER )
struct lfds611_ringbuffer_state {
struct lfds611_queue_state
*qs;
struct lfds611_freelist_state
*fs;
};
#pragma pack( pop )
/***** externs *****/
/***** private prototypes *****/
void lfds611_ringbuffer_internal_validate( struct lfds611_ringbuffer_state *rs, struct lfds611_validation_info *vi, enum lfds611_data_structure_validity *lfds611_queue_validity,
enum lfds611_data_structure_validity *lfds611_freelist_validity );
#include "lfds611_ringbuffer_internal.h"
/****************************************************************************/
int lfds611_ringbuffer_new( struct lfds611_ringbuffer_state **rs, lfds611_atom_t number_elements, int (*user_data_init_function)(void **user_data, void *user_state), void *user_state )
{
int
rv = 0;
assert( rs != NULL );
// TRD : number_elements can be any value in its range
// TRD : user_data_init_function can be NULL
// TRD : user_state can be NULL
*rs = (struct lfds611_ringbuffer_state *) lfds611_liblfds_aligned_malloc( sizeof(struct lfds611_ringbuffer_state), LFDS611_ALIGN_DOUBLE_POINTER );
if( *rs != NULL ) {
lfds611_freelist_new( &(*rs)->fs, number_elements, user_data_init_function, user_state );
if( (*rs)->fs != NULL ) {
lfds611_queue_new( &(*rs)->qs, number_elements );
if( (*rs)->qs != NULL )
rv = 1;
if( (*rs)->qs == NULL ) {
lfds611_liblfds_aligned_free( *rs );
*rs = NULL;
}
}
if( (*rs)->fs == NULL ) {
lfds611_liblfds_aligned_free( *rs );
*rs = NULL;
}
}
LFDS611_BARRIER_STORE;
return( rv );
}
/****************************************************************************/
//#pragma warning( disable : 4100 )
void lfds611_ringbuffer_use( struct lfds611_ringbuffer_state *rs )
{
assert( rs != NULL );
LFDS611_BARRIER_LOAD;
return;
}
//#pragma warning( default : 4100 )
#include "lfds611_ringbuffer_internal.h"
/****************************************************************************/
//#pragma warning( disable : 4100 )
void lfds611_ringbuffer_query( struct lfds611_ringbuffer_state *rs, enum lfds611_ringbuffer_query_type query_type, void *query_input, void *query_output )
{
assert( rs != NULL );
// TRD : query_type can be any value in its range
// TRD : query_input can be NULL
assert( query_output != NULL );
switch( query_type ) {
case LFDS611_RINGBUFFER_QUERY_VALIDATE:
// TRD : query_input can be NULL
lfds611_ringbuffer_internal_validate( rs, (struct lfds611_validation_info *) query_input, (enum lfds611_data_structure_validity *) query_output,
((enum lfds611_data_structure_validity *) query_output)+2 );
break;
}
return;
}
//#pragma warning( default : 4100 )
/****************************************************************************/
void lfds611_ringbuffer_internal_validate( struct lfds611_ringbuffer_state *rs, struct lfds611_validation_info *vi, enum lfds611_data_structure_validity *lfds611_queue_validity,
enum lfds611_data_structure_validity *lfds611_freelist_validity )
{
assert( rs != NULL );
// TRD : vi can be NULL
assert( lfds611_queue_validity != NULL );
assert( lfds611_freelist_validity != NULL );
lfds611_queue_query( rs->qs, LFDS611_QUEUE_QUERY_VALIDATE, vi, lfds611_queue_validity );
if( vi != NULL ) {
struct lfds611_validation_info
lfds611_freelist_vi;
lfds611_atom_t
total_elements;
lfds611_freelist_query( rs->fs, LFDS611_FREELIST_QUERY_ELEMENT_COUNT, NULL, (void *) &total_elements );
lfds611_freelist_vi.min_elements = total_elements - vi->max_elements;
lfds611_freelist_vi.max_elements = total_elements - vi->min_elements;
lfds611_freelist_query( rs->fs, LFDS611_FREELIST_QUERY_VALIDATE, (void *) &lfds611_freelist_vi, (void *) lfds611_freelist_validity );
}
if( vi == NULL )
lfds611_freelist_query( rs->fs, LFDS611_FREELIST_QUERY_VALIDATE, NULL, (void *) lfds611_freelist_validity );
return;
}
#include "lfds611_slist_internal.h"
/****************************************************************************/
void lfds611_slist_delete( struct lfds611_slist_state *ss )
{
lfds611_slist_single_threaded_physically_delete_all_elements( ss );
lfds611_liblfds_aligned_free( ss );
return;
}
/****************************************************************************/
int lfds611_slist_logically_delete_element( struct lfds611_slist_state *ss, struct lfds611_slist_element *se )
{
LFDS611_ALIGN(LFDS611_ALIGN_DOUBLE_POINTER) void
*volatile user_data_and_flags[2],
*volatile new_user_data_and_flags[2];
unsigned char
cas_rv = 0;
assert( ss != NULL );
assert( se != NULL );
LFDS611_BARRIER_LOAD;
user_data_and_flags[LFDS611_SLIST_USER_DATA] = se->user_data_and_flags[LFDS611_SLIST_USER_DATA];
user_data_and_flags[LFDS611_SLIST_FLAGS] = se->user_data_and_flags[LFDS611_SLIST_FLAGS];
do {
new_user_data_and_flags[LFDS611_SLIST_USER_DATA] = user_data_and_flags[LFDS611_SLIST_USER_DATA];
new_user_data_and_flags[LFDS611_SLIST_FLAGS] = (void *) ((lfds611_atom_t) user_data_and_flags[LFDS611_SLIST_FLAGS] | LFDS611_SLIST_FLAG_DELETED);
} while( !((lfds611_atom_t) user_data_and_flags[LFDS611_SLIST_FLAGS] & LFDS611_SLIST_FLAG_DELETED)
and 0 == (cas_rv = lfds611_abstraction_dcas((volatile lfds611_atom_t *) se->user_data_and_flags, (lfds611_atom_t *) new_user_data_and_flags, (lfds611_atom_t *) user_data_and_flags)) );
if( cas_rv == 1 )
if( ss->user_data_delete_function != NULL )
ss->user_data_delete_function( (void *) user_data_and_flags[LFDS611_SLIST_USER_DATA], ss->user_state );
return( cas_rv );
}
/****************************************************************************/
void lfds611_slist_single_threaded_physically_delete_all_elements( struct lfds611_slist_state *ss )
{
struct lfds611_slist_element
*volatile se,
*volatile se_temp;
LFDS611_BARRIER_LOAD;
se = ss->head;
while( se != NULL ) {
// TRD : if a non-deleted element and there is a delete function, call the delete function
if( ss->user_data_delete_function != NULL )
ss->user_data_delete_function( (void *) se->user_data_and_flags[LFDS611_SLIST_USER_DATA], ss->user_state );
se_temp = se;
se = se->next;
lfds611_liblfds_aligned_free( (void *) se_temp );
}
lfds611_slist_internal_init_slist( ss, ss->user_data_delete_function, ss->user_state );
LFDS611_BARRIER_STORE;
return;
}
#include "lfds611_slist_internal.h"
/****************************************************************************/
int lfds611_slist_get_user_data_from_element( struct lfds611_slist_element *se, void **user_data )
{
int
rv = 1;
assert( se != NULL );
assert( user_data != NULL );
LFDS611_BARRIER_LOAD;
*user_data = (void *) se->user_data_and_flags[LFDS611_SLIST_USER_DATA];
if( (lfds611_atom_t) se->user_data_and_flags[LFDS611_SLIST_FLAGS] & LFDS611_SLIST_FLAG_DELETED )
rv = 0;
return( rv );
}
/****************************************************************************/
int lfds611_slist_set_user_data_in_element( struct lfds611_slist_element *se, void *user_data )
{
LFDS611_ALIGN(LFDS611_ALIGN_DOUBLE_POINTER) void
*user_data_and_flags[2],
*new_user_data_and_flags[2];
int
rv = 1;
assert( se != NULL );
// TRD : user_data can be NULL
LFDS611_BARRIER_LOAD;
user_data_and_flags[LFDS611_SLIST_USER_DATA] = se->user_data_and_flags[LFDS611_SLIST_USER_DATA];
user_data_and_flags[LFDS611_SLIST_FLAGS] = se->user_data_and_flags[LFDS611_SLIST_FLAGS];
new_user_data_and_flags[LFDS611_SLIST_USER_DATA] = user_data;
do {
new_user_data_and_flags[LFDS611_SLIST_FLAGS] = user_data_and_flags[LFDS611_SLIST_FLAGS];
} while( !((lfds611_atom_t) user_data_and_flags[LFDS611_SLIST_FLAGS] & LFDS611_SLIST_FLAG_DELETED) and
0 == lfds611_abstraction_dcas((volatile lfds611_atom_t *) se->user_data_and_flags, (lfds611_atom_t *) new_user_data_and_flags, (lfds611_atom_t *) user_data_and_flags) );
if( (lfds611_atom_t) user_data_and_flags[LFDS611_SLIST_FLAGS] & LFDS611_SLIST_FLAG_DELETED )
rv = 0;
LFDS611_BARRIER_STORE;
return( rv );
}
/****************************************************************************/
struct lfds611_slist_element *lfds611_slist_get_head( struct lfds611_slist_state *ss, struct lfds611_slist_element **se )
{
assert( ss != NULL );
assert( se != NULL );
LFDS611_BARRIER_LOAD;
*se = (struct lfds611_slist_element *) ss->head;
lfds611_slist_internal_move_to_first_undeleted_element( se );
return( *se );
}
/****************************************************************************/
struct lfds611_slist_element *lfds611_slist_get_next( struct lfds611_slist_element *se, struct lfds611_slist_element **next_se )
{
assert( se != NULL );
assert( next_se != NULL );
LFDS611_BARRIER_LOAD;
*next_se = (struct lfds611_slist_element *) se->next;
lfds611_slist_internal_move_to_first_undeleted_element( next_se );
return( *next_se );
}
/****************************************************************************/
struct lfds611_slist_element *lfds611_slist_get_head_and_then_next( struct lfds611_slist_state *ss, struct lfds611_slist_element **se )
{
assert( ss != NULL );
assert( se != NULL );
if( *se == NULL )
lfds611_slist_get_head( ss, se );
else
lfds611_slist_get_next( *se, se );
return( *se );
}
/****************************************************************************/
void lfds611_slist_internal_move_to_first_undeleted_element( struct lfds611_slist_element **se )
{
assert( se != NULL );
while( *se != NULL and (lfds611_atom_t) (*se)->user_data_and_flags[LFDS611_SLIST_FLAGS] & LFDS611_SLIST_FLAG_DELETED )
(*se) = (struct lfds611_slist_element *) (*se)->next;
return;
}
/***** the library wide include file *****/
#include "liblfds611_internal.h"
/***** defines *****/
#define LFDS611_SLIST_USER_DATA 0
#define LFDS611_SLIST_FLAGS 1
#define LFDS611_SLIST_NO_FLAGS 0x0
#define LFDS611_SLIST_FLAG_DELETED 0x1
/***** structures *****/
#pragma pack( push, LFDS611_ALIGN_SINGLE_POINTER )
struct lfds611_slist_state {
struct lfds611_slist_element
*volatile head;
void
(*user_data_delete_function)( void *user_data, void *user_state ),
*user_state;
};
#pragma pack( pop )
#pragma pack( push, LFDS611_ALIGN_DOUBLE_POINTER )
/* TRD : this pragma pack doesn't seem to work under Windows
if the structure members are the correct way round
(next first), then user_data_and_flags ends up on
a single pointer boundary and DCAS crashes
accordingly, I've moved user_data_and_flags first
*/
struct lfds611_slist_element {
void
*volatile user_data_and_flags[2];
// TRD : requires volatile as is target of CAS
struct lfds611_slist_element
*volatile next;
};
#pragma pack( pop )
/***** private prototypes *****/
void lfds611_slist_internal_init_slist( struct lfds611_slist_state *ss, void (*user_data_delete_function)(void *user_data, void *user_state), void *user_state );
void lfds611_slist_internal_link_element_to_head( struct lfds611_slist_state *lfds611_slist_state, struct lfds611_slist_element *volatile se );
void lfds611_slist_internal_link_element_after_element( struct lfds611_slist_element *volatile lfds611_slist_in_list_element, struct lfds611_slist_element *volatile se );
void lfds611_slist_internal_move_to_first_undeleted_element( struct lfds611_slist_element **se );
#include "lfds611_slist_internal.h"
/****************************************************************************/
void lfds611_slist_internal_link_element_to_head( struct lfds611_slist_state *ss, struct lfds611_slist_element *volatile se )
{
LFDS611_ALIGN(LFDS611_ALIGN_SINGLE_POINTER) struct lfds611_slist_element
*se_next;
assert( ss != NULL );
assert( se != NULL );
LFDS611_BARRIER_LOAD;
se_next = ss->head;
do {
se->next = se_next;
} while( se->next != (se_next = (struct lfds611_slist_element *) lfds611_abstraction_cas((volatile lfds611_atom_t *) &ss->head, (lfds611_atom_t) se, (lfds611_atom_t) se->next)) );
return;
}
/****************************************************************************/
void lfds611_slist_internal_link_element_after_element( struct lfds611_slist_element *volatile lfds611_slist_in_list_element, struct lfds611_slist_element *volatile se )
{
LFDS611_ALIGN(LFDS611_ALIGN_SINGLE_POINTER) struct lfds611_slist_element
*se_prev,
*se_next;
assert( lfds611_slist_in_list_element != NULL );
assert( se != NULL );
LFDS611_BARRIER_LOAD;
se_prev = (struct lfds611_slist_element *) lfds611_slist_in_list_element;
se_next = se_prev->next;
do {
se->next = se_next;
} while( se->next != (se_next = (struct lfds611_slist_element *) lfds611_abstraction_cas((volatile lfds611_atom_t *) &se_prev->next, (lfds611_atom_t) se, (lfds611_atom_t) se->next)) );
return;
}
#include "lfds611_slist_internal.h"
/****************************************************************************/
int lfds611_slist_new( struct lfds611_slist_state **ss, void (*user_data_delete_function)(void *user_data, void *user_state), void *user_state )
{
int
rv = 0;
assert( ss != NULL );
// TRD : user_data_delete_function can be NULL
// TRD : user_state can be NULL
*ss = (struct lfds611_slist_state *) lfds611_liblfds_aligned_malloc( sizeof(struct lfds611_slist_state), LFDS611_ALIGN_SINGLE_POINTER );
if( *ss != NULL ) {
lfds611_slist_internal_init_slist( *ss, user_data_delete_function, user_state );
rv = 1;
}
LFDS611_BARRIER_STORE;
return( rv );
}
/****************************************************************************/
//#pragma warning( disable : 4100 )
void lfds611_slist_use( struct lfds611_slist_state *ss )
{
assert( ss != NULL );
LFDS611_BARRIER_LOAD;
return;
}
//#pragma warning( default : 4100 )
/****************************************************************************/
void lfds611_slist_internal_init_slist( struct lfds611_slist_state *ss, void (*user_data_delete_function)(void *user_data, void *user_state), void *user_state )
{
assert( ss != NULL );
// TRD : user_data_delete_function can be NULL
// TRD : user_state can be NULL
ss->head = NULL;
ss->user_data_delete_function = user_data_delete_function;
ss->user_state = user_state;
return;
}
/****************************************************************************/
struct lfds611_slist_element *lfds611_slist_new_head( struct lfds611_slist_state *ss, void *user_data )
{
LFDS611_ALIGN(LFDS611_ALIGN_SINGLE_POINTER) struct lfds611_slist_element
*volatile se;
assert( ss != NULL );
// TRD : user_data can be NULL
se = (struct lfds611_slist_element *) lfds611_liblfds_aligned_malloc( sizeof(struct lfds611_slist_element), LFDS611_ALIGN_DOUBLE_POINTER );
if( se != NULL ) {
se->user_data_and_flags[LFDS611_SLIST_USER_DATA] = user_data;
se->user_data_and_flags[LFDS611_SLIST_FLAGS] = LFDS611_SLIST_NO_FLAGS;
lfds611_slist_internal_link_element_to_head( ss, se );
}
return( (struct lfds611_slist_element *) se );
}
/****************************************************************************/
struct lfds611_slist_element *lfds611_slist_new_next( struct lfds611_slist_element *se, void *user_data )
{
LFDS611_ALIGN(LFDS611_ALIGN_SINGLE_POINTER) struct lfds611_slist_element
*volatile se_next;
assert( se != NULL );
// TRD : user_data can be NULL
se_next = (struct lfds611_slist_element *) lfds611_liblfds_aligned_malloc( sizeof(struct lfds611_slist_element), LFDS611_ALIGN_DOUBLE_POINTER );
if( se_next != NULL ) {
se_next->user_data_and_flags[LFDS611_SLIST_USER_DATA] = user_data;
se_next->user_data_and_flags[LFDS611_SLIST_FLAGS] = LFDS611_SLIST_NO_FLAGS;
lfds611_slist_internal_link_element_after_element( se, se_next );
}
return( (struct lfds611_slist_element *) se_next );
}
#include "lfds611_stack_internal.h"
/****************************************************************************/
void lfds611_stack_delete( struct lfds611_stack_state *ss, void (*user_data_delete_function)(void *user_data, void *user_state), void *user_state )
{
void
*user_data;
assert( ss != NULL );
// TRD : user_data_delete_function can be NULL
// TRD : user_state can be NULL
while( lfds611_stack_pop(ss, &user_data) )
if( user_data_delete_function != NULL )
user_data_delete_function( user_data, user_state );
lfds611_freelist_delete( ss->fs, lfds611_stack_internal_freelist_delete_function, NULL );
lfds611_liblfds_aligned_free( ss );
return;
}
/****************************************************************************/
void lfds611_stack_clear( struct lfds611_stack_state *ss, void (*user_data_clear_function)(void *user_data, void *user_state), void *user_state )
{
void
*user_data;
assert( ss != NULL );
// TRD : user_data_clear_function can be NULL
// TRD : user_state can be NULL
while( lfds611_stack_pop(ss, &user_data) )
if( user_data_clear_function != NULL )
user_data_clear_function( user_data, user_state );
return;
}
/****************************************************************************/
//#pragma warning( disable : 4100 )
void lfds611_stack_internal_freelist_delete_function( void *user_data, void *user_state )
{
assert( user_data != NULL );
assert( user_state == NULL );
lfds611_liblfds_aligned_free( user_data );
return;
}
//#pragma warning( default : 4100 )
/***** the library wide include file *****/
#include "liblfds611_internal.h"
/***** pragmas *****/
/***** defines *****/
#define LFDS611_STACK_POINTER 0
#define LFDS611_STACK_COUNTER 1
#define LFDS611_STACK_PAC_SIZE 2
/***** structures *****/
#pragma pack( push, LFDS611_ALIGN_DOUBLE_POINTER )
struct lfds611_stack_state {
// TRD : must come first for alignment
struct lfds611_stack_element
*volatile top[LFDS611_STACK_PAC_SIZE];
lfds611_atom_t
aba_counter;
struct lfds611_freelist_state
*fs;
};
struct lfds611_stack_element {
struct lfds611_stack_element
*next[LFDS611_STACK_PAC_SIZE];
struct lfds611_freelist_element
*fe;
void
*user_data;
};
#pragma pack( pop )
/***** private prototypes *****/
int lfds611_stack_internal_freelist_init_function( void **user_data, void *user_state );
void lfds611_stack_internal_freelist_delete_function( void *user_data, void *user_state );
void lfds611_stack_internal_new_element_from_freelist( struct lfds611_stack_state *ss, struct lfds611_stack_element *se[LFDS611_STACK_PAC_SIZE], void *user_data );
void lfds611_stack_internal_new_element( struct lfds611_stack_state *ss, struct lfds611_stack_element *se[LFDS611_STACK_PAC_SIZE], void *user_data );
void lfds611_stack_internal_init_element( struct lfds611_stack_state *ss, struct lfds611_stack_element *se[LFDS611_STACK_PAC_SIZE], struct lfds611_freelist_element *fe, void *user_data );
void lfds611_stack_internal_push( struct lfds611_stack_state *ss, struct lfds611_stack_element *se[LFDS611_STACK_PAC_SIZE] );
void lfds611_stack_internal_validate( struct lfds611_stack_state *ss, struct lfds611_validation_info *vi, enum lfds611_data_structure_validity *stack_validity,
enum lfds611_data_structure_validity *freelist_validity );
#include "lfds611_stack_internal.h"
/****************************************************************************/
int lfds611_stack_new( struct lfds611_stack_state **ss, lfds611_atom_t number_elements )
{
int
rv = 0;
assert( ss != NULL );
// TRD : number_elements can be any value in its range
*ss = (struct lfds611_stack_state *) lfds611_liblfds_aligned_malloc( sizeof(struct lfds611_stack_state), LFDS611_ALIGN_DOUBLE_POINTER );
if( *ss != NULL ) {
// TRD : the size of the lfds611_freelist is the size of the lfds611_stack
lfds611_freelist_new( &(*ss)->fs, number_elements, lfds611_stack_internal_freelist_init_function, NULL );
if( (*ss)->fs == NULL ) {
lfds611_liblfds_aligned_free( *ss );
*ss = NULL;
}
if( (*ss)->fs != NULL ) {
(*ss)->top[LFDS611_STACK_POINTER] = NULL;
(*ss)->top[LFDS611_STACK_COUNTER] = 0;
(*ss)->aba_counter = 0;
rv = 1;
}
}
LFDS611_BARRIER_STORE;
return( rv );
}
/****************************************************************************/
//#pragma warning( disable : 4100 )
void lfds611_stack_use( struct lfds611_stack_state *ss )
{
assert( ss != NULL );
LFDS611_BARRIER_LOAD;
return;
}
//#pragma warning( default : 4100 )
/****************************************************************************/
//#pragma warning( disable : 4100 )
int lfds611_stack_internal_freelist_init_function( void **user_data, void *user_state )
{
int
rv = 0;
assert( user_data != NULL );
assert( user_state == NULL );
*user_data = lfds611_liblfds_aligned_malloc( sizeof(struct lfds611_stack_element), LFDS611_ALIGN_DOUBLE_POINTER );
if( *user_data != NULL )
rv = 1;
return( rv );
}
//#pragma warning( default : 4100 )
/****************************************************************************/
void lfds611_stack_internal_new_element_from_freelist( struct lfds611_stack_state *ss, struct lfds611_stack_element *se[LFDS611_STACK_PAC_SIZE], void *user_data )
{
struct lfds611_freelist_element
*fe;
assert( ss != NULL );
assert( se != NULL );
// TRD : user_data can be any value in its range
lfds611_freelist_pop( ss->fs, &fe );
if( fe == NULL )
se[LFDS611_STACK_POINTER] = NULL;
if( fe != NULL )
lfds611_stack_internal_init_element( ss, se, fe, user_data );
return;
}
/****************************************************************************/
void lfds611_stack_internal_new_element( struct lfds611_stack_state *ss, struct lfds611_stack_element *se[LFDS611_STACK_PAC_SIZE], void *user_data )
{
struct lfds611_freelist_element
*fe;
assert( ss != NULL );
assert( se != NULL );
// TRD : user_data can be any value in its range
lfds611_freelist_guaranteed_pop( ss->fs, &fe );
if( fe == NULL )
se[LFDS611_STACK_POINTER] = NULL;
if( fe != NULL )
lfds611_stack_internal_init_element( ss, se, fe, user_data );
return;
}
/****************************************************************************/
void lfds611_stack_internal_init_element( struct lfds611_stack_state *ss, struct lfds611_stack_element *se[LFDS611_STACK_PAC_SIZE], struct lfds611_freelist_element *fe, void *user_data )
{
assert( ss != NULL );
assert( se != NULL );
assert( fe != NULL );
// TRD : user_data can be any value in its range
lfds611_freelist_get_user_data_from_element( fe, (void **) &se[LFDS611_STACK_POINTER] );
se[LFDS611_STACK_COUNTER] = (struct lfds611_stack_element *) lfds611_abstraction_increment( (lfds611_atom_t *) &ss->aba_counter );
se[LFDS611_STACK_POINTER]->next[LFDS611_STACK_POINTER] = NULL;
se[LFDS611_STACK_POINTER]->next[LFDS611_STACK_COUNTER] = 0;
se[LFDS611_STACK_POINTER]->fe = fe;
se[LFDS611_STACK_POINTER]->user_data = user_data;
return;
}
#include "lfds611_stack_internal.h"
/****************************************************************************/
int lfds611_stack_push( struct lfds611_stack_state *ss, void *user_data )
{
LFDS611_ALIGN(LFDS611_ALIGN_DOUBLE_POINTER) struct lfds611_stack_element
*se[LFDS611_STACK_PAC_SIZE];
assert( ss != NULL );
// TRD : user_data can be NULL
lfds611_stack_internal_new_element_from_freelist( ss, se, user_data );
if( se[LFDS611_STACK_POINTER] == NULL )
return( 0 );
lfds611_stack_internal_push( ss, se );
return( 1 );
}
/****************************************************************************/
int lfds611_stack_guaranteed_push( struct lfds611_stack_state *ss, void *user_data )
{
LFDS611_ALIGN(LFDS611_ALIGN_DOUBLE_POINTER) struct lfds611_stack_element
*se[LFDS611_STACK_PAC_SIZE];
assert( ss != NULL );
// TRD : user_data can be NULL
/* TRD : this function allocated a new lfds611_freelist element and uses that
to push onto the lfds611_stack, guaranteeing success (unless malloc()
fails of course)
*/
lfds611_stack_internal_new_element( ss, se, user_data );
// TRD : malloc failed
if( se[LFDS611_STACK_POINTER] == NULL )
return( 0 );
lfds611_stack_internal_push( ss, se );
return( 1 );
}
/****************************************************************************/
void lfds611_stack_internal_push( struct lfds611_stack_state *ss, struct lfds611_stack_element *se[LFDS611_STACK_PAC_SIZE] )
{
LFDS611_ALIGN(LFDS611_ALIGN_DOUBLE_POINTER) struct lfds611_stack_element
*original_se_next[LFDS611_STACK_PAC_SIZE];
assert( ss != NULL );
assert( se != NULL );
LFDS611_BARRIER_LOAD;
original_se_next[LFDS611_STACK_POINTER] = ss->top[LFDS611_STACK_POINTER];
original_se_next[LFDS611_STACK_COUNTER] = ss->top[LFDS611_STACK_COUNTER];
do {
se[LFDS611_STACK_POINTER]->next[LFDS611_STACK_POINTER] = original_se_next[LFDS611_STACK_POINTER];
se[LFDS611_STACK_POINTER]->next[LFDS611_STACK_COUNTER] = original_se_next[LFDS611_STACK_COUNTER];
} while( 0 == lfds611_abstraction_dcas((volatile lfds611_atom_t *) ss->top, (lfds611_atom_t *) se, (lfds611_atom_t *) original_se_next) );
return;
}
/****************************************************************************/
int lfds611_stack_pop( struct lfds611_stack_state *ss, void **user_data )
{
LFDS611_ALIGN(LFDS611_ALIGN_DOUBLE_POINTER) struct lfds611_stack_element
*se[LFDS611_STACK_PAC_SIZE];
assert( ss != NULL );
assert( user_data != NULL );
LFDS611_BARRIER_LOAD;
se[LFDS611_STACK_COUNTER] = ss->top[LFDS611_STACK_COUNTER];
se[LFDS611_STACK_POINTER] = ss->top[LFDS611_STACK_POINTER];
do {
if( se[LFDS611_STACK_POINTER] == NULL )
return( 0 );
} while( 0 == lfds611_abstraction_dcas((volatile lfds611_atom_t *) ss->top, (lfds611_atom_t *) se[LFDS611_STACK_POINTER]->next, (lfds611_atom_t *) se) );
*user_data = se[LFDS611_STACK_POINTER]->user_data;
lfds611_freelist_push( ss->fs, se[LFDS611_STACK_POINTER]->fe );
return( 1 );
}
#include "lfds611_stack_internal.h"
/****************************************************************************/
void lfds611_stack_query( struct lfds611_stack_state *ss, enum lfds611_stack_query_type query_type, void *query_input, void *query_output )
{
assert( ss != NULL );
// TRD : query_type can be any value in its range
// TRD : query_iput can be NULL
assert( query_output != NULL );
LFDS611_BARRIER_LOAD;
switch( query_type ) {
case LFDS611_STACK_QUERY_ELEMENT_COUNT:
assert( query_input == NULL );
lfds611_freelist_query( ss->fs, LFDS611_FREELIST_QUERY_ELEMENT_COUNT, NULL, query_output );
break;
case LFDS611_STACK_QUERY_VALIDATE:
// TRD : query_input can be NULL
/* TRD : the validation info passed in is for the stack
it indicates the minimum and maximum number of elements
which should be present
we need to validate the freelist
and validate the stack
we cannot know the min/max for the freelist, given only
the min/max for the stack
*/
lfds611_freelist_query( ss->fs, LFDS611_FREELIST_QUERY_VALIDATE, NULL, (enum lfds611_data_structure_validity *) query_output );
if( *(enum lfds611_data_structure_validity *) query_output == LFDS611_VALIDITY_VALID )
lfds611_stack_internal_validate( ss, (struct lfds611_validation_info *) query_input, (enum lfds611_data_structure_validity *) query_output, ((enum lfds611_data_structure_validity *) query_output)+1 );
break;
}
return;
}
/****************************************************************************/
void lfds611_stack_internal_validate( struct lfds611_stack_state *ss, struct lfds611_validation_info *vi, enum lfds611_data_structure_validity *stack_validity,
enum lfds611_data_structure_validity *freelist_validity )
{
struct lfds611_stack_element
*se,
*se_slow,
*se_fast;
lfds611_atom_t
element_count = 0,
total_elements;
struct lfds611_validation_info
freelist_vi;
assert( ss != NULL );
// TRD : vi can be NULL
assert( stack_validity != NULL );
*stack_validity = LFDS611_VALIDITY_VALID;
se_slow = se_fast = (struct lfds611_stack_element *) ss->top[LFDS611_STACK_POINTER];
/* TRD : first, check for a loop
we have two pointers
both of which start at the top of the stack
we enter a loop
and on each iteration
we advance one pointer by one element
and the other by two
we exit the loop when both pointers are NULL
(have reached the end of the stack)
or
if we fast pointer 'sees' the slow pointer
which means we have a loop
*/
if( se_slow != NULL )
do {
se_slow = se_slow->next[LFDS611_STACK_POINTER];
if( se_fast != NULL )
se_fast = se_fast->next[LFDS611_STACK_POINTER];
if( se_fast != NULL )
se_fast = se_fast->next[LFDS611_STACK_POINTER];
} while( se_slow != NULL and se_fast != se_slow );
if( se_fast != NULL and se_slow != NULL and se_fast == se_slow )
*stack_validity = LFDS611_VALIDITY_INVALID_LOOP;
/* TRD : now check for expected number of elements
vi can be NULL, in which case we do not check
we know we don't have a loop from our earlier check
*/
if( *stack_validity == LFDS611_VALIDITY_VALID and vi != NULL ) {
se = (struct lfds611_stack_element *) ss->top[LFDS611_STACK_POINTER];
while( se != NULL ) {
element_count++;
se = (struct lfds611_stack_element *) se->next[LFDS611_STACK_POINTER];
}
if( element_count < vi->min_elements )
*stack_validity = LFDS611_VALIDITY_INVALID_MISSING_ELEMENTS;
if( element_count > vi->max_elements )
*stack_validity = LFDS611_VALIDITY_INVALID_ADDITIONAL_ELEMENTS;
}
/* TRD : now we validate the freelist
we may be able to check for the expected number of
elements in the freelist
if the caller has given us an expected min and max
number of elements in the stack, then the total number
of elements in the freelist, minus that min and max,
gives us the expected number of elements in the
freelist
*/
if( vi != NULL ) {
lfds611_freelist_query( ss->fs, LFDS611_FREELIST_QUERY_ELEMENT_COUNT, NULL, (void *) &total_elements );
freelist_vi.min_elements = total_elements - vi->max_elements;
freelist_vi.max_elements = total_elements - vi->min_elements;
lfds611_freelist_query( ss->fs, LFDS611_FREELIST_QUERY_VALIDATE, (void *) &freelist_vi, (void *) freelist_validity );
}
if( vi == NULL )
lfds611_freelist_query( ss->fs, LFDS611_FREELIST_QUERY_VALIDATE, NULL, (void *) freelist_validity );
return;
}
/***** public prototypes *****/
#include "liblfds611.h"
/***** defines *****/
#define and &&
#define or ||
#define RAISED 1
#define LOWERED 0
#define NO_FLAGS 0x0
/***** private prototypes *****/
void *lfds611_liblfds_aligned_malloc( size_t size, size_t align_in_bytes );
void lfds611_liblfds_aligned_free( void *memory );
static LFDS611_INLINE lfds611_atom_t lfds611_abstraction_cas( volatile lfds611_atom_t *destination, lfds611_atom_t exchange, lfds611_atom_t compare );
static LFDS611_INLINE unsigned char lfds611_abstraction_dcas( volatile lfds611_atom_t *destination, lfds611_atom_t *exchange, lfds611_atom_t *compare );
static LFDS611_INLINE lfds611_atom_t lfds611_abstraction_increment( volatile lfds611_atom_t *value );
/***** inlined code *****/
#include "lfds611_abstraction/lfds611_abstraction_cas.c"
#include "lfds611_abstraction/lfds611_abstraction_dcas.c"
#include "lfds611_abstraction/lfds611_abstraction_increment.c"
building test
=============
Windows (user-mode)
===================
1. Use Microsoft Visual Studio 2008 or Visual C++ 2008 Express Edition (or
later versions) to load "liblfds.sln". The "Win32" platform is x86,
the "x64" platform is x64. The test programme provides the "Release"
and "Debug" targets. The other targets ("Release DLL", "Release Lib",
"Debug DLL" and "Debug Lib") are carried over will-nilly from the liblds
library.
All builds will work, but DLL builds will require the DLL from liblfds
to be placed into a location where the test executable can find it (e.g.
the same directory).
2. Use Microsoft Windows SDK and GNUmake to run makefile.windows (obviously
you'll need to have run setenv.bat or the appropriate vcvars*.bat first;
you can build for x64/64-bit and x86/32-bit - just run the correct batch
file).
If liblfds has been built as a DLL, the DLL from liblfds needs to be
placed into a location where the test executable can find it (e.g. the
same directory).
Targets are "rel", "dbg" and "clean". You need to clean between switching
targets.
Windows (kernel)
================
No build supported, since this is a command line utility.
Linux
=====
Use GNUmake to run "makefile.linux". Targets are "rel", "dbg" and
"clean". You need to clean between switching targets.
If liblfds has been built as a shared object, the shared object file from
liblfds will need to be placed somewhere the text executable can find it.
A convenient solution is to place the shared object file in the same
directory as the text executable and set the environment variable
"LD_LIBRARY_PATH" to ".", e.g. in bash;
export LD_LIBRARY_PATH=.
Remember to unset after finishing testing, or your system will continue
to scan the current directory for shared object files.
unset LD_LIBRARY_PATH
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment