source: valtobtest/subversion-1.6.2/subversion/bindings/javahl/native/JNIThreadData.cpp @ 3

Last change on this file since 3 was 3, checked in by valtob, 15 years ago

subversion source 1.6.2 as test

File size: 4.1 KB
Line 
1/**
2 * @copyright
3 * ====================================================================
4 * Copyright (c) 2003 CollabNet.  All rights reserved.
5 *
6 * This software is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution.  The terms
8 * are also available at http://subversion.tigris.org/license-1.html.
9 * If newer versions of this license are posted there, you may use a
10 * newer version instead, at your option.
11 *
12 * This software consists of voluntary contributions made by many
13 * individuals.  For exact contribution history, see the revision
14 * history and logs, available at http://subversion.tigris.org/.
15 * ====================================================================
16 * @endcopyright
17 *
18 * @file JNIThreadData.cpp
19 * @brief Implementation of the class JNIThreadData
20 */
21
22#include "JNIThreadData.h"
23#include <apr_strings.h>
24#include <apr_tables.h>
25#include <apr_general.h>
26#include <apr_lib.h>
27#include <apr_thread_proc.h>
28#include "JNIUtil.h"
29
30apr_threadkey_t *JNIThreadData::g_key;
31
32/**
33 * Create and initialize a new object.
34 */
35JNIThreadData::JNIThreadData()
36{
37  m_env = NULL;
38  m_exceptionThrown = false;
39  m_requestPool = NULL;
40  m_previous = NULL;
41}
42
43JNIThreadData::~JNIThreadData()
44{
45}
46
47/**
48 * Initialize the thread local storage.
49 * @return success or failure
50 */
51bool JNIThreadData::initThreadData()
52{
53  // If already initialized -> nothing to do.
54  if (g_key != NULL)
55    return false;
56
57  // Request a key for the thread local storage from the global pool
58  // and register a callback function called when the thread is
59  // deleted.
60  apr_status_t apr_err = apr_threadkey_private_create(&g_key,
61                                                      del,
62                                                      JNIUtil::getPool());
63  if (apr_err)
64    {
65      JNIUtil::handleAPRError(apr_err, "apr_threadkey_private_create");
66      return false;
67    }
68
69  return true;
70}
71
72/**
73 * Get the thread local storage for this thread.
74 * @return thread local storage
75 */
76JNIThreadData *JNIThreadData::getThreadData()
77{
78  // We should never be called before initThreadData
79  if (g_key == NULL)
80    return NULL;
81
82  // Retrieve the thread local storage from APR.
83  JNIThreadData *data = NULL;
84  apr_status_t apr_err = apr_threadkey_private_get
85    (reinterpret_cast<void**>(&data), g_key);
86  if (apr_err)
87    {
88      JNIUtil::handleAPRError(apr_err, "apr_threadkey_private_get");
89      return NULL;
90    }
91
92  // Not already allocated.
93  if (data == NULL)
94    {
95      // Allocate and store to APR.
96      data = new JNIThreadData;
97      apr_err = apr_threadkey_private_set (data, g_key);
98      if (apr_err)
99        {
100          JNIUtil::handleAPRError(apr_err, "apr_threadkey_private_set");
101          return NULL;
102        }
103    }
104  return data;
105}
106
107/**
108 * Allocate a new ThreadData for the current call from Java and push
109 * it on the stack
110 */
111void JNIThreadData::pushNewThreadData()
112{
113  JNIThreadData *data = NULL;
114  apr_status_t apr_err = apr_threadkey_private_get
115    (reinterpret_cast<void**>(&data), g_key);
116  if (apr_err)
117    {
118      JNIUtil::handleAPRError(apr_err, "apr_threadkey_private_get");
119      return;
120    }
121  JNIThreadData *newData = new JNIThreadData();
122  newData->m_previous =data;
123  apr_err = apr_threadkey_private_set (newData, g_key);
124  if (apr_err)
125    {
126      JNIUtil::handleAPRError(apr_err, "apr_threadkey_private_set");
127      return;
128    }
129}
130
131/**
132 * Pop the current ThreadData from the stack, because the call
133 * completed.
134 */
135void JNIThreadData::popThreadData()
136{
137  JNIThreadData *data = NULL;
138  apr_status_t apr_err = apr_threadkey_private_get
139    (reinterpret_cast<void**>(&data), g_key);
140  if (apr_err)
141    {
142      JNIUtil::handleAPRError(apr_err, "apr_threadkey_private_get");
143      return;
144    }
145  if (data == NULL)
146    return;
147
148  JNIThreadData *oldData = data->m_previous;
149  delete data;
150  apr_err = apr_threadkey_private_set (oldData, g_key);
151  if (apr_err)
152    {
153      JNIUtil::handleAPRError(apr_err, "apr_threadkey_private_set");
154      return;
155    }
156}
157
158/**
159 * Callback called by APR when the thread dies.  Deletes the thread
160 * local storage.
161 */
162void JNIThreadData::del(void *p)
163{
164  delete reinterpret_cast<JNIThreadData*>(p);
165}
Note: See TracBrowser for help on using the repository browser.