5 #include "lock_protocol.h"
6 #include "lock_client.h"
13 #include "lang/verify.h"
14 #include "lock_client_cache_rsm.h"
16 #include <sys/types.h>
19 char tprintf_thread_prefix = 'c';
22 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.
24 lock_client_cache_rsm **lc = new lock_client_cache_rsm * [nt];
25 lock_protocol::lockid_t a = 1;
26 lock_protocol::lockid_t b = 2;
27 lock_protocol::lockid_t c = 3;
29 // check_grant() and check_release() check that the lock server
30 // doesn't grant the same lock to both clients.
31 // it assumes that lock names are distinct in the first byte.
33 pthread_mutex_t count_mutex;
36 check_grant(lock_protocol::lockid_t lid)
38 ScopedLock ml(&count_mutex);
41 fprintf(stderr, "error: server granted %016llx twice\n", lid);
42 fprintf(stdout, "error: server granted %016llx twice\n", lid);
49 check_release(lock_protocol::lockid_t lid)
51 ScopedLock ml(&count_mutex);
54 fprintf(stderr, "error: client released un-held lock %016llx\n", lid);
63 tprintf ("acquire a release a acquire a release a\n");
73 tprintf ("acquire a acquire b release b release a\n");
89 tprintf ("test2: client %d acquire a release a\n", i);
91 tprintf ("test2: client %d acquire done\n", i);
94 tprintf ("test2: client %d release\n", i);
97 tprintf ("test2: client %d release done\n", i);
106 tprintf ("test3: client %d acquire a release a concurrent\n", i);
107 for (int j = 0; j < 10; j++) {
110 tprintf ("test3: client %d got lock\n", i);
122 tprintf ("test4: thread %d acquire a release a concurrent; same clnt\n", i);
123 for (int j = 0; j < 10; j++) {
126 tprintf ("test4: thread %d on client 0 got lock\n", i);
138 tprintf ("test5: client %d acquire a release a concurrent; same and diff clnt\n", i);
139 for (int j = 0; j < 10; j++) {
140 if (i < 5) lc[0]->acquire(a);
141 else lc[1]->acquire(a);
143 tprintf ("test5: client %d got lock\n", i);
145 if (i < 5) lc[0]->release(a);
146 else lc[1]->release(a);
152 main(int argc, char *argv[])
158 setvbuf(stdout, NULL, _IONBF, 0);
159 setvbuf(stderr, NULL, _IONBF, 0);
165 fprintf(stderr, "Usage: %s [host:]port [test]\n", argv[0]);
172 test = atoi(argv[2]);
173 if(test < 1 || test > 5){
174 tprintf("Test number must be between 1 and 5\n");
179 VERIFY(pthread_mutex_init(&count_mutex, NULL) == 0);
180 tprintf("cache lock client\n");
181 for (int i = 0; i < nt; i++) lc[i] = new lock_client_cache_rsm(dst);
183 if(!test || test == 1){
187 if(!test || test == 2){
189 for (int i = 0; i < nt; i++) {
190 int *a = new int (i);
191 r = pthread_create(&th[i], NULL, test2, (void *) a);
194 for (int i = 0; i < nt; i++) {
195 pthread_join(th[i], NULL);
199 if(!test || test == 3){
203 for (int i = 0; i < nt; i++) {
204 int *a = new int (i);
205 r = pthread_create(&th[i], NULL, test3, (void *) a);
208 for (int i = 0; i < nt; i++) {
209 pthread_join(th[i], NULL);
213 if(!test || test == 4){
217 for (int i = 0; i < 2; i++) {
218 int *a = new int (i);
219 r = pthread_create(&th[i], NULL, test4, (void *) a);
222 for (int i = 0; i < 2; i++) {
223 pthread_join(th[i], NULL);
227 if(!test || test == 5){
232 for (int i = 0; i < nt; i++) {
233 int *a = new int (i);
234 r = pthread_create(&th[i], NULL, test5, (void *) a);
237 for (int i = 0; i < nt; i++) {
238 pthread_join(th[i], NULL);
242 tprintf ("%s: passed all tests successfully\n", argv[0]);