Compare commits

...

5 commits

Author SHA1 Message Date
Imbus
e27ef8ca49 cjson ignore and comment 2025-11-03 03:10:44 +01:00
Imbus
8661c89633 Atomics demo 2025-11-03 03:08:07 +01:00
Imbus
e1bd06eb69 winsz comments 2025-11-03 03:06:58 +01:00
Imbus
319cfe0dbb Helpful comment in fir2 2025-11-03 03:06:44 +01:00
Imbus
dd24ae4c07 config_ini: header guards in conf.h 2025-11-03 03:06:29 +01:00
7 changed files with 73 additions and 7 deletions

43
atomics.c Normal file
View file

@ -0,0 +1,43 @@
#include <stdatomic.h>
#include <stdio.h>
#include <threads.h>
atomic_int x; // _Atomic int
int thread1(void *arg) {
(void)arg;
printf("Thread 1: Sleeping for 1.5 seconds\n");
thrd_sleep(&(struct timespec){.tv_sec = 1, .tv_nsec = 500000000}, NULL);
printf("Thread 1: Setting x to 3490\n");
x = 3490;
printf("Thread 1: Exiting\n");
return 0;
}
int thread2(void *arg) {
(void)arg;
printf("Thread 2: Waiting for 3490\n");
while (x != 3490); // spin here
printf("Thread 2: Got 3490--exiting!\n");
return 0;
}
int main(void) {
x = 0;
thrd_t t1, t2;
thrd_create(&t1, thread1, NULL);
thrd_create(&t2, thread2, NULL);
thrd_join(t1, NULL);
thrd_join(t2, NULL);
printf("Main : Threads are done, so x better be 3490\n");
printf("Main : And indeed, x == %d\n", x);
}

1
cjson/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
cjson*

View file

@ -1,12 +1,22 @@
CC ?= gcc CC ?= cc
CFLAGS ?= -Wall -O2 -lcjson CFLAGS ?= -Wall -O2 -I.
LDFLAGS ?=
TARGET = main.elf TARGET = main.elf
SRC = main.c SRC = main.c ./cjson/cJSON.c
$(TARGET): $(SRC) $(TARGET): $(SRC)
@echo CC $@ @echo CC $@
@$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) @$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
CJSON_URL = https://github.com/DaveGamble/cJSON/archive/refs/heads/master.tar.gz
CJSON_TAR = cjson.tar.gz
CJSON_DIR = cJSON-master
cjson:
curl -L $(CJSON_URL) -o $(CJSON_TAR)
tar -xzf $(CJSON_TAR)
mv ./cJSON-master ./cjson
clean: clean:
rm -f $(TARGET) rm -f $(TARGET)

View file

@ -1,5 +1,6 @@
#include <cjson/cJSON.h> #include "cjson/cJSON.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
char *build_set_level_command(int level) { char *build_set_level_command(int level) {
cJSON *root = cJSON_CreateObject(); cJSON *root = cJSON_CreateObject();
@ -9,10 +10,12 @@ char *build_set_level_command(int level) {
// char *message = cJSON_PrintUnformatted(root); // char *message = cJSON_PrintUnformatted(root);
char *msg2 = cJSON_Print(root); char *msg2 = cJSON_Print(root);
cJSON_Delete(root); cJSON_Delete(root);
return msg2; // Remember to free this when done! return msg2;
} }
int main(void) { int main(void) {
printf("%s\n", build_set_level_command(10)); char *msg = build_set_level_command(10);
printf("%s\n", msg);
free(msg);
return 0; return 0;
} }

View file

@ -1,3 +1,6 @@
#ifndef CONF_H
#define CONF_H
typedef struct ConfigEntry { typedef struct ConfigEntry {
char *key; char *key;
char *value; char *value;
@ -11,3 +14,5 @@ typedef struct {
Config *config_load(const char *filename); Config *config_load(const char *filename);
const char *config_get(Config *cfg, const char *key); const char *config_get(Config *cfg, const char *key);
void config_free(Config *cfg); void config_free(Config *cfg);
#endif // CONF_H

2
fir2.c
View file

@ -6,7 +6,7 @@
/* This needs to be a power of two for the bitmask optimizations to work */ /* This needs to be a power of two for the bitmask optimizations to work */
/* If a non power of two length is desired, change to modulo */ /* If a non power of two length is desired, change to modulo */
#define FIR_LEN (1u << 5) #define FIR_LEN (1u << 5) // = 32
_Static_assert((FIR_LEN & (FIR_LEN - 1u)) == 0, "FIR_LEN must be a power of two"); _Static_assert((FIR_LEN & (FIR_LEN - 1u)) == 0, "FIR_LEN must be a power of two");
typedef struct { typedef struct {

View file

@ -5,6 +5,10 @@
int main(void) { int main(void) {
struct winsize w; struct winsize w;
/* See: man 2 ioctl */
/* See: man 2 TIOCGWINSZ */
/* See: linux/fs/ioctl.c */
/* See: linux/drivers/tty/tty_io.c (L: ~2709 and ~2359) */
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == -1) { if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == -1) {
perror("ioctl"); perror("ioctl");
return 1; return 1;