mirror of
https://github.com/tailix/libkernaux.git
synced 2025-06-23 18:42:03 -04:00
Add an example
This commit is contained in:
parent
e1a96e2b45
commit
57ef5ac19b
4 changed files with 49 additions and 2 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -97,6 +97,7 @@
|
||||||
/examples/printf_fmt
|
/examples/printf_fmt
|
||||||
/examples/printf_str
|
/examples/printf_str
|
||||||
/examples/printf_str_va
|
/examples/printf_str_va
|
||||||
|
/examples/spinlock
|
||||||
/examples/units_human
|
/examples/units_human
|
||||||
|
|
||||||
/tests/multiboot2_header_print1
|
/tests/multiboot2_header_print1
|
||||||
|
|
|
@ -59,10 +59,12 @@ zero). Work-in-progress APIs can change at any time.
|
||||||
* [Example](/examples/generic_mutex.c)
|
* [Example](/examples/generic_mutex.c)
|
||||||
* Algorithms
|
* Algorithms
|
||||||
* [Free list memory allocator](/include/kernaux/free_list.h) (*non-breaking since* **0.5.0**)
|
* [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*)
|
* [Page Frame Allocator](/include/kernaux/pfa.h) (*work in progress*)
|
||||||
* [Example](/examples/pfa.c)
|
* [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
|
* Data formats
|
||||||
* [ELF](/include/kernaux/elf.h) (*work in progress*)
|
* [ELF](/include/kernaux/elf.h) (*work in progress*)
|
||||||
* [MBR](/include/kernaux/mbr.h) (*work in progress*)
|
* [MBR](/include/kernaux/mbr.h) (*work in progress*)
|
||||||
|
|
|
@ -185,6 +185,18 @@ printf_str_va_LDADD = $(top_builddir)/libkernaux.la
|
||||||
printf_str_va_SOURCES = main.c printf_str_va.c
|
printf_str_va_SOURCES = main.c printf_str_va.c
|
||||||
endif
|
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 #
|
# units_human #
|
||||||
###############
|
###############
|
||||||
|
|
32
examples/spinlock.c
Normal file
32
examples/spinlock.c
Normal 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);
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue