5 #include "lock_protocol.h"
6 #include "lock_client.h"
13 #include "lang/verify.h"
14 #include "lock_client_cache_rsm.h"
17 char tprintf_thread_prefix = 'c';
20 int nt = 6; //XXX: lab1's rpc handlers are blocking. Since rpcs uses a thread pool of 10 threads, we cannot test more than 10 blocking rpc.
22 lock_client_cache_rsm **lc = new lock_client_cache_rsm * [nt];
23 lock_protocol::lockid_t a = 1;
24 lock_protocol::lockid_t b = 2;
25 lock_protocol::lockid_t c = 3;
27 // check_grant() and check_release() check that the lock server
28 // doesn't grant the same lock to both clients.
29 // it assumes that lock names are distinct in the first byte.
31 pthread_mutex_t count_mutex;
34 check_grant(lock_protocol::lockid_t lid)
36 ScopedLock ml(&count_mutex);
39 fprintf(stderr, "error: server granted %016llx twice\n", lid);
40 fprintf(stdout, "error: server granted %016llx twice\n", lid);
47 check_release(lock_protocol::lockid_t lid)
49 ScopedLock ml(&count_mutex);
52 fprintf(stderr, "error: client released un-held lock %016llx\n", lid);
61 tprintf ("acquire a release a acquire a release a\n");
71 tprintf ("acquire a acquire b release b release a\n");
87 tprintf ("test2: client %d acquire a release a\n", i);
89 tprintf ("test2: client %d acquire done\n", i);
92 tprintf ("test2: client %d release\n", i);
95 tprintf ("test2: client %d release done\n", i);
104 tprintf ("test3: client %d acquire a release a concurrent\n", i);
105 for (int j = 0; j < 10; j++) {
108 tprintf ("test3: client %d got lock\n", i);
120 tprintf ("test4: thread %d acquire a release a concurrent; same clnt\n", i);
121 for (int j = 0; j < 10; j++) {
124 tprintf ("test4: thread %d on client 0 got lock\n", i);
136 tprintf ("test5: client %d acquire a release a concurrent; same and diff clnt\n", i);
137 for (int j = 0; j < 10; j++) {
138 if (i < 5) lc[0]->acquire(a);
139 else lc[1]->acquire(a);
141 tprintf ("test5: client %d got lock\n", i);
143 if (i < 5) lc[0]->release(a);
144 else lc[1]->release(a);
150 main(int argc, char *argv[])
156 setvbuf(stdout, NULL, _IONBF, 0);
157 setvbuf(stderr, NULL, _IONBF, 0);
163 fprintf(stderr, "Usage: %s [host:]port [test]\n", argv[0]);
170 test = atoi(argv[2]);
171 if(test < 1 || test > 5){
172 tprintf("Test number must be between 1 and 5\n");
177 VERIFY(pthread_mutex_init(&count_mutex, NULL) == 0);
178 tprintf("cache lock client\n");
179 for (int i = 0; i < nt; i++) lc[i] = new lock_client_cache_rsm(dst);
181 if(!test || test == 1){
185 if(!test || test == 2){
187 for (int i = 0; i < nt; i++) {
188 int *a = new int (i);
189 r = pthread_create(&th[i], NULL, test2, (void *) a);
192 for (int i = 0; i < nt; i++) {
193 pthread_join(th[i], NULL);
197 if(!test || test == 3){
201 for (int i = 0; i < nt; i++) {
202 int *a = new int (i);
203 r = pthread_create(&th[i], NULL, test3, (void *) a);
206 for (int i = 0; i < nt; i++) {
207 pthread_join(th[i], NULL);
211 if(!test || test == 4){
215 for (int i = 0; i < 2; i++) {
216 int *a = new int (i);
217 r = pthread_create(&th[i], NULL, test4, (void *) a);
220 for (int i = 0; i < 2; i++) {
221 pthread_join(th[i], NULL);
225 if(!test || test == 5){
230 for (int i = 0; i < nt; i++) {
231 int *a = new int (i);
232 r = pthread_create(&th[i], NULL, test5, (void *) a);
235 for (int i = 0; i < nt; i++) {
236 pthread_join(th[i], NULL);
240 tprintf ("%s: passed all tests successfully\n", argv[0]);