Commit cd9dba51 authored by Swann Perarnau's avatar Swann Perarnau

[feature] make scratch_seq thread-safe

Add mutex to make request creation and destruction thread-safe. As we
need to deal both we requests and tiles during these functions, we lock
the entire section.
parent 7a69c840
......@@ -732,6 +732,7 @@ struct aml_scratch_seq_data {
void * sch_ptr;
struct aml_vector tilemap;
struct aml_vector requests;
pthread_mutex_t lock;
};
struct aml_scratch_seq_ops {
......
......@@ -69,6 +69,7 @@ int aml_scratch_seq_create_request(struct aml_scratch_data *d,
struct aml_scratch_request_seq *req;
pthread_mutex_lock(&scratch->data.lock);
req = aml_vector_add(&scratch->data.requests);
/* init the request */
if(type == AML_SCRATCH_REQUEST_TYPE_PUSH)
......@@ -125,6 +126,7 @@ int aml_scratch_seq_create_request(struct aml_scratch_data *d,
scratchptr, *scratchid,
srcptr, srcid);
}
pthread_mutex_unlock(&scratch->data.lock);
scratch->ops.doit(&scratch->data, req);
*r = (struct aml_scratch_request *)req;
......@@ -147,12 +149,14 @@ int aml_scratch_seq_destroy_request(struct aml_scratch_data *d,
aml_scratch_request_seq_destroy(req);
/* destroy removes the tile from the scratch */
pthread_mutex_lock(&scratch->data.lock);
if(req->type == AML_SCRATCH_REQUEST_TYPE_PUSH)
tile = aml_vector_get(&scratch->data.tilemap,req->srcid);
else if(req->type == AML_SCRATCH_REQUEST_TYPE_PULL)
tile = aml_vector_get(&scratch->data.tilemap,req->dstid);
aml_vector_remove(&scratch->data.tilemap, tile);
aml_vector_remove(&scratch->data.requests, req);
pthread_mutex_unlock(&scratch->data.lock);
return 0;
}
......@@ -171,12 +175,14 @@ int aml_scratch_seq_wait_request(struct aml_scratch_data *d,
/* cleanup a completed request. In case of push, free up the tile */
aml_scratch_request_seq_destroy(req);
pthread_mutex_lock(&scratch->data.lock);
if(req->type == AML_SCRATCH_REQUEST_TYPE_PUSH)
{
tile = aml_vector_get(&scratch->data.tilemap,req->srcid);
aml_vector_remove(&scratch->data.tilemap, tile);
}
aml_vector_remove(&scratch->data.requests, req);
pthread_mutex_unlock(&scratch->data.lock);
return 0;
}
......@@ -243,6 +249,7 @@ int aml_scratch_seq_vinit(struct aml_scratch *d, va_list ap)
size_t tilesize = aml_tiling_tilesize(scratch->data.tiling, 0);
scratch->data.sch_ptr = aml_area_calloc(scratch->data.sch_area,
nbtiles, tilesize);
pthread_mutex_init(&scratch->data.lock, NULL);
return 0;
}
int aml_scratch_seq_init(struct aml_scratch *d, ...)
......@@ -261,5 +268,6 @@ int aml_scratch_seq_destroy(struct aml_scratch *d)
aml_vector_destroy(&scratch->data.requests);
aml_vector_destroy(&scratch->data.tilemap);
aml_area_free(scratch->data.sch_area, scratch->data.sch_ptr);
pthread_mutex_destroy(&scratch->data.lock);
return 0;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment