tdb: Handle TDB_NEXT_LOCK_ERR in tdb_traverse_internal
[samba.git] / lib / tdb / test / run-mutex-allrecord-bench.c
1 #include "../common/tdb_private.h"
2 #include "../common/io.c"
3 #include "../common/tdb.c"
4 #include "../common/lock.c"
5 #include "../common/freelist.c"
6 #include "../common/traverse.c"
7 #include "../common/transaction.c"
8 #include "../common/error.c"
9 #include "../common/open.c"
10 #include "../common/check.c"
11 #include "../common/hash.c"
12 #include "../common/mutex.c"
13 #include "tap-interface.h"
14 #include <stdlib.h>
15 #include <sys/types.h>
16 #include <sys/wait.h>
17 #include <stdarg.h>
18
19 static TDB_DATA key, data;
20
21 static void log_fn(struct tdb_context *tdb, enum tdb_debug_level level,
22                    const char *fmt, ...)
23 {
24         va_list ap;
25         va_start(ap, fmt);
26         vfprintf(stderr, fmt, ap);
27         va_end(ap);
28 }
29
30 static double timeval_elapsed2(const struct timeval *tv1, const struct timeval *tv2)
31 {
32         return (tv2->tv_sec - tv1->tv_sec) +
33                (tv2->tv_usec - tv1->tv_usec)*1.0e-6;
34 }
35
36 static double timeval_elapsed(const struct timeval *tv)
37 {
38         struct timeval tv2;
39         gettimeofday(&tv2, NULL);
40         return timeval_elapsed2(tv, &tv2);
41 }
42
43 /* The code should barf on TDBs created with rwlocks. */
44 int main(int argc, char *argv[])
45 {
46         struct tdb_context *tdb;
47         unsigned int log_count;
48         struct tdb_logging_context log_ctx = { log_fn, &log_count };
49         int ret;
50         struct timeval start;
51         double elapsed;
52         bool runtime_support;
53
54         runtime_support = tdb_runtime_check_for_robust_mutexes();
55
56         if (!runtime_support) {
57                 skip(1, "No robust mutex support");
58                 return exit_status();
59         }
60
61         key.dsize = strlen("hi");
62         key.dptr = discard_const_p(uint8_t, "hi");
63         data.dsize = strlen("world");
64         data.dptr = discard_const_p(uint8_t, "world");
65
66         tdb = tdb_open_ex("mutex-allrecord-bench.tdb", 1000000,
67                           TDB_INCOMPATIBLE_HASH|
68                           TDB_MUTEX_LOCKING|
69                           TDB_CLEAR_IF_FIRST,
70                           O_RDWR|O_CREAT, 0755, &log_ctx, NULL);
71         ok(tdb, "tdb_open_ex should succeed");
72
73         gettimeofday(&start, NULL);
74         ret = tdb_allrecord_lock(tdb, F_WRLCK, TDB_LOCK_WAIT, false);
75         elapsed = timeval_elapsed(&start);
76
77         ok(ret == 0, "tdb_allrecord_lock should succeed");
78
79         diag("allrecord_lock took %f seconds", elapsed);
80
81         return exit_status();
82 }