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 TS_INLINE Event *
65 EventProcessor::schedule(Event *e, EventType etype, bool fast_signal)
66 {
67   ink_assert(etype < MAX_EVENT_TYPES);
68 
69   EThread *ethread = e->continuation->getThreadAffinity();
70   if (ethread != nullptr && ethread->is_event_type(etype)) {
71     e->ethread = ethread;
72   } else {
73     ethread = this_ethread();
74     // Is the current thread eligible?
75     if (ethread != nullptr && ethread->is_event_type(etype)) {
76       e->ethread = ethread;
77     } else {
78       e->ethread = assign_thread(etype);
79     }
80     if (e->continuation->getThreadAffinity() == nullptr) {
81       e->continuation->setThreadAffinity(e->ethread);
82     }
83   }
84 
85   if (e->continuation->mutex) {
86     e->mutex = e->continuation->mutex;
87   } else {
88     e->mutex = e->continuation->mutex = e->ethread->mutex;
89   }
90   e->ethread->EventQueueExternal.enqueue(e, fast_signal);
91   return e;
92 }
93 
94 TS_INLINE Event *
95 EventProcessor::schedule_imm_signal(Continuation *cont, EventType et, int callback_event, void *cookie)
96 {
97   Event *e = eventAllocator.alloc();
98 
99   ink_assert(et < MAX_EVENT_TYPES);
100 #ifdef ENABLE_TIME_TRACE
101   e->start_time = Thread::get_hrtime();
102 #endif
103   e->callback_event = callback_event;
104   e->cookie         = cookie;
105   return schedule(e->init(cont, 0, 0), et, true);
106 }
107 
108 TS_INLINE Event *
109 EventProcessor::schedule_imm(Continuation *cont, EventType et, int callback_event, void *cookie)
110 {
111   Event *e = eventAllocator.alloc();
112 
113   ink_assert(et < MAX_EVENT_TYPES);
114 #ifdef ENABLE_TIME_TRACE
115   e->start_time = Thread::get_hrtime();
116 #endif
117   e->callback_event = callback_event;
118   e->cookie         = cookie;
119   return schedule(e->init(cont, 0, 0), et);
120 }
121 
122 TS_INLINE Event *
123 EventProcessor::schedule_at(Continuation *cont, ink_hrtime t, EventType et, int callback_event, void *cookie)
124 {
125   Event *e = eventAllocator.alloc();
126 
127   ink_assert(t > 0);
128   ink_assert(et < MAX_EVENT_TYPES);
129   e->callback_event = callback_event;
130   e->cookie         = cookie;
131   return schedule(e->init(cont, t, 0), et);
132 }
133 
134 TS_INLINE Event *
135 EventProcessor::schedule_in(Continuation *cont, ink_hrtime t, EventType et, int callback_event, void *cookie)
136 {
137   Event *e = eventAllocator.alloc();
138 
139   ink_assert(et < MAX_EVENT_TYPES);
140   e->callback_event = callback_event;
141   e->cookie         = cookie;
142   return schedule(e->init(cont, Thread::get_hrtime() + t, 0), et);
143 }
144 
145 TS_INLINE Event *
146 EventProcessor::schedule_every(Continuation *cont, ink_hrtime t, EventType et, int callback_event, void *cookie)
147 {
148   Event *e = eventAllocator.alloc();
149 
150   ink_assert(t != 0);
151   ink_assert(et < MAX_EVENT_TYPES);
152   e->callback_event = callback_event;
153   e->cookie         = cookie;
154   if (t < 0) {
155     return schedule(e->init(cont, t, t), et);
156   } else {
157     return schedule(e->init(cont, Thread::get_hrtime() + t, t), et);
158   }
159 }
160