Created
April 29, 2026 19:54
-
-
Save imaami/cd10d5782eb654e9698a39a0f8cf82ae to your computer and use it in GitHub Desktop.
Hell no, world
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <pthread.h> | |
| #include <stdatomic.h> | |
| #include <stddef.h> | |
| #include <stdint.h> | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #define len(x) (sizeof (x) / sizeof (x)[0]) | |
| static void *run (void *a); | |
| #define HELL "Hello, World!" | |
| static struct { | |
| pthread_t tid[sizeof HELL-1]; | |
| pthread_barrier_t bar; | |
| _Atomic(uint16_t) ctr; | |
| uint16_t there[sizeof HELL-1]; | |
| char world[sizeof HELL]; | |
| } hello (void) | |
| { | |
| union { | |
| unsigned char d[sizeof hello()]; | |
| typeof(hello()) o; | |
| } l = {0}; | |
| pthread_barrier_init(&l.o.bar, NULL, len(l.o.there)); | |
| atomic_init(&l.o.ctr, 0); | |
| for (uint16_t i = 0; i < len(l.o.tid); ++i) { | |
| l.o.there[i] = i; | |
| pthread_create(&l.o.tid[i], NULL, run, &l.o.there[i]); | |
| } | |
| pthread_join(l.o.tid[0], NULL); | |
| return l.o; | |
| } | |
| int | |
| main (void) | |
| { | |
| puts(hello().world); | |
| } | |
| #define container_of(P,T,M) ((T *) \ | |
| (void *)((unsigned char *) \ | |
| (1 ? (P) : &((T *)0)->M) - \ | |
| offsetof(T, M))) | |
| static inline typeof(hello()) * | |
| to_hello (uint16_t *p) | |
| { | |
| return container_of(p, typeof(hello()), there[0]); | |
| } | |
| static int | |
| cmp (void const *a, | |
| void const *b) | |
| { | |
| return (int)*(uint16_t const *)a | |
| - (int)*(uint16_t const *)b; | |
| } | |
| static void * | |
| run (void *a) | |
| { | |
| uint16_t *p = a; | |
| uint16_t o = *p; | |
| typeof(hello()) *hi = to_hello(p - *p); | |
| int e = pthread_barrier_wait(&hi->bar); | |
| hi->there[ | |
| atomic_fetch_add_explicit( | |
| &hi->ctr, 1, | |
| memory_order_relaxed | |
| ) | |
| ] = (o << 8U) | (unsigned char)HELL[o]; | |
| if (e == PTHREAD_BARRIER_SERIAL_THREAD) | |
| pthread_barrier_destroy(&hi->bar); | |
| if (o) | |
| return NULL; | |
| while (++o < len(hi->tid)) | |
| pthread_join(hi->tid[o], NULL); | |
| qsort(hi->there, len(hi->there), | |
| sizeof hi->there[0], cmp); | |
| for (o = 0; o < len(hi->there); ++o) | |
| hi->world[o] = (char)(hi->there[o] & 255U); | |
| return NULL; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment