2 * Copyright (C) 1995-2006, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: tstflock.c,v 1.12 2006-06-30 15:10:29 adam Exp $
17 #include <sys/types.h>
36 #include <idzebra/flock.h>
39 static char seq[1000];
40 static char *seqp = 0;
42 #define NUM_THREADS 100
46 static void small_sleep()
55 void *run_func(void *arg)
58 int *pdata = (int*) arg;
59 int use_write_lock = *pdata;
60 ZebraLockHandle lh = zebra_lock_create(0, "my.LCK");
63 int write_lock = use_write_lock;
65 if (use_write_lock == 2) /* random lock */
66 write_lock = (rand() & 3) == 3 ? 1 : 0;
72 write(test_fd, "L", 1);
76 write(test_fd, "U", 1);
84 write(test_fd, "l", 1);
88 write(test_fd, "u", 1);
93 zebra_lock_destroy(lh);
99 DWORD WINAPI ThreadProc(void *p)
106 static void tst_thread(int num, int write_flag)
109 HANDLE handles[NUM_THREADS];
110 DWORD dwThreadId[NUM_THREADS];
112 #if YAZ_POSIX_THREADS
113 pthread_t child_thread[NUM_THREADS];
115 int i, id[NUM_THREADS];
118 assert (num <= NUM_THREADS);
119 for (i = 0; i < num; i++)
122 #if YAZ_POSIX_THREADS
123 pthread_create(&child_thread[i], 0 /* attr */, run_func, &id[i]);
128 void *pData = &id[i];
129 handles[i] = CreateThread(
130 NULL, /* default security attributes */
131 0, /* use default stack size */
132 ThreadProc, /* thread function */
133 pData, /* argument to thread function */
134 0, /* use default creation flags */
135 &dwThreadId[i]); /* returns the thread identifier */
140 #if YAZ_POSIX_THREADS
141 for (i = 0; i<num; i++)
142 pthread_join(child_thread[i], 0);
145 WaitForMultipleObjects(num, handles, TRUE, INFINITE);
147 for (i = 0; i < num; i++)
148 YAZ_CHECK(id[i] == 123);
150 yaz_log(YLOG_LOG, "tst_thread(%d,%d) returns seq=%s",
151 num, write_flag, seq);
156 tst_thread(4, 1); /* write locks */
162 YAZ_CHECK_EQ(seq[i], 'L');
163 YAZ_CHECK_EQ(seq[i+1], 'U');
169 tst_thread(6, 0); /* read locks */
172 tst_thread(20, 2); /* random locks */
182 for (i = 0; i<2; i++)
191 for (i = 0; i<2; i++)
194 waitpid(pid[i], &status, 0);
195 YAZ_CHECK(status == 0);
203 int main(int argc, char **argv)
206 YAZ_CHECK_INIT(argc, argv);
208 sprintf(logname, "%.200s.log", argv[0]);
209 yaz_log_init_file(logname);
211 yaz_log_time_format("%s:%!");
215 test_fd = open("tstflock.out", (O_BINARY|O_CREAT|O_RDWR), 0666);
216 YAZ_CHECK(test_fd != -1);
228 * indent-tabs-mode: nil
230 * vim: shiftwidth=4 tabstop=8 expandtab