2 * Copyright (C) 1995-2006, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: tstflock.c,v 1.15 2006-07-05 12:33:38 adam Exp $
21 #include <sys/types.h>
44 #include <idzebra/flock.h>
47 static char seq[1000];
48 static char *seqp = 0;
50 #define NUM_THREADS 100
53 pthread_cond_t sleep_cond = PTHREAD_COND_INITIALIZER;
54 pthread_mutex_t sleep_mutex = PTHREAD_MUTEX_INITIALIZER;
59 static void small_sleep()
65 struct timespec abstime;
68 gettimeofday(&now, 0);
69 abstime.tv_sec = now.tv_sec;
70 abstime.tv_nsec = 1000000 + now.tv_usec * 1000;
71 if (abstime.tv_nsec > 1000000000) /* 1s = 1e9 ns */
73 abstime.tv_nsec -= 1000000000;
76 pthread_mutex_lock(&sleep_mutex);
77 pthread_cond_timedwait(&sleep_cond, &sleep_mutex, &abstime);
78 pthread_mutex_unlock(&sleep_mutex);
83 void *run_func(void *arg)
86 int *pdata = (int*) arg;
87 int use_write_lock = *pdata;
88 ZebraLockHandle lh = zebra_lock_create(0, "my.LCK");
91 int write_lock = use_write_lock;
93 if (use_write_lock == 2) /* random lock */
94 write_lock = (rand() & 3) == 3 ? 1 : 0;
100 write(test_fd, "L", 1);
104 write(test_fd, "U", 1);
112 write(test_fd, "l", 1);
116 write(test_fd, "u", 1);
121 zebra_lock_destroy(lh);
127 DWORD WINAPI ThreadProc(void *p)
134 static void tst_thread(int num, int write_flag)
137 HANDLE handles[NUM_THREADS];
138 DWORD dwThreadId[NUM_THREADS];
140 #if YAZ_POSIX_THREADS
141 pthread_t child_thread[NUM_THREADS];
143 int i, id[NUM_THREADS];
146 assert (num <= NUM_THREADS);
147 for (i = 0; i < num; i++)
150 #if YAZ_POSIX_THREADS
151 pthread_create(&child_thread[i], 0 /* attr */, run_func, &id[i]);
156 void *pData = &id[i];
157 handles[i] = CreateThread(
158 NULL, /* default security attributes */
159 0, /* use default stack size */
160 ThreadProc, /* thread function */
161 pData, /* argument to thread function */
162 0, /* use default creation flags */
163 &dwThreadId[i]); /* returns the thread identifier */
168 #if YAZ_POSIX_THREADS
169 for (i = 0; i<num; i++)
170 pthread_join(child_thread[i], 0);
173 WaitForMultipleObjects(num, handles, TRUE, INFINITE);
175 for (i = 0; i < num; i++)
176 YAZ_CHECK(id[i] == 123);
178 yaz_log(YLOG_LOG, "tst_thread(%d,%d) returns seq=%s",
179 num, write_flag, seq);
184 tst_thread(4, 1); /* write locks */
190 YAZ_CHECK_EQ(seq[i], 'L');
191 YAZ_CHECK_EQ(seq[i+1], 'U');
196 tst_thread(6, 0); /* read locks */
198 tst_thread(20, 2); /* random locks */
207 for (i = 0; i<2; i++)
216 for (i = 0; i<2; i++)
219 waitpid(pid[i], &status, 0);
220 YAZ_CHECK(status == 0);
227 int main(int argc, char **argv)
230 YAZ_CHECK_INIT(argc, argv);
232 sprintf(logname, "%.200s.log", argv[0]);
233 yaz_log_init_file(logname);
235 /* log time + thread id (%!) */
236 yaz_log_time_format("%c:%!");
238 /* ensure the flock system logs in our test */
239 yaz_log_init_level(yaz_log_mask_str("flock"));
243 test_fd = open("tstflock.out", (O_BINARY|O_CREAT|O_RDWR), 0666);
244 YAZ_CHECK(test_fd != -1);
256 * indent-tabs-mode: nil
258 * vim: shiftwidth=4 tabstop=8 expandtab