1 /** @file
2 
3   A brief file description
4 
5   @section license License
6 
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
14 
15       http://www.apache.org/licenses/LICENSE-2.0
16 
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.
22  */
23 
24 #pragma once
25 
26 #include "tscore/ink_platform.h"
27 #include "I_Tasks.h"
28 
29 // Note that these should not be used for memory that wishes to retain
30 // NUMA socket affinity. We'll potentially return these on an arbitrarily
31 // selected processor/socket.
32 
33 template <class C> struct DeleterContinuation : public Continuation {
34 public: // Needed by WinNT compiler (compiler bug)
35   C *p;
36   int
37   dieEvent(int event, void *e)
38   {
39     (void)event;
40     (void)e;
41     if (p) {
42       delete p;
43     }
44     delete this;
45     return EVENT_DONE;
46   }
47   DeleterContinuation(C *ap) : Continuation(new_ProxyMutex()), p(ap) { SET_HANDLER(&DeleterContinuation::dieEvent); }
48 };
49 
50 // This can be useful for two things (or both):
51 //    1. Make sure to schedule a delete on an ET_TASK thread
52 //    2. Delay the delete (this should be used sparingly)
53 template <class C>
54 TS_INLINE void
55 new_Deleter(C *ap, ink_hrtime t)
56 {
57   if (t > 0) {
58     eventProcessor.schedule_in(new DeleterContinuation<C>(ap), t, ET_TASK);
59   } else {
60     eventProcessor.schedule_imm(new DeleterContinuation<C>(ap), ET_TASK);
61   }
62 }
63 
64 template <class C> struct FreeCallContinuation : public Continuation {
65 public: // Needed by WinNT compiler (compiler bug)
66   C *p;
67   int
68   dieEvent(int event, void *e)
69   {
70     (void)event;
71     (void)e;
72     p->free();
73     delete this;
74     return EVENT_DONE;
75   }
76   FreeCallContinuation(C *ap) : Continuation(nullptr), p(ap) { SET_HANDLER(&FreeCallContinuation::dieEvent); }
77 };
78 
79 template <class C>
80 TS_INLINE void
81 new_FreeCaller(C *ap, ink_hrtime t)
82 {
83   eventProcessor.schedule_in(new FreeCallContinuation<C>(ap), t, ET_TASK);
84 }
85 
86 struct FreerContinuation;
87 typedef int (FreerContinuation::*FreerContHandler)(int, void *);
88 
89 struct FreerContinuation : public Continuation {
90   void *p;
91 
92   int
93   dieEvent(int event, Event *e)
94   {
95     (void)event;
96     (void)e;
97     ats_free(p);
98     delete this;
99     return EVENT_DONE;
100   }
101 
102   FreerContinuation(void *ap) : Continuation(nullptr), p(ap) { SET_HANDLER((FreerContHandler)&FreerContinuation::dieEvent); }
103 };
104 
105 TS_INLINE void
106 new_Freer(void *ap, ink_hrtime t)
107 {
108   eventProcessor.schedule_in(new FreerContinuation(ap), t, ET_TASK);
109 }
110 
111 template <class C> struct DereferContinuation : public Continuation {
112   C *p;
113 
114   int
115   dieEvent(int, Event *)
116   {
117     p->refcount_dec();
118     if (p->refcount_dec() == 0) {
119       delete p;
120     }
121 
122     delete this;
123     return EVENT_DONE;
124   }
125 
126   DereferContinuation(C *ap) : Continuation(nullptr), p(ap) { SET_HANDLER(&DereferContinuation::dieEvent); }
127 };
128 
129 template <class C>
130 TS_INLINE void
131 new_Derefer(C *ap, ink_hrtime t)
132 {
133   eventProcessor.schedule_in(new DereferContinuation<C>(ap), t, ET_TASK);
134 }
135