Clean up more code

The *NSYNC linked list API is good enough that it deserves to be part of
the C libray, so this change writes an improved version of it which uses
that offsetof() trick from the Linux Kernel. We vendor all of the *NSYNC
tests in third_party which helped confirm the needed refactoring is safe

This change also deletes more old code that didn't pan out. My goal here
is to work towards a vision where the Cosmopolitan core libraries become
less experimental and more focused on curation. This better reflects the
current level of quality we've managed to achieve.
This commit is contained in:
Justine Tunney 2023-07-06 06:57:28 -07:00
parent 88612a2cd7
commit 0a24b4fc3c
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
268 changed files with 632 additions and 8688 deletions

View file

@ -15,13 +15,13 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "libc/intrin/dll.h"
#include "libc/mem/mem.h"
#include "libc/str/str.h"
#include "third_party/nsync/atomic.h"
#include "third_party/nsync/atomic.internal.h"
#include "third_party/nsync/common.internal.h"
#include "third_party/nsync/counter.h"
#include "third_party/nsync/dll.h"
#include "third_party/nsync/mu_semaphore.h"
#include "third_party/nsync/races.internal.h"
#include "third_party/nsync/wait_s.internal.h"
@ -38,13 +38,13 @@ struct nsync_counter_s_ {
nsync_atomic_uint32_ waited; /* wait has been called */
nsync_mu counter_mu; /* protects fields below except reads of "value" */
nsync_atomic_uint32_ value; /* value of counter */
struct nsync_dll_element_s_ *waiters; /* list of waiters */
struct Dll *waiters; /* list of waiters */
};
nsync_counter nsync_counter_new (uint32_t value) {
nsync_counter c = (nsync_counter) malloc (sizeof (*c));
if (c != NULL) {
memset ((void *) c, 0, sizeof (*c));
bzero ((void *) c, sizeof (*c));
ATM_STORE (&c->value, value);
}
return (c);
@ -52,7 +52,7 @@ nsync_counter nsync_counter_new (uint32_t value) {
void nsync_counter_free (nsync_counter c) {
nsync_mu_lock (&c->counter_mu);
ASSERT (nsync_dll_is_empty_ (c->waiters));
ASSERT (dll_is_empty (c->waiters));
nsync_mu_unlock (&c->counter_mu);
free (c);
}
@ -77,10 +77,10 @@ uint32_t nsync_counter_add (nsync_counter c, int32_t delta) {
ASSERT (value < value - delta); /* Crash on overflow. */
}
if (value == 0) {
nsync_dll_element_ *p;
while ((p = nsync_dll_first_ (c->waiters)) != NULL) {
struct Dll *p;
while ((p = dll_first (c->waiters)) != NULL) {
struct nsync_waiter_s *nw = DLL_NSYNC_WAITER (p);
c->waiters = nsync_dll_remove_ (c->waiters, p);
dll_remove (&c->waiters, p);
ATM_STORE_REL (&nw->waiting, 0);
nsync_mu_semaphore_v (nw->sem);
}
@ -127,7 +127,7 @@ static int counter_enqueue (void *v, struct nsync_waiter_s *nw) {
nsync_mu_lock (&c->counter_mu);
value = ATM_LOAD_ACQ (&c->value);
if (value != 0) {
c->waiters = nsync_dll_make_last_in_list_ (c->waiters, &nw->q);
dll_make_last (&c->waiters, &nw->q);
ATM_STORE (&nw->waiting, 1);
} else {
ATM_STORE (&nw->waiting, 0);
@ -142,7 +142,7 @@ static int counter_dequeue (void *v, struct nsync_waiter_s *nw) {
nsync_mu_lock (&c->counter_mu);
value = ATM_LOAD_ACQ (&c->value);
if (ATM_LOAD_ACQ (&nw->waiting) != 0) {
c->waiters = nsync_dll_remove_ (c->waiters, &nw->q);
dll_remove (&c->waiters, &nw->q);
ATM_STORE (&nw->waiting, 0);
}
nsync_mu_unlock (&c->counter_mu);