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 /****************************************************************************
25 
26   Basic Threads
27 
28 
29 
30 **************************************************************************/
31 #include "P_EventSystem.h"
32 #include "tscore/ink_string.h"
33 
34 ///////////////////////////////////////////////
35 // Common Interface impl                     //
36 ///////////////////////////////////////////////
37 
38 ink_hrtime Thread::cur_time = ink_get_hrtime_internal();
39 inkcoreapi ink_thread_key Thread::thread_data_key;
40 
41 namespace
42 {
__anon96c9690e0202() 43 static bool initialized ATS_UNUSED = ([]() -> bool {
44   // File scope initialization goes here.
45   ink_thread_key_create(&Thread::thread_data_key, nullptr);
46   return true;
47 })();
48 }
49 
Thread()50 Thread::Thread()
51 {
52   mutex = new_ProxyMutex();
53   MUTEX_TAKE_LOCK(mutex, static_cast<EThread *>(this));
54   mutex->nthread_holding += THREAD_MUTEX_THREAD_HOLDING;
55 }
56 
~Thread()57 Thread::~Thread()
58 {
59   ink_release_assert(mutex->thread_holding == static_cast<EThread *>(this));
60 
61   if (ink_thread_getspecific(Thread::thread_data_key) == this) {
62     // Clear pointer to this object stored in thread-specific data by set_specific.
63     //
64     ink_thread_setspecific(Thread::thread_data_key, nullptr);
65   }
66 
67   mutex->nthread_holding -= THREAD_MUTEX_THREAD_HOLDING;
68   MUTEX_UNTAKE_LOCK(mutex, static_cast<EThread *>(this));
69 }
70 
71 ///////////////////////////////////////////////
72 // Unix & non-NT Interface impl              //
73 ///////////////////////////////////////////////
74 
75 struct thread_data_internal {
76   ThreadFunction f;                  ///< Function to execute in the thread.
77   Thread *me;                        ///< The class instance.
78   char name[MAX_THREAD_NAME_LENGTH]; ///< Name for the thread.
79 };
80 
81 static void *
spawn_thread_internal(void * a)82 spawn_thread_internal(void *a)
83 {
84   auto *p = static_cast<thread_data_internal *>(a);
85 
86   p->me->set_specific();
87   ink_set_thread_name(p->name);
88 
89   if (p->f) {
90     p->f();
91   } else {
92     p->me->execute();
93   }
94 
95   delete p;
96   return nullptr;
97 }
98 
99 void
start(const char * name,void * stack,size_t stacksize,ThreadFunction const & f)100 Thread::start(const char *name, void *stack, size_t stacksize, ThreadFunction const &f)
101 {
102   auto *p = new thread_data_internal{f, this, ""};
103 
104   ink_zero(p->name);
105   ink_strlcpy(p->name, name, MAX_THREAD_NAME_LENGTH);
106   if (stacksize == 0) {
107     stacksize = DEFAULT_STACKSIZE;
108   }
109   ink_thread_create(&tid, spawn_thread_internal, p, 0, stacksize, stack);
110 }
111