diff --git a/.gitignore b/.gitignore index c44b26b..0adca75 100644 --- a/.gitignore +++ b/.gitignore @@ -97,6 +97,7 @@ /examples/printf_fmt /examples/printf_str /examples/printf_str_va +/examples/spinlock /examples/units_human /tests/multiboot2_header_print1 diff --git a/README.md b/README.md index 6bf2612..551e903 100644 --- a/README.md +++ b/README.md @@ -59,10 +59,12 @@ zero). Work-in-progress APIs can change at any time. * [Example](/examples/generic_mutex.c) * Algorithms * [Free list memory allocator](/include/kernaux/free_list.h) (*non-breaking since* **0.5.0**) - * [Simple command line parser](/include/kernaux/cmdline.h) (*non-breaking since* **0.2.0**) - * [Example](/examples/cmdline.c) * [Page Frame Allocator](/include/kernaux/pfa.h) (*work in progress*) * [Example](/examples/pfa.c) + * [Simple command line parser](/include/kernaux/cmdline.h) (*non-breaking since* **0.2.0**) + * [Example](/examples/cmdline.c) + * Spinlock + * [Example](/examples/spinlock.c) * Data formats * [ELF](/include/kernaux/elf.h) (*work in progress*) * [MBR](/include/kernaux/mbr.h) (*work in progress*) diff --git a/examples/Makefile.am b/examples/Makefile.am index 29e32d4..604a535 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -185,6 +185,18 @@ printf_str_va_LDADD = $(top_builddir)/libkernaux.la printf_str_va_SOURCES = main.c printf_str_va.c endif +############ +# spinlock # +############ + +if WITH_SPINLOCK +if ENABLE_CHECKS_PTHREADS +TESTS += spinlock +spinlock_LDADD = $(top_builddir)/libkernaux.la -lpthread +spinlock_SOURCES = main.c spinlock.c +endif +endif + ############### # units_human # ############### diff --git a/examples/spinlock.c b/examples/spinlock.c new file mode 100644 index 0000000..93233d2 --- /dev/null +++ b/examples/spinlock.c @@ -0,0 +1,32 @@ +#include + +#include +#include +#include + +#define THREADS_COUNT 10 +#define INCR_COUNT 10000000 +#define TOTAL_COUNT (THREADS_COUNT * INCR_COUNT) + +static unsigned long long count = 0; + +static void *work(void *const arg KERNAUX_UNUSED) +{ + for (unsigned long i = 0; i < INCR_COUNT; ++i) { + ++count; + } + + return NULL; +} + +void example_main() +{ + pthread_t threads[THREADS_COUNT]; + for (unsigned long i = 0; i < THREADS_COUNT; ++i) { + assert(!pthread_create(&threads[i], NULL, work, NULL)); + } + for (unsigned long i = 0; i < THREADS_COUNT; ++i) { + assert(!pthread_join(threads[i], NULL)); + } + assert(count == TOTAL_COUNT); +}