mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-01-31 11:37:35 +00:00
7ba9a73840
It's been thirteen years and C++ still hasn't implemented this wonderful simple builtin keyword. In C++23 a solution was provided for making this work in C++ which is libcxx's stdatomic.h. Including that header schleps in literally 253 unique header files!! Many of the header files it needs are libc header files like pthread.h where we need to have the _Atomic() keyword, but since <atomic> depends on pthreads we can't have it include the <stdatomic.h> header that defines _Atomic for C++ users, and instead we simply make the type non-atomic, hoping and praying only C code shall use those internal data structures. This just shows how STL clowns can't be trusted to define the innermost primitives of a language. They should instead be focusing on being the best at algorithms and data structures.
58 lines
3 KiB
C
58 lines
3 KiB
C
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
|
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
│ Copyright 2021 Justine Alexandra Roberts Tunney │
|
|
│ │
|
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
|
│ any purpose with or without fee is hereby granted, provided that the │
|
|
│ above copyright notice and this permission notice appear in all copies. │
|
|
│ │
|
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
|
#include "libc/intrin/atomic.h"
|
|
#include "libc/mem/mem.h"
|
|
#include "libc/runtime/internal.h"
|
|
#include "libc/x/x.h"
|
|
#include "third_party/zlib/zlib.h"
|
|
|
|
/**
|
|
* Inflates data once atomically, e.g.
|
|
*
|
|
* void *GetData(void) {
|
|
* static _Atomic(void *) ptr;
|
|
* static const unsigned char rodata[] = {...};
|
|
* return xload(&ptr, rodata, 112, 1024);
|
|
* }
|
|
*
|
|
* The above is an example of how this helper may be used to have lazy
|
|
* loading of big infrequently accessed image data.
|
|
*
|
|
* @param a points to your static pointer holder
|
|
* @param p is read-only data compressed using raw deflate
|
|
* @param n is byte length of deflated data
|
|
* @param m is byte length of inflated data
|
|
* @return pointer to inflated data
|
|
*/
|
|
void *xload(void *a_, const void *p, size_t n, size_t m) {
|
|
_Atomic(void *) *a = (_Atomic(void *) *)a_;
|
|
void *r, *z;
|
|
if ((r = atomic_load_explicit(a, memory_order_acquire)))
|
|
return r;
|
|
if (!(r = malloc(m)))
|
|
return 0;
|
|
if (__inflate(r, m, p, n))
|
|
return 0;
|
|
z = 0;
|
|
if (!atomic_compare_exchange_strong_explicit(a, &z, r, memory_order_release,
|
|
memory_order_relaxed)) {
|
|
r = z;
|
|
}
|
|
return r;
|
|
}
|