Threads.cpp revision edbf3b6af777b721cd2a1ef461947e51e88241e1
1edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project/*
2edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * Copyright (C) 2007 The Android Open Source Project
3edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project *
4edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
5edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * you may not use this file except in compliance with the License.
6edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * You may obtain a copy of the License at
7edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project *
8edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
9edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project *
10edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
11edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
12edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * See the License for the specific language governing permissions and
14edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * limitations under the License.
15edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project */
16edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
17edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#define LOG_TAG "libutils.threads"
18edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
19edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <utils/threads.h>
20edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <utils/Log.h>
21edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
22edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <stdio.h>
23edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <stdlib.h>
24edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <memory.h>
25edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <errno.h>
26edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <assert.h>
27edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <unistd.h>
28edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
29edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#if defined(HAVE_PTHREADS)
30edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project# include <pthread.h>
31edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project# include <sched.h>
32edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project# include <sys/resource.h>
33edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#elif defined(HAVE_WIN32_THREADS)
34edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project# include <windows.h>
35edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project# include <stdint.h>
36edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project# include <process.h>
37edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project# define HAVE_CREATETHREAD  // Cygwin, vs. HAVE__BEGINTHREADEX for MinGW
38edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#endif
39edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
40edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#if defined(HAVE_FUTEX)
41edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <private/utils/futex_synchro.h>
42edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#endif
43edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
44edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#if defined(HAVE_PRCTL)
45edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#include <sys/prctl.h>
46edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#endif
47edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
48edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project/*
49edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * ===========================================================================
50edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project *      Thread wrappers
51edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * ===========================================================================
52edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project */
53edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
54edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectusing namespace android;
55edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
56edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project// ----------------------------------------------------------------------------
57edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#if defined(HAVE_PTHREADS)
58edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#if 0
59edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#pragma mark -
60edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#pragma mark PTHREAD
61edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#endif
62edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project// ----------------------------------------------------------------------------
63edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
64edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project/*
65edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * Create and run a new thead.
66edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project *
67edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * We create it "detached", so it cleans up after itself.
68edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project */
69edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
70edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projecttypedef void* (*android_pthread_entry)(void*);
71edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
72edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectstruct thread_data_t {
73edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    thread_func_t   entryFunction;
74edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    void*           userData;
75edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    int             priority;
76edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    char *          threadName;
77edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
78edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    // we use this trampoline when we need to set the priority with
79edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    // nice/setpriority.
80edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    static int trampoline(const thread_data_t* t) {
81edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        thread_func_t f = t->entryFunction;
82edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        void* u = t->userData;
83edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        int prio = t->priority;
84edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        char * name = t->threadName;
85edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        delete t;
86edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        setpriority(PRIO_PROCESS, 0, prio);
87edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        if (name) {
88edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#if defined(HAVE_PRCTL)
89edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            // Mac OS doesn't have this, and we build libutil for the host too
90edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            int hasAt = 0;
91edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            int hasDot = 0;
92edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            char *s = name;
93edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            while (*s) {
94edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                if (*s == '.') hasDot = 1;
95edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                else if (*s == '@') hasAt = 1;
96edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                s++;
97edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            }
98edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            int len = s - name;
99edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            if (len < 15 || hasAt || !hasDot) {
100edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                s = name;
101edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            } else {
102edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                s = name + len - 15;
103edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            }
104edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            prctl(PR_SET_NAME, (unsigned long) s, 0, 0, 0);
105edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#endif
106edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            free(name);
107edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        }
108edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        return f(u);
109edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
110edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project};
111edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
112edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectint androidCreateRawThreadEtc(android_thread_func_t entryFunction,
113edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                               void *userData,
114edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                               const char* threadName,
115edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                               int32_t threadPriority,
116edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                               size_t threadStackSize,
117edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                               android_thread_id_t *threadId)
118edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
119edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    pthread_attr_t attr;
120edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    pthread_attr_init(&attr);
121edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
122edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
123edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#ifdef HAVE_ANDROID_OS  /* valgrind is rejecting RT-priority create reqs */
124edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    if (threadPriority != PRIORITY_DEFAULT || threadName != NULL) {
125edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        // We could avoid the trampoline if there was a way to get to the
126edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        // android_thread_id_t (pid) from pthread_t
127edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        thread_data_t* t = new thread_data_t;
128edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        t->priority = threadPriority;
129edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        t->threadName = threadName ? strdup(threadName) : NULL;
130edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        t->entryFunction = entryFunction;
131edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        t->userData = userData;
132edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        entryFunction = (android_thread_func_t)&thread_data_t::trampoline;
133edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        userData = t;
134edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
135edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#endif
136edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
137edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    if (threadStackSize) {
138edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        pthread_attr_setstacksize(&attr, threadStackSize);
139edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
140edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
141edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    errno = 0;
142edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    pthread_t thread;
143edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    int result = pthread_create(&thread, &attr,
144edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                    (android_pthread_entry)entryFunction, userData);
145edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    if (result != 0) {
146edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        LOGE("androidCreateRawThreadEtc failed (entry=%p, res=%d, errno=%d)\n"
147edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project             "(android threadPriority=%d)",
148edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            entryFunction, result, errno, threadPriority);
149edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        return 0;
150edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
151edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
152edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    if (threadId != NULL) {
153edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        *threadId = (android_thread_id_t)thread; // XXX: this is not portable
154edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
155edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    return 1;
156edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
157edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
158edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectandroid_thread_id_t androidGetThreadId()
159edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
160edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    return (android_thread_id_t)pthread_self();
161edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
162edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
163edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project// ----------------------------------------------------------------------------
164edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#elif defined(HAVE_WIN32_THREADS)
165edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#if 0
166edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#pragma mark -
167edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#pragma mark WIN32_THREADS
168edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#endif
169edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project// ----------------------------------------------------------------------------
170edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
171edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project/*
172edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * Trampoline to make us __stdcall-compliant.
173edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project *
174edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * We're expected to delete "vDetails" when we're done.
175edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project */
176edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectstruct threadDetails {
177edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    int (*func)(void*);
178edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    void* arg;
179edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project};
180edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectstatic __stdcall unsigned int threadIntermediary(void* vDetails)
181edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
182edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    struct threadDetails* pDetails = (struct threadDetails*) vDetails;
183edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    int result;
184edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
185edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    result = (*(pDetails->func))(pDetails->arg);
186edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
187edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    delete pDetails;
188edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
189edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    LOG(LOG_VERBOSE, "thread", "thread exiting\n");
190edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    return (unsigned int) result;
191edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
192edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
193edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project/*
194edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * Create and run a new thread.
195edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project */
196edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectstatic bool doCreateThread(android_thread_func_t fn, void* arg, android_thread_id_t *id)
197edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
198edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    HANDLE hThread;
199edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    struct threadDetails* pDetails = new threadDetails; // must be on heap
200edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    unsigned int thrdaddr;
201edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
202edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    pDetails->func = fn;
203edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    pDetails->arg = arg;
204edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
205edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#if defined(HAVE__BEGINTHREADEX)
206edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    hThread = (HANDLE) _beginthreadex(NULL, 0, threadIntermediary, pDetails, 0,
207edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                    &thrdaddr);
208edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    if (hThread == 0)
209edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#elif defined(HAVE_CREATETHREAD)
210edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    hThread = CreateThread(NULL, 0,
211edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                    (LPTHREAD_START_ROUTINE) threadIntermediary,
212edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                    (void*) pDetails, 0, (DWORD*) &thrdaddr);
213edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    if (hThread == NULL)
214edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#endif
215edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    {
216edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        LOG(LOG_WARN, "thread", "WARNING: thread create failed\n");
217edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        return false;
218edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
219edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
220edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#if defined(HAVE_CREATETHREAD)
221edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    /* close the management handle */
222edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    CloseHandle(hThread);
223edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#endif
224edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
225edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    if (id != NULL) {
226edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project      	*id = (android_thread_id_t)thrdaddr;
227edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
228edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
229edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    return true;
230edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
231edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
232edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectint androidCreateRawThreadEtc(android_thread_func_t fn,
233edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                               void *userData,
234edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                               const char* threadName,
235edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                               int32_t threadPriority,
236edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                               size_t threadStackSize,
237edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                               android_thread_id_t *threadId)
238edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
239edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    return doCreateThread(  fn, userData, threadId);
240edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
241edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
242edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectandroid_thread_id_t androidGetThreadId()
243edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
244edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    return (android_thread_id_t)GetCurrentThreadId();
245edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
246edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
247edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project// ----------------------------------------------------------------------------
248edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#else
249edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#error "Threads not supported"
250edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#endif
251edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
252edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project// ----------------------------------------------------------------------------
253edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
254edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#if 0
255edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#pragma mark -
256edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#pragma mark Common Thread functions
257edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#endif
258edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
259edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectint androidCreateThread(android_thread_func_t fn, void* arg)
260edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
261edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    return createThreadEtc(fn, arg);
262edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
263edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
264edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectint androidCreateThreadGetID(android_thread_func_t fn, void *arg, android_thread_id_t *id)
265edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
266edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    return createThreadEtc(fn, arg, "android:unnamed_thread",
267edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                           PRIORITY_DEFAULT, 0, id);
268edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
269edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
270edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectstatic android_create_thread_fn gCreateThreadFn = androidCreateRawThreadEtc;
271edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
272edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectint androidCreateThreadEtc(android_thread_func_t entryFunction,
273edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                            void *userData,
274edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                            const char* threadName,
275edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                            int32_t threadPriority,
276edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                            size_t threadStackSize,
277edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                            android_thread_id_t *threadId)
278edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
279edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    return gCreateThreadFn(entryFunction, userData, threadName,
280edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        threadPriority, threadStackSize, threadId);
281edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
282edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
283edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectvoid androidSetCreateThreadFunc(android_create_thread_fn func)
284edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
285edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    gCreateThreadFn = func;
286edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
287edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
288edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectnamespace android {
289edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
290edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project/*
291edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * ===========================================================================
292edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project *      Mutex class
293edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * ===========================================================================
294edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project */
295edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
296edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#if 0
297edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#pragma mark -
298edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#pragma mark Mutex
299edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#endif
300edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
301edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#if defined(HAVE_PTHREADS) && !defined(HAVE_FUTEX)
302edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project/*
303edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * Simple pthread wrapper.
304edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project */
305edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
306edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source ProjectMutex::Mutex()
307edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
308edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    _init();
309edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
310edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
311edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source ProjectMutex::Mutex(const char* name)
312edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
313edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    // XXX: name not used for now
314edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    _init();
315edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
316edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
317edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectvoid Mutex::_init()
318edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
319edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    pthread_mutex_t* pMutex = new pthread_mutex_t;
320edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    pthread_mutex_init(pMutex, NULL);
321edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    mState = pMutex;
322edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
323edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
324edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source ProjectMutex::~Mutex()
325edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
326edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    delete (pthread_mutex_t*) mState;
327edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
328edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
329edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectstatus_t Mutex::lock()
330edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
331edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    int res;
332edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    while ((res=pthread_mutex_lock((pthread_mutex_t*) mState)) == EINTR) ;
333edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    return -res;
334edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
335edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
336edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectvoid Mutex::unlock()
337edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
338edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    pthread_mutex_unlock((pthread_mutex_t*) mState);
339edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
340edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
341edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectstatus_t Mutex::tryLock()
342edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
343edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    int res;
344edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    while ((res=pthread_mutex_trylock((pthread_mutex_t*) mState)) == EINTR) ;
345edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    return -res;
346edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
347edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
348edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#elif defined(HAVE_FUTEX)
349edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#if 0
350edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#pragma mark -
351edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#endif
352edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
353edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#define STATE ((futex_mutex_t*) (&mState))
354edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
355edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source ProjectMutex::Mutex()
356edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
357edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    _init();
358edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
359edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
360edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source ProjectMutex::Mutex(const char* name)
361edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
362edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    _init();
363edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
364edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
365edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectvoid
366edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source ProjectMutex::_init()
367edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
368edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    futex_mutex_init(STATE);
369edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
370edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
371edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source ProjectMutex::~Mutex()
372edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
373edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
374edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
375edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectstatus_t Mutex::lock()
376edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
377edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    int res;
378edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    while ((res=futex_mutex_lock(STATE, FUTEX_WAIT_INFINITE)) == EINTR) ;
379edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    return -res;
380edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
381edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
382edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectvoid Mutex::unlock()
383edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
384edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    futex_mutex_unlock(STATE);
385edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
386edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
387edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectstatus_t Mutex::tryLock()
388edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
389edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    int res;
390edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    while ((res=futex_mutex_trylock(STATE)) == EINTR) ;
391edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    return -res;
392edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
393edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#undef STATE
394edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
395edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#elif defined(HAVE_WIN32_THREADS)
396edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#if 0
397edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#pragma mark -
398edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#endif
399edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
400edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source ProjectMutex::Mutex()
401edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
402edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    HANDLE hMutex;
403edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
404edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    assert(sizeof(hMutex) == sizeof(mState));
405edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
406edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    hMutex = CreateMutex(NULL, FALSE, NULL);
407edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    mState = (void*) hMutex;
408edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
409edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
410edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source ProjectMutex::Mutex(const char* name)
411edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
412edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    // XXX: name not used for now
413edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    HANDLE hMutex;
414edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
415edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    hMutex = CreateMutex(NULL, FALSE, NULL);
416edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    mState = (void*) hMutex;
417edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
418edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
419edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source ProjectMutex::~Mutex()
420edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
421edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    CloseHandle((HANDLE) mState);
422edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
423edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
424edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectstatus_t Mutex::lock()
425edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
426edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    DWORD dwWaitResult;
427edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    dwWaitResult = WaitForSingleObject((HANDLE) mState, INFINITE);
428edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    return dwWaitResult != WAIT_OBJECT_0 ? -1 : NO_ERROR;
429edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
430edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
431edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectvoid Mutex::unlock()
432edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
433edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    if (!ReleaseMutex((HANDLE) mState))
434edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        LOG(LOG_WARN, "thread", "WARNING: bad result from unlocking mutex\n");
435edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
436edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
437edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectstatus_t Mutex::tryLock()
438edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
439edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    DWORD dwWaitResult;
440edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
441edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    dwWaitResult = WaitForSingleObject((HANDLE) mState, 0);
442edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    if (dwWaitResult != WAIT_OBJECT_0 && dwWaitResult != WAIT_TIMEOUT)
443edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        LOG(LOG_WARN, "thread", "WARNING: bad result from try-locking mutex\n");
444edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    return (dwWaitResult == WAIT_OBJECT_0) ? 0 : -1;
445edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
446edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
447edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#else
448edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#error "Somebody forgot to implement threads for this platform."
449edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#endif
450edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
451edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
452edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project/*
453edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * ===========================================================================
454edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project *      Condition class
455edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * ===========================================================================
456edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project */
457edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
458edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#if 0
459edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#pragma mark -
460edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#pragma mark Condition
461edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#endif
462edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
463edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#if defined(HAVE_PTHREADS) && !defined(HAVE_FUTEX)
464edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
465edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project/*
466edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * Constructor.  This is a simple pthread wrapper.
467edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project */
468edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source ProjectCondition::Condition()
469edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
470edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    pthread_cond_t* pCond = new pthread_cond_t;
471edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
472edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    pthread_cond_init(pCond, NULL);
473edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    mState = pCond;
474edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
475edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
476edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project/*
477edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * Destructor.
478edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project */
479edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source ProjectCondition::~Condition()
480edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
481edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    pthread_cond_destroy((pthread_cond_t*) mState);
482edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    delete (pthread_cond_t*) mState;
483edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
484edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
485edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project/*
486edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * Wait on a condition variable.  Lock the mutex before calling.
487edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project */
488edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
489edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectstatus_t Condition::wait(Mutex& mutex)
490edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
491edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    assert(mutex.mState != NULL);
492edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
493edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    int cc;
494edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    while ((cc = pthread_cond_wait((pthread_cond_t*)mState,
495edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                (pthread_mutex_t*) mutex.mState)) == EINTR) ;
496edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    return -cc;
497edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
498edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
499edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectstatus_t Condition::wait(Mutex& mutex, nsecs_t abstime)
500edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
501edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    assert(mutex.mState != NULL);
502edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
503edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    struct timespec ts;
504edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    ts.tv_sec = abstime/1000000000;
505edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    ts.tv_nsec = abstime-(ts.tv_sec*1000000000);
506edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
507edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    int cc;
508edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    while ((cc = pthread_cond_timedwait((pthread_cond_t*)mState,
509edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            (pthread_mutex_t*) mutex.mState, &ts)) == EINTR) ;
510edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    return -cc;
511edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
512edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
513edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectstatus_t Condition::waitRelative(Mutex& mutex, nsecs_t reltime)
514edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
515edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    return wait(mutex, systemTime()+reltime);
516edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
517edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
518edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project/*
519edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * Signal the condition variable, allowing one thread to continue.
520edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project */
521edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectvoid Condition::signal()
522edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
523edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    pthread_cond_signal((pthread_cond_t*) mState);
524edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
525edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
526edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project/*
527edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * Signal the condition variable, allowing all threads to continue.
528edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project */
529edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectvoid Condition::broadcast()
530edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
531edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    pthread_cond_broadcast((pthread_cond_t*) mState);
532edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
533edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
534edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#elif defined(HAVE_FUTEX)
535edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#if 0
536edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#pragma mark -
537edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#endif
538edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
539edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#define STATE ((futex_cond_t*) (&mState))
540edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
541edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project/*
542edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * Constructor.  This is a simple pthread wrapper.
543edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project */
544edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source ProjectCondition::Condition()
545edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
546edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    futex_cond_init(STATE);
547edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
548edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
549edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project/*
550edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * Destructor.
551edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project */
552edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source ProjectCondition::~Condition()
553edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
554edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
555edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
556edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project/*
557edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * Wait on a condition variable.  Lock the mutex before calling.
558edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project */
559edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
560edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectstatus_t Condition::wait(Mutex& mutex)
561edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
562edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    assert(mutex.mState != NULL);
563edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
564edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    int res;
565edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    while ((res = futex_cond_wait(STATE,
566edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        (futex_mutex_t*)(&mutex.mState), FUTEX_WAIT_INFINITE)) == -EINTR) ;
567edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
568edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    return -res;
569edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
570edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
571edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectstatus_t Condition::wait(Mutex& mutex, nsecs_t abstime)
572edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
573edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    nsecs_t reltime = abstime - systemTime();
574edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    if (reltime <= 0) return true;
575edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    return waitRelative(mutex, reltime);
576edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
577edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
578edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectstatus_t Condition::waitRelative(Mutex& mutex, nsecs_t reltime)
579edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
580edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    assert(mutex.mState != NULL);
581edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    int res;
582edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    unsigned msec = ns2ms(reltime);
583edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    if(msec == 0)
584edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        return true;
585edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    // This code will not time out at the correct time if interrupted by signals
586edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    while ((res = futex_cond_wait(STATE,
587edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        (futex_mutex_t*)(&mutex.mState), msec)) == -EINTR) ;
588edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    return res;
589edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
590edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
591edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project/*
592edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * Signal the condition variable, allowing one thread to continue.
593edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project */
594edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectvoid Condition::signal()
595edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
596edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    futex_cond_signal(STATE);
597edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
598edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
599edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project/*
600edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * Signal the condition variable, allowing all threads to continue.
601edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project */
602edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectvoid Condition::broadcast()
603edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
604edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    futex_cond_broadcast(STATE);
605edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
606edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
607edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#undef STATE
608edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
609edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#elif defined(HAVE_WIN32_THREADS)
610edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#if 0
611edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#pragma mark -
612edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#endif
613edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
614edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project/*
615edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * Windows doesn't have a condition variable solution.  It's possible
616edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * to create one, but it's easy to get it wrong.  For a discussion, and
617edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * the origin of this implementation, see:
618edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project *
619edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project *  http://www.cs.wustl.edu/~schmidt/win32-cv-1.html
620edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project *
621edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * The implementation shown on the page does NOT follow POSIX semantics.
622edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * As an optimization they require acquiring the external mutex before
623edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * calling signal() and broadcast(), whereas POSIX only requires grabbing
624edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * it before calling wait().  The implementation here has been un-optimized
625edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * to have the correct behavior.
626edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project */
627edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projecttypedef struct WinCondition {
628edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    // Number of waiting threads.
629edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    int                 waitersCount;
630edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
631edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    // Serialize access to waitersCount.
632edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    CRITICAL_SECTION    waitersCountLock;
633edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
634edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    // Semaphore used to queue up threads waiting for the condition to
635edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    // become signaled.
636edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    HANDLE              sema;
637edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
638edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    // An auto-reset event used by the broadcast/signal thread to wait
639edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    // for all the waiting thread(s) to wake up and be released from
640edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    // the semaphore.
641edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    HANDLE              waitersDone;
642edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
643edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    // This mutex wouldn't be necessary if we required that the caller
644edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    // lock the external mutex before calling signal() and broadcast().
645edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    // I'm trying to mimic pthread semantics though.
646edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    HANDLE              internalMutex;
647edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
648edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    // Keeps track of whether we were broadcasting or signaling.  This
649edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    // allows us to optimize the code if we're just signaling.
650edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    bool                wasBroadcast;
651edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
652edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    status_t wait(WinCondition* condState, HANDLE hMutex, nsecs_t* abstime)
653edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    {
654edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        // Increment the wait count, avoiding race conditions.
655edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        EnterCriticalSection(&condState->waitersCountLock);
656edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        condState->waitersCount++;
657edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        //printf("+++ wait: incr waitersCount to %d (tid=%ld)\n",
658edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        //    condState->waitersCount, getThreadId());
659edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        LeaveCriticalSection(&condState->waitersCountLock);
660edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
661edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        DWORD timeout = INFINITE;
662edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        if (abstime) {
663edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            nsecs_t reltime = *abstime - systemTime();
664edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            if (reltime < 0)
665edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                reltime = 0;
666edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            timeout = reltime/1000000;
667edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        }
668edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
669edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        // Atomically release the external mutex and wait on the semaphore.
670edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        DWORD res =
671edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            SignalObjectAndWait(hMutex, condState->sema, timeout, FALSE);
672edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
673edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        //printf("+++ wait: awake (tid=%ld)\n", getThreadId());
674edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
675edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        // Reacquire lock to avoid race conditions.
676edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        EnterCriticalSection(&condState->waitersCountLock);
677edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
678edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        // No longer waiting.
679edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        condState->waitersCount--;
680edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
681edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        // Check to see if we're the last waiter after a broadcast.
682edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        bool lastWaiter = (condState->wasBroadcast && condState->waitersCount == 0);
683edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
684edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        //printf("+++ wait: lastWaiter=%d (wasBc=%d wc=%d)\n",
685edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        //    lastWaiter, condState->wasBroadcast, condState->waitersCount);
686edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
687edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        LeaveCriticalSection(&condState->waitersCountLock);
688edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
689edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        // If we're the last waiter thread during this particular broadcast
690edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        // then signal broadcast() that we're all awake.  It'll drop the
691edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        // internal mutex.
692edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        if (lastWaiter) {
693edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            // Atomically signal the "waitersDone" event and wait until we
694edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            // can acquire the internal mutex.  We want to do this in one step
695edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            // because it ensures that everybody is in the mutex FIFO before
696edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            // any thread has a chance to run.  Without it, another thread
697edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            // could wake up, do work, and hop back in ahead of us.
698edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            SignalObjectAndWait(condState->waitersDone, condState->internalMutex,
699edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                INFINITE, FALSE);
700edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        } else {
701edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            // Grab the internal mutex.
702edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            WaitForSingleObject(condState->internalMutex, INFINITE);
703edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        }
704edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
705edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        // Release the internal and grab the external.
706edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        ReleaseMutex(condState->internalMutex);
707edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        WaitForSingleObject(hMutex, INFINITE);
708edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
709edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        return res == WAIT_OBJECT_0 ? NO_ERROR : -1;
710edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
711edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project} WinCondition;
712edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
713edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project/*
714edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * Constructor.  Set up the WinCondition stuff.
715edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project */
716edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source ProjectCondition::Condition()
717edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
718edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    WinCondition* condState = new WinCondition;
719edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
720edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    condState->waitersCount = 0;
721edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    condState->wasBroadcast = false;
722edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    // semaphore: no security, initial value of 0
723edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    condState->sema = CreateSemaphore(NULL, 0, 0x7fffffff, NULL);
724edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    InitializeCriticalSection(&condState->waitersCountLock);
725edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    // auto-reset event, not signaled initially
726edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    condState->waitersDone = CreateEvent(NULL, FALSE, FALSE, NULL);
727edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    // used so we don't have to lock external mutex on signal/broadcast
728edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    condState->internalMutex = CreateMutex(NULL, FALSE, NULL);
729edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
730edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    mState = condState;
731edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
732edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
733edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project/*
734edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * Destructor.  Free Windows resources as well as our allocated storage.
735edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project */
736edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source ProjectCondition::~Condition()
737edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
738edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    WinCondition* condState = (WinCondition*) mState;
739edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    if (condState != NULL) {
740edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        CloseHandle(condState->sema);
741edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        CloseHandle(condState->waitersDone);
742edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        delete condState;
743edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
744edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
745edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
746edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
747edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectstatus_t Condition::wait(Mutex& mutex)
748edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
749edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    WinCondition* condState = (WinCondition*) mState;
750edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    HANDLE hMutex = (HANDLE) mutex.mState;
751edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
752edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    return ((WinCondition*)mState)->wait(condState, hMutex, NULL);
753edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
754edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
755edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectstatus_t Condition::wait(Mutex& mutex, nsecs_t abstime)
756edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
757edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    WinCondition* condState = (WinCondition*) mState;
758edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    HANDLE hMutex = (HANDLE) mutex.mState;
759edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
760edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    return ((WinCondition*)mState)->wait(condState, hMutex, &abstime);
761edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
762edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
763edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectstatus_t Condition::waitRelative(Mutex& mutex, nsecs_t reltime)
764edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
765edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    return wait(mutex, systemTime()+reltime);
766edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
767edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
768edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project/*
769edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * Signal the condition variable, allowing one thread to continue.
770edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project */
771edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectvoid Condition::signal()
772edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
773edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    WinCondition* condState = (WinCondition*) mState;
774edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
775edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    // Lock the internal mutex.  This ensures that we don't clash with
776edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    // broadcast().
777edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    WaitForSingleObject(condState->internalMutex, INFINITE);
778edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
779edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    EnterCriticalSection(&condState->waitersCountLock);
780edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    bool haveWaiters = (condState->waitersCount > 0);
781edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    LeaveCriticalSection(&condState->waitersCountLock);
782edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
783edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    // If no waiters, then this is a no-op.  Otherwise, knock the semaphore
784edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    // down a notch.
785edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    if (haveWaiters)
786edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        ReleaseSemaphore(condState->sema, 1, 0);
787edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
788edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    // Release internal mutex.
789edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    ReleaseMutex(condState->internalMutex);
790edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
791edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
792edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project/*
793edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * Signal the condition variable, allowing all threads to continue.
794edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project *
795edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * First we have to wake up all threads waiting on the semaphore, then
796edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * we wait until all of the threads have actually been woken before
797edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * releasing the internal mutex.  This ensures that all threads are woken.
798edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project */
799edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectvoid Condition::broadcast()
800edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
801edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    WinCondition* condState = (WinCondition*) mState;
802edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
803edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    // Lock the internal mutex.  This keeps the guys we're waking up
804edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    // from getting too far.
805edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    WaitForSingleObject(condState->internalMutex, INFINITE);
806edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
807edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    EnterCriticalSection(&condState->waitersCountLock);
808edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    bool haveWaiters = false;
809edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
810edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    if (condState->waitersCount > 0) {
811edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        haveWaiters = true;
812edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        condState->wasBroadcast = true;
813edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
814edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
815edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    if (haveWaiters) {
816edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        // Wake up all the waiters.
817edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        ReleaseSemaphore(condState->sema, condState->waitersCount, 0);
818edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
819edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        LeaveCriticalSection(&condState->waitersCountLock);
820edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
821edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        // Wait for all awakened threads to acquire the counting semaphore.
822edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        // The last guy who was waiting sets this.
823edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        WaitForSingleObject(condState->waitersDone, INFINITE);
824edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
825edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        // Reset wasBroadcast.  (No crit section needed because nobody
826edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        // else can wake up to poke at it.)
827edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        condState->wasBroadcast = 0;
828edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    } else {
829edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        // nothing to do
830edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        LeaveCriticalSection(&condState->waitersCountLock);
831edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
832edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
833edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    // Release internal mutex.
834edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    ReleaseMutex(condState->internalMutex);
835edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
836edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
837edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#else
838edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#error "condition variables not supported on this platform"
839edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#endif
840edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
841edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
842edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project/*
843edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * ===========================================================================
844edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project *      ReadWriteLock class
845edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * ===========================================================================
846edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project */
847edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
848edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#if 0
849edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#pragma mark -
850edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#pragma mark ReadWriteLock
851edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#endif
852edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
853edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project/*
854edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * Add a reader.  Readers are nice.  They share.
855edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project */
856edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectvoid ReadWriteLock::lockForRead()
857edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
858edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    mLock.lock();
859edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    while (mNumWriters > 0) {
860edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        LOG(LOG_DEBUG, "thread", "+++ lockForRead: waiting\n");
861edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        mReadWaiter.wait(mLock);
862edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
863edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    assert(mNumWriters == 0);
864edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    mNumReaders++;
865edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#if defined(PRINT_RENDER_TIMES)
866edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    if (mNumReaders == 1)
867edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        mDebugTimer.start();
868edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#endif
869edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    mLock.unlock();
870edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
871edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
872edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project/*
873edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * Try to add a reader.  If it doesn't work right away, return "false".
874edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project */
875edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectbool ReadWriteLock::tryLockForRead()
876edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
877edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    mLock.lock();
878edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    if (mNumWriters > 0) {
879edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        mLock.unlock();
880edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        return false;
881edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
882edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    assert(mNumWriters == 0);
883edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    mNumReaders++;
884edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#if defined(PRINT_RENDER_TIMES)
885edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    if (mNumReaders == 1)
886edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        mDebugTimer.start();
887edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#endif
888edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    mLock.unlock();
889edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    return true;
890edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
891edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
892edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project/*
893edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * Remove a reader.
894edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project */
895edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectvoid ReadWriteLock::unlockForRead()
896edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
897edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    mLock.lock();
898edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    if (mNumReaders == 0) {
899edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        mLock.unlock();
900edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        LOG(LOG_WARN, "thread",
901edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            "WARNING: unlockForRead requested, but not locked\n");
902edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        return;
903edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
904edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    assert(mNumReaders > 0);
905edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    assert(mNumWriters == 0);
906edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    mNumReaders--;
907edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    if (mNumReaders == 0) {           // last reader?
908edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#if defined(PRINT_RENDER_TIMES)
909edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        mDebugTimer.stop();
910edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        printf(" rdlk held %.3f msec\n",
911edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            (double) mDebugTimer.durationUsecs() / 1000.0);
912edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#endif
913edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        //printf("+++ signaling writers (if any)\n");
914edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        mWriteWaiter.signal();      // wake one writer (if any)
915edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
916edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    mLock.unlock();
917edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
918edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
919edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project/*
920edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * Add a writer.  This requires exclusive access to the object.
921edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project */
922edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectvoid ReadWriteLock::lockForWrite()
923edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
924edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    mLock.lock();
925edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    while (mNumReaders > 0 || mNumWriters > 0) {
926edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        LOG(LOG_DEBUG, "thread", "+++ lockForWrite: waiting\n");
927edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        mWriteWaiter.wait(mLock);
928edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
929edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    assert(mNumReaders == 0);
930edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    assert(mNumWriters == 0);
931edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    mNumWriters++;
932edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#if defined(PRINT_RENDER_TIMES)
933edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    mDebugTimer.start();
934edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#endif
935edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    mLock.unlock();
936edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
937edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
938edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project/*
939edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * Try to add a writer.  If it doesn't work right away, return "false".
940edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project */
941edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectbool ReadWriteLock::tryLockForWrite()
942edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
943edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    mLock.lock();
944edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    if (mNumReaders > 0 || mNumWriters > 0) {
945edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        mLock.unlock();
946edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        return false;
947edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
948edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    assert(mNumReaders == 0);
949edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    assert(mNumWriters == 0);
950edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    mNumWriters++;
951edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#if defined(PRINT_RENDER_TIMES)
952edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    mDebugTimer.start();
953edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#endif
954edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    mLock.unlock();
955edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    return true;
956edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
957edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
958edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project/*
959edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * Remove a writer.
960edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project */
961edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectvoid ReadWriteLock::unlockForWrite()
962edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
963edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    mLock.lock();
964edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    if (mNumWriters == 0) {
965edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        mLock.unlock();
966edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        LOG(LOG_WARN, "thread",
967edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            "WARNING: unlockForWrite requested, but not locked\n");
968edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        return;
969edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
970edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    assert(mNumWriters == 1);
971edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    mNumWriters--;
972edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#if defined(PRINT_RENDER_TIMES)
973edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    mDebugTimer.stop();
974edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    //printf(" wrlk held %.3f msec\n",
975edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    //    (double) mDebugTimer.durationUsecs() / 1000.0);
976edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#endif
977edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    mWriteWaiter.signal();         // should other writers get first dibs?
978edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    //printf("+++ signaling readers (if any)\n");
979edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    mReadWaiter.broadcast();        // wake all readers (if any)
980edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    mLock.unlock();
981edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
982edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
983edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project// ----------------------------------------------------------------------------
984edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
985edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#if 0
986edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#pragma mark -
987edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#pragma mark Thread::Thread
988edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project#endif
989edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
990edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project/*
991edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project * This is our thread object!
992edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project */
993edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
994edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source ProjectThread::Thread(bool canCallJava)
995edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    :   mCanCallJava(canCallJava),
996edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        mThread(thread_id_t(-1)),
997edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        mLock("Thread::mLock"),
998edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        mStatus(NO_ERROR),
999edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        mExitPending(false), mRunning(false)
1000edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
1001edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
1002edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1003edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source ProjectThread::~Thread()
1004edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
1005edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
1006edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1007edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectstatus_t Thread::readyToRun()
1008edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
1009edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    return NO_ERROR;
1010edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
1011edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1012edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectstatus_t Thread::run(const char* name, int32_t priority, size_t stack)
1013edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
1014edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    Mutex::Autolock _l(mLock);
1015edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1016edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    if (mRunning) {
1017edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        // thread already started
1018edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        return INVALID_OPERATION;
1019edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
1020edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1021edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    // reset status and exitPending to their default value, so we can
1022edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    // try again after an error happened (either below, or in readyToRun())
1023edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    mStatus = NO_ERROR;
1024edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    mExitPending = false;
1025edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    mThread = thread_id_t(-1);
1026edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1027edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    // hold a strong reference on ourself
1028edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    mHoldSelf = this;
1029edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1030edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    bool res;
1031edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    if (mCanCallJava) {
1032edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        res = createThreadEtc(_threadLoop,
1033edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                this, name, priority, stack, &mThread);
1034edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    } else {
1035edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        res = androidCreateRawThreadEtc(_threadLoop,
1036edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project                this, name, priority, stack, &mThread);
1037edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
1038edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1039edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    if (res == false) {
1040edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        mStatus = UNKNOWN_ERROR;   // something happened!
1041edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        mRunning = false;
1042edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        mThread = thread_id_t(-1);
1043edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
1044edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1045edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    if (mStatus < 0) {
1046edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        // something happened, don't leak
1047edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        mHoldSelf.clear();
1048edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
1049edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1050edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    return mStatus;
1051edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
1052edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1053edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectint Thread::_threadLoop(void* user)
1054edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
1055edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    Thread* const self = static_cast<Thread*>(user);
1056edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    sp<Thread> strong(self->mHoldSelf);
1057edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    wp<Thread> weak(strong);
1058edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    self->mHoldSelf.clear();
1059edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1060edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    // we're about to run...
1061edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    self->mStatus = self->readyToRun();
1062edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    if (self->mStatus!=NO_ERROR || self->mExitPending) {
1063edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        // pretend the thread never started...
1064edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        self->mExitPending = false;
1065edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        self->mRunning = false;
1066edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        return 0;
1067edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
1068edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1069edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    // thread is running now
1070edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    self->mRunning = true;
1071edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1072edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    do {
1073edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        bool result = self->threadLoop();
1074edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        if (result == false || self->mExitPending) {
1075edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            self->mExitPending = true;
1076edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            self->mLock.lock();
1077edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            self->mRunning = false;
1078edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            self->mThreadExitedCondition.signal();
1079edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            self->mLock.unlock();
1080edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            break;
1081edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        }
1082edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1083edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        // Release our strong reference, to let a chance to the thread
1084edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        // to die a peaceful death.
1085edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        strong.clear();
1086edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        // And immediately, reacquire a strong reference for the next loop
1087edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        strong = weak.promote();
1088edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    } while(strong != 0);
1089edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1090edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    return 0;
1091edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
1092edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1093edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectvoid Thread::requestExit()
1094edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
1095edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    mExitPending = true;
1096edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
1097edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1098edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectstatus_t Thread::requestExitAndWait()
1099edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
1100edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    if (mStatus == OK) {
1101edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1102edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        if (mThread == getThreadId()) {
1103edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            LOGW(
1104edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            "Thread (this=%p): don't call waitForExit() from this "
1105edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            "Thread object's thread. It's a guaranteed deadlock!",
1106edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            this);
1107edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            return WOULD_BLOCK;
1108edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        }
1109edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1110edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        requestExit();
1111edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1112edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        Mutex::Autolock _l(mLock);
1113edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        while (mRunning == true) {
1114edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project            mThreadExitedCondition.wait(mLock);
1115edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        }
1116edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project        mExitPending = false;
1117edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    }
1118edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    return mStatus;
1119edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
1120edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1121edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Projectbool Thread::exitPending() const
1122edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project{
1123edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project    return mExitPending;
1124edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project}
1125edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1126edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1127edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project
1128edbf3b6af777b721cd2a1ef461947e51e88241e1The Android Open Source Project};  // namespace android
1129