2 * Copyright (C) 1995-2006, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: tstflock.c,v 1.10 2006-06-30 13:02:20 adam Exp $
20 #include <idzebra/flock.h>
26 static char seq[1000];
27 static char *seqp = 0;
29 #define NUM_THREADS 100
31 static void small_sleep()
40 void *run_func(void *arg)
43 int *pdata = (int*) arg;
44 int use_write_lock = *pdata;
45 ZebraLockHandle lh = zebra_lock_create(0, "my.LCK");
50 if (use_write_lock == 2)
51 write_lock = (rand() & 3) == 3 ? 1 : 0;
53 write_lock = use_write_lock;
76 zebra_lock_destroy(lh);
82 DWORD WINAPI ThreadProc(void *p)
89 static void tst_thread(int num, int write_flag)
92 HANDLE handles[NUM_THREADS];
93 DWORD dwThreadId[NUM_THREADS];
96 pthread_t child_thread[NUM_THREADS];
98 int i, id[NUM_THREADS];
101 assert (num <= NUM_THREADS);
102 for (i = 0; i < num; i++)
105 #if YAZ_POSIX_THREADS
106 pthread_create(&child_thread[i], 0 /* attr */, run_func, &id[i]);
111 void *pData = &id[i];
112 handles[i] = CreateThread(
113 NULL, /* default security attributes */
114 0, /* use default stack size */
115 ThreadProc, /* thread function */
116 pData, /* argument to thread function */
117 0, /* use default creation flags */
118 &dwThreadId[i]); /* returns the thread identifier */
123 #if YAZ_POSIX_THREADS
124 for (i = 0; i<num; i++)
125 pthread_join(child_thread[i], 0);
128 WaitForMultipleObjects(num, handles, TRUE, INFINITE);
130 for (i = 0; i < num; i++)
131 YAZ_CHECK(id[i] == 123);
137 tst_thread(4, 1); /* write locks */
139 printf("seq=%s\n", seq);
146 YAZ_CHECK_EQ(seq[i], 'L');
147 YAZ_CHECK_EQ(seq[i+1], 'U');
153 tst_thread(6, 0); /* read locks */
154 printf("seq=%s\n", seq);
157 tst_thread(20, 2); /* random locks */
158 printf("seq=%s\n", seq);
162 int main(int argc, char **argv)
164 YAZ_CHECK_INIT(argc, argv);
166 yaz_log_time_format("%s:%!");
179 * indent-tabs-mode: nil
181 * vim: shiftwidth=4 tabstop=8 expandtab