2 * Copyright (C) 1995-2006, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: tstflock.c,v 1.13 2006-07-03 21:19:13 adam Exp $
19 #include <sys/types.h>
38 #include <idzebra/flock.h>
41 static char seq[1000];
42 static char *seqp = 0;
44 #define NUM_THREADS 100
47 pthread_cond_t sleep_cond = PTHREAD_COND_INITIALIZER;
48 pthread_mutex_t sleep_mutex = PTHREAD_MUTEX_INITIALIZER;
53 static void small_sleep()
59 struct timespec abstime;
62 gettimeofday(&now, 0);
63 abstime.tv_sec = now.tv_sec;
64 abstime.tv_nsec = 1000000 + now.tv_usec * 1000;
65 if (abstime.tv_nsec > 1000000000) /* 1s = 1e9 ns */
67 abstime.tv_nsec -= 1000000000;
70 pthread_mutex_lock(&sleep_mutex);
71 pthread_cond_timedwait(&sleep_cond, &sleep_mutex, &abstime);
72 pthread_mutex_unlock(&sleep_mutex);
77 void *run_func(void *arg)
80 int *pdata = (int*) arg;
81 int use_write_lock = *pdata;
82 ZebraLockHandle lh = zebra_lock_create(0, "my.LCK");
85 int write_lock = use_write_lock;
87 if (use_write_lock == 2) /* random lock */
88 write_lock = (rand() & 3) == 3 ? 1 : 0;
94 write(test_fd, "L", 1);
98 write(test_fd, "U", 1);
106 write(test_fd, "l", 1);
110 write(test_fd, "u", 1);
115 zebra_lock_destroy(lh);
121 DWORD WINAPI ThreadProc(void *p)
128 static void tst_thread(int num, int write_flag)
131 HANDLE handles[NUM_THREADS];
132 DWORD dwThreadId[NUM_THREADS];
134 #if YAZ_POSIX_THREADS
135 pthread_t child_thread[NUM_THREADS];
137 int i, id[NUM_THREADS];
140 assert (num <= NUM_THREADS);
141 for (i = 0; i < num; i++)
144 #if YAZ_POSIX_THREADS
145 pthread_create(&child_thread[i], 0 /* attr */, run_func, &id[i]);
150 void *pData = &id[i];
151 handles[i] = CreateThread(
152 NULL, /* default security attributes */
153 0, /* use default stack size */
154 ThreadProc, /* thread function */
155 pData, /* argument to thread function */
156 0, /* use default creation flags */
157 &dwThreadId[i]); /* returns the thread identifier */
162 #if YAZ_POSIX_THREADS
163 for (i = 0; i<num; i++)
164 pthread_join(child_thread[i], 0);
167 WaitForMultipleObjects(num, handles, TRUE, INFINITE);
169 for (i = 0; i < num; i++)
170 YAZ_CHECK(id[i] == 123);
172 yaz_log(YLOG_LOG, "tst_thread(%d,%d) returns seq=%s",
173 num, write_flag, seq);
178 tst_thread(4, 1); /* write locks */
184 YAZ_CHECK_EQ(seq[i], 'L');
185 YAZ_CHECK_EQ(seq[i+1], 'U');
191 tst_thread(6, 0); /* read locks */
194 tst_thread(20, 2); /* random locks */
204 for (i = 0; i<2; i++)
213 for (i = 0; i<2; i++)
216 waitpid(pid[i], &status, 0);
217 YAZ_CHECK(status == 0);
225 int main(int argc, char **argv)
228 YAZ_CHECK_INIT(argc, argv);
230 sprintf(logname, "%.200s.log", argv[0]);
231 yaz_log_init_file(logname);
233 yaz_log_time_format("%s:%!");
237 test_fd = open("tstflock.out", (O_BINARY|O_CREAT|O_RDWR), 0666);
238 YAZ_CHECK(test_fd != -1);
250 * indent-tabs-mode: nil
252 * vim: shiftwidth=4 tabstop=8 expandtab