summaryrefslogtreecommitdiff
path: root/subversion/bindings/javahl/native/JNIThreadData.cpp
blob: e3ce3343c8c9f578846b17d3ddadebd2c301a433 (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
/**
 * @copyright
 * ====================================================================
 *    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.
 * ====================================================================
 * @endcopyright
 *
 * @file JNIThreadData.cpp
 * @brief Implementation of the class JNIThreadData
 */

#include "JNIThreadData.h"
#include <apr_strings.h>
#include <apr_tables.h>
#include <apr_general.h>
#include <apr_lib.h>
#include <apr_thread_proc.h>
#include "JNIUtil.h"

apr_threadkey_t *JNIThreadData::g_key;

/**
 * Create and initialize a new object.
 */
JNIThreadData::JNIThreadData()
{
  m_env = NULL;
  m_exceptionThrown = false;
  m_previous = NULL;
}

JNIThreadData::~JNIThreadData()
{
}

/**
 * Initialize the thread local storage.
 * @return success or failure
 */
bool JNIThreadData::initThreadData()
{
  // If already initialized -> nothing to do.
  if (g_key != NULL)
    return false;

  // Request a key for the thread local storage from the global pool
  // and register a callback function called when the thread is
  // deleted.
  apr_status_t apr_err = apr_threadkey_private_create(&g_key,
                                                      del,
                                                      JNIUtil::getPool());
  if (apr_err)
    {
      JNIUtil::handleAPRError(apr_err, "apr_threadkey_private_create");
      return false;
    }

  return true;
}

/**
 * Get the thread local storage for this thread.
 * @return thread local storage
 */
JNIThreadData *JNIThreadData::getThreadData()
{
  // We should never be called before initThreadData
  if (g_key == NULL)
    return NULL;

  // Retrieve the thread local storage from APR.
  JNIThreadData *data = NULL;
  apr_status_t apr_err = apr_threadkey_private_get
    (reinterpret_cast<void**>(&data), g_key);
  if (apr_err)
    {
      JNIUtil::handleAPRError(apr_err, "apr_threadkey_private_get");
      return NULL;
    }

  // Not already allocated.
  if (data == NULL)
    {
      // Allocate and store to APR.
      data = new JNIThreadData;
      apr_err = apr_threadkey_private_set (data, g_key);
      if (apr_err)
        {
          JNIUtil::handleAPRError(apr_err, "apr_threadkey_private_set");
          return NULL;
        }
    }
  return data;
}

/**
 * Allocate a new ThreadData for the current call from Java and push
 * it on the stack
 */
void JNIThreadData::pushNewThreadData()
{
  JNIThreadData *data = NULL;
  apr_status_t apr_err = apr_threadkey_private_get
    (reinterpret_cast<void**>(&data), g_key);
  if (apr_err)
    {
      JNIUtil::handleAPRError(apr_err, "apr_threadkey_private_get");
      return;
    }
  JNIThreadData *newData = new JNIThreadData();
  newData->m_previous =data;
  apr_err = apr_threadkey_private_set(newData, g_key);
  if (apr_err)
    {
      JNIUtil::handleAPRError(apr_err, "apr_threadkey_private_set");
      return;
    }
}

/**
 * Pop the current ThreadData from the stack, because the call
 * completed.
 */
void JNIThreadData::popThreadData()
{
  JNIThreadData *data = NULL;
  apr_status_t apr_err = apr_threadkey_private_get
    (reinterpret_cast<void**>(&data), g_key);
  if (apr_err)
    {
      JNIUtil::handleAPRError(apr_err, "apr_threadkey_private_get");
      return;
    }
  if (data == NULL)
    return;

  JNIThreadData *oldData = data->m_previous;
  delete data;
  apr_err = apr_threadkey_private_set(oldData, g_key);
  if (apr_err)
    {
      JNIUtil::handleAPRError(apr_err, "apr_threadkey_private_set");
      return;
    }
}

/**
 * Callback called by APR when the thread dies.  Deletes the thread
 * local storage.
 */
void JNIThreadData::del(void *p)
{
  delete reinterpret_cast<JNIThreadData*>(p);
}