Move locking to tus_file_del()

parent 2966db6a
...@@ -209,7 +209,6 @@ tus_file_final_birth(struct tus_file_core **fcorep, ...@@ -209,7 +209,6 @@ tus_file_final_birth(struct tus_file_core **fcorep,
struct concat_embryo *embryo) struct concat_embryo *embryo)
{ {
struct tus_file_core *fcore; struct tus_file_core *fcore;
struct VPFX(tus_server) *srv;
struct tus_file_disk *fdisk; struct tus_file_disk *fdisk;
struct vsb *vsb; struct vsb *vsb;
...@@ -226,12 +225,8 @@ tus_file_final_birth(struct tus_file_core **fcorep, ...@@ -226,12 +225,8 @@ tus_file_final_birth(struct tus_file_core **fcorep,
vsb = embryo->spec_vsb; vsb = embryo->spec_vsb;
assert(fcore->fd >= 0); assert(fcore->fd >= 0);
if (write(fcore->fd, VSB_data(vsb), VSB_len(vsb)) < 0) { if (write(fcore->fd, VSB_data(vsb), VSB_len(vsb)) < 0)
srv = fcore->server;
tus_server_lock(srv);
tus_file_del(&fcore); tus_file_del(&fcore);
tus_server_unlock(srv);
}
VSB_destroy(&vsb); VSB_destroy(&vsb);
memset(embryo, 0, sizeof *embryo); memset(embryo, 0, sizeof *embryo);
*fcorep = fcore; *fcorep = fcore;
...@@ -519,17 +514,39 @@ tus_file_unlock(struct tus_file_core **fcorep) ...@@ -519,17 +514,39 @@ tus_file_unlock(struct tus_file_core **fcorep)
AZ(pthread_mutex_unlock(&fcore->mtx)); AZ(pthread_mutex_unlock(&fcore->mtx));
} }
/* caller holds tus_server mtx */
static void
tus_file_del_locked(struct tus_file_core **fcorep)
{
struct tus_file_core *fcore;
const struct tus_file_core *rm;
TAKE_OBJ_NOTNULL(fcore, fcorep, VMOD_TUS_FILE_CORE_MAGIC);
rm = VSPLAY_REMOVE(tus_files, tus_server_files(fcore->server),
fcore);
assert (rm == fcore);
tus_exp_delete(fcore);
(void) tus_file_unref_locked(fcore);
}
/* under tus_server mtx to protect tus_files */ /* under tus_server mtx to protect tus_files */
void void
tus_file_del(struct tus_file_core **fcorep) tus_file_del(struct tus_file_core **fcorep)
{ {
struct VPFX(tus_server) *srv;
struct tus_file_core *fcore; struct tus_file_core *fcore;
const struct tus_file_core *rm; const struct tus_file_core *rm;
TAKE_OBJ_NOTNULL(fcore, fcorep, VMOD_TUS_FILE_CORE_MAGIC); TAKE_OBJ_NOTNULL(fcore, fcorep, VMOD_TUS_FILE_CORE_MAGIC);
srv = fcore->server;
tus_server_lock(srv);
rm = VSPLAY_REMOVE(tus_files, tus_server_files(srv), fcore);
tus_server_unlock(srv);
rm = VSPLAY_REMOVE(tus_files, tus_server_files(fcore->server),
fcore);
assert (rm == fcore); assert (rm == fcore);
tus_exp_delete(fcore); tus_exp_delete(fcore);
...@@ -543,12 +560,14 @@ tus_file_shutdown(struct VPFX(tus_server) *srv) ...@@ -543,12 +560,14 @@ tus_file_shutdown(struct VPFX(tus_server) *srv)
struct tus_file_core *fcore; struct tus_file_core *fcore;
struct tus_files *files = tus_server_files(srv); struct tus_files *files = tus_server_files(srv);
tus_server_lock(srv);
VSPLAY_FOREACH(fcore, tus_files, files) { VSPLAY_FOREACH(fcore, tus_files, files) {
REPLACE(fcore->filename, NULL); // prevent unlink REPLACE(fcore->filename, NULL); // prevent unlink
AZ(pthread_mutex_lock(&fcore->mtx)); AZ(pthread_mutex_lock(&fcore->mtx));
tus_file_del(&fcore); tus_file_del_locked(&fcore);
AZ(fcore); AZ(fcore);
} }
tus_server_unlock(srv);
} }
#ifdef BAD_IDEA #ifdef BAD_IDEA
......
...@@ -70,7 +70,6 @@ fcore_when(const void *p) ...@@ -70,7 +70,6 @@ fcore_when(const void *p)
static void * static void *
tus_exp_thread(void *p) tus_exp_thread(void *p)
{ {
struct VPFX(tus_server) *srv;
struct tus_file_core *fcore; struct tus_file_core *fcore;
struct timespec ts; struct timespec ts;
struct tus_exp *e; struct tus_exp *e;
...@@ -101,16 +100,13 @@ tus_exp_thread(void *p) ...@@ -101,16 +100,13 @@ tus_exp_thread(void *p)
AZ(pthread_mutex_unlock(&e->mtx)); AZ(pthread_mutex_unlock(&e->mtx));
AN(fcore); AN(fcore);
srv = fcore->server;
while (fcore != NULL) { while (fcore != NULL) {
struct tus_file_core *has_lock = fcore; struct tus_file_core *has_lock = fcore;
tus_server_lock(srv);
(void) tus_file_trylock(&has_lock); (void) tus_file_trylock(&has_lock);
if (has_lock != NULL) if (has_lock != NULL)
tus_file_del(&fcore); tus_file_del(&fcore);
tus_server_unlock(srv);
if (has_lock == NULL) { if (has_lock == NULL) {
fprintf(stderr, "tus expiry: fcore %p locked\n", fprintf(stderr, "tus expiry: fcore %p locked\n",
......
...@@ -362,9 +362,7 @@ tus_request(VRT_CTX, struct VPFX(tus_server) *tussrv, ...@@ -362,9 +362,7 @@ tus_request(VRT_CTX, struct VPFX(tus_server) *tussrv,
return (0); return (0);
} }
AZ(pthread_mutex_lock(&tussrv->mtx));
tus_file_del(&r->fcore); tus_file_del(&r->fcore);
AZ(pthread_mutex_unlock(&tussrv->mtx));
AZ(r->fcore); AZ(r->fcore);
r->status = 204; r->status = 204;
} }
...@@ -492,9 +490,7 @@ tus_request(VRT_CTX, struct VPFX(tus_server) *tussrv, ...@@ -492,9 +490,7 @@ tus_request(VRT_CTX, struct VPFX(tus_server) *tussrv,
} }
if (r->status == 413) { if (r->status == 413) {
AZ(pthread_mutex_lock(&tussrv->mtx));
tus_file_del(&r->fcore); tus_file_del(&r->fcore);
AZ(pthread_mutex_unlock(&tussrv->mtx));
AZ(r->fcore); AZ(r->fcore);
return (0); 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