lockfree

lockfree.git
git clone git://git.lenczewski.org/lockfree.git
Log | Files | Refs | README | LICENSE

commit f0af480fe89fa1c6b1c28d39fc9e28f88f5bb907
parent 44e93ef894e6ab476848dcdb8b6dac6569c1b020
Author: MikoĊ‚aj Lenczewski <mblenczewski@gmail.com>
Date:   Thu, 14 Aug 2025 00:33:26 +0100

Added dummy implementations

Diffstat:
Mbuild.sh | 7++++---
Mc/lockfree.c | 66+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
Ac/lru.h | 20++++++++++++++++++++
Ac/queue.h | 20++++++++++++++++++++
Ac/ring.h | 20++++++++++++++++++++
Ac/stack.h | 20++++++++++++++++++++
Acommon/benchmark.h | 25+++++++++++++++++++++++++
Mcpp/lockfree.cpp | 64+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
Acpp/lru.hpp | 27+++++++++++++++++++++++++++
Acpp/queue.hpp | 27+++++++++++++++++++++++++++
Acpp/ring.hpp | 27+++++++++++++++++++++++++++
Acpp/stack.hpp | 28++++++++++++++++++++++++++++
12 files changed, 346 insertions(+), 5 deletions(-)

diff --git a/build.sh b/build.sh @@ -3,15 +3,16 @@ CC="${CC:-clang}" CXX="${CXX:-clang++}" -WARNINGS="-Wall -Wextra ${WERROR:+-Werror} -Wno-unused-parameter" +WARNINGS="-Wall -Wextra ${WERROR:+-Werror} -Wno-missing-field-initializers -Wno-unused-parameter" CFLAGS="-std=c11 -g3" CXXFLAGS="-std=c++20 -g3" OPTFLAGS="-O${OPT:-0}" +FLAGS="-Icommon $OPTFLAGS" set -ex mkdir -p bin -$CC -o bin/lockfree c/lockfree.c $WARNINGS $CFLAGS $OPTFLAGS -$CXX -o bin/lockfree-cpp cpp/lockfree.cpp $WARNINGS $CXXFLAGS $OPTFLAGS +$CC -o bin/lockfree c/lockfree.c $WARNINGS $CFLAGS $FLAGS +$CXX -o bin/lockfree-cpp cpp/lockfree.cpp $WARNINGS $CXXFLAGS $FLAGS diff --git a/c/lockfree.c b/c/lockfree.c @@ -1,5 +1,69 @@ +#include <stdio.h> +#include <stdlib.h> + +#include "stack.h" +#include "queue.h" +#include "ring.h" +#include "lru.h" + +struct opts { + size_t threads; +}; + +#include <getopt.h> + +static int +parse_opts(int argc, char **argv, struct opts *opts) +{ + opts->threads = 4; + + int opt; + while ((opt = getopt(argc, argv, "ht:")) != -1) { + switch (opt) { + case 't': + if ((opts->threads = strtoull(optarg, NULL, 0)) == 0) + return -1; + break; + + default: + return -1; + } + } + + return 0; +} + +static void +usage(char **argv) +{ + fprintf(stderr, "Usage: %s [-t <threads>]\n", argv[0]); +} + +static void +bench(struct opts const *opts, struct benchmark benchmark) +{ + printf("%s - %zu threads:\n", benchmark.name, opts->threads); + printf("=========================\n"); + + printf("results:\n"); + printf("=========================\n"); + + printf("\n"); +} + int main(int argc, char **argv) { - return 0; + struct opts opts; + if (parse_opts(argc, argv, &opts) < 0) { + usage(argv); + exit(EXIT_FAILURE); + } + + bench(&opts, lf_stack_benchmark()); + bench(&opts, lf_queue_benchmark()); + bench(&opts, lf_ring_benchmark()); + bench(&opts, lf_lru_benchmark()); + + exit(EXIT_SUCCESS); } diff --git a/c/lru.h b/c/lru.h @@ -0,0 +1,20 @@ +#ifndef LRU_H +#define LRU_H + +#include <stdatomic.h> +#include <stddef.h> +#include <stdint.h> + +#include "benchmark.h" + +struct lf_lru { + int foo; +}; + +static inline struct benchmark +lf_lru_benchmark(void) +{ + return (struct benchmark) {"Lock-free LRU Cache"}; +} + +#endif /* LRU_H */ diff --git a/c/queue.h b/c/queue.h @@ -0,0 +1,20 @@ +#ifndef QUEUE_H +#define QUEUE_H + +#include <stdatomic.h> +#include <stddef.h> +#include <stdint.h> + +#include "benchmark.h" + +struct lf_queue { + int foo; +}; + +static inline struct benchmark +lf_queue_benchmark(void) +{ + return (struct benchmark) {"Lock-free Queue"}; +} + +#endif /* QUEUE_H */ diff --git a/c/ring.h b/c/ring.h @@ -0,0 +1,20 @@ +#ifndef RING_H +#define RING_H + +#include <stdatomic.h> +#include <stddef.h> +#include <stdint.h> + +#include "benchmark.h" + +struct lf_ring { + int foo; +}; + +static inline struct benchmark +lf_ring_benchmark(void) +{ + return (struct benchmark) {"Lock-free Ring Buffer"}; +} + +#endif /* RING_H */ diff --git a/c/stack.h b/c/stack.h @@ -0,0 +1,20 @@ +#ifndef STACK_H +#define STACK_H + +#include <stdatomic.h> +#include <stddef.h> +#include <stdint.h> + +#include "benchmark.h" + +struct lf_stack { + int foo; +}; + +static inline struct benchmark +lf_stack_benchmark(void) +{ + return (struct benchmark) {"Lock-free Stack"}; +} + +#endif /* STACK_H */ diff --git a/common/benchmark.h b/common/benchmark.h @@ -0,0 +1,25 @@ +#ifndef BENCHMARK_H +#define BENCHMARK_H + +#include <stdint.h> + +#ifdef __cplusplus +extern "C" { +#endif + +typedef void *(benchmark_init_t)(size_t threads); +typedef void (benchmark_free_t)(void *state); +typedef int (benchmark_work_t)(void *state, size_t thread_id); + +struct benchmark { + char const *name; + benchmark_init_t *init; + benchmark_free_t *free; + benchmark_work_t *work; +}; + +#ifdef __cplusplus +}; +#endif + +#endif /* BENCHMARK_H */ diff --git a/cpp/lockfree.cpp b/cpp/lockfree.cpp @@ -1,5 +1,67 @@ +#include <cstdio> +#include <cstdlib> + +#include "stack.hpp" +#include "queue.hpp" +#include "ring.hpp" +#include "lru.hpp" + +struct opts { + size_t threads = 4; +}; + +#include <getopt.h> + +static int +parse_opts(int argc, char **argv, opts &opts) +{ + int opt; + while ((opt = getopt(argc, argv, "ht:")) != -1) { + switch (opt) { + case 't': + if ((opts.threads = strtoull(optarg, NULL, 0)) == 0) + return -1; + break; + + default: + return -1; + } + } + + return 0; +} + +static void +usage(char **argv) +{ + fprintf(stderr, "Usage: %s [-t <threads>]\n", argv[0]); +} + +static void +bench(const opts &opts, const struct benchmark &benchmark) +{ + printf("%s - %zu threads:\n", benchmark.name, opts.threads); + printf("=========================\n"); + + printf("results:\n"); + printf("=========================\n"); + + printf("\n"); +} + int main(int argc, char **argv) { - return 0; + struct opts opts{}; + if (parse_opts(argc, argv, opts) < 0) { + usage(argv); + exit(EXIT_FAILURE); + } + + bench(opts, lf::stack_benchmark()); + bench(opts, lf::queue_benchmark()); + bench(opts, lf::ring_benchmark()); + bench(opts, lf::lru_benchmark()); + + exit(EXIT_SUCCESS); } diff --git a/cpp/lru.hpp b/cpp/lru.hpp @@ -0,0 +1,27 @@ +#ifndef LRU_HPP +#define LRU_HPP + +#include <atomic> +#include <cstddef> +#include <cstdint> + +#include "benchmark.h" + +namespace lf { + +template <typename T> +struct lru { +private: + struct entry { + }; +}; + +struct benchmark +lru_benchmark(void) +{ + return {"Lock-free LRU Cache"}; +} + +}; + +#endif /* LRU_HPP */ diff --git a/cpp/queue.hpp b/cpp/queue.hpp @@ -0,0 +1,27 @@ +#ifndef QUEUE_HPP +#define QUEUE_HPP + +#include <atomic> +#include <cstddef> +#include <cstdint> + +#include "benchmark.h" + +namespace lf { + +template <typename T> +struct queue { +private: + T *buffer; + size_t cap, cur; +}; + +struct benchmark +queue_benchmark(void) +{ + return {"Lock-free Queue"}; +} + +}; + +#endif /* QUEUE_HPP */ diff --git a/cpp/ring.hpp b/cpp/ring.hpp @@ -0,0 +1,27 @@ +#ifndef RING_HPP +#define RING_HPP + +#include <atomic> +#include <cstddef> +#include <cstdint> + +#include "benchmark.h" + +namespace lf { + +template <typename T> +struct ring { +private: + T *buffer; + size_t cap, cur; +}; + +struct benchmark +ring_benchmark(void) +{ + return {"Lock-free Ring Buffer"}; +} + +}; + +#endif /* RING_HPP */ diff --git a/cpp/stack.hpp b/cpp/stack.hpp @@ -0,0 +1,28 @@ +#ifndef STACK_HPP +#define STACK_HPP + +#include <atomic> +#include <cstddef> +#include <cstdint> + +#include "benchmark.h" + +namespace lf { + +template <typename T> +struct stack { + +private: + T *buffer; + size_t cap, cur; +}; + +struct benchmark +stack_benchmark(void) +{ + return {"Lock-free Stack"}; +} + +}; + +#endif /* STACK_HPP */