Use a varnish lock for struct fcore

to be able to use Lck_AssertHeld()
parent 945e754f
...@@ -58,7 +58,6 @@ static unsigned tus_file_srvref(struct tus_file_core *); ...@@ -58,7 +58,6 @@ static unsigned tus_file_srvref(struct tus_file_core *);
static unsigned tus_file_unref(struct tus_file_core *); static unsigned tus_file_unref(struct tus_file_core *);
static void tus_touch(const struct tus_file_core *, VCL_DURATION); static void tus_touch(const struct tus_file_core *, VCL_DURATION);
/* /*
* ------------------------------------------------------------ * ------------------------------------------------------------
* specific to TUS_CONCAT * specific to TUS_CONCAT
...@@ -123,7 +122,6 @@ tus_file_final_concat(struct VPFX(tus_server) *srv, ...@@ -123,7 +122,6 @@ tus_file_final_concat(struct VPFX(tus_server) *srv,
const struct tus_file_disk *pdisk; const struct tus_file_disk *pdisk;
struct vsb *vsb; struct vsb *vsb;
unsigned i, n = 0; unsigned i, n = 0;
struct timespec ts;
ssize_t length = 0; ssize_t length = 0;
size_t l, ml; size_t l, ml;
...@@ -153,9 +151,6 @@ tus_file_final_concat(struct VPFX(tus_server) *srv, ...@@ -153,9 +151,6 @@ tus_file_final_concat(struct VPFX(tus_server) *srv,
if (n == 0) if (n == 0)
goto err; goto err;
// wait for parts to become ready
AZ(clock_gettime(CLOCK_REALTIME, &ts));
ts.tv_sec += READY_TIMEOUT;
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
part = parts[i]; part = parts[i];
AN(part); AN(part);
...@@ -167,13 +162,13 @@ tus_file_final_concat(struct VPFX(tus_server) *srv, ...@@ -167,13 +162,13 @@ tus_file_final_concat(struct VPFX(tus_server) *srv,
continue; continue;
} }
AZ(pthread_mutex_lock(&part->mtx)); Lck_Lock(&part->mtx);
errno = EINTR; errno = EINTR;
while (part->ptr == NULL && errno == EINTR) { while (part->ptr == NULL && errno == EINTR) {
errno = pthread_cond_timedwait(&part->cond, errno = Lck_CondWaitTimeout(&part->cond,
&part->mtx, &ts); &part->mtx, READY_TIMEOUT);
} }
AZ(pthread_mutex_unlock(&part->mtx)); Lck_Unlock(&part->mtx);
if (part->ptr == NULL) if (part->ptr == NULL)
goto err; goto err;
assert(TFCP_READABLE(part->ptr_type)); assert(TFCP_READABLE(part->ptr_type));
...@@ -394,7 +389,7 @@ tus_file_core_new(struct VPFX(tus_server) *srv, ...@@ -394,7 +389,7 @@ tus_file_core_new(struct VPFX(tus_server) *srv,
fcore->server = srv; fcore->server = srv;
fcore->fd = fd; fcore->fd = fd;
REPLACE(fcore->filename, filename); REPLACE(fcore->filename, filename);
AZ(pthread_mutex_init(&fcore->mtx, NULL)); Lck_New(&fcore->mtx, lck_fcore);
AZ(pthread_cond_init(&fcore->cond, NULL)); AZ(pthread_cond_init(&fcore->cond, NULL));
fcore->disk = fdisk; fcore->disk = fdisk;
fcore->srvref = 1; fcore->srvref = 1;
...@@ -404,7 +399,7 @@ tus_file_core_new(struct VPFX(tus_server) *srv, ...@@ -404,7 +399,7 @@ tus_file_core_new(struct VPFX(tus_server) *srv,
return (fcore); return (fcore);
} }
AZ(pthread_mutex_destroy(&fcore->mtx)); Lck_Delete(&fcore->mtx);
AZ(pthread_cond_destroy(&fcore->cond)); AZ(pthread_cond_destroy(&fcore->cond));
REPLACE(fcore->filename, NULL); REPLACE(fcore->filename, NULL);
FREE_OBJ(fcore); FREE_OBJ(fcore);
...@@ -541,7 +536,7 @@ tus_file_fini(struct tus_file_core *fcore) ...@@ -541,7 +536,7 @@ tus_file_fini(struct tus_file_core *fcore)
AZ(fdisk); AZ(fdisk);
assert(fcore->fd == -1); assert(fcore->fd == -1);
AZ(pthread_mutex_destroy(&fcore->mtx)); Lck_Delete(&fcore->mtx);
AZ(pthread_cond_destroy(&fcore->cond)); AZ(pthread_cond_destroy(&fcore->cond));
REPLACE(fcore->filename, NULL); REPLACE(fcore->filename, NULL);
FREE_OBJ(fcore); FREE_OBJ(fcore);
...@@ -561,7 +556,7 @@ tus_file_trylock(struct tus_file_core **fcorep) ...@@ -561,7 +556,7 @@ tus_file_trylock(struct tus_file_core **fcorep)
CHECK_OBJ(fcore, VMOD_TUS_FILE_CORE_MAGIC); CHECK_OBJ(fcore, VMOD_TUS_FILE_CORE_MAGIC);
do { do {
err = pthread_mutex_trylock(&fcore->mtx); err = Lck_Trylock(&fcore->mtx);
if (err == 0) if (err == 0)
return (err); return (err);
} while (err == EINTR); } while (err == EINTR);
...@@ -577,7 +572,7 @@ tus_file_unlock(struct tus_file_core **fcorep) ...@@ -577,7 +572,7 @@ tus_file_unlock(struct tus_file_core **fcorep)
struct tus_file_core *fcore; struct tus_file_core *fcore;
TAKE_OBJ_NOTNULL(fcore, fcorep, VMOD_TUS_FILE_CORE_MAGIC); TAKE_OBJ_NOTNULL(fcore, fcorep, VMOD_TUS_FILE_CORE_MAGIC);
AZ(pthread_mutex_unlock(&fcore->mtx)); Lck_Unlock(&fcore->mtx);
} }
/* under both fcore and server mtx */ /* under both fcore and server mtx */
...@@ -651,7 +646,7 @@ tus_file_exp(struct tus_file_core **fcorep) ...@@ -651,7 +646,7 @@ tus_file_exp(struct tus_file_core **fcorep)
TAKE_OBJ_NOTNULL(fcore, fcorep, VMOD_TUS_FILE_CORE_MAGIC); TAKE_OBJ_NOTNULL(fcore, fcorep, VMOD_TUS_FILE_CORE_MAGIC);
AZ(pthread_mutex_lock(&fcore->mtx)); Lck_Lock(&fcore->mtx);
tus_file_remove(fcore); tus_file_remove(fcore);
(void) tus_file_unref_locked(fcore); (void) tus_file_unref_locked(fcore);
} }
...@@ -666,7 +661,7 @@ tus_file_shutdown(struct VPFX(tus_server) *srv) ...@@ -666,7 +661,7 @@ tus_file_shutdown(struct VPFX(tus_server) *srv)
tus_server_lock(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)); Lck_Lock(&fcore->mtx);
tus_file_del_shutdown(&fcore); tus_file_del_shutdown(&fcore);
AZ(fcore); AZ(fcore);
} }
...@@ -693,9 +688,9 @@ tus_file_ref(struct tus_file_core *fcore) ...@@ -693,9 +688,9 @@ tus_file_ref(struct tus_file_core *fcore)
int r; int r;
CHECK_OBJ_NOTNULL(fcore, VMOD_TUS_FILE_CORE_MAGIC); CHECK_OBJ_NOTNULL(fcore, VMOD_TUS_FILE_CORE_MAGIC);
AZ(pthread_mutex_lock(&fcore->mtx)); Lck_Lock(&fcore->mtx);
r = fcore->ref++ + fcore->srvref; r = fcore->ref++ + fcore->srvref;
AZ(pthread_mutex_unlock(&fcore->mtx)); Lck_Unlock(&fcore->mtx);
assert(r >= 0); assert(r >= 0);
return (r); return (r);
} }
...@@ -709,7 +704,7 @@ tus_file_unref_locked(struct tus_file_core *fcore) ...@@ -709,7 +704,7 @@ tus_file_unref_locked(struct tus_file_core *fcore)
CHECK_OBJ_NOTNULL(fcore, VMOD_TUS_FILE_CORE_MAGIC); CHECK_OBJ_NOTNULL(fcore, VMOD_TUS_FILE_CORE_MAGIC);
r = --fcore->ref + fcore->srvref; r = --fcore->ref + fcore->srvref;
AZ(pthread_mutex_unlock(&fcore->mtx)); Lck_Unlock(&fcore->mtx);
assert (r >= 0); assert (r >= 0);
if (r == 0) if (r == 0)
tus_file_fini(fcore); tus_file_fini(fcore);
...@@ -721,7 +716,7 @@ tus_file_unref(struct tus_file_core *fcore) ...@@ -721,7 +716,7 @@ tus_file_unref(struct tus_file_core *fcore)
{ {
CHECK_OBJ_NOTNULL(fcore, VMOD_TUS_FILE_CORE_MAGIC); CHECK_OBJ_NOTNULL(fcore, VMOD_TUS_FILE_CORE_MAGIC);
AZ(pthread_mutex_lock(&fcore->mtx)); Lck_Lock(&fcore->mtx);
return (tus_file_unref_locked(fcore)); return (tus_file_unref_locked(fcore));
} }
......
...@@ -35,6 +35,11 @@ ...@@ -35,6 +35,11 @@
#include "tus_server.h" #include "tus_server.h"
/* ============================================================
* vmod shared object globals in vmod_tus.c
*/
extern struct VSC_lck *lck_fcore;
#define TUS_PATH_MAX PATH_MAX #define TUS_PATH_MAX PATH_MAX
#define TUS_METADATA_MAX 2048u // ballpark of AWS S3 metadata #define TUS_METADATA_MAX 2048u // ballpark of AWS S3 metadata
...@@ -84,7 +89,7 @@ struct tus_file_core { ...@@ -84,7 +89,7 @@ struct tus_file_core {
VSPLAY_ENTRY(tus_file_core) entry; VSPLAY_ENTRY(tus_file_core) entry;
pthread_mutex_t mtx; struct lock mtx;
pthread_cond_t cond; pthread_cond_t cond;
struct tus_file_disk *disk; struct tus_file_disk *disk;
unsigned exp_idx; unsigned exp_idx;
......
...@@ -33,8 +33,7 @@ ...@@ -33,8 +33,7 @@
#include <unistd.h> #include <unistd.h>
#include <stdio.h> #include <stdio.h>
#include "vdef.h" #include "cache/cache.h" // for struct lock
#include "vrt.h"
#include "miniobj.h" #include "miniobj.h"
#include "vas.h" #include "vas.h"
#include "vbh.h" #include "vbh.h"
......
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