Experiments with file locks
[idzebra-moved-to-github.git] / util / tstflock.c
1 /*
2  * Copyright (C) 1995-2006, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  * $Id: tstflock.c,v 1.9 2006-06-30 11:10:17 adam Exp $
6  */
7
8 #include <assert.h>
9 #include <yaz/test.h>
10 #if YAZ_POSIX_THREADS
11 #include <pthread.h>
12 #endif
13 #ifdef WIN32
14 #include <windows.h>
15 #include <process.h>
16 #endif
17
18 #include <idzebra/flock.h>
19 #if HAVE_UNISTD_H
20 #include <unistd.h>
21 #endif
22 #include <string.h>
23
24 static char seq[40];
25 static char *seqp = seq;
26
27 #define NUM_THREADS 5
28
29 static void small_sleep()
30 {
31 #ifdef WIN32
32     Sleep(50);
33 #else
34     sleep(1);
35 #endif
36 }
37
38 void *run_func(void *arg)
39 {
40     int i;
41     int *pdata = (int*) arg;
42     int use_write_lock = *pdata;
43     ZebraLockHandle lh = zebra_lock_create(0, "my.LCK");
44     for (i = 0; i<2; i++)
45     {
46         if (use_write_lock)
47         {
48             zebra_lock_w(lh);
49             
50             *seqp++ = 'L';
51             small_sleep();
52             *seqp++ = 'U';
53             
54             zebra_unlock(lh);
55         }
56         else
57         {
58             zebra_lock_r(lh);
59             
60             *seqp++ = 'l';
61             small_sleep();
62             *seqp++ = 'u';
63             
64             zebra_unlock(lh);
65         }
66     }
67     zebra_lock_destroy(lh);
68     *pdata = 123;
69     return 0;
70 }
71
72 #ifdef WIN32
73 DWORD WINAPI ThreadProc(void *p)
74 {
75     run_func(p);
76     return 0;
77 }
78
79 static void tst_win32(int num)
80 {
81     HANDLE handles[NUM_THREADS];
82     DWORD dwThreadId[NUM_THREADS];
83     int i, id[NUM_THREADS];
84     
85     assert (num <= NUM_THREADS);
86     for (i = 0; i<num; i++)
87     {
88         void *pData = &id[i];
89         id[i] = i >= 2 ? 0 : 1; /* first two are writing.. rest is reading */
90         handles[i] = CreateThread(
91             NULL,              /* default security attributes */
92             0,                 /* use default stack size */
93             ThreadProc,        /* thread function */
94             pData,             /* argument to thread function */
95             0,                 /* use default creation flags */
96             &dwThreadId[i]);   /* returns the thread identifier */
97     }
98     /* join */
99     WaitForMultipleObjects(num, handles, TRUE, INFINITE);
100 }
101 #endif
102
103 #if YAZ_POSIX_THREADS
104 static void tst_pthread(int num)
105 {
106     pthread_t child_thread[NUM_THREADS];
107     int i, id[NUM_THREADS];
108
109     assert (num <= NUM_THREADS);
110     for (i = 0; i<num; i++)
111     {
112         id[i] = i >= 2 ? 0 : 1; /* first two are writing.. rest is reading */
113         pthread_create(&child_thread[i], 0 /* attr */, run_func, &id[i]);
114     }
115
116     for (i = 0; i<num; i++)
117         pthread_join(child_thread[i], 0);
118
119     for (i = 0; i<num; i++)
120         YAZ_CHECK(id[i] == 123);
121 }
122 #endif
123
124 int main(int argc, char **argv)
125 {
126     YAZ_CHECK_INIT(argc, argv);
127
128     yaz_log_time_format("%s:%!");
129
130     zebra_flock_init();
131 #ifdef WIN32
132     tst_win32(2);
133 #endif
134 #if YAZ_POSIX_THREADS
135     tst_pthread(2);
136 #endif
137
138     *seqp++ = '\0';
139 #if 0
140     printf("seq=%s\n", seq);
141 #endif
142 #if 1
143     /* does not pass.. for bug 529 */
144     YAZ_CHECK(strcmp(seq, "LULULULU") == 0); /* for tst_pthread when num=2 */
145 #endif
146     YAZ_CHECK_TERM;
147 }
148
149
150 /*
151  * Local variables:
152  * c-basic-offset: 4
153  * indent-tabs-mode: nil
154  * End:
155  * vim: shiftwidth=4 tabstop=8 expandtab
156  */
157