Use dynamic memory for *NSYNC waiters

This commit is contained in:
Justine Tunney 2023-11-10 01:42:06 -08:00
parent 15af5c2d7e
commit 241f949540
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
12 changed files with 171 additions and 72 deletions

View file

@ -149,7 +149,7 @@ int nsync_mu_wait_with_deadline (nsync_mu *mu,
lock_type *l_type;
int first_wait;
int condition_is_true;
waiter w[1];
waiter *w;
int outcome;
/* Work out in which mode the lock is held. */
uint32_t old_word;
@ -165,12 +165,12 @@ int nsync_mu_wait_with_deadline (nsync_mu *mu,
l_type = nsync_reader_type_;
}
w->tag = 0; /* avoid allocating system resources */
first_wait = 1; /* first time through the loop below. */
condition_is_true = (condition == NULL || (*condition) (condition_arg));
/* Loop until either the condition becomes true, or "outcome" indicates
cancellation or timeout. */
w = NULL;
outcome = 0;
while (outcome == 0 && !condition_is_true) {
uint32_t has_condition;
@ -180,10 +180,8 @@ int nsync_mu_wait_with_deadline (nsync_mu *mu,
int sem_outcome;
unsigned attempts;
int have_lock;
/* initialize the waiter if we haven't already */
if (!w->tag) {
nsync_waiter_init_ (w);
if (w == NULL) {
w = nsync_waiter_new_ (); /* get a waiter struct if we need one. */
}
/* Prepare to wait. */
@ -261,7 +259,9 @@ int nsync_mu_wait_with_deadline (nsync_mu *mu,
}
condition_is_true = (condition == NULL || (*condition) (condition_arg));
}
nsync_waiter_destroy_ (w);
if (w != NULL) {
nsync_waiter_free_ (w); /* free waiter if we allocated one. */
}
if (condition_is_true) {
outcome = 0; /* condition is true trumps other outcomes. */
}