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
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
|
// $Id$
#define ACE_BUILD_DLL
#include "ace/POSIX_Proactor.h"
#if defined (ACE_HAS_AIO_CALLS)
#include "ace/Task_T.h"
#include "ace/Log_Msg.h"
#include "ace/Object_Manager.h"
#if !defined (__ACE_INLINE__)
#include "ace/POSIX_Proactor.i"
#endif /* __ACE_INLINE__ */
class ACE_Export ACE_POSIX_Wakeup_Completion : public ACE_POSIX_Asynch_Result
{
// = TITLE
//
// This is result object is used by the <end_event_loop> of the
// ACE_Proactor interface to wake up all the threads blocking
// for completions.
//
// = DESCRIPTION
//
public:
ACE_POSIX_Wakeup_Completion (ACE_Handler &handler,
const void *act = 0,
ACE_HANDLE event = ACE_INVALID_HANDLE,
int priority = 0,
int signal_number = ACE_SIGRTMIN);
// Constructor.
virtual ~ACE_POSIX_Wakeup_Completion (void);
// Destructor.
virtual void complete (u_long bytes_transferred = 0,
int success = 1,
const void *completion_key = 0,
u_long error = 0);
// This method calls the <handler>'s <handle_wakeup> method.
};
// *********************************************************************
ACE_POSIX_Proactor::~ACE_POSIX_Proactor (void)
{
this->close ();
}
int
ACE_POSIX_Proactor::close (void)
{
return 0;
}
int
ACE_POSIX_Proactor::register_handle (ACE_HANDLE handle,
const void *completion_key)
{
ACE_UNUSED_ARG (handle);
ACE_UNUSED_ARG (completion_key);
return 0;
}
int
ACE_POSIX_Proactor::wake_up_dispatch_threads (void)
{
return 0;
}
int
ACE_POSIX_Proactor::close_dispatch_threads (int)
{
return 0;
}
size_t
ACE_POSIX_Proactor::number_of_threads (void) const
{
// @@ Implement it.
ACE_NOTSUP_RETURN (0);
}
void
ACE_POSIX_Proactor::number_of_threads (size_t threads)
{
// @@ Implement it.
ACE_UNUSED_ARG (threads);
}
ACE_HANDLE
ACE_POSIX_Proactor::get_handle (void) const
{
return ACE_INVALID_HANDLE;
}
ACE_Asynch_Read_Stream_Result_Impl *
ACE_POSIX_Proactor::create_asynch_read_stream_result (ACE_Handler &handler,
ACE_HANDLE handle,
ACE_Message_Block &message_block,
u_long bytes_to_read,
const void* act,
ACE_HANDLE event,
int priority,
int signal_number)
{
ACE_Asynch_Read_Stream_Result_Impl *implementation;
ACE_NEW_RETURN (implementation,
ACE_POSIX_Asynch_Read_Stream_Result (handler,
handle,
message_block,
bytes_to_read,
act,
event,
priority,
signal_number),
0);
return implementation;
}
ACE_Asynch_Write_Stream_Result_Impl *
ACE_POSIX_Proactor::create_asynch_write_stream_result (ACE_Handler &handler,
ACE_HANDLE handle,
ACE_Message_Block &message_block,
u_long bytes_to_write,
const void* act,
ACE_HANDLE event,
int priority,
int signal_number)
{
ACE_Asynch_Write_Stream_Result_Impl *implementation;
ACE_NEW_RETURN (implementation,
ACE_POSIX_Asynch_Write_Stream_Result (handler,
handle,
message_block,
bytes_to_write,
act,
event,
priority,
signal_number),
0);
return implementation;
}
ACE_Asynch_Read_File_Result_Impl *
ACE_POSIX_Proactor::create_asynch_read_file_result (ACE_Handler &handler,
ACE_HANDLE handle,
ACE_Message_Block &message_block,
u_long bytes_to_read,
const void* act,
u_long offset,
u_long offset_high,
ACE_HANDLE event,
int priority,
int signal_number)
{
ACE_Asynch_Read_File_Result_Impl *implementation;
ACE_NEW_RETURN (implementation,
ACE_POSIX_Asynch_Read_File_Result (handler,
handle,
message_block,
bytes_to_read,
act,
offset,
offset_high,
event,
priority,
signal_number),
0);
return implementation;
}
ACE_Asynch_Write_File_Result_Impl *
ACE_POSIX_Proactor::create_asynch_write_file_result (ACE_Handler &handler,
ACE_HANDLE handle,
ACE_Message_Block &message_block,
u_long bytes_to_write,
const void* act,
u_long offset,
u_long offset_high,
ACE_HANDLE event,
int priority,
int signal_number)
{
ACE_Asynch_Write_File_Result_Impl *implementation;
ACE_NEW_RETURN (implementation,
ACE_POSIX_Asynch_Write_File_Result (handler,
handle,
message_block,
bytes_to_write,
act,
offset,
offset_high,
event,
priority,
signal_number),
0);
return implementation;
}
ACE_Asynch_Accept_Result_Impl *
ACE_POSIX_Proactor::create_asynch_accept_result (ACE_Handler &handler,
ACE_HANDLE listen_handle,
ACE_HANDLE accept_handle,
ACE_Message_Block &message_block,
u_long bytes_to_read,
const void* act,
ACE_HANDLE event,
int priority,
int signal_number)
{
ACE_Asynch_Accept_Result_Impl *implementation;
ACE_NEW_RETURN (implementation,
ACE_POSIX_Asynch_Accept_Result (handler,
listen_handle,
accept_handle,
message_block,
bytes_to_read,
act,
event,
priority,
signal_number),
0);
return implementation;
}
ACE_Asynch_Transmit_File_Result_Impl *
ACE_POSIX_Proactor::create_asynch_transmit_file_result (ACE_Handler &handler,
ACE_HANDLE socket,
ACE_HANDLE file,
ACE_Asynch_Transmit_File::Header_And_Trailer *header_and_trailer,
u_long bytes_to_write,
u_long offset,
u_long offset_high,
u_long bytes_per_send,
u_long flags,
const void *act,
ACE_HANDLE event,
int priority,
int signal_number)
{
ACE_Asynch_Transmit_File_Result_Impl *implementation;
ACE_NEW_RETURN (implementation,
ACE_POSIX_Asynch_Transmit_File_Result (handler,
socket,
file,
header_and_trailer,
bytes_to_write,
offset,
offset_high,
bytes_per_send,
flags,
act,
event,
priority,
signal_number),
0);
return implementation;
}
ACE_Asynch_Result_Impl *
ACE_POSIX_Proactor::create_asynch_timer (ACE_Handler &handler,
const void *act,
const ACE_Time_Value &tv,
ACE_HANDLE event,
int priority,
int signal_number)
{
ACE_Asynch_Result_Impl *implementation;
ACE_NEW_RETURN (implementation,
ACE_POSIX_Asynch_Timer (handler,
act,
tv,
event,
priority,
signal_number),
0);
return implementation;
}
ACE_POSIX_Proactor::ACE_POSIX_Proactor (void)
{
}
#if 0
int
ACE_POSIX_Proactor::handle_signal (int, siginfo_t *, ucontext_t *)
{
// Perform a non-blocking "poll" for all the I/O events that have
// completed in the I/O completion queue.
ACE_Time_Value timeout (0, 0);
int result = 0;
while (1)
{
result = this->handle_events (timeout);
if (result != 0 || errno == ETIME)
break;
}
// If our handle_events failed, we'll report a failure to the
// Reactor.
return result == -1 ? -1 : 0;
}
int
ACE_POSIX_Proactor::handle_close (ACE_HANDLE handle,
ACE_Reactor_Mask close_mask)
{
ACE_UNUSED_ARG (close_mask);
ACE_UNUSED_ARG (handle);
return this->close ();
}
#endif /* 0 */
void
ACE_POSIX_Proactor::application_specific_code (ACE_POSIX_Asynch_Result *asynch_result,
u_long bytes_transferred,
int success,
const void */* completion_key*/,
u_long error)
{
ACE_SEH_TRY
{
// Call completion hook
asynch_result->complete (bytes_transferred,
success,
0, // No completion key.
error);
}
ACE_SEH_FINALLY
{
// This is crucial to prevent memory leaks
delete asynch_result;
}
}
int
ACE_POSIX_Proactor::post_wakeup_completions (int how_many)
{
ACE_POSIX_Wakeup_Completion *wakeup_completion = 0;
for (ssize_t ci = 0; ci < how_many; ci++)
{
ACE_NEW_RETURN (wakeup_completion,
ACE_POSIX_Wakeup_Completion (this->wakeup_handler_),
-1);
if (wakeup_completion->post_completion (this) == -1)
return -1;
}
return 0;
}
class ACE_Export ACE_AIOCB_Notify_Pipe_Manager : public ACE_Handler
{
// = TITLE
// This class manages the notify pipe of the AIOCB
// Proactor. This class acts as the Handler for the
// <Asynch_Read> operations issued on the notify pipe. This
// class is very useful in implementing <Asynch_Accept> operation
// class for the <AIOCB_Proactor>. This is also useful for
// implementing <post_completion> for <AIOCB_Proactor>.
//
// = DESCRIPTION
// <AIOCB_Proactor> class issues a <Asynch_Read> on
// the pipe, using this class as the
// Handler. <POSIX_Asynch_Result *>'s are sent through the
// notify pipe. When <POSIX_Asynch_Result *>'s show up on the
// notify pipe, the <POSIX_AIOCB_Proactor> dispatches the
// completion of the <Asynch_Read_Stream> and calls the
// <handle_read_stream> of this class. This class calls
// <complete> on the <POSIX_Asynch_Result *> and thus calls the
// application handler.
// Handling the MessageBlock:
// We give this message block to read the result pointer through
// the notify pipe. We expect that to read 4 bytes from the
// notify pipe, for each <accept> call. Before giving this
// message block to another <accept>, we update <wr_ptr> and put
// it in its initial position.
public:
ACE_AIOCB_Notify_Pipe_Manager (ACE_POSIX_AIOCB_Proactor *posix_aiocb_proactor);
// Constructor. You need the posix proactor because you need to call
// <application_specific_code>
virtual ~ACE_AIOCB_Notify_Pipe_Manager (void);
// Destructor.
int notify (ACE_POSIX_Asynch_Result *result);
// Send the result pointer through the notification pipe.
virtual void handle_read_stream (const ACE_Asynch_Read_Stream::Result &result);
// This is the call back method when <Asynch_Read> from the pipe is
// complete.
private:
ACE_POSIX_AIOCB_Proactor *posix_aiocb_proactor_;
// The implementation proactor class.
ACE_Message_Block message_block_;
// Message block to get ACE_POSIX_Asynch_Result pointer from the
// pipe.
ACE_Pipe pipe_;
// Pipe for the communication between Proactor and the
// Asynch_Accept.
ACE_POSIX_AIOCB_Asynch_Read_Stream read_stream_;
// To do asynch_read on the pipe.
ACE_AIOCB_Notify_Pipe_Manager (void);
// Default constructor. Shouldnt be called.
};
ACE_AIOCB_Notify_Pipe_Manager::ACE_AIOCB_Notify_Pipe_Manager (ACE_POSIX_AIOCB_Proactor *posix_aiocb_proactor)
: posix_aiocb_proactor_ (posix_aiocb_proactor),
message_block_ (sizeof (ACE_POSIX_Asynch_Accept_Result *)),
read_stream_ (posix_aiocb_proactor)
{
// Open the pipe.
this->pipe_.open ();
// Open the read stream.
if (this->read_stream_.open (*this,
this->pipe_.read_handle (),
0, // Completion Key
0) // Proactor
== -1)
ACE_ERROR ((LM_ERROR,
"%N:%l:%p\n",
"ACE_AIOCB_Notify_Pipe_Manager::ACE_AIOCB_Notify_Pipe_Manager:"
"Open on Read Stream failed"));
// Issue an asynch_read on the read_stream of the notify pipe.
if (this->read_stream_.read (this->message_block_,
sizeof (ACE_POSIX_Asynch_Result *),
0, // ACT
0) // Priority
== -1)
ACE_ERROR ((LM_ERROR,
"%N:%l:%p\n",
"ACE_AIOCB_Notify_Pipe_Manager::ACE_AIOCB_Notify_Pipe_Manager:"
"Read from pipe failed"));
}
ACE_AIOCB_Notify_Pipe_Manager::~ACE_AIOCB_Notify_Pipe_Manager (void)
{
}
int
ACE_AIOCB_Notify_Pipe_Manager::notify (ACE_POSIX_Asynch_Result *result)
{
// Send the result pointer through the pipe.
int return_val = ACE::send (this->pipe_.write_handle (),
ACE_reinterpret_cast (char *,
&result),
sizeof (result));
if (return_val != sizeof (result))
ACE_ERROR_RETURN ((LM_ERROR,
"(%P %t):%p\n",
"ACE_AIOCB_Notify_Pipe_Manager::notify"
"Error:Writing on to notify pipe failed"),
-1);
return 0;
}
void
ACE_AIOCB_Notify_Pipe_Manager::handle_read_stream (const ACE_Asynch_Read_Stream::Result &result)
{
// The message block actually contains the ACE_POSIX_Asynch_Result
// pointer.
ACE_POSIX_Asynch_Result *asynch_result = 0;
asynch_result = *(ACE_POSIX_Asynch_Result **) result.message_block ().rd_ptr ();
// Do the upcall.
this->posix_aiocb_proactor_->application_specific_code (asynch_result,
0, // No Bytes transferred.
1, // Success.
0, // Completion token.
0); // Error.
// Set the message block properly. Put the <wr_ptr> back in the
// initial position.
if (this->message_block_.length () > 0)
this->message_block_.wr_ptr (this->message_block_.rd_ptr ());
// One accept has completed. Issue a read to handle any
// <post_completion>s in the future.
if (this->read_stream_.read (this->message_block_,
sizeof (ACE_POSIX_Asynch_Result *),
0, // ACT
0) // Priority
== -1)
ACE_ERROR ((LM_ERROR,
"%N:%l:(%P | %t):%p\n",
"ACE_AIOCB_Notify_Pipe_Manager::handle_read_stream:"
"Read from pipe failed"));
}
// *********************************************************************
ACE_POSIX_AIOCB_Proactor::ACE_POSIX_AIOCB_Proactor (void)
: aiocb_notify_pipe_manager_ (0),
aiocb_list_max_size_ (ACE_RTSIG_MAX),
aiocb_list_cur_size_ (0)
{
// Initialize the array.
for (size_t ai = 0; ai < this->aiocb_list_max_size_; ai++)
{
aiocb_list_[ai] = 0;
result_list_ [ai] = 0;
}
// Accept Handler for aio_accept. Remember! this issues a Asynch_Read
// on the notify pipe for doing the Asynch_Accept.
ACE_NEW (aiocb_notify_pipe_manager_,
ACE_AIOCB_Notify_Pipe_Manager (this));
}
// Destructor.
ACE_POSIX_AIOCB_Proactor::~ACE_POSIX_AIOCB_Proactor (void)
{
}
int
ACE_POSIX_AIOCB_Proactor::handle_events (ACE_Time_Value &wait_time)
{
// Decrement <wait_time> with the amount of time spent in the method
ACE_Countdown_Time countdown (&wait_time);
return this->handle_events (wait_time.msec ());
}
int
ACE_POSIX_AIOCB_Proactor::handle_events (void)
{
return this->handle_events (ACE_INFINITE);
}
int
ACE_POSIX_AIOCB_Proactor::post_completion (ACE_POSIX_Asynch_Result *result)
{
// Notify to the completion queue.
return this->aiocb_notify_pipe_manager_->notify (result);
}
ACE_Asynch_Read_Stream_Impl *
ACE_POSIX_AIOCB_Proactor::create_asynch_read_stream (void)
{
ACE_Asynch_Read_Stream_Impl *implementation = 0;
ACE_NEW_RETURN (implementation,
ACE_POSIX_AIOCB_Asynch_Read_Stream (this),
0);
return implementation;
}
ACE_Asynch_Write_Stream_Impl *
ACE_POSIX_AIOCB_Proactor::create_asynch_write_stream (void)
{
ACE_Asynch_Write_Stream_Impl *implementation = 0;
ACE_NEW_RETURN (implementation,
ACE_POSIX_AIOCB_Asynch_Write_Stream (this),
0);
return implementation;
}
ACE_Asynch_Read_File_Impl *
ACE_POSIX_AIOCB_Proactor::create_asynch_read_file (void)
{
ACE_Asynch_Read_File_Impl *implementation = 0;
ACE_NEW_RETURN (implementation,
ACE_POSIX_AIOCB_Asynch_Read_File (this),
0);
return implementation;
}
ACE_Asynch_Write_File_Impl *
ACE_POSIX_AIOCB_Proactor::create_asynch_write_file (void)
{
ACE_Asynch_Write_File_Impl *implementation = 0;
ACE_NEW_RETURN (implementation,
ACE_POSIX_AIOCB_Asynch_Write_File (this),
0);
return implementation;
}
ACE_Asynch_Accept_Impl *
ACE_POSIX_AIOCB_Proactor::create_asynch_accept (void)
{
ACE_Asynch_Accept_Impl *implementation = 0;
ACE_NEW_RETURN (implementation,
ACE_POSIX_AIOCB_Asynch_Accept (this),
0);
return implementation;
}
ACE_Asynch_Transmit_File_Impl *
ACE_POSIX_AIOCB_Proactor::create_asynch_transmit_file (void)
{
ACE_Asynch_Transmit_File_Impl *implementation = 0;
ACE_NEW_RETURN (implementation,
ACE_POSIX_AIOCB_Asynch_Transmit_File (this),
0);
return implementation;
}
int
ACE_POSIX_AIOCB_Proactor::handle_events (unsigned long milli_seconds)
{
int result_suspend = 0;
if (milli_seconds == ACE_INFINITE)
{
// Indefinite blocking.
result_suspend = aio_suspend (this->aiocb_list_,
this->aiocb_list_max_size_,
0);
}
else
{
// Block on <aio_suspend> for <milli_seconds>
timespec timeout;
timeout.tv_sec = milli_seconds / 1000;
timeout.tv_nsec = (milli_seconds - (timeout.tv_sec * 1000)) * 1000;
result_suspend = aio_suspend (this->aiocb_list_,
this->aiocb_list_max_size_,
&timeout);
}
// Check for errors
if (result_suspend == -1)
{
// If failure is because of timeout, then return *0*, otherwise
// return -1.
if (errno == EAGAIN)
return 0;
else
ACE_ERROR_RETURN ((LM_ERROR,
"%N:%l:(%P | %t)::%p\n",
"ACE_POSIX_AIOCB_Proactor::handle_events:"
"aio_suspend failed"),
-1);
}
// No errors, check which aio has finished.
size_t ai;
int error_status = 0;
int return_status = 0;
for (ai = 0; ai < this->aiocb_list_max_size_; ai++)
{
// Dont process null blocks.
if (aiocb_list_ [ai] == 0)
continue;
// Analyze error and return values.
// Get the error status of the aio_ operation.
error_status = aio_error (aiocb_list_[ai]);
if (error_status == -1)
// <aio_error> itself has failed.
ACE_ERROR_RETURN ((LM_ERROR,
"%N:%l:(%P | %t)::%p\n",
"ACE_POSIX_AIOCB_Proactor::handle_events:"
"<aio_error> has failed"),
-1);
// Continue the loop if <aio_> operation is still in progress.
if (error_status == EINPROGRESS)
continue;
// Handle cancel'ed asynchronous operation. We dont have to call
// <aio_return> in this case, since return_status is going to be
// -1. We will pass 0 for the <bytes_transferred> in this case
if (error_status == ECANCELED)
{
return_status = 0;
break;
}
// Error_status is not -1 and not EINPROGRESS. So, an <aio_>
// operation has finished (successfully or unsuccessfully!!!)
// Get the return_status of the <aio_> operation.
return_status = aio_return (aiocb_list_[ai]);
if (return_status == -1)
ACE_ERROR_RETURN ((LM_ERROR,
"%N:%l:(%P | %t)::%p\n",
"ACE_POSIX_AIOCB_Proactor::handle_events:"
"<aio_return> failed"),
-1);
else
// This AIO has finished.
break;
}
// Something should have completed.
ACE_ASSERT (ai != this->aiocb_list_max_size_);
// Retrive the result pointer.
ACE_POSIX_Asynch_Result *asynch_result = this->result_list_ [ai];
// ACE_reinterpret_cast (ACE_POSIX_Asynch_Result *,
// this->aiocb_list_[ai]);
// ACE_dynamic_cast (ACE_POSIX_Asynch_Result *,
// this->aiocb_list_[ai]);
// Invalidate entry in the aiocb list.
this->aiocb_list_[ai] = 0;
this->result_list_ [ai] = 0;
this->aiocb_list_cur_size_--;
// Call the application code.
this->application_specific_code (asynch_result,
return_status, // Bytes transferred.
1, // Success
0, // No completion key.
error_status); // Error
// Success
return 1;
}
void
ACE_POSIX_AIOCB_Proactor::application_specific_code (ACE_POSIX_Asynch_Result *asynch_result,
u_long bytes_transferred,
int success,
const void *completion_key,
u_long error)
{
ACE_POSIX_Proactor::application_specific_code (asynch_result,
bytes_transferred,
success,
completion_key,
error);
}
int
ACE_POSIX_AIOCB_Proactor::register_aio_with_proactor (ACE_POSIX_Asynch_Result *result)
{
ACE_TRACE ("ACE_POSIX_AIOCB_Proactor::register_aio_with_proactor");
if (result == 0)
{
// Just check the status of the list.
if (this->aiocb_list_cur_size_ >=
this->aiocb_list_max_size_)
return -1;
else
return 0;
}
// Non-zero ptr. Find a free slot and store.
// Make sure again.
if (this->aiocb_list_cur_size_ >=
this->aiocb_list_max_size_)
ACE_ERROR_RETURN ((LM_ERROR,
"Error:Asynch_Operation: No space to store the <aio> info.\n"),
-1);
// Slot(s) available. Find a free one.
size_t ai;
for (ai = 0;
ai < this->aiocb_list_max_size_;
ai++)
if (this->aiocb_list_[ai] == 0)
break;
// Sanity check.
if (ai == this->aiocb_list_max_size_)
ACE_ERROR_RETURN ((LM_ERROR,
"Error:Asynch_Operation: No space to store the <aio> info.\n"),
-1);
// Store the pointers.
this->aiocb_list_[ai] = result;
this->result_list_ [ai] = result;
this->aiocb_list_cur_size_ ++;
return 0;
}
// *********************************************************************
ACE_POSIX_SIG_Proactor::ACE_POSIX_SIG_Proactor (void)
{
// = Mask all the signals, keep a mask set with ACE_SIGRTMIN and set
// up signal handler for SIGRTMIN.
// Mask all the signals.
if (this->mask_all () != 0)
return;
// = Keep a mask set with ACE_SIGRTMIN.
// Clear the signal set.
if (sigemptyset (&this->RT_completion_signals_) == -1)
ACE_ERROR ((LM_ERROR,
"Error:%p\n",
"Couldn't init the RT completion signal set"));
// Add the signal number to the signal set.
if (sigaddset (&this->RT_completion_signals_, ACE_SIGRTMIN) == -1)
ACE_ERROR ((LM_ERROR,
"Error:%p\n",
"Couldnt init the RT completion signal set"));
// Set up the signal handler for SIGRTMIN.
setup_signal_handler (ACE_SIGRTMIN);
}
ACE_POSIX_SIG_Proactor::ACE_POSIX_SIG_Proactor (const sigset_t signal_set)
{
// = Keep <Signal_set> with the Proactor, mask all the signals and
// setup signal handlers for the signals in the <signal_set>.
// = Keep <signal_set> with the Proactor.
// Empty the signal set first.
if (sigemptyset (&this->RT_completion_signals_) == -1)
ACE_ERROR ((LM_ERROR,
"Error:(%P | %t):%p\n",
"sigemptyset failed"));
// Put the <signal_set>.
if (ACE_OS::pthread_sigmask (SIG_SETMASK, &signal_set, 0) != 0)
ACE_ERROR ((LM_ERROR,
"Error:(%P | %t):%p\n",
"pthread_sigmask failed"));
// Get the <signal_set> back from the OS.
if (ACE_OS::pthread_sigmask (SIG_SETMASK, 0, &this->RT_completion_signals_) != 0)
ACE_ERROR ((LM_ERROR,
"Error:(%P | %t):%p\n",
"ACE_OS::pthread_sigmask failed"));
// Mask all the signals.
if (this->mask_all () != 0)
return;
// For each signal number present in the <signal_set>, set up the
// signal handler.
int member = 0;
for (int si = ACE_SIGRTMIN; si <= ACE_SIGRTMAX; si++)
{
member = sigismember (&signal_set,
si);
if (member == -1)
ACE_ERROR ((LM_ERROR,
"%N:%l:(%P | %t)::%p\n",
"ACE_POSIX_SIG_Proactor::ACE_POSIX_SIG_Proactor:"
"sigismember failed"));
else if (member == 1)
{
if (this->setup_signal_handler (si) == -1)
return;
}
}
}
ACE_POSIX_SIG_Proactor::~ACE_POSIX_SIG_Proactor (void)
{
// @@ Enable the masked signals again.
}
int
ACE_POSIX_SIG_Proactor::handle_events (ACE_Time_Value &wait_time)
{
// Decrement <wait_time> with the amount of time spent in the method
ACE_Countdown_Time countdown (&wait_time);
return this->handle_events (wait_time.msec ());
}
int
ACE_POSIX_SIG_Proactor::handle_events (void)
{
return this->handle_events (ACE_INFINITE);
}
int
ACE_POSIX_SIG_Proactor::post_completion (ACE_POSIX_Asynch_Result *result)
{
// Get this process id.
pid_t pid = ACE_OS::getpid ();
if (pid == (pid_t) -1)
ACE_ERROR_RETURN ((LM_ERROR,
"Error:%N:%l(%P | %t):%p",
"<getpid> failed"),
-1);
// Set the signal information.
sigval value;
value.sival_ptr = ACE_reinterpret_cast (void *,
result);
// Queue the signal.
if (sigqueue (pid, result->signal_number (), value) == -1)
ACE_ERROR_RETURN ((LM_ERROR,
"Error:%N:%l:(%P | %t):%p\n",
"<sigqueue> failed"),
-1);
return 0;
}
ACE_Asynch_Read_Stream_Impl *
ACE_POSIX_SIG_Proactor::create_asynch_read_stream (void)
{
ACE_Asynch_Read_Stream_Impl *implementation = 0;
ACE_NEW_RETURN (implementation,
ACE_POSIX_SIG_Asynch_Read_Stream (this),
0);
return implementation;
}
ACE_Asynch_Write_Stream_Impl *
ACE_POSIX_SIG_Proactor::create_asynch_write_stream (void)
{
ACE_Asynch_Write_Stream_Impl *implementation = 0;
ACE_NEW_RETURN (implementation,
ACE_POSIX_SIG_Asynch_Write_Stream (this),
0);
return implementation;
}
ACE_Asynch_Read_File_Impl *
ACE_POSIX_SIG_Proactor::create_asynch_read_file (void)
{
ACE_Asynch_Read_File_Impl *implementation = 0;
ACE_NEW_RETURN (implementation,
ACE_POSIX_SIG_Asynch_Read_File (this),
0);
return implementation;
}
ACE_Asynch_Write_File_Impl *
ACE_POSIX_SIG_Proactor::create_asynch_write_file (void)
{
ACE_Asynch_Write_File_Impl *implementation = 0;
ACE_NEW_RETURN (implementation,
ACE_POSIX_SIG_Asynch_Write_File (this),
0);
return implementation;
}
ACE_Asynch_Accept_Impl *
ACE_POSIX_SIG_Proactor::create_asynch_accept (void)
{
ACE_Asynch_Accept_Impl *implementation = 0;
ACE_NEW_RETURN (implementation,
ACE_POSIX_SIG_Asynch_Accept (this),
0);
return implementation;
}
ACE_Asynch_Transmit_File_Impl *
ACE_POSIX_SIG_Proactor::create_asynch_transmit_file (void)
{
ACE_Asynch_Transmit_File_Impl *implementation = 0;
ACE_NEW_RETURN (implementation,
ACE_POSIX_SIG_Asynch_Transmit_File (this),
0);
return implementation;
}
ACE_Asynch_Result_Impl *
ACE_POSIX_SIG_Proactor::create_asynch_timer (ACE_Handler &handler,
const void *act,
const ACE_Time_Value &tv,
ACE_HANDLE event,
int priority,
int signal_number)
{
int is_member = 0;
// Fix the signal number.
if (signal_number == -1)
{
int si;
for (si = ACE_SIGRTMAX;
(is_member == 0) && (si >= ACE_SIGRTMIN);
si--)
{
is_member = sigismember (&this->RT_completion_signals_,
si);
if (is_member == -1)
ACE_ERROR_RETURN ((LM_ERROR,
"%N:%l:(%P | %t)::\n",
"ACE_POSIX_SIG_Proactor::create_asynch_timer:"
"sigismember failed"),
0);
}
if (is_member == 0)
ACE_ERROR_RETURN ((LM_ERROR,
"Error:%N:%l:(%P | %t)::%s\n",
"ACE_POSIX_SIG_Proactor::ACE_POSIX_SIG_Proactor:"
"Signal mask set empty"),
0);
else
// + 1 to nullify loop increment.
signal_number = si + 1;
}
ACE_Asynch_Result_Impl *implementation;
ACE_NEW_RETURN (implementation,
ACE_POSIX_Asynch_Timer (handler,
act,
tv,
event,
priority,
signal_number),
0);
return implementation;
}
int
ACE_POSIX_SIG_Proactor::setup_signal_handler (int signal_number) const
{
// Set up the handler(actually Null handler) for this real-time
// signal.
struct sigaction reaction;
sigemptyset (&reaction.sa_mask); // Nothing else to mask.
reaction.sa_flags = SA_SIGINFO; // Realtime flag.
#if defined (SA_SIGACTION)
// Lynx says, it is better to set this bit, to be portable.
reaction.sa_flags &= SA_SIGACTION;
#endif /* SA_SIGACTION */
// Null handler function.
reaction.sa_sigaction =
ACE_SIGNAL_C_FUNC (&ACE_POSIX_SIG_Proactor::null_handler);
int sigaction_return = sigaction (signal_number,
&reaction,
0);
if (sigaction_return == -1)
ACE_ERROR_RETURN ((LM_ERROR,
"Error:%p\n",
"Proactor couldnt do sigaction for the RT SIGNAL"),
-1);
return 0;
}
void
ACE_POSIX_SIG_Proactor::null_handler (int signal_number,
siginfo_t * /* info */,
void * /* context */)
{
ACE_ERROR ((LM_ERROR,
"Error:(%P | %t):ACE_POSIX_SIG_Proactor::null_handler called,"
"Signal number %d,"
"Mask all the RT signals for this thread\n",
signal_number));
}
int
ACE_POSIX_SIG_Proactor::mask_all (void) const
{
sigset_t full_set;
// Get full set.
if (sigfillset (&full_set) != 0)
ACE_ERROR_RETURN ((LM_ERROR,
"Error:(%P | %t):%p\n",
"sigfillset failed"),
-1);
// Mask them.
if (ACE_OS::pthread_sigmask (SIG_SETMASK, &full_set, 0) != 0)
ACE_ERROR_RETURN ((LM_ERROR,
"Error:(%P | %t):%p\n",
"pthread_sigmask failed"),
-1);
return 0;
}
int
ACE_POSIX_SIG_Proactor::handle_events (unsigned long milli_seconds)
{
int result_sigwait = 0;
siginfo_t sig_info;
// Mask all the signals.
if (this->mask_all () != 0)
return -1;
// Wait for the signals.
if (milli_seconds == ACE_INFINITE)
{
result_sigwait = sigwaitinfo (&this->RT_completion_signals_,
&sig_info);
}
else
{
// Wait for <milli_seconds> amount of time.
timespec timeout;
timeout.tv_sec = milli_seconds / 1000;
timeout.tv_nsec = (milli_seconds - (timeout.tv_sec * 1000)) * 1000;
result_sigwait = sigtimedwait (&this->RT_completion_signals_,
&sig_info,
&timeout);
}
// Check for errors
if (result_sigwait == -1)
{
// If failure is coz of timeout, then return *0* but set errno
// appropriately.
if (errno == EAGAIN)
return 0;
else
ACE_ERROR_RETURN ((LM_ERROR,
"%N:%l:(%P | %t)::%p\n",
"ACE_POSIX_SIG_Proactor::handle_events:"
"sigtimedwait/sigwaitinfo failed"),
-1);
}
// No errors, RT compleion signal is received.
// Is the signo returned consistent with the sig info?
if (sig_info.si_signo != result_sigwait)
ACE_ERROR_RETURN ((LM_ERROR,
"Error:%N:%l:(%P | %t):"
"ACE_POSIX_SIG_Proactor::handle_events:"
"Inconsistent signal number (%d) in the signal info block",
sig_info.si_signo),
-1);
// Retrive the result pointer.
ACE_POSIX_Asynch_Result *asynch_result = ACE_reinterpret_cast (ACE_POSIX_Asynch_Result *,
sig_info.si_value.sival_ptr);
// Check the <signal code> and act according to that.
if (sig_info.si_code == SI_ASYNCIO)
{
// Analyze error and return values.
int error_status = 0;
int return_status = 0;
// Check the error status
error_status = aio_error (asynch_result);
// <aio_error> itself has failed.
if (error_status == -1)
ACE_ERROR_RETURN ((LM_ERROR,
"%N:%l:(%P | %t)::%p\n",
"ACE_POSIX_SIG_Proactor::handle_events:"
"<aio_error> has failed"),
-1);
// Completion signal has been received, so it can't be in
// progress.
if (error_status == EINPROGRESS)
ACE_ERROR_RETURN ((LM_ERROR,
"%N:%l:(%P | %t)::%p\n",
"ACE_POSIX_SIG_Proactor::handle_events:"
"Internal error: AIO in progress. "
"But completion signal was received"),
-1);
// Handle cancel'ed asynchronous operation. We dont have to call
// <aio_return> in this case, since return_status is going to be
// -1. We will pass 0 for the <bytes_transferred> in this case
if (error_status == ECANCELED)
{
return_status = 0;
}
else
{
// Get the return_status of the <aio_> operation.
return_status = aio_return (asynch_result);
// Failure.
if (return_status == -1)
ACE_ERROR_RETURN ((LM_ERROR,
"%N:%l:(%P | %t)::%p\n",
"ACE_POSIX_SIG_Proactor::handle_events:"
"<aio_return> failed"),
-1);
}
// error status and return status are obtained. Dispatch the
// completion .
this->application_specific_code (asynch_result,
return_status,
1, // Result : True.
0, // No completion key.
error_status); // Error.
}
else if (sig_info.si_code == SI_QUEUE)
{
this->application_specific_code (asynch_result,
0, // No bytes transferred.
1, // Result : True.
0, // No completion key.
0); // No error.
}
else
// Unknown signal code.
ACE_ERROR_RETURN ((LM_DEBUG,
"%N:%l:(%P | %t):",
"ACE_POSIX_SIG_Proactor::handle_events:\n"
"Unexpected signal code (%d) returned on completion querying\n",
sig_info.si_code),
-1);
// Success
return 1;
}
// *********************************************************************
ACE_POSIX_Asynch_Timer::ACE_POSIX_Asynch_Timer (ACE_Handler &handler,
const void *act,
const ACE_Time_Value &tv,
ACE_HANDLE event,
int priority,
int signal_number)
: ACE_Asynch_Result_Impl (),
ACE_POSIX_Asynch_Result (handler, act, event, 0, 0, priority, signal_number),
time_ (tv)
{
}
void
ACE_POSIX_Asynch_Timer::complete (u_long /* bytes_transferred */,
int /* success */,
const void * /* completion_key */,
u_long /* error */)
{
this->handler_.handle_time_out (this->time_, this->act ());
}
// *********************************************************************
ACE_POSIX_Wakeup_Completion::ACE_POSIX_Wakeup_Completion (ACE_Handler &handler,
const void *act,
ACE_HANDLE event,
int priority,
int signal_number)
: ACE_Asynch_Result_Impl (),
ACE_POSIX_Asynch_Result (handler, act, event, 0, 0, priority, signal_number)
{
}
ACE_POSIX_Wakeup_Completion::~ACE_POSIX_Wakeup_Completion (void)
{
}
void
ACE_POSIX_Wakeup_Completion::complete (u_long /* bytes_transferred */,
int /* success */,
const void * /* completion_key */,
u_long /* error */)
{
this->handler_.handle_wakeup ();
}
#endif /* ACE_HAS_AIO_CALLS */
|