1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/include/am-thread-posix.h Fri Aug 23 20:34:17 2013 -0700
1.3 @@ -0,0 +1,213 @@
1.4 +// vim: set sts=8 ts=2 sw=2 tw=99 et:
1.5 +//
1.6 +// Copyright (C) 2013, David Anderson and AlliedModders LLC
1.7 +// All rights reserved.
1.8 +//
1.9 +// Redistribution and use in source and binary forms, with or without
1.10 +// modification, are permitted provided that the following conditions are met:
1.11 +//
1.12 +// * Redistributions of source code must retain the above copyright notice, this
1.13 +// list of conditions and the following disclaimer.
1.14 +// * Redistributions in binary form must reproduce the above copyright notice,
1.15 +// this list of conditions and the following disclaimer in the documentation
1.16 +// and/or other materials provided with the distribution.
1.17 +// * Neither the name of AlliedModders LLC nor the names of its contributors
1.18 +// may be used to endorse or promote products derived from this software
1.19 +// without specific prior written permission.
1.20 +//
1.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1.22 +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1.23 +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1.24 +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
1.25 +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
1.26 +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
1.27 +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
1.28 +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
1.29 +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1.30 +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
1.31 +// POSSIBILITY OF SUCH DAMAGE.
1.32 +
1.33 +#ifndef _include_amtl_thread_posix_h_
1.34 +#define _include_amtl_thread_posix_h_
1.35 +
1.36 +#include <pthread.h>
1.37 +#include <sys/time.h>
1.38 +#include <errno.h>
1.39 +#include <stdio.h>
1.40 +#if defined(__linux__)
1.41 +# include <sys/prctl.h>
1.42 +#endif
1.43 +#if defined(__APPLE__)
1.44 +# include <dlfcn.h>
1.45 +#endif
1.46 +
1.47 +namespace ke {
1.48 +
1.49 +class Mutex : public Lockable
1.50 +{
1.51 + public:
1.52 + Mutex() {
1.53 +#if !defined(NDEBUG)
1.54 + int rv =
1.55 +#endif
1.56 + pthread_mutex_init(&mutex_, NULL);
1.57 + assert(rv == 0);
1.58 + }
1.59 + ~Mutex() {
1.60 + pthread_mutex_destroy(&mutex_);
1.61 + }
1.62 +
1.63 + bool DoTryLock() KE_OVERRIDE {
1.64 + return pthread_mutex_trylock(&mutex_) == 0;
1.65 + }
1.66 +
1.67 + void DoLock() KE_OVERRIDE {
1.68 + pthread_mutex_lock(&mutex_);
1.69 + }
1.70 +
1.71 + void DoUnlock() KE_OVERRIDE {
1.72 + pthread_mutex_unlock(&mutex_);
1.73 + }
1.74 +
1.75 + pthread_mutex_t *raw() {
1.76 + return &mutex_;
1.77 + }
1.78 +
1.79 + private:
1.80 + pthread_mutex_t mutex_;
1.81 +};
1.82 +
1.83 +// Currently, this class only supports single-listener CVs.
1.84 +class ConditionVariable : public Lockable
1.85 +{
1.86 + public:
1.87 + ConditionVariable() {
1.88 +#if !defined(NDEBUG)
1.89 + int rv =
1.90 +#endif
1.91 + pthread_cond_init(&cv_, NULL);
1.92 + assert(rv == 0);
1.93 + }
1.94 + ~ConditionVariable() {
1.95 + pthread_cond_destroy(&cv_);
1.96 + }
1.97 +
1.98 + bool DoTryLock() KE_OVERRIDE {
1.99 + return mutex_.DoTryLock();
1.100 + }
1.101 + void DoLock() KE_OVERRIDE {
1.102 + mutex_.DoLock();
1.103 + }
1.104 + void DoUnlock() KE_OVERRIDE {
1.105 + mutex_.DoUnlock();
1.106 + }
1.107 +
1.108 + void Notify() {
1.109 + AssertCurrentThreadOwns();
1.110 + pthread_cond_signal(&cv_);
1.111 + }
1.112 +
1.113 + WaitResult Wait(size_t timeout_ms) {
1.114 + AssertCurrentThreadOwns();
1.115 +
1.116 +#if defined(__linux__)
1.117 + struct timespec ts;
1.118 + if (clock_gettime(CLOCK_REALTIME, &ts) == -1)
1.119 + return Wait_Error;
1.120 +#else
1.121 + struct timeval tv;
1.122 + gettimeofday(&tv, NULL);
1.123 +
1.124 + struct timespec ts;
1.125 + ts.tv_sec = tv.tv_sec;
1.126 + ts.tv_nsec = tv.tv_usec * 1000;
1.127 +#endif
1.128 +
1.129 + ts.tv_sec += timeout_ms / 1000;
1.130 + ts.tv_nsec += (timeout_ms % 1000) * 1000000;
1.131 + if (ts.tv_nsec >= 1000000000) {
1.132 + ts.tv_sec++;
1.133 + ts.tv_nsec -= 1000000000;
1.134 + }
1.135 +
1.136 + DebugSetUnlocked();
1.137 + int rv = pthread_cond_timedwait(&cv_, mutex_.raw(), &ts);
1.138 + DebugSetLocked();
1.139 +
1.140 + if (rv == ETIMEDOUT)
1.141 + return Wait_Timeout;
1.142 + if (rv == 0)
1.143 + return Wait_Signaled;
1.144 + return Wait_Error;
1.145 + }
1.146 +
1.147 + WaitResult Wait() {
1.148 + AssertCurrentThreadOwns();
1.149 +
1.150 + DebugSetUnlocked();
1.151 + int rv = pthread_cond_wait(&cv_, mutex_.raw());
1.152 + DebugSetLocked();
1.153 +
1.154 + if (rv == 0)
1.155 + return Wait_Signaled;
1.156 + return Wait_Error;
1.157 + }
1.158 +
1.159 + private:
1.160 + Mutex mutex_;
1.161 + pthread_cond_t cv_;
1.162 +};
1.163 +
1.164 +class Thread
1.165 +{
1.166 + struct ThreadData {
1.167 + IRunnable *run;
1.168 + char name[17];
1.169 + };
1.170 + public:
1.171 + Thread(IRunnable *run, const char *name = NULL) {
1.172 + ThreadData *data = new ThreadData;
1.173 + data->run = run;
1.174 + snprintf(data->name, sizeof(data->name), "%s", name ? name : "");
1.175 +
1.176 + initialized_ = (pthread_create(&thread_, NULL, Main, data) == 0);
1.177 + if (!initialized_)
1.178 + delete data;
1.179 + }
1.180 +
1.181 + bool Succeeded() const {
1.182 + return initialized_;
1.183 + }
1.184 +
1.185 + void Join() {
1.186 + if (!Succeeded())
1.187 + return;
1.188 + pthread_join(thread_, NULL);
1.189 + }
1.190 +
1.191 + private:
1.192 + static void *Main(void *arg) {
1.193 + AutoPtr<ThreadData> data((ThreadData *)arg);
1.194 +
1.195 + if (data->name[0]) {
1.196 +#if defined(__linux__)
1.197 + prctl(PR_SET_NAME, (unsigned long)data->name);
1.198 +#elif defined(__APPLE__)
1.199 + int (*fn)(const char *) = (int (*)(const char *))dlsym(RTLD_DEFAULT, "pthread_setname_np");
1.200 + if (fn)
1.201 + fn(data->name);
1.202 +#endif
1.203 + }
1.204 + data->run->Run();
1.205 + return NULL;
1.206 + }
1.207 +
1.208 + private:
1.209 + bool initialized_;
1.210 + pthread_t thread_;
1.211 +};
1.212 +
1.213 +} // namespace ke
1.214 +
1.215 +#endif // _include_amtl_thread_posix_h_
1.216 +