Doxygen looks in more directories for source
[idzebra-moved-to-github.git] / util / tstlockscope.c
1 /* $Id: tstlockscope.c,v 1.1 2006-07-02 21:22:17 adam Exp $
2    Copyright (C) 2006
3    Index Data ApS
4 */
5
6 /** \file tstlockscope.c
7     \brief tests scope of fcntl locks.. Either "process"  or "thread"
8 */
9
10 #include <errno.h>
11 #include <assert.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <stdlib.h>
15 #include <pthread.h>
16 #if YAZ_POSIX_THREADS
17 #include <fcntl.h>
18 #endif
19
20 int fd;
21
22 const char *scope = "unknown";
23
24 static int file_lock(int fd, int type, int cmd)
25 {
26     struct flock area;
27     area.l_type = type;
28     area.l_whence = SEEK_SET;
29     area.l_len = area.l_start = 0L;
30     
31     return fcntl(fd, cmd, &area);
32 }
33
34 void *run_func(void *arg)
35 {
36     if (file_lock(fd, F_WRLCK, F_SETLK) == -1)
37         scope = "thread";
38     else
39         scope = "process";
40     return 0;
41 }
42
43 int main(int argc, char **argv)
44 {
45     pthread_t child_thread;
46     fd = open("my.LCK", (O_CREAT|O_RDWR), 0666);
47     if (fd == -1)
48     {
49         fprintf(stderr, "open: %s\n", strerror(errno));
50         exit(1);
51     }
52
53     if (file_lock(fd, F_WRLCK, F_SETLKW) == -1)
54     {
55         fprintf(stderr, "fcntl: %s\n", strerror(errno));
56         exit(1);
57     }
58
59 #if YAZ_POSIX_THREADS
60     pthread_create(&child_thread, 0 /* attr */, run_func, 0);
61     pthread_join(child_thread, 0);
62 #endif
63     printf("fcntl lock scope: %s\n", scope);
64     return 0;
65 }
66
67 /*
68  * Local variables:
69  * c-basic-offset: 4
70  * indent-tabs-mode: nil
71  * End:
72  * vim: shiftwidth=4 tabstop=8 expandtab
73  */
74