3
Generic interface which enables any event or async activity to be cancelled
5
@section license License
7
Licensed to the Apache Software Foundation (ASF) under one
8
or more contributor license agreements. See the NOTICE file
9
distributed with this work for additional information
10
regarding copyright ownership. The ASF licenses this file
11
to you under the Apache License, Version 2.0 (the
12
"License"); you may not use this file except in compliance
13
with the License. You may obtain a copy of the License at
15
http://www.apache.org/licenses/LICENSE-2.0
17
Unless required by applicable law or agreed to in writing, software
18
distributed under the License is distributed on an "AS IS" BASIS,
19
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
See the License for the specific language governing permissions and
21
limitations under the License.
30
#include "I_Continuation.h"
33
Represents an operation initiated on a Processor.
35
The Action class is an abstract representation of an operation
36
being executed by some Processor. A reference to an Action object
37
allows you to cancel an ongoing asynchronous operation before it
38
completes. This means that the Continuation specified for the
39
operation will not be called back.
41
Actions or classes derived from Action are the typical return
42
type of methods exposed by Processors in the Event System and
43
throughout the IO Core libraries.
45
The canceller of an action must be the state machine that will
46
be called back by the task and that state machine's lock must be
47
held while calling cancel.
49
Processor implementers:
51
You must ensure that no events are sent to the state machine after
52
the operation has been cancelled appropriately.
56
Processor functions that are asynchronous must return actions to
57
allow the calling state machine to cancel the task before completion.
58
Because some processor functions are reentrant, they can call
59
back the state machine before the returning from the call that
60
creates the actions. To handle this case, special values are
61
returned in place of an action to indicate to the state machine
62
that the action is already completed.
64
- @b ACTION_RESULT_DONE The processor has completed the task
65
and called the state machine back inline.
66
- @b ACTION_RESULT_INLINE Not currently used.
67
- @b ACTION_RESULT_IO_ERROR Not currently used.
69
To make matters more complicated, it's possible if the result is
70
ACTION_RESULT_DONE that state machine deallocated itself on the
71
reentrant callback. Thus, state machine implementers MUST either
72
use a scheme to never deallocate their machines on reentrant
73
callbacks OR immediately check the returned action when creating
74
an asynchronous task and if it is ACTION_RESULT_DONE neither read
75
nor write any state variables. With either method, it's imperative
76
that the returned action always be checked for special values and
77
the value handled accordingly.
81
Actions are allocated by the Processor performing the actions.
82
It is the processor's responsbility to handle deallocation once
83
the action is complete or cancelled. A state machine MUST NOT
84
access an action once the operation that returned the Action has
85
completed or it has cancelled the Action.
94
Contination that initiated this action.
96
The reference to the initiating continuation is only used to
97
verify that the action is being cancelled by the correct
98
continuation. This field should not be accesed or modified
99
directly by the state machine.
102
Continuation * continuation;
106
Reference to the Continuation's lock.
108
Keeps a reference to the Continuation's lock to preserve the
109
access to the cancelled field valid even when the state machine
110
has been deallocated. This field should not be accesed or
111
modified directly by the state machine.
114
Ptr<ProxyMutex> mutex;
117
Internal flag used to indicate whether the action has been
120
This flag is set after a call to cancel or cancel_action and
121
it should not be accesed or modified directly by the state
125
volatile int cancelled;
128
Cancels the asynchronous operation represented by this action.
130
This method is called by state machines willing to cancel an
131
ongoing asynchronous operation. Classes derived from Action may
132
perform additional steps before flagging this action as cancelled.
133
There are certain rules that must be followed in order to cancel
134
an action (see the Remarks section).
136
@param c Continuation associated with this Action.
139
virtual void cancel(Continuation * c = NULL) {
140
ink_assert(!c || c == continuation);
142
ink_assert(!cancelled);
151
Cancels the asynchronous operation represented by this action.
153
This method is called by state machines willing to cancel an
154
ongoing asynchronous operation. There are certain rules that
155
must be followed in order to cancel an action (see the Remarks
158
@param c Continuation associated with this Action.
161
void cancel_action(Continuation * c = NULL) {
162
ink_assert(!c || c == continuation);
164
ink_assert(!cancelled);
172
Continuation *operator =(Continuation * acont)
174
continuation = acont;
176
mutex = acont->mutex;
183
Constructor of the Action object. Processor implementers are
184
responsible for associating this action with the proper
188
Action():continuation(NULL), cancelled(false) {
191
#if defined(__GNUC__)
197
#define ACTION_RESULT_NONE MAKE_ACTION_RESULT(0)
198
#define ACTION_RESULT_DONE MAKE_ACTION_RESULT(1)
199
#define ACTION_IO_ERROR MAKE_ACTION_RESULT(2)
200
#define ACTION_RESULT_INLINE MAKE_ACTION_RESULT(3)
202
// Use these classes by
203
// #define ACTION_RESULT_HOST_DB_OFFLINE
204
// MAKE_ACTION_RESULT(ACTION_RESULT_HOST_DB_BASE + 0)
206
#define MAKE_ACTION_RESULT(_x) (Action*)(((uintptr_t)((_x<<1)+1)))
208
#define ACTION_RESULT(_x) \
209
(int)((((uintptr_t)_x)&1)!=0?(((uintptr_t)>>1):(uintptr_t)0))
211
#define IS_ACTION_RESULT(_x) ((((uintptr_t)_x)&1) != 0)
213
#endif /*_Action_h_*/