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
dieEventDeleterContinuation37   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   }
DeleterContinuationDeleterContinuation47   explicit 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
new_Deleter(C * ap,ink_hrtime t)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
dieEventFreeCallContinuation68   dieEvent(int event, void *e)
69   {
70     (void)event;
71     (void)e;
72     p->free();
73     delete this;
74     return EVENT_DONE;
75   }
FreeCallContinuationFreeCallContinuation76   explicit FreeCallContinuation(C *ap) : Continuation(nullptr), p(ap) { SET_HANDLER(&FreeCallContinuation::dieEvent); }
77 };
78 
79 template <class C>
80 TS_INLINE void
new_FreeCaller(C * ap,ink_hrtime t)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
dieEventFreerContinuation93   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 
FreerContinuationFreerContinuation102   explicit FreerContinuation(void *ap) : Continuation(nullptr), p(ap)
103   {
104     SET_HANDLER((FreerContHandler)&FreerContinuation::dieEvent);
105   }
106 };
107 
108 TS_INLINE void
new_Freer(void * ap,ink_hrtime t)109 new_Freer(void *ap, ink_hrtime t)
110 {
111   eventProcessor.schedule_in(new FreerContinuation(ap), t, ET_TASK);
112 }
113 
114 template <class C> struct DereferContinuation : public Continuation {
115   C *p;
116 
117   int
dieEventDereferContinuation118   dieEvent(int, Event *)
119   {
120     p->refcount_dec();
121     if (p->refcount_dec() == 0) {
122       delete p;
123     }
124 
125     delete this;
126     return EVENT_DONE;
127   }
128 
DereferContinuationDereferContinuation129   explicit DereferContinuation(C *ap) : Continuation(nullptr), p(ap) { SET_HANDLER(&DereferContinuation::dieEvent); }
130 };
131 
132 template <class C>
133 TS_INLINE void
new_Derefer(C * ap,ink_hrtime t)134 new_Derefer(C *ap, ink_hrtime t)
135 {
136   eventProcessor.schedule_in(new DereferContinuation<C>(ap), t, ET_TASK);
137 }
138