1
0
Fork 0
mirror of https://github.com/tailix/libkernaux.git synced 2025-06-16 18:41:45 -04:00

Add an example

This commit is contained in:
Alex Kotov 2022-12-12 02:14:17 +04:00
parent e1a96e2b45
commit 57ef5ac19b
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
4 changed files with 49 additions and 2 deletions

1
.gitignore vendored
View file

@ -97,6 +97,7 @@
/examples/printf_fmt
/examples/printf_str
/examples/printf_str_va
/examples/spinlock
/examples/units_human
/tests/multiboot2_header_print1

View file

@ -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*)

View file

@ -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 #
###############

32
examples/spinlock.c Normal file
View file

@ -0,0 +1,32 @@
#include <kernaux/macro.h>
#include <assert.h>
#include <pthread.h>
#include <stddef.h>
#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);
}