Added hash table tests.
1 // vim: set sts=8 ts=2 sw=2 tw=99 et:
3 // Copyright (C) 2013, David Anderson and AlliedModders LLC
4 // All rights reserved.
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
9 // * Redistributions of source code must retain the above copyright notice, this
10 // list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution.
14 // * Neither the name of AlliedModders LLC nor the names of its contributors
15 // may be used to endorse or promote products derived from this software
16 // without specific prior written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 // POSSIBILITY OF SUCH DAMAGE.
30 #ifndef _include_amtl_thread_posix_h_
31 #define _include_amtl_thread_posix_h_
37 #if defined(__linux__)
38 # include <sys/prctl.h>
40 #if defined(__APPLE__)
46 class Mutex : public Lockable
53 pthread_mutex_init(&mutex_, NULL);
57 pthread_mutex_destroy(&mutex_);
60 bool DoTryLock() KE_OVERRIDE {
61 return pthread_mutex_trylock(&mutex_) == 0;
64 void DoLock() KE_OVERRIDE {
65 pthread_mutex_lock(&mutex_);
68 void DoUnlock() KE_OVERRIDE {
69 pthread_mutex_unlock(&mutex_);
72 pthread_mutex_t *raw() {
77 pthread_mutex_t mutex_;
80 // Currently, this class only supports single-listener CVs.
81 class ConditionVariable : public Lockable
88 pthread_cond_init(&cv_, NULL);
91 ~ConditionVariable() {
92 pthread_cond_destroy(&cv_);
95 bool DoTryLock() KE_OVERRIDE {
96 return mutex_.DoTryLock();
98 void DoLock() KE_OVERRIDE {
101 void DoUnlock() KE_OVERRIDE {
106 AssertCurrentThreadOwns();
107 pthread_cond_signal(&cv_);
110 WaitResult Wait(size_t timeout_ms) {
111 AssertCurrentThreadOwns();
113 #if defined(__linux__)
115 if (clock_gettime(CLOCK_REALTIME, &ts) == -1)
119 gettimeofday(&tv, NULL);
122 ts.tv_sec = tv.tv_sec;
123 ts.tv_nsec = tv.tv_usec * 1000;
126 ts.tv_sec += timeout_ms / 1000;
127 ts.tv_nsec += (timeout_ms % 1000) * 1000000;
128 if (ts.tv_nsec >= 1000000000) {
130 ts.tv_nsec -= 1000000000;
134 int rv = pthread_cond_timedwait(&cv_, mutex_.raw(), &ts);
140 return Wait_Signaled;
145 AssertCurrentThreadOwns();
148 int rv = pthread_cond_wait(&cv_, mutex_.raw());
152 return Wait_Signaled;
168 Thread(IRunnable *run, const char *name = NULL) {
169 ThreadData *data = new ThreadData;
171 snprintf(data->name, sizeof(data->name), "%s", name ? name : "");
173 initialized_ = (pthread_create(&thread_, NULL, Main, data) == 0);
178 bool Succeeded() const {
185 pthread_join(thread_, NULL);
189 static void *Main(void *arg) {
190 AutoPtr<ThreadData> data((ThreadData *)arg);
193 #if defined(__linux__)
194 prctl(PR_SET_NAME, (unsigned long)data->name);
195 #elif defined(__APPLE__)
196 int (*fn)(const char *) = (int (*)(const char *))dlsym(RTLD_DEFAULT, "pthread_setname_np");
212 #endif // _include_amtl_thread_posix_h_