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_align.h"
27 #include "I_EventProcessor.h"
28 
29 const int LOAD_BALANCE_INTERVAL = 1;
30 
31 TS_INLINE off_t
32 EventProcessor::allocate(int size)
33 {
34   static off_t start = INK_ALIGN(offsetof(EThread, thread_private), 16);
35   static off_t loss  = start - offsetof(EThread, thread_private);
36   size               = INK_ALIGN(size, 16); // 16 byte alignment
37 
38   int old;
39   do {
40     old = thread_data_used;
41     if (old + loss + size > PER_THREAD_DATA) {
42       return -1;
43     }
44   } while (!ink_atomic_cas(&thread_data_used, old, old + size));
45 
46   return (off_t)(old + start);
47 }
48 
49 TS_INLINE EThread *
50 EventProcessor::assign_thread(EventType etype)
51 {
52   int next;
53   ThreadGroupDescriptor *tg = &thread_group[etype];
54 
55   ink_assert(etype < MAX_EVENT_TYPES);
56   if (tg->_count > 1) {
57     next = ++tg->_next_round_robin % tg->_count;
58   } else {
59     next = 0;
60   }
61   return tg->_thread[next];
62 }
63 
64 // If thread_holding is the correct type, return it.
65 //
66 // Otherwise check if there is already an affinity associated with the continuation,
67 // return it if the type is the same, return the next available thread of "etype" if
68 // the type is different.
69 //
70 // Only assign new affinity when there is currently none.
71 TS_INLINE EThread *
72 EventProcessor::assign_affinity_by_type(Continuation *cont, EventType etype)
73 {
74   EThread *ethread = cont->mutex->thread_holding;
75   if (!ethread->is_event_type(etype)) {
76     ethread = cont->getThreadAffinity();
77     if (ethread == nullptr || !ethread->is_event_type(etype)) {
78       ethread = assign_thread(etype);
79     }
80   }
81 
82   if (cont->getThreadAffinity() == nullptr) {
83     cont->setThreadAffinity(ethread);
84   }
85 
86   return ethread;
87 }
88 
89 TS_INLINE Event *
90 EventProcessor::schedule(Event *e, EventType etype, bool fast_signal)
91 {
92   ink_assert(etype < MAX_EVENT_TYPES);
93 
94   EThread *ethread = e->continuation->getThreadAffinity();
95   if (ethread != nullptr && ethread->is_event_type(etype)) {
96     e->ethread = ethread;
97   } else {
98     ethread = this_ethread();
99     // Is the current thread eligible?
100     if (ethread != nullptr && ethread->is_event_type(etype)) {
101       e->ethread = ethread;
102     } else {
103       e->ethread = assign_thread(etype);
104     }
105     if (e->continuation->getThreadAffinity() == nullptr) {
106       e->continuation->setThreadAffinity(e->ethread);
107     }
108   }
109 
110   if (e->continuation->mutex) {
111     e->mutex = e->continuation->mutex;
112   } else {
113     e->mutex = e->continuation->mutex = e->ethread->mutex;
114   }
115   e->ethread->EventQueueExternal.enqueue(e, fast_signal);
116   return e;
117 }
118 
119 TS_INLINE Event *
120 EventProcessor::schedule_imm_signal(Continuation *cont, EventType et, int callback_event, void *cookie)
121 {
122   Event *e = eventAllocator.alloc();
123 
124   ink_assert(et < MAX_EVENT_TYPES);
125 #ifdef ENABLE_TIME_TRACE
126   e->start_time = Thread::get_hrtime();
127 #endif
128   e->callback_event = callback_event;
129   e->cookie         = cookie;
130   return schedule(e->init(cont, 0, 0), et, true);
131 }
132 
133 TS_INLINE Event *
134 EventProcessor::schedule_imm(Continuation *cont, EventType et, int callback_event, void *cookie)
135 {
136   Event *e = eventAllocator.alloc();
137 
138   ink_assert(et < MAX_EVENT_TYPES);
139 #ifdef ENABLE_TIME_TRACE
140   e->start_time = Thread::get_hrtime();
141 #endif
142   e->callback_event = callback_event;
143   e->cookie         = cookie;
144   return schedule(e->init(cont, 0, 0), et);
145 }
146 
147 TS_INLINE Event *
148 EventProcessor::schedule_at(Continuation *cont, ink_hrtime t, EventType et, int callback_event, void *cookie)
149 {
150   Event *e = eventAllocator.alloc();
151 
152   ink_assert(t > 0);
153   ink_assert(et < MAX_EVENT_TYPES);
154   e->callback_event = callback_event;
155   e->cookie         = cookie;
156   return schedule(e->init(cont, t, 0), et);
157 }
158 
159 TS_INLINE Event *
160 EventProcessor::schedule_in(Continuation *cont, ink_hrtime t, EventType et, int callback_event, void *cookie)
161 {
162   Event *e = eventAllocator.alloc();
163 
164   ink_assert(et < MAX_EVENT_TYPES);
165   e->callback_event = callback_event;
166   e->cookie         = cookie;
167   return schedule(e->init(cont, Thread::get_hrtime() + t, 0), et);
168 }
169 
170 TS_INLINE Event *
171 EventProcessor::schedule_every(Continuation *cont, ink_hrtime t, EventType et, int callback_event, void *cookie)
172 {
173   Event *e = eventAllocator.alloc();
174 
175   ink_assert(t != 0);
176   ink_assert(et < MAX_EVENT_TYPES);
177   e->callback_event = callback_event;
178   e->cookie         = cookie;
179   if (t < 0) {
180     return schedule(e->init(cont, t, t), et);
181   } else {
182     return schedule(e->init(cont, Thread::get_hrtime() + t, t), et);
183   }
184 }
185