cache: Move cache implementation over to read-writer locks.

Signed-off-by: Hans-Kristian Arntzen <post@arntzen-software.no>
This commit is contained in:
Hans-Kristian Arntzen 2022-01-24 15:18:33 +01:00
parent 7da708ea69
commit 41c977d616
2 changed files with 20 additions and 20 deletions

View File

@ -236,7 +236,7 @@ static void d3d12_pipeline_library_cleanup(struct d3d12_pipeline_library *pipeli
hash_map_clear(&pipeline_library->map); hash_map_clear(&pipeline_library->map);
vkd3d_private_store_destroy(&pipeline_library->private_store); vkd3d_private_store_destroy(&pipeline_library->private_store);
pthread_mutex_destroy(&pipeline_library->mutex); rwlock_destroy(&pipeline_library->mutex);
} }
static HRESULT STDMETHODCALLTYPE d3d12_pipeline_library_QueryInterface(d3d12_pipeline_library_iface *iface, static HRESULT STDMETHODCALLTYPE d3d12_pipeline_library_QueryInterface(d3d12_pipeline_library_iface *iface,
@ -342,7 +342,7 @@ static HRESULT STDMETHODCALLTYPE d3d12_pipeline_library_StorePipeline(d3d12_pipe
TRACE("iface %p, name %s, pipeline %p.\n", iface, debugstr_w(name), pipeline); TRACE("iface %p, name %s, pipeline %p.\n", iface, debugstr_w(name), pipeline);
if ((rc = pthread_mutex_lock(&pipeline_library->mutex))) if ((rc = rwlock_lock_write(&pipeline_library->mutex)))
{ {
ERR("Failed to lock mutex, rc %d.\n", rc); ERR("Failed to lock mutex, rc %d.\n", rc);
return hresult_from_errno(rc); return hresult_from_errno(rc);
@ -354,14 +354,14 @@ static HRESULT STDMETHODCALLTYPE d3d12_pipeline_library_StorePipeline(d3d12_pipe
if (hash_map_find(&pipeline_library->map, &entry.key)) if (hash_map_find(&pipeline_library->map, &entry.key))
{ {
WARN("Pipeline %s already exists.\n", debugstr_w(name)); WARN("Pipeline %s already exists.\n", debugstr_w(name));
pthread_mutex_unlock(&pipeline_library->mutex); rwlock_unlock_write(&pipeline_library->mutex);
return E_INVALIDARG; return E_INVALIDARG;
} }
/* We need to allocate persistent storage for the name */ /* We need to allocate persistent storage for the name */
if (!(new_name = malloc(entry.key.name_length))) if (!(new_name = malloc(entry.key.name_length)))
{ {
pthread_mutex_unlock(&pipeline_library->mutex); rwlock_unlock_write(&pipeline_library->mutex);
return E_OUTOFMEMORY; return E_OUTOFMEMORY;
} }
@ -371,14 +371,14 @@ static HRESULT STDMETHODCALLTYPE d3d12_pipeline_library_StorePipeline(d3d12_pipe
if (FAILED(vr = vkd3d_serialize_pipeline_state(pipeline_state, &entry.data.blob_length, NULL))) if (FAILED(vr = vkd3d_serialize_pipeline_state(pipeline_state, &entry.data.blob_length, NULL)))
{ {
vkd3d_free(new_name); vkd3d_free(new_name);
pthread_mutex_unlock(&pipeline_library->mutex); rwlock_unlock_write(&pipeline_library->mutex);
return hresult_from_vk_result(vr); return hresult_from_vk_result(vr);
} }
if (!(new_blob = malloc(entry.data.blob_length))) if (!(new_blob = malloc(entry.data.blob_length)))
{ {
vkd3d_free(new_name); vkd3d_free(new_name);
pthread_mutex_unlock(&pipeline_library->mutex); rwlock_unlock_write(&pipeline_library->mutex);
return E_OUTOFMEMORY; return E_OUTOFMEMORY;
} }
@ -386,7 +386,7 @@ static HRESULT STDMETHODCALLTYPE d3d12_pipeline_library_StorePipeline(d3d12_pipe
{ {
vkd3d_free(new_name); vkd3d_free(new_name);
vkd3d_free(new_blob); vkd3d_free(new_blob);
pthread_mutex_unlock(&pipeline_library->mutex); rwlock_unlock_write(&pipeline_library->mutex);
return hresult_from_vk_result(vr); return hresult_from_vk_result(vr);
} }
@ -397,11 +397,11 @@ static HRESULT STDMETHODCALLTYPE d3d12_pipeline_library_StorePipeline(d3d12_pipe
{ {
vkd3d_free(new_name); vkd3d_free(new_name);
vkd3d_free(new_blob); vkd3d_free(new_blob);
pthread_mutex_unlock(&pipeline_library->mutex); rwlock_unlock_write(&pipeline_library->mutex);
return E_OUTOFMEMORY; return E_OUTOFMEMORY;
} }
pthread_mutex_unlock(&pipeline_library->mutex); rwlock_unlock_write(&pipeline_library->mutex);
return S_OK; return S_OK;
} }
@ -412,7 +412,7 @@ static HRESULT d3d12_pipeline_library_load_pipeline(struct d3d12_pipeline_librar
struct vkd3d_cached_pipeline_key key; struct vkd3d_cached_pipeline_key key;
int rc; int rc;
if ((rc = pthread_mutex_lock(&pipeline_library->mutex))) if ((rc = rwlock_lock_read(&pipeline_library->mutex)))
{ {
ERR("Failed to lock mutex, rc %d.\n", rc); ERR("Failed to lock mutex, rc %d.\n", rc);
return hresult_from_errno(rc); return hresult_from_errno(rc);
@ -424,13 +424,13 @@ static HRESULT d3d12_pipeline_library_load_pipeline(struct d3d12_pipeline_librar
if (!(e = (const struct vkd3d_cached_pipeline_entry*)hash_map_find(&pipeline_library->map, &key))) if (!(e = (const struct vkd3d_cached_pipeline_entry*)hash_map_find(&pipeline_library->map, &key)))
{ {
WARN("Pipeline %s does not exist.\n", debugstr_w(name)); WARN("Pipeline %s does not exist.\n", debugstr_w(name));
pthread_mutex_unlock(&pipeline_library->mutex); rwlock_unlock_read(&pipeline_library->mutex);
return E_INVALIDARG; return E_INVALIDARG;
} }
desc->cached_pso.CachedBlobSizeInBytes = e->data.blob_length; desc->cached_pso.CachedBlobSizeInBytes = e->data.blob_length;
desc->cached_pso.pCachedBlob = e->data.blob; desc->cached_pso.pCachedBlob = e->data.blob;
pthread_mutex_unlock(&pipeline_library->mutex); rwlock_unlock_read(&pipeline_library->mutex);
return d3d12_pipeline_state_create(pipeline_library->device, bind_point, desc, state); return d3d12_pipeline_state_create(pipeline_library->device, bind_point, desc, state);
} }
@ -488,7 +488,7 @@ static SIZE_T STDMETHODCALLTYPE d3d12_pipeline_library_GetSerializedSize(d3d12_p
TRACE("iface %p.\n", iface); TRACE("iface %p.\n", iface);
if ((rc = pthread_mutex_lock(&pipeline_library->mutex))) if ((rc = rwlock_lock_read(&pipeline_library->mutex)))
{ {
ERR("Failed to lock mutex, rc %d.\n", rc); ERR("Failed to lock mutex, rc %d.\n", rc);
return 0; return 0;
@ -509,7 +509,7 @@ static SIZE_T STDMETHODCALLTYPE d3d12_pipeline_library_GetSerializedSize(d3d12_p
} }
} }
pthread_mutex_unlock(&pipeline_library->mutex); rwlock_unlock_read(&pipeline_library->mutex);
return total_size; return total_size;
} }
@ -529,7 +529,7 @@ static HRESULT STDMETHODCALLTYPE d3d12_pipeline_library_Serialize(d3d12_pipeline
if (data_size < sizeof(*header)) if (data_size < sizeof(*header))
return E_INVALIDARG; return E_INVALIDARG;
if ((rc = pthread_mutex_lock(&pipeline_library->mutex))) if ((rc = rwlock_lock_read(&pipeline_library->mutex)))
{ {
ERR("Failed to lock mutex, rc %d.\n", rc); ERR("Failed to lock mutex, rc %d.\n", rc);
return 0; return 0;
@ -553,7 +553,7 @@ static HRESULT STDMETHODCALLTYPE d3d12_pipeline_library_Serialize(d3d12_pipeline
/* Fails if the provided buffer is too small to fit the pipeline */ /* Fails if the provided buffer is too small to fit the pipeline */
if (!d3d12_pipeline_library_serialize_entry(pipeline_library, e, &pipeline_size, serialized_data)) if (!d3d12_pipeline_library_serialize_entry(pipeline_library, e, &pipeline_size, serialized_data))
{ {
pthread_mutex_unlock(&pipeline_library->mutex); rwlock_unlock_read(&pipeline_library->mutex);
return E_INVALIDARG; return E_INVALIDARG;
} }
@ -562,7 +562,7 @@ static HRESULT STDMETHODCALLTYPE d3d12_pipeline_library_Serialize(d3d12_pipeline
} }
} }
pthread_mutex_unlock(&pipeline_library->mutex); rwlock_unlock_read(&pipeline_library->mutex);
return S_OK; return S_OK;
} }
@ -676,7 +676,7 @@ static HRESULT d3d12_pipeline_library_init(struct d3d12_pipeline_library *pipeli
if (!blob_length && blob) if (!blob_length && blob)
return E_INVALIDARG; return E_INVALIDARG;
if ((rc = pthread_mutex_init(&pipeline_library->mutex, NULL))) if ((rc = rwlock_init(&pipeline_library->mutex)))
return hresult_from_errno(rc); return hresult_from_errno(rc);
hash_map_init(&pipeline_library->map, &vkd3d_cached_pipeline_hash, hash_map_init(&pipeline_library->map, &vkd3d_cached_pipeline_hash,
@ -697,7 +697,7 @@ static HRESULT d3d12_pipeline_library_init(struct d3d12_pipeline_library *pipeli
cleanup_hash_map: cleanup_hash_map:
hash_map_clear(&pipeline_library->map); hash_map_clear(&pipeline_library->map);
cleanup_mutex: cleanup_mutex:
pthread_mutex_destroy(&pipeline_library->mutex); rwlock_destroy(&pipeline_library->mutex);
return hr; return hr;
} }

View File

@ -1633,7 +1633,7 @@ struct d3d12_pipeline_library
struct d3d12_device *device; struct d3d12_device *device;
pthread_mutex_t mutex; rwlock_t mutex;
struct hash_map map; struct hash_map map;
struct vkd3d_private_store private_store; struct vkd3d_private_store private_store;