/
vrpn_Mutex.C
1407 lines (1169 loc) · 36.9 KB
/
vrpn_Mutex.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h> // for fprintf, stderr, sprintf
#include <string.h> // for NULL, memcpy, strlen, etc
#include "vrpn_Connection.h" // for vrpn_Connection, etc
#include "vrpn_Mutex.h"
#include "vrpn_Shared.h" // for vrpn_buffer, vrpn_unbuffer, etc
#ifndef VRPN_USE_WINSOCK_SOCKETS
#include <arpa/inet.h> // for inet_addr
#include <netdb.h> // for gethostbyname, hostent, etc
//#include <sys/socket.h>
#include <netinet/in.h> // for in_addr, ntohl, INADDR_NONE
#include <unistd.h> // for getpid, gethostname
#endif
#ifdef _WIN32
#include <process.h> // for _getpid()
#endif
#ifdef sparc
#define INADDR_NONE -1
#endif
/*
Server State diagram:
handle_release()
+-------------------------+
| |
v |
handle_request()
FREE ------------------> HELD
Peer State diagram:
release()
+--------------------------------------------+
| |
v |
request()
AVAILABLE --------------> REQUESTING ----> OURS
| ^ |
v | |
|
HELD_REMOTELY <---------------+
*/
//#define VERBOSE
// static const char * myID = "vrpn_Mutex";
static const char *requestIndex_type = "vrpn_Mutex Request Index";
static const char *requestMutex_type = "vrpn_Mutex Request Mutex";
static const char *release_type = "vrpn_Mutex Release";
static const char *releaseNotification_type = "vrpn_Mutex Release_Notification";
static const char *grantRequest_type = "vrpn_Mutex Grant_Request";
static const char *denyRequest_type = "vrpn_Mutex Deny_Request";
// static const char * losePeer_type = "vrpn_Mutex Lose_Peer";
static const char *initialize_type = "vrpn_Mutex Initialize";
struct losePeerData {
vrpn_Connection *connection;
vrpn_PeerMutex *mutex;
};
// Mostly copied from vrpn_Connection.C's vrpn_getmyIP() and some man pages.
// Should return in host order.
// Returns 0 on error.
static vrpn_uint32 getmyIP(const char *NICaddress = NULL)
{
struct hostent *host;
char myname[100];
in_addr in;
int retval;
if (NICaddress) {
// First, see if NIC is specified as IP address.
// inet_addr() comes back in network order
in.s_addr = inet_addr(NICaddress);
if (in.s_addr != INADDR_NONE) {
return ntohl(in.s_addr);
}
// Second, try a name.
// gethostbyname() comes back in network order
host = gethostbyname(NICaddress);
if (host) {
memcpy(&in.s_addr, host->h_addr, host->h_length);
return ntohl(in.s_addr);
}
fprintf(stderr, "getmyIP: Can't get host entry for %s.\n", NICaddress);
return 0;
}
retval = gethostname(myname, sizeof(myname));
if (retval) {
fprintf(stderr, "getmyIP: Couldn't determine local hostname.\n");
return 0;
}
// gethostname() is guaranteed to produce something gethostbyname() can
// parse.
// gethostbyname() comes back in network order
host = gethostbyname(myname);
if (!host) {
fprintf(stderr, "getmyIP: Couldn't find host by name (%s).\n", myname);
return 0;
}
memcpy(&in.s_addr, host->h_addr, host->h_length);
return ntohl(in.s_addr);
}
vrpn_Mutex::vrpn_Mutex(const char *name, vrpn_Connection *c)
: d_connection(c)
{
char *servicename;
servicename = vrpn_copy_service_name(name);
if (c) {
c->addReference();
d_myId = c->register_sender(servicename);
d_requestIndex_type = c->register_message_type(requestIndex_type);
d_requestMutex_type = c->register_message_type(requestMutex_type);
d_release_type = c->register_message_type(release_type);
d_releaseNotification_type =
c->register_message_type(releaseNotification_type);
d_grantRequest_type = c->register_message_type(grantRequest_type);
d_denyRequest_type = c->register_message_type(denyRequest_type);
d_initialize_type = c->register_message_type(initialize_type);
}
if (servicename) {
delete[] servicename;
}
}
// virtual
vrpn_Mutex::~vrpn_Mutex(void)
{
if (d_connection) {
d_connection->removeReference(); // possibly destroy connection.
}
}
void vrpn_Mutex::mainloop(void)
{
if (d_connection) {
d_connection->mainloop();
}
}
void vrpn_Mutex::sendRequest(vrpn_int32 index)
{
timeval now;
char buffer[32];
char *b = buffer;
vrpn_int32 bl = 32;
if (!d_connection) return;
vrpn_gettimeofday(&now, NULL);
vrpn_buffer(&b, &bl, index);
d_connection->pack_message(32 - bl, now, d_requestMutex_type, d_myId,
buffer, vrpn_CONNECTION_RELIABLE);
}
void vrpn_Mutex::sendRelease(void)
{
timeval now;
if (!d_connection) return;
vrpn_gettimeofday(&now, NULL);
d_connection->pack_message(0, now, d_release_type, d_myId, NULL,
vrpn_CONNECTION_RELIABLE);
}
void vrpn_Mutex::sendReleaseNotification(void)
{
timeval now;
if (!d_connection) return;
vrpn_gettimeofday(&now, NULL);
d_connection->pack_message(0, now, d_releaseNotification_type, d_myId, NULL,
vrpn_CONNECTION_RELIABLE);
}
void vrpn_Mutex::sendGrantRequest(vrpn_int32 index)
{
timeval now;
char buffer[32];
char *b = buffer;
vrpn_int32 bl = 32;
if (!d_connection) return;
vrpn_gettimeofday(&now, NULL);
vrpn_buffer(&b, &bl, index);
d_connection->pack_message(32 - bl, now, d_grantRequest_type, d_myId,
buffer, vrpn_CONNECTION_RELIABLE);
}
void vrpn_Mutex::sendDenyRequest(vrpn_int32 index)
{
timeval now;
char buffer[32];
char *b = buffer;
vrpn_int32 bl = 32;
if (!d_connection) return;
vrpn_gettimeofday(&now, NULL);
vrpn_buffer(&b, &bl, index);
d_connection->pack_message(32 - bl, now, d_denyRequest_type, d_myId, buffer,
vrpn_CONNECTION_RELIABLE);
}
vrpn_Mutex_Server::vrpn_Mutex_Server(const char *name, vrpn_Connection *c)
: vrpn_Mutex(name, c)
, d_state(FREE)
, d_remoteIndex(0)
{
vrpn_int32 got;
vrpn_int32 droppedLast;
if (c) {
c->register_handler(d_requestIndex_type, handle_requestIndex, this);
c->register_handler(d_requestMutex_type, handle_requestMutex, this);
c->register_handler(d_release_type, handle_release, this);
got = c->register_message_type(vrpn_got_connection);
c->register_handler(got, handle_gotConnection, this);
droppedLast = c->register_message_type(vrpn_dropped_last_connection);
c->register_handler(droppedLast, handle_dropLastConnection, this);
}
}
// virtual
vrpn_Mutex_Server::~vrpn_Mutex_Server(void)
{
if (d_connection) {
vrpn_int32 got, droppedLast;
got = d_connection->register_message_type(vrpn_got_connection);
droppedLast =
d_connection->register_message_type(vrpn_dropped_last_connection);
d_connection->unregister_handler(d_requestIndex_type,
handle_requestIndex, this);
d_connection->unregister_handler(d_requestMutex_type,
handle_requestMutex, this);
d_connection->unregister_handler(d_release_type, handle_release, this);
d_connection->unregister_handler(got, handle_gotConnection, this);
d_connection->unregister_handler(droppedLast, handle_dropLastConnection,
this);
}
}
// static
int vrpn_Mutex_Server::handle_requestMutex(void *userdata, vrpn_HANDLERPARAM p)
{
vrpn_Mutex_Server *me = (vrpn_Mutex_Server *)userdata;
const char *b = p.buffer;
vrpn_int32 remoteId;
vrpn_unbuffer(&b, &remoteId);
#ifdef VERBOSE
fprintf(stderr, "vrpn_Mutex_Server::handle_request from %d.\n", remoteId);
#endif
if (me->d_state == FREE) {
me->d_state = HELD;
// BUG BUG BUG - how does the Mutex_Remote recognize that this grant
// is for it, not for some other?
me->sendGrantRequest(remoteId);
return 0;
}
else {
me->sendDenyRequest(remoteId);
return 0;
}
}
// static
int vrpn_Mutex_Server::handle_release(void *userdata, vrpn_HANDLERPARAM)
{
vrpn_Mutex_Server *me = (vrpn_Mutex_Server *)userdata;
#ifdef VERBOSE
fprintf(stderr, "vrpn_Mutex_Server::handle_release.\n");
#endif
me->d_state = FREE;
me->sendReleaseNotification();
return 0;
}
// static
int vrpn_Mutex_Server::handle_requestIndex(void *userdata, vrpn_HANDLERPARAM p)
{
vrpn_Mutex_Server *me = (vrpn_Mutex_Server *)userdata;
timeval now;
vrpn_int32 msg_len = sizeof(vrpn_int32) + p.payload_len;
char *buffer = new char[msg_len];
char *b = buffer;
vrpn_int32 bl = msg_len;
#ifdef VERBOSE
fprintf(stderr, "vrpn_Mutex_Server::handle_requestIndex: "
"Initializing client %d (%lu %d).\n",
me->d_remoteIndex, ntohl(*(vrpn_uint32 *)p.buffer),
ntohl(*(vrpn_int32 *)(p.buffer + sizeof(vrpn_uint32))));
#endif
if (me->d_connection) {
vrpn_gettimeofday(&now, NULL);
// echo back whatever the client gave us as a unique identifier
vrpn_buffer(&b, &bl, p.buffer, p.payload_len);
vrpn_buffer(&b, &bl, (me->d_remoteIndex));
me->d_connection->pack_message(msg_len, now, me->d_initialize_type,
me->d_myId, buffer,
vrpn_CONNECTION_RELIABLE);
}
me->d_remoteIndex++;
delete[] buffer;
return 0;
}
// static
int vrpn_Mutex_Server::handle_gotConnection(void *, vrpn_HANDLERPARAM)
{
return 0;
// return handle_requestIndex(userdata, p);
}
// static
int vrpn_Mutex_Server::handle_dropLastConnection(void *userdata,
vrpn_HANDLERPARAM)
{
vrpn_Mutex_Server *me = (vrpn_Mutex_Server *)userdata;
// Force the lock to release, to avoid deadlock.
if (me->d_state == HELD) {
fprintf(stderr, "vrpn_Mutex_Server::handle_dropLastConnection: "
"Forcing the state to FREE to avoid deadlock.\n");
}
me->d_state = FREE;
return 0;
}
vrpn_Mutex_Remote::vrpn_Mutex_Remote(const char *name, vrpn_Connection *c)
: vrpn_Mutex(name, c ? c : ((strcmp(name, "null") == 0)
? (vrpn_Connection *)NULL
: vrpn_get_connection_by_name(name)))
, d_state(AVAILABLE)
, d_myIndex(-1)
, d_requestBeforeInit(vrpn_FALSE)
, d_reqGrantedCB(NULL)
, d_reqDeniedCB(NULL)
, d_takeCB(NULL)
, d_releaseCB(NULL)
{
if (d_connection) {
d_connection->register_handler(d_grantRequest_type, handle_grantRequest,
this);
d_connection->register_handler(d_denyRequest_type, handle_denyRequest,
this);
d_connection->register_handler(d_releaseNotification_type,
handle_releaseNotification, this);
d_connection->register_handler(d_initialize_type, handle_initialize,
this);
if (d_connection->connected()) {
requestIndex();
}
vrpn_int32 got =
d_connection->register_message_type(vrpn_got_connection);
d_connection->register_handler(got, handle_gotConnection, this);
}
}
// virtual
vrpn_Mutex_Remote::~vrpn_Mutex_Remote(void)
{
// Make sure we don't deadlock things
release();
if (d_connection) {
d_connection->unregister_handler(d_grantRequest_type,
handle_grantRequest, this);
d_connection->unregister_handler(d_denyRequest_type, handle_denyRequest,
this);
d_connection->unregister_handler(d_releaseNotification_type,
handle_releaseNotification, this);
d_connection->unregister_handler(d_initialize_type, handle_initialize,
this);
vrpn_int32 got =
d_connection->register_message_type(vrpn_got_connection);
d_connection->unregister_handler(got, handle_gotConnection, this);
}
}
vrpn_bool vrpn_Mutex_Remote::isAvailable(void) const
{
return (d_state == AVAILABLE);
}
vrpn_bool vrpn_Mutex_Remote::isHeldLocally(void) const
{
return (d_state == OURS);
}
vrpn_bool vrpn_Mutex_Remote::isHeldRemotely(void) const
{
return (d_state == HELD_REMOTELY);
}
void vrpn_Mutex_Remote::requestIndex(void)
{
timeval now;
vrpn_int32 buflen = sizeof(vrpn_int32) + sizeof(vrpn_uint32);
char *buf = new char[buflen];
char *bufptr = buf;
vrpn_int32 len = buflen;
vrpn_uint32 ip_addr = getmyIP();
#ifdef _WIN32
vrpn_int32 pid = _getpid();
#else
vrpn_int32 pid = getpid();
#endif
vrpn_buffer(&bufptr, &len, ip_addr);
vrpn_buffer(&bufptr, &len, pid);
#ifdef VERBOSE
printf("requesting index for %lu, %d\n", ip_addr, pid);
#endif
vrpn_gettimeofday(&now, NULL);
d_connection->pack_message(buflen, now, d_requestIndex_type, d_myId, buf,
vrpn_CONNECTION_RELIABLE);
delete[] buf;
return;
}
void vrpn_Mutex_Remote::request(void)
{
if (!isAvailable()) {
#ifdef VERBOSE
fprintf(stderr, "Requested unavailable mutex.\n");
#endif
triggerDenyCallbacks();
return;
}
else if (d_myIndex == -1) {
#ifdef VERBOSE
fprintf(stderr, "Requested mutex before initialization; deferring.\n");
#endif
d_requestBeforeInit = vrpn_TRUE;
return;
}
#ifdef VERBOSE
fprintf(stderr, "Requesting mutex\n");
#endif
d_state = REQUESTING;
sendRequest(d_myIndex);
return;
}
void vrpn_Mutex_Remote::release(void)
{
if (!isHeldLocally()) {
return;
}
#ifdef VERBOSE
fprintf(stderr, "Releasing mutex.\n");
#endif
d_state = AVAILABLE;
sendRelease();
triggerReleaseCallbacks();
}
void vrpn_Mutex_Remote::addRequestGrantedCallback(void *userdata,
int (*f)(void *))
{
mutexCallback *cb = new mutexCallback;
if (!cb) {
fprintf(stderr, "vrpn_Mutex_Remote::addRequestGrantedCallback: "
"Out of memory.\n");
return;
}
cb->userdata = userdata;
cb->f = f;
cb->next = d_reqGrantedCB;
d_reqGrantedCB = cb;
}
void vrpn_Mutex_Remote::addRequestDeniedCallback(void *userdata,
int (*f)(void *))
{
mutexCallback *cb = new mutexCallback;
if (!cb) {
fprintf(stderr, "vrpn_Mutex_Remote::addRequestDeniedCallback: "
"Out of memory.\n");
return;
}
cb->userdata = userdata;
cb->f = f;
cb->next = d_reqDeniedCB;
d_reqDeniedCB = cb;
}
void vrpn_Mutex_Remote::addTakeCallback(void *userdata, int (*f)(void *))
{
mutexCallback *cb = new mutexCallback;
if (!cb) {
fprintf(stderr,
"vrpn_Mutex_Remote::addTakeCallback: Out of memory.\n");
return;
}
cb->userdata = userdata;
cb->f = f;
cb->next = d_takeCB;
d_takeCB = cb;
}
// static
void vrpn_Mutex_Remote::addReleaseCallback(void *userdata, int (*f)(void *))
{
mutexCallback *cb = new mutexCallback;
if (!cb) {
fprintf(stderr, "vrpn_Mutex_Remote::addReleaseCallback: "
"Out of memory.\n");
return;
}
cb->userdata = userdata;
cb->f = f;
cb->next = d_releaseCB;
d_releaseCB = cb;
}
// static
int vrpn_Mutex_Remote::handle_grantRequest(void *userdata, vrpn_HANDLERPARAM p)
{
vrpn_Mutex_Remote *me = (vrpn_Mutex_Remote *)userdata;
const char *b = p.buffer;
vrpn_int32 index;
vrpn_unbuffer(&b, &index);
#ifdef VERBOSE
fprintf(stderr, "vrpn_Mutex_Remote::handle_grantRequest for %d.\n", index);
#endif
if (me->d_myIndex != index) {
me->d_state = HELD_REMOTELY;
me->triggerTakeCallbacks();
return 0;
}
me->d_state = OURS;
me->triggerGrantCallbacks();
me->triggerTakeCallbacks();
return 0;
}
// static
int vrpn_Mutex_Remote::handle_denyRequest(void *userdata, vrpn_HANDLERPARAM p)
{
vrpn_Mutex_Remote *me = (vrpn_Mutex_Remote *)userdata;
const char *b = p.buffer;
vrpn_int32 index;
vrpn_unbuffer(&b, &index);
#ifdef VERBOSE
fprintf(stderr, "vrpn_Mutex_Remote::handle_denyRequest for %d.\n", index);
#endif
if (me->d_myIndex != index) {
return 0;
}
me->d_state = HELD_REMOTELY;
me->triggerDenyCallbacks();
return 0;
}
// static
int vrpn_Mutex_Remote::handle_releaseNotification(void *userdata,
vrpn_HANDLERPARAM)
{
vrpn_Mutex_Remote *me = (vrpn_Mutex_Remote *)userdata;
#ifdef VERBOSE
fprintf(stderr, "vrpn_Mutex_Remote::handle_releaseNotification.\n");
#endif
me->d_state = AVAILABLE;
me->triggerReleaseCallbacks();
return 0;
}
// static
int vrpn_Mutex_Remote::handle_initialize(void *userdata, vrpn_HANDLERPARAM p)
{
vrpn_Mutex_Remote *me = (vrpn_Mutex_Remote *)userdata;
const char *b = p.buffer;
// Only pay attention to the first initialize() message we get
// after startup.
if (me->d_myIndex != -1) {
return 0;
}
vrpn_int32 expected_payload_len =
2 * sizeof(vrpn_int32) + sizeof(vrpn_uint32);
if (p.payload_len != expected_payload_len) {
fprintf(stderr,
"vrpn_Mutex_Remote::handle_initialize: "
"Warning: Ignoring message with length %d, expected %d\n",
p.payload_len, expected_payload_len);
return 0;
}
vrpn_uint32 ip_addr;
vrpn_int32 pid;
vrpn_unbuffer(&b, &ip_addr);
vrpn_unbuffer(&b, &pid);
vrpn_int32 my_pid = 0;
#ifdef _WIN32
my_pid = _getpid();
#else
my_pid = getpid();
#endif
if ((ip_addr != getmyIP()) || (pid != my_pid)) {
fprintf(
stderr,
"vrpn_Mutex_Remote::handle_initialize: "
"Warning: Ignoring message that doesn't match ip/pid identifier\n");
#ifdef VERBOSE
fprintf(stderr, "Got %lu %d, expected %lu %d.\n", ip_addr, pid,
getmyIP(), my_pid);
#endif
return 0;
}
vrpn_unbuffer(&b, &(me->d_myIndex));
#ifdef VERBOSE
fprintf(stderr, "vrpn_Mutex_Remote::handle_initialize: "
"Got assigned index %d.\n",
me->d_myIndex);
#endif
if (me->d_requestBeforeInit) {
#ifdef VERBOSE
fprintf(stderr, "vrpn_Mutex_Remote::handle_initialize: "
"Sending request\n");
#endif
me->request();
}
return 0;
}
// static
int vrpn_Mutex_Remote::handle_gotConnection(void *userdata, vrpn_HANDLERPARAM)
{
vrpn_Mutex_Remote *me = (vrpn_Mutex_Remote *)userdata;
if (me->d_myIndex == -1) {
me->requestIndex();
}
return 0;
}
void vrpn_Mutex_Remote::triggerGrantCallbacks(void)
{
mutexCallback *cb;
// trigger callbacks
for (cb = d_reqGrantedCB; cb; cb = cb->next) {
(cb->f)(cb->userdata);
}
}
void vrpn_Mutex_Remote::triggerDenyCallbacks(void)
{
mutexCallback *cb;
// trigger callbacks
for (cb = d_reqDeniedCB; cb; cb = cb->next) {
(cb->f)(cb->userdata);
}
}
void vrpn_Mutex_Remote::triggerTakeCallbacks(void)
{
mutexCallback *cb;
// trigger callbacks
for (cb = d_takeCB; cb; cb = cb->next) {
(cb->f)(cb->userdata);
}
}
void vrpn_Mutex_Remote::triggerReleaseCallbacks(void)
{
mutexCallback *cb;
// trigger callbacks
for (cb = d_releaseCB; cb; cb = cb->next) {
(cb->f)(cb->userdata);
}
}
// Implementation notes:
//
// We broadcast messages over d_peer[] tagged with our IP number in
// network format.
// We broadcast REPLIES over d_peer[] tagged with the sender's IP number
// in network format. When we get a reply, we check the IP number it
// contains against our own and discard it if it doesn't match.
//
// This is an ugly n^2 implementation, when we could do it in 2n, but the
// data structures would be a little more complex - we'd have to have a
// mapping from IP numbers to vrpn_Connections in d_peer[]. What I'd probably
// do would be abandon IP numbers and ports and just use lexographical
// ordering of station names. But then we'd have to ensure that every
// instance used the same station name (evans vs. evans.cs.unc.edu vs.
// evans.cs.unc.edu:4500) for each peer, which is ugly.
vrpn_PeerMutex::vrpn_PeerMutex(const char *name, int port,
const char *NICaddress)
: d_state(AVAILABLE)
, d_server(NULL)
, d_peer(NULL)
, d_numPeers(0)
, d_numConnectionsAllocated(0)
, d_myIP(getmyIP(NICaddress))
, d_myPort(port)
, d_holderIP(0)
, d_holderPort(-1)
, d_reqGrantedCB(NULL)
, d_reqDeniedCB(NULL)
, d_takeCB(NULL)
, d_releaseCB(NULL)
, d_peerData(NULL)
{
if (!name) {
fprintf(stderr, "vrpn_PeerMutex: NULL name!\n");
return;
}
// XXX Won't work with non-IP connections (MPI, for example)
char con_name[512];
sprintf(con_name, "%s:%d", NICaddress, port);
d_server = vrpn_create_server_connection(con_name);
if (d_server) {
d_server->addReference();
d_server->setAutoDeleteStatus(true);
}
else {
fprintf(stderr, "vrpn_PeerMutex: "
"Couldn't open connection on port %d!\n",
port);
return;
}
init(name);
}
vrpn_PeerMutex::vrpn_PeerMutex(const char *name, vrpn_Connection *server)
: d_state(AVAILABLE)
, d_server(server)
, d_peer(NULL)
, d_numPeers(0)
, d_numConnectionsAllocated(0)
, d_myIP(getmyIP(NULL))
, d_myPort(0)
, d_holderIP(0)
, d_holderPort(-1)
, d_reqGrantedCB(NULL)
, d_reqDeniedCB(NULL)
, d_takeCB(NULL)
, d_releaseCB(NULL)
, d_peerData(NULL)
{
if (!name) {
fprintf(stderr, "vrpn_PeerMutex: NULL name!\n");
return;
}
if (server) {
d_server->addReference();
}
else {
fprintf(stderr, "vrpn_PeerMutex: NULL connection!\n");
return;
}
init(name);
}
vrpn_PeerMutex::~vrpn_PeerMutex(void)
{
// Send an explicit message so they know we're shutting down, not
// just disconnecting temporarily and will be back.
// Probably a safer way to do it would be to do addPeer and losePeer
// implicitly through dropped_connection/got_connection on d_server
// and d_peer?
// There is no good solution!
// Possible improvement: if we lose our connection and regain it,
// send some sort of announcement message to everybody who used to
// be our peer so they add us back into their tables.
if (isHeldLocally()) {
release();
}
if (d_mutexName) {
delete[] d_mutexName;
}
for (int i = 0; i < d_numPeers; ++i) {
if (d_peer[i]) {
d_peer[i]->removeReference();
}
}
if (d_peer) {
delete[] d_peer;
}
if (d_server) {
d_server->removeReference();
}
}
vrpn_bool vrpn_PeerMutex::isAvailable(void) const
{
return (d_state == AVAILABLE);
}
vrpn_bool vrpn_PeerMutex::isHeldLocally(void) const
{
return (d_state == OURS);
}
vrpn_bool vrpn_PeerMutex::isHeldRemotely(void) const
{
return (d_state == HELD_REMOTELY);
}
int vrpn_PeerMutex::numPeers(void) const { return d_numPeers; }
void vrpn_PeerMutex::mainloop(void)
{
int i;
d_server->mainloop();
for (i = 0; i < d_numPeers; i++) {
d_peer[i]->mainloop();
}
checkGrantMutex();
}
void vrpn_PeerMutex::request(void)
{
int i;
// No point in sending requests if it's not currently available.
// However, we need to trigger any local denial callbacks; otherwise
// this looks like a silent failure.
if (!isAvailable()) {
triggerDenyCallbacks();
#ifdef VERBOSE
fprintf(stderr,
"vrpn_PeerMutex::request: the mutex isn't available.\n");
#endif
return;
}
d_state = REQUESTING;
d_numPeersGrantingLock = 0;
for (i = 0; i < d_numPeers; i++) {
// d_peerData[i].grantedLock = VRPN_FALSE;
sendRequest(d_peer[i]);
}
// If somebody else sends a request before we get all our grants,
// and their IP/port# is lower than ours, we have to yield to them.
d_holderIP = d_myIP;
d_holderPort = d_myPort;
// We used to wait until the next pass through mainloop() to check
// this (our request could be trivially granted if we have no
// peers), but that leads to bad things (TM), like having to
// insert extra calls to mainloop() in client code just to guarantee
// that we have the mutex.
checkGrantMutex();
#ifdef VERBOSE
fprintf(stderr, "vrpn_PeerMutex::request: requested the mutex "
"(from %d peers).\n",
d_numPeers);
#endif
}
void vrpn_PeerMutex::release(void)
{
int i;
// Can't release it if we don't already have it.
// There aren't any appropriate callbacks to trigger here. :)
if (!isHeldLocally()) {
#ifdef VERBOSE
fprintf(stderr, "vrpn_PeerMutex::release: we don't hold the mutex.\n");
#endif
return;
}
d_state = AVAILABLE;
d_holderIP = 0;
d_holderPort = -1;
for (i = 0; i < d_numPeers; i++) {
sendRelease(d_peer[i]);
}
triggerReleaseCallbacks();
#ifdef VERBOSE
fprintf(stderr, "vrpn_PeerMutex::release: released the mutex.\n");
#endif
}
void vrpn_PeerMutex::addPeer(const char *stationName)
{
vrpn_Connection **newc;
peerData *newg;
losePeerData *d;
int i;
// complex
while (d_numPeers >= d_numConnectionsAllocated) {
// reallocate arrays
d_numConnectionsAllocated = 2 * (d_numConnectionsAllocated + 1);
newc = new vrpn_Connection *[d_numConnectionsAllocated];
newg = new peerData[d_numConnectionsAllocated];
if (!newc || !newg) {
fprintf(stderr, "vrpn_PeerMutex::addPeer: Out of memory.\n");
return;
}
for (i = 0; i < d_numPeers; i++) {
newc[i] = d_peer[i];
newg[i] = d_peerData[i];
}
if (d_peer) {
delete[] d_peer;
}
if (d_peerData) {
delete[] d_peerData;
}
d_peer = newc;
d_peerData = newg;
}
d_peer[d_numPeers] = vrpn_get_connection_by_name(stationName);
// d_peerData[d_numPeers].grantedLock = vrpn_false;
d = new losePeerData;
if (!d) {