summaryrefslogtreecommitdiff
path: root/src/3rd_party/apache-log4cxx-0.10.0/src/main/cpp/threadcxx.cpp
blob: fffb43a66af67050c1b6a3f1e99b25471fbb557b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <log4cxx/logstring.h>
#include <log4cxx/helpers/thread.h>
#include <log4cxx/helpers/exception.h>
#include <apr_thread_proc.h>
#include <apr_atomic.h>
#include <log4cxx/helpers/pool.h>
#include <log4cxx/helpers/threadlocal.h>

using namespace log4cxx::helpers;
using namespace log4cxx;

Thread::Thread() : thread(NULL), alive(0), interruptedStatus(0) {
}

Thread::~Thread() {
    join();
}

Thread::LaunchPackage::LaunchPackage(Thread* t, Runnable r, void* d) : thread(t), runnable(r), data(d) {
}

Thread* Thread::LaunchPackage::getThread() const {
    return thread;
}

Runnable Thread::LaunchPackage::getRunnable() const {
    return runnable;
}

void* Thread::LaunchPackage::getData() const {
    return data;
}

void* Thread::LaunchPackage::operator new(size_t sz, Pool& p) {
    return p.palloc(sz);
}

void Thread::LaunchPackage::operator delete(void* mem, Pool& p) {
}

void Thread::run(Runnable start, void* data) {
#if APR_HAS_THREADS
        //
        //    if attempting a second run method on the same Thread object
        //         throw an exception
        //
        if (thread != NULL) {
            throw IllegalStateException();
        }
        apr_threadattr_t* attrs;
        apr_status_t stat = apr_threadattr_create(&attrs, p.getAPRPool());
        if (stat != APR_SUCCESS) {
                throw ThreadException(stat);
        }
        
        //   create LaunchPackage on the thread's memory pool
        LaunchPackage* package = new(p) LaunchPackage(this, start, data);
        stat = apr_thread_create(&thread, attrs,
            launcher, package, p.getAPRPool());
        if (stat != APR_SUCCESS) {
                throw ThreadException(stat);
        }
#else
        throw ThreadException(LOG4CXX_STR("APR_HAS_THREADS is not true"));
#endif
}


Thread::LaunchStatus::LaunchStatus(volatile unsigned int* p) : alive(p) {
    apr_atomic_set32(alive, 0xFFFFFFFF);
}

Thread::LaunchStatus::~LaunchStatus() {
    apr_atomic_set32(alive, 0);
}
    
#if APR_HAS_THREADS
void* LOG4CXX_THREAD_FUNC Thread::launcher(apr_thread_t* thread, void* data) {
    LaunchPackage* package = (LaunchPackage*) data;
    ThreadLocal& tls = getThreadLocal();
    tls.set(package->getThread());
    LaunchStatus alive(&package->getThread()->alive);
    void* retval = (package->getRunnable())(thread, package->getData());
    apr_thread_exit(thread, 0);
    return retval;
}
#endif


void Thread::join() {
#if APR_HAS_THREADS
        if (thread != NULL) {
                apr_status_t startStat;
                apr_status_t stat = apr_thread_join(&startStat, thread);
                thread = NULL;
                if (stat != APR_SUCCESS) {
                        // please see https://issues.apache.org/jira/browse/LOGCXX-278.
                        //throw ThreadException(stat);
                }
        }
#endif
}

ThreadLocal& Thread::getThreadLocal() {
     static ThreadLocal tls;
     return tls;
}

void Thread::currentThreadInterrupt() {
#if APR_HAS_THREADS
   void* tls = getThreadLocal().get();
   if (tls != 0) {
       ((Thread*) tls)->interrupt();
   }
#endif
}

void Thread::interrupt() {
    apr_atomic_set32(&interruptedStatus, 0xFFFFFFFF);
}

bool Thread::interrupted() {
#if APR_HAS_THREADS
   void* tls = getThreadLocal().get();
   if (tls != 0) {
       return apr_atomic_xchg32(&(((Thread*) tls)->interruptedStatus), 0) != 0;
   }
#endif
   return false;
}

bool Thread::isCurrentThread() const {
#if APR_HAS_THREADS
    const void* tls = getThreadLocal().get();
    return (tls == this);
#else
    return true;
#endif
}

bool Thread::isAlive() {
    return apr_atomic_read32(&alive) != 0;
}

void Thread::ending() {
    apr_atomic_set32(&alive, 0);
}


void Thread::sleep(int duration) {
#if APR_HAS_THREADS
    if(interrupted()) {
         throw InterruptedException();
    }
#endif    
    if (duration > 0) {
        apr_sleep(duration*1000);
    }
}