summaryrefslogtreecommitdiff
path: root/innobase/com/com0shm.c
blob: 72ab23b9be8b02c2f4140c08da218477bb73c9ea (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
/******************************************************
The communication through shared memory

(c) 1995 Innobase Oy

Created 9/25/1995 Heikki Tuuri
*******************************************************/

#include "com0shm.h"
#ifdef UNIV_NONINL
#include "com0shm.ic"
#endif

#include "mem0mem.h"
#include "ut0mem.h"
#include "com0com.h"
#include "os0shm.h"
#include "sync0sync.h"
#include "sync0ipm.h"
#include "hash0hash.h"

/*
	IMPLEMENTATION OF COMMUNICATION PRIMITIVES
	==========================================

When bind is called for an endpoint, a shared memory area of
a size specified by com_shm_set_option is created with the
name of the address given concatenated to "_IBSHM".
Also a mutex is created for controlling the access to the
shared memory area. The name of the mutex is address + "_IBSHM_MTX".
An event with name address + "_IBSHM_EV_NE" is created. This event
is in signaled state when the shared memory area is not empty, i.e.,
there is a datagram to read. An event address + "_IBSHM_EV_EM"
is signaled, when the area is empty, i.e., a datagram can be
written to it.

The shared memory area consists of an info struct
at the beginning, containing fields telling:
if the area is valid, i.e., is anybody going to
read it, whether it currently contains a datagram, the
length of the address from which the datagram was received,
the length of the datagram, and the maximum allowed length of
a datagram.
After the info struct, there is a string of bytes
containing the sender address and the data
of the datagram.
*/

/* The following is set TRUE when the first endpoint is created. */

ibool	com_shm_initialized	= FALSE;

/* When a datagram is sent, the shared memory area
corresponding to the destination address is mapped
to the address space of this (sender) process.
We preserve it and keep the relevant info in the
following list. We can save a lot of CPU time
if the destination can be found on the list. The list is
protected by the mutex below. */

mutex_t			com_shm_destination_mutex;
hash_table_t*		com_shm_destination_cache;
UT_LIST_BASE_NODE_T(com_shm_endpoint_t)
			com_shm_destination_list;

/* The number of successfully bound endpoints in this process. When this
number drops to 0, the destination cache is freed. This variable is protected
by com_shm_destination_mutex above. */

ulint			com_shm_bind_count	= 0;

/* The performance of communication in NT depends on how
many times a system call is made (excluding os_thread_yield,
as that is the fastest way to switch thread).
The following variable counts such events. */

ulint	com_shm_system_call_count = 0;

/* The info struct at the beginning of a shared memory area */

typedef struct com_shm_info_struct	com_shm_info_t;

/* An area of shared memory consists of an info struct followed
by a string of bytes. */

typedef com_shm_info_t	com_shm_t;

struct com_shm_endpoint_struct{
	ibool		owns_shm; /* This is true if the shared memory
				area is owned by this endpoint structure
				(it may also be opened for this endpoint,
				not created, in which case does not own it) */
	char*		addr;	/* pointer to address the endpoint is bound
				to, NULL if not bound */
	ulint		addr_len; /* address length */
	ulint		size;	/* maximum allowed datagram size, initialized
				to 0 at creation */
	os_shm_t	shm;	/* operating system handle of the shared
				memory area */
	com_shm_t*	map;	/* pointer to the start address of the shared
				memory area */
	os_event_t	not_empty; /* this is in the signaled state if
				the area currently may contain a datagram;
				NOTE: automatic event */
	os_event_t	empty; 	/* this is in the signaled state if the area
				currently may be empty; NOTE: automatic event */
	ip_mutex_hdl_t*	ip_mutex; /* handle to the interprocess mutex
				protecting the shared memory */
	UT_LIST_NODE_T(com_shm_endpoint_t) list; /* If the endpoint struct
				is inserted into a list, this contains
				pointers to next and prev */
	com_shm_endpoint_t* addr_hash;
				/* hash table link */
};

struct com_shm_info_struct{
	ulint		valid;	/* This is COM_SHM_VALID if the creator
				of the shared memory area has it still
				mapped to its address space. Otherwise,
				we may conclude that the datagram cannot
				be delivered. */
	ibool		not_empty; /* TRUE if the area currently contains
				a datagram */
	ulint		empty_waiters; /* Count of (writer) threads which are
				waiting for the empty-event */
	ulint		max_len;/* maximum allowed length for a datagram */
	ulint		addr_len;/* address length for the sender address */
	ulint		data_len;/* datagram length */
	ip_mutex_t	ip_mutex;/* fast interprocess mutex protecting
				the shared memory area */
};

#define COM_SHM_VALID	76640

/*************************************************************************
Accessor functions for a shared memory endpoint */

UNIV_INLINE
ibool
com_shm_endpoint_get_owns_shm(
/*==========================*/
	com_shm_endpoint_t*	ep)
{
	ut_ad(ep);
	return(ep->owns_shm);
}

UNIV_INLINE
void
com_shm_endpoint_set_owns_shm(
/*==========================*/
	com_shm_endpoint_t*	ep,
	ibool			flag)
{
	ut_ad(ep);

	ep->owns_shm = flag;
}

UNIV_INLINE
char*
com_shm_endpoint_get_addr(
/*======================*/
	com_shm_endpoint_t*	ep)
{
	ut_ad(ep);
	return(ep->addr);
}

UNIV_INLINE
void
com_shm_endpoint_set_addr(
/*======================*/
	com_shm_endpoint_t*	ep,
	char*			addr)
{
	ut_ad(ep);

	ep->addr = addr;
}

UNIV_INLINE
ulint
com_shm_endpoint_get_addr_len(
/*==========================*/
	com_shm_endpoint_t*	ep)
{
	return(ep->addr_len);
}

UNIV_INLINE
void
com_shm_endpoint_set_addr_len(
/*==========================*/
	com_shm_endpoint_t*	ep,
	ulint			len)
{
	ut_ad(ep);
	ut_ad(len > 0);

	ep->addr_len = len;
}

ulint
com_shm_endpoint_get_size(
/*======================*/
	com_shm_endpoint_t*	ep)
{
	return(ep->size);
}

UNIV_INLINE
void
com_shm_endpoint_set_size(
/*======================*/
	com_shm_endpoint_t*	ep,
	ulint			size)
{
	ut_ad(ep);

	ep->size = size;
}

UNIV_INLINE
os_shm_t
com_shm_endpoint_get_shm(
/*=====================*/
	com_shm_endpoint_t*	ep)
{
	return(ep->shm);
}

UNIV_INLINE
void
com_shm_endpoint_set_shm(
/*=====================*/
	com_shm_endpoint_t*	ep,
	os_shm_t		shm)
{
	ut_ad(ep);
	ut_ad(shm);

	ep->shm = shm;
}

UNIV_INLINE
com_shm_t*
com_shm_endpoint_get_map(
/*=====================*/
	com_shm_endpoint_t*	ep)
{
	return(ep->map);
}

UNIV_INLINE
void
com_shm_endpoint_set_map(
/*=====================*/
	com_shm_endpoint_t*	ep,
	com_shm_t*		map)
{
	ut_ad(ep);
	ut_ad(map);

	ep->map = map;
}

UNIV_INLINE
os_event_t
com_shm_endpoint_get_empty(
/*=======================*/
	com_shm_endpoint_t*	ep)
{
	return(ep->empty);
}

UNIV_INLINE
void
com_shm_endpoint_set_empty(
/*=======================*/
	com_shm_endpoint_t*	ep,
	os_event_t		event)
{
	ut_ad(ep);
	ut_ad(event);

	ep->empty = event;
}

UNIV_INLINE
os_event_t
com_shm_endpoint_get_not_empty(
/*===========================*/
	com_shm_endpoint_t*	ep)
{
	return(ep->not_empty);
}

UNIV_INLINE
void
com_shm_endpoint_set_not_empty(
/*===========================*/
	com_shm_endpoint_t*	ep,
	os_event_t		event)
{
	ut_ad(ep);
	ut_ad(event);

	ep->not_empty = event;
}

/************************************************************************
Accessor functions for the shared memory area info struct. */
UNIV_INLINE
ulint
com_shm_get_valid(
/*==============*/
	com_shm_info_t*	info)
{
	return(info->valid);
}

UNIV_INLINE
void
com_shm_set_valid(
/*==============*/
	com_shm_info_t*		info,
	ulint			flag)
{
	ut_ad(info);

	info->valid = flag;
}

UNIV_INLINE
ibool
com_shm_get_not_empty(
/*==================*/
	com_shm_info_t*	info)
{
	return(info->not_empty);
}

UNIV_INLINE
void
com_shm_set_not_empty(
/*==================*/
	com_shm_info_t*		info,
	ibool			flag)
{
	ut_ad(info);

	info->not_empty = flag;
}

UNIV_INLINE
ulint
com_shm_get_empty_waiters(
/*======================*/
	com_shm_info_t*	info)
{
	ut_ad(info->empty_waiters < 1000);

	return(info->empty_waiters);
}

UNIV_INLINE
void
com_shm_set_empty_waiters(
/*======================*/
	com_shm_info_t*	info,
	ulint		count)
{
	ut_ad(info);
	ut_ad(count < 1000);

	info->empty_waiters = count;
}

UNIV_INLINE
ulint
com_shm_get_max_len(
/*================*/
	com_shm_info_t*	info)
{
	return(info->max_len);
}

UNIV_INLINE
void
com_shm_set_max_len(
/*================*/
	com_shm_info_t*		info,
	ulint			len)
{
	ut_ad(info);
	ut_ad(len > 0);

	info->max_len = len;
}

UNIV_INLINE
ulint
com_shm_get_addr_len(
/*=================*/
	com_shm_info_t*	info)
{
	return(info->addr_len);
}

UNIV_INLINE
void
com_shm_set_addr_len(
/*=================*/
	com_shm_info_t*		info,
	ulint			len)
{
	ut_ad(info);
	ut_ad(len > 0);

	info->addr_len = len;
}

UNIV_INLINE
ulint
com_shm_get_data_len(
/*=================*/
	com_shm_info_t*	info)
{
	return(info->data_len);
}

UNIV_INLINE
void
com_shm_set_data_len(
/*=================*/
	com_shm_info_t*		info,
	ulint			len)
{
	ut_ad(info);
	ut_ad(len > 0);

	info->data_len = len;
}

UNIV_INLINE
ip_mutex_t*
com_shm_get_ip_mutex(
/*=================*/
	com_shm_info_t*	info)
{
	return(&(info->ip_mutex));
}

/*************************************************************************
Accessor functions for the address and datagram fields inside a
shared memory area. */

UNIV_INLINE
char*
com_shm_get_addr(
/*=============*/
	com_shm_t*	area)
{
	return((char*)area + sizeof(com_shm_info_t));
}

UNIV_INLINE
byte*
com_shm_get_data(
/*=============*/
	com_shm_t*	area)
{
	return((byte*)com_shm_get_addr(area) + com_shm_get_addr_len(area));
}

/*************************************************************************
Initializes the shared memory communication system for this
process. */
UNIV_INLINE
void
com_shm_init(void)
/*==============*/
{
	mutex_create(&com_shm_destination_mutex);

	mutex_set_level(&com_shm_destination_mutex, SYNC_ANY_LATCH);

	com_shm_destination_cache = hash_create(1000);

	UT_LIST_INIT(com_shm_destination_list);

	com_shm_initialized = TRUE;
}

/*************************************************************************
Reserves the ip mutex of the shared memory area of an endpoint. */
UNIV_INLINE
void
com_shm_enter(
/*==========*/
	com_shm_endpoint_t*	ep)
{
	ulint	ret;

	ret = ip_mutex_enter(ep->ip_mutex, 10000000);

	if (ret != 0) {
		mutex_list_print_info();

		ut_error;
	}
}

/*************************************************************************
Releases the ip mutex of the shared memory area of an endpoint. */
UNIV_INLINE
void
com_shm_exit(
/*=========*/
	com_shm_endpoint_t*	ep)
{
	ip_mutex_exit(ep->ip_mutex);
}

/*************************************************************************
Looks for the given address in the cached destination addresses. */
UNIV_INLINE
com_shm_endpoint_t*
com_shm_destination_cache_search(
/*=============================*/
			/* out: cached endpoint structure if found, else NULL */
	char*	addr,	/* in: destination address */
	ulint	len)	/* in: address length */
{
	com_shm_endpoint_t*	ep;
	ulint			fold;

	fold = ut_fold_binary((byte*)addr, len);
/*	
	printf("Searching dest. cache %s %lu fold %lu\n", addr, len, fold);
*/
	mutex_enter(&com_shm_destination_mutex);

	HASH_SEARCH(addr_hash, com_shm_destination_cache, fold, ep,
			((ep->addr_len == len)
		 		&& (0 == ut_memcmp(addr, ep->addr, len))));

	mutex_exit(&com_shm_destination_mutex);

	return(ep);
}

/*************************************************************************
Inserts the given endpoint structure in the cached destination addresses. */
static
void
com_shm_destination_cache_insert(
/*=============================*/
	com_shm_endpoint_t*	ep)	/* in: endpoint struct to insert */
{
	ulint	fold;

	fold = ut_fold_binary((byte*)(ep->addr), ep->addr_len);
	
	mutex_enter(&com_shm_destination_mutex);

	/* Add to hash table */
	HASH_INSERT(com_shm_endpoint_t,
			addr_hash, com_shm_destination_cache, fold, ep);

	UT_LIST_ADD_LAST(list, com_shm_destination_list, ep);

/*	printf("Inserting to dest. cache %s %lu fold %lu\n", ep->addr,
					ep->addr_len, fold);
*/
	mutex_exit(&com_shm_destination_mutex);
}

/*************************************************************************
Frees the endpoint structs in the destination cache if the bind count is zero.
If it is not, some send operation may just be using a cached endpoint and it
cannot be freed. */
static
void
com_shm_destination_cache_no_binds(void)
/*====================================*/
{
	com_shm_endpoint_t*	ep;
	ulint			fold;

	mutex_enter(&com_shm_destination_mutex);

	if (com_shm_bind_count != 0) {
		mutex_exit(&com_shm_destination_mutex);

		return;
	}

	while (UT_LIST_GET_LEN(com_shm_destination_list) != 0) {

		ep = UT_LIST_GET_FIRST(com_shm_destination_list);

		UT_LIST_REMOVE(list, com_shm_destination_list, ep);

		fold = ut_fold_binary((byte*)ep->addr, ep->addr_len);
/*	
		printf("Deleting from dest. cache %s %lu fold %lu\n",
					ep->addr,
					ep->addr_len, fold);
*/
		HASH_DELETE(com_shm_endpoint_t, addr_hash,
					com_shm_destination_cache, fold, ep);

		com_shm_endpoint_free(ep);
	}	

	mutex_exit(&com_shm_destination_mutex);
}

/***********************************************************************
Unbinds an endpoint at the time of freeing. */
static
void
com_shm_unbind(
/*===========*/
	com_shm_endpoint_t*	ep)	/* in: endpoint */
{
	com_shm_t*	map;

	map = com_shm_endpoint_get_map(ep);

	/* Mark the shared memory area invalid */

	com_shm_set_valid(map, 0);

	/* Decrement the count of bindings */

	mutex_enter(&com_shm_destination_mutex);

	com_shm_bind_count--;

	mutex_exit(&com_shm_destination_mutex);

	/* If there are no binds left, free the cached endpoints */

	com_shm_destination_cache_no_binds();
}

/*************************************************************************
Creates a communications endpoint. */

com_shm_endpoint_t*
com_shm_endpoint_create(void)
/*=========================*/
			/* out, own: communications endpoint, NULL if
			did not succeed */
{
	com_shm_endpoint_t*	ep;

	if (!com_shm_initialized) {

		com_shm_init();
	}
	
	ep = mem_alloc(sizeof(com_shm_endpoint_t));
	
	com_shm_endpoint_set_owns_shm(ep, FALSE);
	com_shm_endpoint_set_addr(ep, NULL);
	com_shm_endpoint_set_size(ep, 0);
	
	return(ep);	
}

/*************************************************************************
Frees a communications endpoint. */

ulint
com_shm_endpoint_free(
/*==================*/
				/* out: O if succeed, else error number */
	com_shm_endpoint_t* ep)	/* in, own: communications endpoint */
{
	com_shm_t*	map;	
	
	ut_ad(ep);

	if (com_shm_endpoint_get_addr(ep) != NULL) {
		
		map = com_shm_endpoint_get_map(ep);
		
		if (com_shm_endpoint_get_owns_shm(ep)) {

			com_shm_unbind(ep);
		}
		
		/* We do not destroy the data structures in the shared memory
		area, because we cannot be sure that there is currently no
		process accessing it. Therefore we just close the ip_mutex
		residing in the area. */

		ip_mutex_close(ep->ip_mutex);

		os_event_free(com_shm_endpoint_get_not_empty(ep));
		os_event_free(com_shm_endpoint_get_empty(ep));
		
		os_shm_unmap(map);
		os_shm_free(com_shm_endpoint_get_shm(ep));

		mem_free(com_shm_endpoint_get_addr(ep));
	}

	mem_free(ep);

	return(0);
}

/*************************************************************************
Sets an option, like the maximum datagram size for an endpoint.
The options may vary depending on the endpoint type. */

ulint
com_shm_endpoint_set_option(
/*========================*/
				/* out: 0 if succeed, else error number */
	com_shm_endpoint_t* ep,	/* in: endpoint */
	ulint		optno,	/* in: option number, only
				COM_OPT_MAX_DGRAM_SIZE currently supported */
	byte*		optval,	/* in: pointer to a buffer containing the
				option value to set */
	ulint		optlen)	/* in: option value buffer length */
{
	ulint	size;

	UT_NOT_USED(optlen);

	ut_ad(ep);
	ut_a(optno == COM_OPT_MAX_DGRAM_SIZE);
	ut_ad(NULL == com_shm_endpoint_get_addr(ep));
 
	size = *((ulint*)optval);

	ut_ad(size > 0);

	com_shm_endpoint_set_size(ep, size);

	return(0);
}
	
/*************************************************************************
This function is used either to create a new shared memory area or open an
existing one, but this does not do the operations necessary with the ip mutex.
They are performed in com_shm_bind or com_shm_open which call this function. */
static
ulint
com_shm_create_or_open(
/*===================*/
				/* out: 0 if succeed, else error number */
	com_shm_endpoint_t* ep,	/* in: communications endpoint */
	char*		name,	/* in: address name */
	ulint		len)	/* in: address name length */
{
	os_shm_t	shm;
	com_shm_t*	map;
	os_event_t	event_ne;
	os_event_t	event_em;
	char*		buf;
	
	ut_ad(ep);
	ut_ad(name);
	ut_ad(len > 0);

	buf = mem_alloc(COM_MAX_ADDR_LEN + 20);

	ut_memcpy(buf, name, len);

	ut_strcpy(buf + len, (char*)"_IBSHM");

	shm = os_shm_create(sizeof(com_shm_info_t) + COM_MAX_ADDR_LEN +
			    		com_shm_endpoint_get_size(ep), buf);
	if (shm == NULL) {

		return(COM_ERR_NOT_SPECIFIED);
	}

	map = os_shm_map(shm);

	if (map == NULL) {
		os_shm_free(shm);

		return(COM_ERR_NOT_SPECIFIED);
	}

	ut_strcpy(buf + len, (char*)"_IBSHM_EV_NE"),

	event_ne = os_event_create_auto(buf);

	ut_ad(event_ne);

	ut_strcpy(buf + len, (char*)"_IBSHM_EV_EM"),

	event_em = os_event_create_auto(buf);

	ut_ad(event_em);

	com_shm_endpoint_set_shm(ep, shm);
	com_shm_endpoint_set_map(ep, map);

	com_shm_endpoint_set_not_empty(ep, event_ne);
	com_shm_endpoint_set_empty(ep, event_em);

	com_shm_endpoint_set_addr(ep, buf);
	com_shm_endpoint_set_addr_len(ep, len);

	return(0);
}

/*************************************************************************
Opens a shared memory area for communication. */
static
ulint
com_shm_open(
/*=========*/
				/* out: 0 if succeed, else error number */
	com_shm_endpoint_t* ep,	/* in: communications endpoint */
	char*		name,	/* in: address name */
	ulint		len)	/* in: address name length */
{
	ip_mutex_hdl_t*	ip_hdl;
	com_shm_t*	map;
	ulint		ret;
	char		buf[COM_MAX_ADDR_LEN + 20];
	
	ret = com_shm_create_or_open(ep, name, len);	

	if (ret != 0) {

		return(ret);
	}

	map = com_shm_endpoint_get_map(ep);

	/* Open the interprocess mutex to protect the shared memory area */

	ut_memcpy(buf, name, len);
	ut_strcpy(buf + len, (char*)"_IBSHM_MTX");
	
	ret = ip_mutex_open(com_shm_get_ip_mutex(map), buf, &ip_hdl);
	
	if (ret != 0) {

		return(COM_ERR_NOT_SPECIFIED);
	}
	
	ep->ip_mutex = ip_hdl;

	return(0);
}

/*************************************************************************
Creates a shared memory area for communication. */

ulint
com_shm_bind(
/*=========*/
				/* out: 0 if succeed, else error number */
	com_shm_endpoint_t* ep,	/* in: communications endpoint */
	char*		name,	/* in: address name */
	ulint		len)	/* in: address name length */
{
	com_shm_t*	map;
	ulint		ret;
	char		buf[COM_MAX_ADDR_LEN + 20];
	ip_mutex_hdl_t*	ip_hdl;
	
	if (com_shm_endpoint_get_size(ep) == 0) {

		return(COM_ERR_MAX_DATAGRAM_SIZE_NOT_SET);
	}
	
	ret = com_shm_create_or_open(ep, name, len);	

	if (ret != 0) {

		return(ret);
	}

	map = com_shm_endpoint_get_map(ep);

	/* Create the interprocess mutex to protect the shared memory area */

	ut_memcpy(buf, name, len);
	ut_strcpy(buf + len, (char*)"_IBSHM_MTX");
	
	ret = ip_mutex_create(com_shm_get_ip_mutex(map), buf, &ip_hdl);
	
	if (ret != 0) {

		return(COM_ERR_NOT_SPECIFIED);
	}
	
	/* This endpoint structure owns the shared memory area */

	com_shm_endpoint_set_owns_shm(ep, TRUE);
	ep->ip_mutex = ip_hdl;

	mutex_enter(&com_shm_destination_mutex);

	/* Increment the count of successful bindings */

	com_shm_bind_count++;

	mutex_exit(&com_shm_destination_mutex);

	com_shm_set_not_empty(map, FALSE);
	com_shm_set_empty_waiters(map, 0);
	com_shm_set_max_len(map, com_shm_endpoint_get_size(ep));

	com_shm_set_valid(map, COM_SHM_VALID);

	os_event_set(com_shm_endpoint_get_empty(ep));

	return(0);
}	

/*************************************************************************
Waits for a datagram to arrive at an endpoint. */

ulint
com_shm_recvfrom(
/*=============*/
				/* out: 0 if succeed, else error number */
	com_shm_endpoint_t* ep,	/* in: communications endpoint */
	byte*		buf,	/* out: datagram buffer; the buffer is
				supplied by the caller */
	ulint		buf_len,/* in: datagram buffer length */
	ulint*		len,	/* out: datagram length */
	char*		from,	/* out: address name buffer; the buffer is
				supplied by the caller */
	ulint		from_len,/* in: address name buffer length */
	ulint*		addr_len)/* out: address name length */
{
	com_shm_t*	map;
	ulint		loop_count;

	map = com_shm_endpoint_get_map(ep);

	loop_count = 0;
loop:
	com_shm_system_call_count++;

	/* NOTE: automatic event */
	
	os_event_wait(com_shm_endpoint_get_not_empty(ep));

	loop_count++;

	if (loop_count > 1) {
		printf("!!!!!!!!COM_SHM loop count %lu\n", loop_count);
	}

	ut_ad(loop_count < 2);

#ifdef notdefined
	if (!com_shm_get_not_empty(map)) {

		/* There was no datagram, give up the time slice
		for some writer thread to insert a datagram */

		com_shm_exit(ep);

		os_thread_yield();

		com_shm_enter(ep);
	}
#endif
	com_shm_enter(ep);

	if (!com_shm_get_not_empty(map)) {
		/* There was no datagram, wait for the event */

		com_shm_exit(ep);

		goto loop;
	}
		
	if (com_shm_get_data_len(map) > buf_len) {

		com_shm_exit(ep);

		return(COM_ERR_DATA_BUFFER_TOO_SMALL);
	}

	if (com_shm_get_addr_len(map) > from_len) {

		com_shm_exit(ep);

		return(COM_ERR_ADDR_BUFFER_TOO_SMALL);
	}

	*len = com_shm_get_data_len(map);
	*addr_len = com_shm_get_addr_len(map);

	ut_memcpy(buf, com_shm_get_data(map), *len);
	ut_memcpy(from, com_shm_get_addr(map), *addr_len);

	com_shm_set_not_empty(map, FALSE);

	/* If there may be writers queuing to insert the datagram, signal the
	empty-event */

	if (com_shm_get_empty_waiters(map) != 0) {

		com_shm_system_call_count++;

		os_event_set(com_shm_endpoint_get_empty(ep));
	}
	
	com_shm_exit(ep);

	return(0);
}

/*************************************************************************
Sends a datagram to the specified destination. */

ulint
com_shm_sendto(
/*===========*/
				/* out: 0 if succeed, else error number */
	com_shm_endpoint_t* ep,	/* in: communications endpoint */
	byte*		buf,	/* in: datagram buffer */
	ulint		len,	/* in: datagram length */
	char*		to,	/* in: address name buffer */
	ulint		tolen)	/* in: address name length */
{
	com_shm_endpoint_t*	ep2;
	com_shm_t*		map;
	ulint			sender_len;
	ulint			ret;
	ulint			loop_count;
	
	/* Try first to find from the cached destination addresses */

	ep2 = com_shm_destination_cache_search(to, tolen);

	if (ep2 == NULL) {
		/* Did not find it in the cache */
		ep2 = com_shm_endpoint_create();

		ret = com_shm_open(ep2, to, tolen);

		if (ret != 0) {
			com_shm_endpoint_free(ep2);

			return(ret);
		}

		/* Insert into the cached destination addresses */

		com_shm_destination_cache_insert(ep2);
	}

	map = com_shm_endpoint_get_map(ep2);

	if (com_shm_get_valid(map) != COM_SHM_VALID) {

		com_shm_exit(ep2);

		return(COM_ERR_DGRAM_NOT_DELIVERED);
	}

	if (com_shm_get_max_len(map) < len) {

		com_shm_exit(ep2);

		return(COM_ERR_DATA_TOO_LONG);
	}

	/* Optimistically, we first go to see if the datagram area is empty,
	without waiting for the empty-event */

	loop_count = 0;
loop:
	loop_count++;

	if (loop_count > 5) {
		printf("!!!!!!COM_SHM Notempty loop count %lu\n", loop_count);
	}

	ut_ad(loop_count < 100);

	com_shm_enter(ep2);

	if (com_shm_get_not_empty(map)) {

		/* Not empty, we cannot insert a datagram */

		com_shm_set_empty_waiters(map,
					1 + com_shm_get_empty_waiters(map));
		com_shm_exit(ep2);

		com_shm_system_call_count++;

		/* Wait for the area to become empty */
		/* NOTE: automatic event */

		ret = os_event_wait_time(com_shm_endpoint_get_empty(ep2),
								10000000);
		ut_a(ret == 0);

		com_shm_enter(ep2);

		com_shm_set_empty_waiters(map,
					com_shm_get_empty_waiters(map) - 1);
		com_shm_exit(ep2);

		goto loop;
	}
		
	sender_len = com_shm_endpoint_get_addr_len(ep);
	
	com_shm_set_data_len(map, len);
	com_shm_set_addr_len(map, sender_len);

	ut_memcpy(com_shm_get_data(map), buf, len);
	ut_memcpy(com_shm_get_addr(map), com_shm_endpoint_get_addr(ep),
					 			sender_len);
	com_shm_set_not_empty(map, TRUE);
#ifdef notdefined
	com_shm_exit(ep2);

	/* Now we give up our time slice voluntarily to give some reader
	thread chance to fetch the datagram */

	os_thread_yield();

	com_shm_enter(ep2);

	if (com_shm_get_not_empty(map)) {
#endif
		com_shm_system_call_count++;

		com_shm_exit(ep2);

		/* Signal the event */

		os_event_set(com_shm_endpoint_get_not_empty(ep2));

		return(0);

#ifdef notdefined
	}

	com_shm_exit(ep2);
	
	return(0);
#endif
}