Skip to content

Commit

Permalink
Factor out generic code from ProcSyncAwait()
Browse files Browse the repository at this point in the history
In preparation for adding more sync object types
that will need Await requests of their own, factor
out some setup and finalization code from
ProcSyncAwait() into SyncAwaitPrologue() and
SyncAwaitEpilogue()

Signed-off-by: James Jones <[email protected]>
Reviewed-by: Keith Packard <[email protected]>
  • Loading branch information
cubanismo committed Dec 7, 2010
1 parent c66a410 commit 12b65de
Showing 1 changed file with 63 additions and 43 deletions.
106 changes: 63 additions & 43 deletions Xext/sync.c
Original file line number Diff line number Diff line change
Expand Up @@ -1465,6 +1465,66 @@ ProcSyncDestroyCounter(ClientPtr client)
return Success;
}

static SyncAwaitUnion*
SyncAwaitPrologue(ClientPtr client, int items)
{
SyncAwaitUnion *pAwaitUnion;

/* all the memory for the entire await list is allocated
* here in one chunk
*/
pAwaitUnion = malloc((items+1) * sizeof(SyncAwaitUnion));
if (!pAwaitUnion)
return NULL;

/* first item is the header, remainder are real wait conditions */

pAwaitUnion->header.delete_id = FakeClientID(client->index);
if (!AddResource(pAwaitUnion->header.delete_id, RTAwait, pAwaitUnion))
{
free(pAwaitUnion);
return NULL;
}

pAwaitUnion->header.client = client;
pAwaitUnion->header.num_waitconditions = 0;

return pAwaitUnion;
}

static void
SyncAwaitEpilogue(ClientPtr client, int items, SyncAwaitUnion *pAwaitUnion)
{
SyncAwait *pAwait;
int i;

IgnoreClient(client);

/* see if any of the triggers are already true */

pAwait = &(pAwaitUnion+1)->await; /* skip over header */
for (i = 0; i < items; i++, pAwait++)
{
CARD64 value;

/* don't have to worry about NULL counters because the request
* errors before we get here out if they occur
*/
switch (pAwait->trigger.pSync->type) {
case SYNC_COUNTER:
value = ((SyncCounter *)pAwait->trigger.pSync)->value;
break;
default:
XSyncIntToValue(&value, 0);
}

if ((*pAwait->trigger.CheckTrigger)(&pAwait->trigger, value))
{
(*pAwait->trigger.TriggerFired)(&pAwait->trigger);
break; /* once is enough */
}
}
}

/*
* ** Await
Expand Down Expand Up @@ -1496,28 +1556,12 @@ ProcSyncAwait(ClientPtr client)
return BadValue;
}

pProtocolWaitConds = (xSyncWaitCondition *) & stuff[1];

/* all the memory for the entire await list is allocated
* here in one chunk
*/
pAwaitUnion = malloc((items+1) * sizeof(SyncAwaitUnion));
if (!pAwaitUnion)
if (!(pAwaitUnion = SyncAwaitPrologue(client, items)))
return BadAlloc;

/* first item is the header, remainder are real wait conditions */

pAwaitUnion->header.delete_id = FakeClientID(client->index);
if (!AddResource(pAwaitUnion->header.delete_id, RTAwait, pAwaitUnion))
{
free(pAwaitUnion);
return BadAlloc;
}

/* don't need to do any more memory allocation for this request! */

pAwaitUnion->header.client = client;
pAwaitUnion->header.num_waitconditions = 0;
pProtocolWaitConds = (xSyncWaitCondition *) & stuff[1];

pAwait = &(pAwaitUnion+1)->await; /* skip over header */
for (i = 0; i < items; i++, pProtocolWaitConds++, pAwait++)
Expand Down Expand Up @@ -1561,32 +1605,8 @@ ProcSyncAwait(ClientPtr client)
pAwaitUnion->header.num_waitconditions++;
}

IgnoreClient(client);

/* see if any of the triggers are already true */
SyncAwaitEpilogue(client, items, pAwaitUnion);

pAwait = &(pAwaitUnion+1)->await; /* skip over header */
for (i = 0; i < items; i++, pAwait++)
{
CARD64 value;

/* don't have to worry about NULL counters because the request
* errors before we get here out if they occur
*/
switch (pAwait->trigger.pSync->type) {
case SYNC_COUNTER:
value = ((SyncCounter *)pAwait->trigger.pSync)->value;
break;
default:
XSyncIntToValue(&value, 0);
}

if ((*pAwait->trigger.CheckTrigger)(&pAwait->trigger, value))
{
(*pAwait->trigger.TriggerFired)(&pAwait->trigger);
break; /* once is enough */
}
}
return Success;
}

Expand Down

0 comments on commit 12b65de

Please sign in to comment.