Moved file locking utilities from index/lockutil.c to util/flock.c
[idzebra-moved-to-github.git] / util / tstflock.c
1 /*
2  * Copyright (C) 1995-2005, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  * $Id: tstflock.c,v 1.1 2006-03-23 09:15:25 adam Exp $
6  */
7
8 #include <yaz/test.h>
9 #if YAZ_POSIX_THREADS
10 #include <pthread.h>
11 #endif
12
13 #include <idzebra/flock.h>
14 #include <unistd.h>
15 #include <string.h>
16
17 static char seq[20];
18 static char *seqp = seq;
19
20 void *run_func(void *arg)
21 {
22     int i;
23     ZebraLockHandle lh = zebra_lock_create(0, "my.LCK");
24     for (i = 0; i<2; i++)
25     {
26         zebra_lock_w(lh);
27      
28         *seqp++ = 'L';
29         sleep(1);
30         *seqp++ = 'U';
31         
32         zebra_unlock(lh);
33     }
34     zebra_lock_destroy(lh);
35     return 0;
36 }
37
38 static void tst1()
39 {
40     pthread_t child_thread[2];
41     int id1 = 1;
42     int id2 = 2;
43     pthread_create(&child_thread[0], 0 /* attr */, run_func, &id1);
44     pthread_create(&child_thread[1], 0 /* attr */, run_func, &id2);
45     pthread_join(child_thread[0], 0);
46     pthread_join(child_thread[1], 0);
47     *seqp++ = '\0';
48 }
49
50 int main(int argc, char **argv)
51 {
52     YAZ_CHECK_INIT(argc, argv);
53     tst1();
54
55 #if 0
56     /* does not pass.. for bug 529 */
57     YAZ_CHECK(strcmp(seq, "LULULULU") == 0);
58 #endif
59     YAZ_CHECK_TERM;
60 }
61 /*
62  * Local variables:
63  * c-basic-offset: 4
64  * indent-tabs-mode: nil
65  * End:
66  * vim: shiftwidth=4 tabstop=8 expandtab
67  */
68