1 /**
2   Licensed to the Apache Software Foundation (ASF) under one
3   or more contributor license agreements.  See the NOTICE file
4   distributed with this work for additional information
5   regarding copyright ownership.  The ASF licenses this file
6   to you under the Apache License, Version 2.0 (the
7   "License"); you may not use this file except in compliance
8   with the License.  You may obtain a copy of the License at
9 
10       http://www.apache.org/licenses/LICENSE-2.0
11 
12   Unless required by applicable law or agreed to in writing, software
13   distributed under the License is distributed on an "AS IS" BASIS,
14   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   See the License for the specific language governing permissions and
16   limitations under the License.
17  */
18 
19 /**
20  * @file Continuation.h
21  * @brief Wrapper class for TS API type TSCont.
22  */
23 
24 #pragma once
25 
26 #include <ts/ts.h>
27 
28 namespace atscppapi
29 {
30 class Continuation
31 {
32 public:
33   using Mutex = TSMutex;
34 
35   using Action = TSAction;
36 
37   // Create continuation, mutexp may be nullptr.
38   //
Continuation(Mutex mutexp)39   explicit Continuation(Mutex mutexp) : _cont(TSContCreate(_generalEventFunc, mutexp))
40   {
41     TSContDataSet(_cont, static_cast<void *>(this));
42   }
43 
44   // Create "empty" continuation, can only be populated by move assignment.
45   //
Continuation()46   Continuation() {}
47 
48   TSCont
asTSCont() const49   asTSCont() const
50   {
51     return _cont;
52   }
53 
54   // Get mutex (for "non-empty" continuation).
55   //
56   Mutex
mutex()57   mutex()
58   {
59     return _cont ? TSContMutexGet(_cont) : nullptr;
60   }
61 
62   void
destroy()63   destroy()
64   {
65     if (_cont) {
66       TSContDestroy(_cont);
67       _cont = nullptr;
68     }
69   }
70 
~Continuation()71   virtual ~Continuation()
72   {
73     if (_cont) {
74       TSContDestroy(_cont);
75     }
76   }
77 
78   // No copying.
79   Continuation(const Continuation &) = delete;
80   Continuation &operator=(const Continuation &) = delete;
81 
82   // Moving allowed.
Continuation(Continuation && that)83   Continuation(Continuation &&that)
84   {
85     _cont      = that._cont;
86     that._cont = nullptr;
87     TSContDataSet(_cont, static_cast<void *>(this));
88   }
89   Continuation &
operator =(Continuation && that)90   operator=(Continuation &&that)
91   {
92     if (&that != this) {
93       if (_cont) {
94         TSContDestroy(_cont);
95       }
96       _cont      = that._cont;
97       that._cont = nullptr;
98       TSContDataSet(_cont, static_cast<void *>(this));
99     }
100     return *this;
101   }
102 
103   explicit operator bool() const { return _cont != nullptr; }
104 
105   int
call(TSEvent event,void * edata=nullptr)106   call(TSEvent event, void *edata = nullptr)
107   {
108     return TSContCall(_cont, event, edata);
109   }
110 
111   // Timeout of zero means no timeout.
112   //
113   Action
schedule(TSHRTime timeout=0,TSThreadPool tp=TS_THREAD_POOL_NET)114   schedule(TSHRTime timeout = 0, TSThreadPool tp = TS_THREAD_POOL_NET)
115   {
116     return TSContScheduleOnPool(_cont, timeout, tp);
117   }
118 
119   // Timeout of zero means no timeout.
120   //
121   Action
httpSchedule(TSHttpTxn httpTransactionp,TSHRTime timeout=0)122   httpSchedule(TSHttpTxn httpTransactionp, TSHRTime timeout = 0)
123   {
124     return TSHttpSchedule(_cont, httpTransactionp, timeout);
125   }
126 
127   Action
scheduleEvery(TSHRTime interval,TSThreadPool tp=TS_THREAD_POOL_NET)128   scheduleEvery(TSHRTime interval /* milliseconds */, TSThreadPool tp = TS_THREAD_POOL_NET)
129   {
130     return TSContScheduleEveryOnPool(_cont, interval, tp);
131   }
132 
133 protected:
134   // Distinct continuation behavior is achieved by overriding this function in a derived continuation type.
135   //
136   virtual int _run(TSEvent event, void *edata) = 0;
137 
138   // This is the event function for all continuations in C++ plugins.
139   //
140   static int _generalEventFunc(TSCont cont, TSEvent event, void *edata);
141 
142   TSCont _cont = nullptr;
143 };
144 
145 } // end namespace atscppapi
146