xsync: Add resource inside of SyncCreate, export SyncCreate

As shown by DRI3 adding the SyncCreateFenceFromFD() function, extensions may
want to create a fence, then initialize it in their own way. This currently
can't be done without adding a function directly to Xext/sync.c due to the fact
that the RTFence resource type is private and there is no external interface to
add to it.

To facilitate other X extensions creating fences and initializing them, this
change exports SyncCreate() and adds the resource directly within it. Callers no
longer need to call AddResource() after SyncCreate(), they only need to
initialize the SyncObject.

To prevent FreeFence() and FreeCounter() from segfaulting if the call to
AddResource() fails before the sync object is initialized, this adds a new
'initialized' parameter to SyncObject that, when FALSE, causes FreeFence() and
FreeCounter() to skip de-initialization and simply free the object.
Initialization after adding the resource shouldn't otherwise be a problem due to
the single-threaded nature of X.

Signed-off-by: Alex Goins <agoins@nvidia.com>
Reviewed-by: James Jones <jajones@nvidia.com>
Signed-off-by: Aaron Plattner <aplattner@nvidia.com>
This commit is contained in:
Alex Goins 2019-04-10 13:48:02 -05:00 committed by Aaron Plattner
parent 8702c938b3
commit 7f962c70b6
5 changed files with 51 additions and 35 deletions

View File

@ -881,18 +881,21 @@ SyncChangeAlarmAttributes(ClientPtr client, SyncAlarm * pAlarm, Mask mask,
return Success; return Success;
} }
static SyncObject * SyncObject *
SyncCreate(ClientPtr client, XID id, unsigned char type) SyncCreate(ClientPtr client, XID id, unsigned char type)
{ {
SyncObject *pSync; SyncObject *pSync;
RESTYPE resType;
switch (type) { switch (type) {
case SYNC_COUNTER: case SYNC_COUNTER:
pSync = malloc(sizeof(SyncCounter)); pSync = malloc(sizeof(SyncCounter));
resType = RTCounter;
break; break;
case SYNC_FENCE: case SYNC_FENCE:
pSync = (SyncObject *) dixAllocateObjectWithPrivates(SyncFence, pSync = (SyncObject *) dixAllocateObjectWithPrivates(SyncFence,
PRIVATE_SYNC_FENCE); PRIVATE_SYNC_FENCE);
resType = RTFence;
break; break;
default: default:
return NULL; return NULL;
@ -901,6 +904,11 @@ SyncCreate(ClientPtr client, XID id, unsigned char type)
if (!pSync) if (!pSync)
return NULL; return NULL;
pSync->initialized = FALSE;
if (!AddResource(id, resType, (void *) pSync))
return NULL;
pSync->client = client; pSync->client = client;
pSync->id = id; pSync->id = id;
pSync->pTriglist = NULL; pSync->pTriglist = NULL;
@ -923,13 +931,10 @@ SyncCreateFenceFromFD(ClientPtr client, DrawablePtr pDraw, XID id, int fd, BOOL
status = miSyncInitFenceFromFD(pDraw, pFence, fd, initially_triggered); status = miSyncInitFenceFromFD(pDraw, pFence, fd, initially_triggered);
if (status != Success) { if (status != Success) {
dixFreeObjectWithPrivates(pFence, PRIVATE_SYNC_FENCE); FreeResource(pFence->sync.id, RT_NONE);
return status; return status;
} }
if (!AddResource(id, RTFence, (void *) pFence))
return BadAlloc;
return Success; return Success;
#else #else
return BadImplementation; return BadImplementation;
@ -957,8 +962,7 @@ SyncCreateCounter(ClientPtr client, XSyncCounter id, int64_t initialvalue)
pCounter->value = initialvalue; pCounter->value = initialvalue;
pCounter->pSysCounterInfo = NULL; pCounter->pSysCounterInfo = NULL;
if (!AddResource(id, RTCounter, (void *) pCounter)) pCounter->sync.initialized = TRUE;
return NULL;
return pCounter; return pCounter;
} }
@ -1137,21 +1141,26 @@ static int
FreeCounter(void *env, XID id) FreeCounter(void *env, XID id)
{ {
SyncCounter *pCounter = (SyncCounter *) env; SyncCounter *pCounter = (SyncCounter *) env;
SyncTriggerList *ptl, *pnext;
pCounter->sync.beingDestroyed = TRUE; pCounter->sync.beingDestroyed = TRUE;
/* tell all the counter's triggers that the counter has been destroyed */
for (ptl = pCounter->sync.pTriglist; ptl; ptl = pnext) { if (pCounter->sync.initialized) {
(*ptl->pTrigger->CounterDestroyed) (ptl->pTrigger); SyncTriggerList *ptl, *pnext;
pnext = ptl->next;
free(ptl); /* destroy the trigger list as we go */ /* tell all the counter's triggers that counter has been destroyed */
} for (ptl = pCounter->sync.pTriglist; ptl; ptl = pnext) {
if (IsSystemCounter(pCounter)) { (*ptl->pTrigger->CounterDestroyed) (ptl->pTrigger);
xorg_list_del(&pCounter->pSysCounterInfo->entry); pnext = ptl->next;
free(pCounter->pSysCounterInfo->name); free(ptl); /* destroy the trigger list as we go */
free(pCounter->pSysCounterInfo->private); }
free(pCounter->pSysCounterInfo); if (IsSystemCounter(pCounter)) {
xorg_list_del(&pCounter->pSysCounterInfo->entry);
free(pCounter->pSysCounterInfo->name);
free(pCounter->pSysCounterInfo->private);
free(pCounter->pSysCounterInfo);
}
} }
free(pCounter); free(pCounter);
return Success; return Success;
} }
@ -1889,9 +1898,6 @@ ProcSyncCreateFence(ClientPtr client)
miSyncInitFence(pDraw->pScreen, pFence, stuff->initially_triggered); miSyncInitFence(pDraw->pScreen, pFence, stuff->initially_triggered);
if (!AddResource(stuff->fid, RTFence, (void *) pFence))
return BadAlloc;
return Success; return Success;
} }

View File

@ -29,6 +29,9 @@
extern _X_EXPORT int extern _X_EXPORT int
SyncVerifyFence(SyncFence ** ppFence, XID fid, ClientPtr client, Mask mode); SyncVerifyFence(SyncFence ** ppFence, XID fid, ClientPtr client, Mask mode);
extern _X_EXPORT SyncObject*
SyncCreate(ClientPtr client, XID id, unsigned char type);
#define VERIFY_SYNC_FENCE(pFence, fid, client, mode) \ #define VERIFY_SYNC_FENCE(pFence, fid, client, mode) \
do { \ do { \
int rc; \ int rc; \

View File

@ -101,24 +101,29 @@ miSyncInitFence(ScreenPtr pScreen, SyncFence * pFence, Bool initially_triggered)
pFence->funcs = miSyncFenceFuncs; pFence->funcs = miSyncFenceFuncs;
pScreenPriv->funcs.CreateFence(pScreen, pFence, initially_triggered); pScreenPriv->funcs.CreateFence(pScreen, pFence, initially_triggered);
pFence->sync.initialized = TRUE;
} }
void void
miSyncDestroyFence(SyncFence * pFence) miSyncDestroyFence(SyncFence * pFence)
{ {
ScreenPtr pScreen = pFence->pScreen;
SyncScreenPrivPtr pScreenPriv = SYNC_SCREEN_PRIV(pScreen);
SyncTriggerList *ptl, *pNext;
pFence->sync.beingDestroyed = TRUE; pFence->sync.beingDestroyed = TRUE;
/* tell all the fence's triggers that the counter has been destroyed */
for (ptl = pFence->sync.pTriglist; ptl; ptl = pNext) {
(*ptl->pTrigger->CounterDestroyed) (ptl->pTrigger);
pNext = ptl->next;
free(ptl); /* destroy the trigger list as we go */
}
pScreenPriv->funcs.DestroyFence(pScreen, pFence); if (pFence->sync.initialized) {
ScreenPtr pScreen = pFence->pScreen;
SyncScreenPrivPtr pScreenPriv = SYNC_SCREEN_PRIV(pScreen);
SyncTriggerList *ptl, *pNext;
/* tell all the fence's triggers that the counter has been destroyed */
for (ptl = pFence->sync.pTriglist; ptl; ptl = pNext) {
(*ptl->pTrigger->CounterDestroyed) (ptl->pTrigger);
pNext = ptl->next;
free(ptl); /* destroy the trigger list as we go */
}
pScreenPriv->funcs.DestroyFence(pScreen, pFence);
}
dixFreeObjectWithPrivates(pFence, PRIVATE_SYNC_FENCE); dixFreeObjectWithPrivates(pFence, PRIVATE_SYNC_FENCE);
} }

View File

@ -28,6 +28,7 @@
#ifndef _MISYNC_H_ #ifndef _MISYNC_H_
#define _MISYNC_H_ #define _MISYNC_H_
typedef struct _SyncObject SyncObject;
typedef struct _SyncFence SyncFence; typedef struct _SyncFence SyncFence;
typedef struct _SyncTrigger SyncTrigger; typedef struct _SyncTrigger SyncTrigger;

View File

@ -38,13 +38,14 @@
#define SYNC_COUNTER 0 #define SYNC_COUNTER 0
#define SYNC_FENCE 1 #define SYNC_FENCE 1
typedef struct _SyncObject { struct _SyncObject {
ClientPtr client; /* Owning client. 0 for system counters */ ClientPtr client; /* Owning client. 0 for system counters */
struct _SyncTriggerList *pTriglist; /* list of triggers */ struct _SyncTriggerList *pTriglist; /* list of triggers */
XID id; /* resource ID */ XID id; /* resource ID */
unsigned char type; /* SYNC_* */ unsigned char type; /* SYNC_* */
Bool initialized; /* FALSE if created but not initialized */
Bool beingDestroyed; /* in process of going away */ Bool beingDestroyed; /* in process of going away */
} SyncObject; };
typedef struct _SyncCounter { typedef struct _SyncCounter {
SyncObject sync; /* Common sync object data */ SyncObject sync; /* Common sync object data */