From b7963f43864a0e8d4e738cf46f7ee4baad6db420 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Sun, 2 Jul 2006 21:22:17 +0000 Subject: [PATCH] Added program to test scope of fcntl lock: process or thread --- util/.cvsignore | 3 ++- util/Makefile.am | 5 ++-- util/tstlockscope.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 util/tstlockscope.c diff --git a/util/.cvsignore b/util/.cvsignore index 9b6b81f..1612649 100644 --- a/util/.cvsignore +++ b/util/.cvsignore @@ -8,6 +8,7 @@ idzebra-config *.lo *.la *.log -tstflock my.LCK +tstflock tstflock.out +tstflockscope diff --git a/util/Makefile.am b/util/Makefile.am index 72832ad..bb4442b 100644 --- a/util/Makefile.am +++ b/util/Makefile.am @@ -1,10 +1,10 @@ -## $Id: Makefile.am,v 1.21 2006-06-29 13:48:07 adam Exp $ +## $Id: Makefile.am,v 1.22 2006-07-02 21:22:17 adam Exp $ lib_LTLIBRARIES = libidzebra-util.la noinst_PROGRAMS = passtest -check_PROGRAMS = tstcharmap tstflock +check_PROGRAMS = tstcharmap tstflock tstlockscope TESTS = $(check_PROGRAMS) @@ -27,6 +27,7 @@ tstcharmap_SOURCES = tstcharmap.c tstflock_SOURCES = tstflock.c +tstlockscope_SOURCES = tstlockscope.c clean-local: -rm -rf *.LCK diff --git a/util/tstlockscope.c b/util/tstlockscope.c new file mode 100644 index 0000000..56faa7b --- /dev/null +++ b/util/tstlockscope.c @@ -0,0 +1,74 @@ +/* $Id: tstlockscope.c,v 1.1 2006-07-02 21:22:17 adam Exp $ + Copyright (C) 2006 + Index Data ApS +*/ + +/** \file tstlockscope.c + \brief tests scope of fcntl locks.. Either "process" or "thread" +*/ + +#include +#include +#include +#include +#include +#include +#if YAZ_POSIX_THREADS +#include +#endif + +int fd; + +const char *scope = "unknown"; + +static int file_lock(int fd, int type, int cmd) +{ + struct flock area; + area.l_type = type; + area.l_whence = SEEK_SET; + area.l_len = area.l_start = 0L; + + return fcntl(fd, cmd, &area); +} + +void *run_func(void *arg) +{ + if (file_lock(fd, F_WRLCK, F_SETLK) == -1) + scope = "thread"; + else + scope = "process"; + return 0; +} + +int main(int argc, char **argv) +{ + pthread_t child_thread; + fd = open("my.LCK", (O_CREAT|O_RDWR), 0666); + if (fd == -1) + { + fprintf(stderr, "open: %s\n", strerror(errno)); + exit(1); + } + + if (file_lock(fd, F_WRLCK, F_SETLKW) == -1) + { + fprintf(stderr, "fcntl: %s\n", strerror(errno)); + exit(1); + } + +#if YAZ_POSIX_THREADS + pthread_create(&child_thread, 0 /* attr */, run_func, 0); + pthread_join(child_thread, 0); +#endif + printf("fcntl lock scope: %s\n", scope); + return 0; +} + +/* + * Local variables: + * c-basic-offset: 4 + * indent-tabs-mode: nil + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */ + -- 1.7.10.4