diff options
Diffstat (limited to 'src/platform/posix')
| -rw-r--r-- | src/platform/posix/posix_file.c | 27 | ||||
| -rw-r--r-- | src/platform/posix/posix_impl.h | 4 |
2 files changed, 31 insertions, 0 deletions
diff --git a/src/platform/posix/posix_file.c b/src/platform/posix/posix_file.c index 7863fdee..83d045fa 100644 --- a/src/platform/posix/posix_file.c +++ b/src/platform/posix/posix_file.c @@ -260,6 +260,33 @@ nni_plat_file_basename(const char *path) return (path); } +int +nni_plat_file_lock(const char *path, nni_plat_flock *lk) +{ + int fd; + if ((fd = open(path, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)) < 0) { + return (nni_plat_errno(errno)); + } + if (lockf(fd, F_TLOCK, 0) < 0) { + int rv = errno; + close(fd); + if (rv == EAGAIN) { + return (NNG_EBUSY); + } + return (nni_plat_errno(rv)); + } + lk->fd = fd; + return (0); +} + +void +nni_plat_file_unlock(nni_plat_flock *lk) +{ + int fd = lk->fd; + lk->fd = -1; + (void) close(fd); +} + char * nni_plat_temp_dir(void) { diff --git a/src/platform/posix/posix_impl.h b/src/platform/posix/posix_impl.h index 0b2a09f0..3616c69b 100644 --- a/src/platform/posix/posix_impl.h +++ b/src/platform/posix/posix_impl.h @@ -76,6 +76,10 @@ struct nni_plat_thr { void *arg; }; +struct nni_plat_flock { + int fd; +}; + #define NNG_PLATFORM_DIR_SEP "/" #endif |
