2011-08-04 15:14:06 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Joakim Sindholt <opensource@zhasha.com>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* on the rights to use, copy, modify, merge, publish, distribute, sub
|
|
|
|
* license, and/or sell copies of the Software, and to permit persons to whom
|
|
|
|
* the Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice (including the next
|
|
|
|
* paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
* Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
* USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
|
|
|
|
#include "iunknown.h"
|
|
|
|
#include "util/u_atomic.h"
|
2016-09-16 17:26:07 +02:00
|
|
|
#include "util/u_hash_table.h"
|
|
|
|
|
2011-08-04 15:14:06 +02:00
|
|
|
#include "nine_helpers.h"
|
2016-09-16 17:26:07 +02:00
|
|
|
#include "nine_pdata.h"
|
st/nine: Protect dtors with mutex
When the flag D3DCREATE_MULTITHREAD is set, a global mutex is used
to protect nine calls.
However for performance reasons, AddRef and Release didn't hold the mutex,
and instead used atomics.
Unfortunately at item release, the item can be destroyed, and that
destruction path should be protected by a mutex (at least for
some objects).
Without this patch, it is possible an app thread is in a dtor
while another thread is making gallium nine calls. It is possible
that two threads are using the same gallium pipe, which is forbiden.
The problem has been made worse with csmt, because it can cause hang,
since nine_csmt_process is not threadsafe.
Fixes Hitman hang, and possibly others.
Signed-off-by: Axel Davy <axel.davy@ens.fr>
2017-01-05 23:04:09 +01:00
|
|
|
#include "nine_lock.h"
|
2011-08-04 15:14:06 +02:00
|
|
|
|
|
|
|
#define DBG_CHANNEL DBG_UNKNOWN
|
|
|
|
|
|
|
|
HRESULT
|
|
|
|
NineUnknown_ctor( struct NineUnknown *This,
|
|
|
|
struct NineUnknownParams *pParams )
|
|
|
|
{
|
2018-09-09 12:36:28 +02:00
|
|
|
if (pParams->container) {
|
|
|
|
This->refs = 0;
|
|
|
|
This->forward = true;
|
|
|
|
This->bind = 0;
|
|
|
|
assert(!pParams->start_with_bind_not_ref);
|
|
|
|
} else if (pParams->start_with_bind_not_ref) {
|
|
|
|
This->refs = 0;
|
|
|
|
This->forward = false;
|
|
|
|
This->bind = 1;
|
|
|
|
} else {
|
|
|
|
This->refs = 1;
|
|
|
|
This->forward = false;
|
|
|
|
This->bind = 0;
|
|
|
|
}
|
|
|
|
|
2011-08-04 15:14:06 +02:00
|
|
|
This->container = pParams->container;
|
|
|
|
This->device = pParams->device;
|
|
|
|
if (This->refs && This->device)
|
|
|
|
NineUnknown_AddRef(NineUnknown(This->device));
|
|
|
|
|
|
|
|
This->vtable = pParams->vtable;
|
2016-03-05 12:17:06 +01:00
|
|
|
This->vtable_internal = pParams->vtable;
|
2011-08-04 15:14:06 +02:00
|
|
|
This->guids = pParams->guids;
|
|
|
|
This->dtor = pParams->dtor;
|
|
|
|
|
2020-02-05 14:52:38 -05:00
|
|
|
This->pdata = _mesa_hash_table_create(NULL, ht_guid_hash, ht_guid_compare);
|
2016-09-16 17:26:07 +02:00
|
|
|
if (!This->pdata)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2011-08-04 15:14:06 +02:00
|
|
|
return D3D_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
NineUnknown_dtor( struct NineUnknown *This )
|
|
|
|
{
|
2016-02-12 19:08:42 +01:00
|
|
|
if (This->refs && This->device) /* Possible only if early exit after a ctor failed */
|
|
|
|
(void) NineUnknown_Release(NineUnknown(This->device));
|
2016-09-16 17:26:07 +02:00
|
|
|
|
|
|
|
if (This->pdata) {
|
|
|
|
util_hash_table_foreach(This->pdata, ht_guid_delete, NULL);
|
2020-02-05 14:52:38 -05:00
|
|
|
_mesa_hash_table_destroy(This->pdata, NULL);
|
2016-09-16 17:26:07 +02:00
|
|
|
}
|
|
|
|
|
2011-08-04 15:14:06 +02:00
|
|
|
FREE(This);
|
|
|
|
}
|
|
|
|
|
2016-02-07 12:29:45 +01:00
|
|
|
HRESULT NINE_WINAPI
|
2011-08-04 15:14:06 +02:00
|
|
|
NineUnknown_QueryInterface( struct NineUnknown *This,
|
|
|
|
REFIID riid,
|
|
|
|
void **ppvObject )
|
|
|
|
{
|
|
|
|
unsigned i = 0;
|
2016-09-14 18:44:41 +02:00
|
|
|
char guid_str[64];
|
2011-08-04 15:14:06 +02:00
|
|
|
|
2016-09-14 18:44:41 +02:00
|
|
|
DBG("This=%p riid=%p id=%s ppvObject=%p\n",
|
|
|
|
This, riid, riid ? GUID_sprintf(guid_str, riid) : "", ppvObject);
|
2014-11-25 00:38:04 +01:00
|
|
|
|
2016-12-06 22:24:53 +01:00
|
|
|
(void)guid_str;
|
|
|
|
|
2011-08-04 15:14:06 +02:00
|
|
|
if (!ppvObject) return E_POINTER;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (GUID_equal(This->guids[i], riid)) {
|
|
|
|
*ppvObject = This;
|
2016-09-14 19:50:16 +02:00
|
|
|
/* Tests showed that this call succeeds even on objects with
|
|
|
|
* zero refcount. This can happen if the app released all references
|
|
|
|
* but the resource is still bound.
|
|
|
|
*/
|
2011-08-04 15:14:06 +02:00
|
|
|
NineUnknown_AddRef(This);
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
} while (This->guids[++i]);
|
|
|
|
|
|
|
|
*ppvObject = NULL;
|
|
|
|
return E_NOINTERFACE;
|
|
|
|
}
|
|
|
|
|
2016-02-07 12:29:45 +01:00
|
|
|
ULONG NINE_WINAPI
|
2011-08-04 15:14:06 +02:00
|
|
|
NineUnknown_AddRef( struct NineUnknown *This )
|
|
|
|
{
|
|
|
|
ULONG r;
|
|
|
|
if (This->forward)
|
|
|
|
return NineUnknown_AddRef(This->container);
|
|
|
|
else
|
|
|
|
r = p_atomic_inc_return(&This->refs);
|
|
|
|
|
|
|
|
if (r == 1) {
|
|
|
|
if (This->device)
|
|
|
|
NineUnknown_AddRef(NineUnknown(This->device));
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2016-02-07 12:29:45 +01:00
|
|
|
ULONG NINE_WINAPI
|
2011-08-04 15:14:06 +02:00
|
|
|
NineUnknown_Release( struct NineUnknown *This )
|
|
|
|
{
|
|
|
|
if (This->forward)
|
|
|
|
return NineUnknown_Release(This->container);
|
|
|
|
|
|
|
|
ULONG r = p_atomic_dec_return(&This->refs);
|
|
|
|
|
|
|
|
if (r == 0) {
|
|
|
|
if (This->device) {
|
|
|
|
if (NineUnknown_Release(NineUnknown(This->device)) == 0)
|
|
|
|
return r; /* everything's gone */
|
|
|
|
}
|
2016-12-03 19:37:06 +01:00
|
|
|
/* Containers (here with !forward) take care of item destruction */
|
|
|
|
if (!This->container && This->bind == 0) {
|
2011-08-04 15:14:06 +02:00
|
|
|
This->dtor(This);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
st/nine: Protect dtors with mutex
When the flag D3DCREATE_MULTITHREAD is set, a global mutex is used
to protect nine calls.
However for performance reasons, AddRef and Release didn't hold the mutex,
and instead used atomics.
Unfortunately at item release, the item can be destroyed, and that
destruction path should be protected by a mutex (at least for
some objects).
Without this patch, it is possible an app thread is in a dtor
while another thread is making gallium nine calls. It is possible
that two threads are using the same gallium pipe, which is forbiden.
The problem has been made worse with csmt, because it can cause hang,
since nine_csmt_process is not threadsafe.
Fixes Hitman hang, and possibly others.
Signed-off-by: Axel Davy <axel.davy@ens.fr>
2017-01-05 23:04:09 +01:00
|
|
|
/* No need to lock the mutex protecting nine (when D3DCREATE_MULTITHREADED)
|
|
|
|
* for AddRef and Release, except for dtor as some of the dtors require it. */
|
|
|
|
ULONG NINE_WINAPI
|
|
|
|
NineUnknown_ReleaseWithDtorLock( struct NineUnknown *This )
|
|
|
|
{
|
|
|
|
if (This->forward)
|
|
|
|
return NineUnknown_ReleaseWithDtorLock(This->container);
|
|
|
|
|
|
|
|
ULONG r = p_atomic_dec_return(&This->refs);
|
|
|
|
|
|
|
|
if (r == 0) {
|
|
|
|
if (This->device) {
|
|
|
|
if (NineUnknown_ReleaseWithDtorLock(NineUnknown(This->device)) == 0)
|
|
|
|
return r; /* everything's gone */
|
|
|
|
}
|
|
|
|
/* Containers (here with !forward) take care of item destruction */
|
|
|
|
if (!This->container && This->bind == 0) {
|
|
|
|
NineLockGlobalMutex();
|
|
|
|
This->dtor(This);
|
|
|
|
NineUnlockGlobalMutex();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2016-02-07 12:29:45 +01:00
|
|
|
HRESULT NINE_WINAPI
|
2011-08-04 15:14:06 +02:00
|
|
|
NineUnknown_GetDevice( struct NineUnknown *This,
|
|
|
|
IDirect3DDevice9 **ppDevice )
|
|
|
|
{
|
|
|
|
user_assert(ppDevice, E_POINTER);
|
|
|
|
NineUnknown_AddRef(NineUnknown(This->device));
|
|
|
|
*ppDevice = (IDirect3DDevice9 *)This->device;
|
|
|
|
return D3D_OK;
|
|
|
|
}
|
2016-09-16 17:26:07 +02:00
|
|
|
|
|
|
|
HRESULT NINE_WINAPI
|
|
|
|
NineUnknown_SetPrivateData( struct NineUnknown *This,
|
|
|
|
REFGUID refguid,
|
|
|
|
const void *pData,
|
|
|
|
DWORD SizeOfData,
|
|
|
|
DWORD Flags )
|
|
|
|
{
|
|
|
|
struct pheader *header;
|
|
|
|
const void *user_data = pData;
|
|
|
|
char guid_str[64];
|
2016-09-01 18:07:55 +02:00
|
|
|
void *header_data;
|
2016-09-16 17:26:07 +02:00
|
|
|
|
|
|
|
DBG("This=%p GUID=%s pData=%p SizeOfData=%u Flags=%x\n",
|
|
|
|
This, GUID_sprintf(guid_str, refguid), pData, SizeOfData, Flags);
|
|
|
|
|
2016-12-06 22:24:53 +01:00
|
|
|
(void)guid_str;
|
|
|
|
|
2016-09-16 17:26:07 +02:00
|
|
|
if (Flags & D3DSPD_IUNKNOWN)
|
|
|
|
user_assert(SizeOfData == sizeof(IUnknown *), D3DERR_INVALIDCALL);
|
|
|
|
|
|
|
|
/* data consists of a header and the actual data. avoiding 2 mallocs */
|
2016-09-01 18:07:55 +02:00
|
|
|
header = CALLOC_VARIANT_LENGTH_STRUCT(pheader, SizeOfData);
|
2016-09-16 17:26:07 +02:00
|
|
|
if (!header) { return E_OUTOFMEMORY; }
|
|
|
|
header->unknown = (Flags & D3DSPD_IUNKNOWN) ? TRUE : FALSE;
|
|
|
|
|
|
|
|
/* if the refguid already exists, delete it */
|
|
|
|
NineUnknown_FreePrivateData(This, refguid);
|
|
|
|
|
|
|
|
/* IUnknown special case */
|
|
|
|
if (header->unknown) {
|
|
|
|
/* here the pointer doesn't point to the data we want, so point at the
|
|
|
|
* pointer making what we eventually copy is the pointer itself */
|
|
|
|
user_data = &pData;
|
|
|
|
}
|
|
|
|
|
|
|
|
header->size = SizeOfData;
|
2016-09-01 18:07:55 +02:00
|
|
|
header_data = (void *)header + sizeof(*header);
|
|
|
|
memcpy(header_data, user_data, header->size);
|
2016-09-16 17:26:07 +02:00
|
|
|
memcpy(&header->guid, refguid, sizeof(header->guid));
|
|
|
|
|
2020-02-05 14:52:38 -05:00
|
|
|
_mesa_hash_table_insert(This->pdata, &header->guid, header);
|
|
|
|
if (header->unknown) { IUnknown_AddRef(*(IUnknown **)header_data); }
|
|
|
|
return D3D_OK;
|
2016-09-16 17:26:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT NINE_WINAPI
|
|
|
|
NineUnknown_GetPrivateData( struct NineUnknown *This,
|
|
|
|
REFGUID refguid,
|
|
|
|
void *pData,
|
|
|
|
DWORD *pSizeOfData )
|
|
|
|
{
|
|
|
|
struct pheader *header;
|
|
|
|
DWORD sizeofdata;
|
|
|
|
char guid_str[64];
|
2016-09-01 18:07:55 +02:00
|
|
|
void *header_data;
|
2016-09-16 17:26:07 +02:00
|
|
|
|
|
|
|
DBG("This=%p GUID=%s pData=%p pSizeOfData=%p\n",
|
|
|
|
This, GUID_sprintf(guid_str, refguid), pData, pSizeOfData);
|
|
|
|
|
2016-12-06 22:24:53 +01:00
|
|
|
(void)guid_str;
|
|
|
|
|
2016-09-16 17:26:07 +02:00
|
|
|
header = util_hash_table_get(This->pdata, refguid);
|
|
|
|
if (!header) { return D3DERR_NOTFOUND; }
|
|
|
|
|
|
|
|
user_assert(pSizeOfData, E_POINTER);
|
|
|
|
sizeofdata = *pSizeOfData;
|
|
|
|
*pSizeOfData = header->size;
|
|
|
|
|
|
|
|
if (!pData) {
|
|
|
|
return D3D_OK;
|
|
|
|
}
|
|
|
|
if (sizeofdata < header->size) {
|
|
|
|
return D3DERR_MOREDATA;
|
|
|
|
}
|
|
|
|
|
2016-09-01 18:07:55 +02:00
|
|
|
header_data = (void *)header + sizeof(*header);
|
|
|
|
if (header->unknown) { IUnknown_AddRef(*(IUnknown **)header_data); }
|
|
|
|
memcpy(pData, header_data, header->size);
|
2016-09-16 17:26:07 +02:00
|
|
|
|
|
|
|
return D3D_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT NINE_WINAPI
|
|
|
|
NineUnknown_FreePrivateData( struct NineUnknown *This,
|
|
|
|
REFGUID refguid )
|
|
|
|
{
|
|
|
|
struct pheader *header;
|
|
|
|
char guid_str[64];
|
|
|
|
|
|
|
|
DBG("This=%p GUID=%s\n", This, GUID_sprintf(guid_str, refguid));
|
|
|
|
|
2016-12-06 22:24:53 +01:00
|
|
|
(void)guid_str;
|
|
|
|
|
2016-09-16 17:26:07 +02:00
|
|
|
header = util_hash_table_get(This->pdata, refguid);
|
|
|
|
if (!header)
|
|
|
|
return D3DERR_NOTFOUND;
|
|
|
|
|
|
|
|
ht_guid_delete(NULL, header, NULL);
|
2020-02-05 14:52:38 -05:00
|
|
|
_mesa_hash_table_remove_key(This->pdata, refguid);
|
2016-09-16 17:26:07 +02:00
|
|
|
|
|
|
|
return D3D_OK;
|
|
|
|
}
|