1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
|
/** @file
A brief file description
@section license License
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/**************************************************************************
Generic threads interface
**************************************************************************/
#ifndef _INK_THREAD_H
#define _INK_THREAD_H
#include "ts/ink_hrtime.h"
#include "ts/ink_defs.h"
#include <sched.h>
//////////////////////////////////////////////////////////////////////////////
//
// The POSIX threads interface
//
//////////////////////////////////////////////////////////////////////////////
#if defined(POSIX_THREAD)
#include <pthread.h>
#include <signal.h>
#include <semaphore.h>
#if HAVE_PTHREAD_NP_H
#include <pthread_np.h>
#endif
#define INK_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
typedef pthread_t ink_thread;
typedef pthread_cond_t ink_cond;
typedef pthread_key_t ink_thread_key;
// Darwin has a sem_init stub that returns ENOSYS. Rather than bodge around doing runtime
// detection, just emulate on Darwin.
#if defined(darwin)
#define TS_EMULATE_ANON_SEMAPHORES 1
#endif
struct ink_semaphore {
#if TS_EMULATE_ANON_SEMAPHORES
sem_t *sema;
int64_t semid;
#else
sem_t sema;
#endif
sem_t *
get()
{
#if TS_EMULATE_ANON_SEMAPHORES
return sema;
#else
return &sema;
#endif
}
};
#endif /* #if defined(POSIX_THREAD) */
/*******************************************************************
*** Condition variables
******************************************************************/
#ifdef POSIX_THREAD_10031c
typedef struct timespec ink_timestruc;
#else
typedef timestruc_t ink_timestruc;
#endif
#include <errno.h>
#include "ts/ink_mutex.h"
#include "ts/ink_assert.h"
//////////////////////////////////////////////////////////////////////////////
//
// The POSIX threads interface
//
//////////////////////////////////////////////////////////////////////////////
#if defined(POSIX_THREAD)
static inline void
ink_thread_key_create(ink_thread_key *key, void (*destructor)(void *value))
{
ink_assert(!pthread_key_create(key, destructor));
}
static inline void
ink_thread_setspecific(ink_thread_key key, void *value)
{
ink_assert(!pthread_setspecific(key, value));
}
static inline void *
ink_thread_getspecific(ink_thread_key key)
{
return pthread_getspecific(key);
}
static inline void
ink_thread_key_delete(ink_thread_key key)
{
ink_assert(!pthread_key_delete(key));
}
static inline ink_thread
ink_thread_create(void *(*f)(void *), void *a, int detached = 0, size_t stacksize = 0)
{
ink_thread t;
int ret;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
if (stacksize) {
pthread_attr_setstacksize(&attr, stacksize);
}
if (detached) {
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
}
ret = pthread_create(&t, &attr, f, a);
ink_assert(ret == 0);
pthread_attr_destroy(&attr);
/**
* Fix for INKqa10118.
* If the thread has not been created successfully return 0.
*/
return ret ? (ink_thread)0 : t;
}
static inline void
ink_thread_cancel(ink_thread who)
{
#if defined(freebsd)
(void)who;
ink_assert(!"not supported");
#else
int ret = pthread_cancel(who);
ink_assert(ret == 0);
#endif
}
static inline void *
ink_thread_join(ink_thread t)
{
void *r;
ink_assert(!pthread_join(t, &r));
return r;
}
static inline ink_thread
ink_thread_self()
{
return (pthread_self());
}
static inline int
ink_thread_get_priority(ink_thread t, int *priority)
{
#if defined(freebsd)
(void)t;
(void)priority;
ink_assert(!"not supported");
return -1;
#else
int policy;
struct sched_param param;
int res = pthread_getschedparam(t, &policy, ¶m);
*priority = param.sched_priority;
return res;
#endif
}
static inline int
ink_thread_sigsetmask(int how, const sigset_t *set, sigset_t *oset)
{
return (pthread_sigmask(how, set, oset));
}
/*******************************************************************
* Posix Semaphores
******************************************************************/
void ink_sem_init(ink_semaphore *sp, unsigned int count);
void ink_sem_destroy(ink_semaphore *sp);
void ink_sem_wait(ink_semaphore *sp);
bool ink_sem_trywait(ink_semaphore *sp);
void ink_sem_post(ink_semaphore *sp);
/*******************************************************************
* Posix Condition Variables
******************************************************************/
static inline void
ink_cond_init(ink_cond *cp)
{
ink_assert(pthread_cond_init(cp, NULL) == 0);
}
static inline void
ink_cond_destroy(ink_cond *cp)
{
ink_assert(pthread_cond_destroy(cp) == 0);
}
static inline void
ink_cond_wait(ink_cond *cp, ink_mutex *mp)
{
ink_assert(pthread_cond_wait(cp, mp) == 0);
}
static inline int
ink_cond_timedwait(ink_cond *cp, ink_mutex *mp, ink_timestruc *t)
{
int err;
while (EINTR == (err = pthread_cond_timedwait(cp, mp, t)))
;
#if defined(freebsd) || defined(openbsd)
ink_assert((err == 0) || (err == ETIMEDOUT));
#else
ink_assert((err == 0) || (err == ETIME) || (err == ETIMEDOUT));
#endif
return err;
}
static inline void
ink_cond_signal(ink_cond *cp)
{
ink_assert(pthread_cond_signal(cp) == 0);
}
static inline void
ink_cond_broadcast(ink_cond *cp)
{
ink_assert(pthread_cond_broadcast(cp) == 0);
}
static inline void
ink_thr_yield()
{
ink_assert(!sched_yield());
}
static inline void
ink_thread_exit(void *status)
{
pthread_exit(status);
}
// This define is from Linux's <sys/prctl.h> and is most likely very
// Linux specific... Feel free to add support for other platforms
// that has a feature to give a thread specific name / tag.
static inline void
ink_set_thread_name(const char *name ATS_UNUSED)
{
#if defined(HAVE_PTHREAD_SETNAME_NP_1)
pthread_setname_np(name);
#elif defined(HAVE_PTHREAD_SETNAME_NP_2)
pthread_setname_np(pthread_self(), name);
#elif defined(HAVE_PTHREAD_SET_NAME_NP_2)
pthread_set_name_np(pthread_self(), name);
#elif defined(HAVE_SYS_PRCTL_H) && defined(PR_SET_NAME)
prctl(PR_SET_NAME, name, 0, 0, 0);
#endif
}
#endif /* #if defined(POSIX_THREAD) */
#endif /*_INK_THREAD_H*/
|