Line data Source code
1 : //
2 : // odb_lock_test: test odb locking with multiple threads
3 : //
4 : // Author: Konstantin Olchanski, 2017-OCT-13
5 : //
6 :
7 : #undef NDEBUG // midas required assert() to be always enabled
8 :
9 : #include <assert.h>
10 :
11 : #include "midas.h"
12 : #include "msystem.h"
13 :
14 0 : void test1(HNDLE hDB)
15 : {
16 0 : printf("test1: test recursive ODB lock\n");
17 0 : printf("lock1\n");
18 0 : db_lock_database(hDB);
19 0 : printf("lock2\n");
20 0 : db_lock_database(hDB);
21 0 : printf("lock3\n");
22 0 : db_lock_database(hDB);
23 0 : printf("sleep\n");
24 0 : sleep(5);
25 0 : printf("unlock3\n");
26 0 : db_unlock_database(hDB);
27 0 : printf("unlock2\n");
28 0 : db_unlock_database(hDB);
29 0 : printf("sleep\n");
30 0 : sleep(5);
31 0 : printf("unlock1\n");
32 0 : db_unlock_database(hDB);
33 0 : printf("done.\n");
34 0 : }
35 :
36 : HNDLE xhDB = -1;
37 : BOOL xThread1done = false;
38 :
39 0 : int thread1(void*)
40 : {
41 0 : HNDLE hDB = xhDB;
42 0 : printf("t1: thread started\n");
43 0 : printf("t1: lock1\n");
44 0 : db_lock_database(hDB);
45 0 : printf("t1: lock1 done\n");
46 0 : printf("t1: sleep\n");
47 0 : sleep(5);
48 0 : printf("t1: unlock1\n");
49 0 : db_unlock_database(hDB);
50 0 : printf("t1: unlock1 done\n");
51 0 : printf("t1: thread done\n");
52 0 : xThread1done = TRUE;
53 0 : return 0;
54 : }
55 :
56 0 : void test2(HNDLE hDB)
57 : {
58 : //int timeout = db_set_lock_timeout(hDB, 0);
59 : //db_set_lock_timeout(hDB, 10000);
60 0 : printf("test2: test multithread locking\n");
61 0 : printf("t0: lock1\n");
62 0 : db_lock_database(hDB);
63 0 : printf("t0: lock1 done\n");
64 0 : xhDB = hDB;
65 0 : ss_thread_create(thread1, NULL);
66 0 : printf("t0: sleep\n");
67 0 : sleep(5);
68 0 : printf("t0: unlock1\n");
69 0 : db_unlock_database(hDB);
70 0 : printf("t0: lock2\n");
71 0 : db_lock_database(hDB);
72 0 : printf("t0: lock2 done\n");
73 0 : printf("t0: unlock2\n");
74 0 : db_unlock_database(hDB);
75 0 : printf("t0: unlock2 done\n");
76 0 : printf("t0: waiting for thread1...\n");
77 0 : while (!xThread1done) {
78 0 : printf("t0: waiting\n");
79 0 : sleep(1);
80 : }
81 0 : printf("t0: done.\n");
82 : //db_set_lock_timeout(hDB, timeout);
83 0 : }
84 :
85 0 : int main(int argc, char *argv[])
86 : {
87 0 : setbuf(stdout, NULL);
88 0 : setbuf(stderr, NULL);
89 0 : int status = 0;
90 : char host_name[256];
91 : char expt_name[256];
92 0 : host_name[0] = 0;
93 0 : expt_name[0] = 0;
94 :
95 0 : cm_get_environment(host_name, sizeof(host_name), expt_name, sizeof(expt_name));
96 :
97 0 : status = cm_connect_experiment1(host_name, expt_name, "odb_lock_test", 0, DEFAULT_ODB_SIZE, 0);
98 0 : assert(status == CM_SUCCESS);
99 :
100 : HNDLE hDB;
101 0 : status = cm_get_experiment_database(&hDB, NULL);
102 0 : assert(status == CM_SUCCESS);
103 :
104 0 : cm_set_watchdog_params(0, 0);
105 :
106 0 : test1(hDB);
107 0 : test2(hDB);
108 :
109 0 : status = cm_disconnect_experiment();
110 0 : assert(status == CM_SUCCESS);
111 :
112 0 : return 0;
113 : }
114 :
115 : /* emacs
116 : * Local Variables:
117 : * tab-width: 8
118 : * c-basic-offset: 3
119 : * indent-tabs-mode: nil
120 : * End:
121 : */
|