File: thread.c

package info (click to toggle)
groonga 16.0.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 188,416 kB
  • sloc: ansic: 772,827; cpp: 52,396; ruby: 40,556; javascript: 10,250; yacc: 7,045; sh: 5,627; python: 2,821; makefile: 1,679
file content (158 lines) | stat: -rw-r--r-- 4,408 bytes parent folder | download | duplicates (2)
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
/*
  Copyright(C) 2015  Brazil
  Copyright(C) 2018-2021  Sutou Kouhei <kou@clear-code.com>

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include "grn_ctx.h"
#include "grn_windows.h"

#ifdef _WIN32
# include <stdio.h>
# include <tlhelp32.h>
#endif

static grn_thread_get_limit_func get_limit_func = NULL;
static void *get_limit_func_data = NULL;
static grn_thread_set_limit_func set_limit_func = NULL;
static void *set_limit_func_data = NULL;

static grn_thread_get_limit_with_ctx_func get_limit_with_ctx_func = NULL;
static void *get_limit_with_ctx_func_data = NULL;
static grn_thread_set_limit_with_ctx_func set_limit_with_ctx_func = NULL;
static void *set_limit_with_ctx_func_data = NULL;

uint32_t
grn_thread_get_limit(void)
{
  if (get_limit_func) {
    return get_limit_func(get_limit_func_data);
  } else {
    return 0;
  }
}

void
grn_thread_set_limit(uint32_t new_limit)
{
  if (!set_limit_func) {
    return;
  }

  set_limit_func(new_limit, set_limit_func_data);
}

uint32_t
grn_thread_get_limit_with_ctx(grn_ctx *ctx)
{
  if (get_limit_with_ctx_func) {
    return get_limit_with_ctx_func(ctx, get_limit_with_ctx_func_data);
  } else {
    return grn_thread_get_limit();
  }
}

void
grn_thread_set_limit_with_ctx(grn_ctx *ctx, uint32_t new_limit)
{
  if (set_limit_with_ctx_func) {
    set_limit_with_ctx_func(ctx, new_limit, set_limit_func_data);
  } else {
    grn_thread_set_limit(new_limit);
  }
}

void
grn_thread_set_get_limit_func(grn_thread_get_limit_func func,
                              void *data)
{
  get_limit_func = func;
  get_limit_func_data = data;
}

void
grn_thread_set_set_limit_func(grn_thread_set_limit_func func, void *data)
{
  set_limit_func = func;
  set_limit_func_data = data;
}

void
grn_thread_set_get_limit_with_ctx_func(grn_thread_get_limit_with_ctx_func func,
                                       void *data)
{
  get_limit_with_ctx_func = func;
  get_limit_with_ctx_func_data = data;
}

void
grn_thread_set_set_limit_with_ctx_func(grn_thread_set_limit_with_ctx_func func,
                                       void *data)
{
  set_limit_with_ctx_func = func;
  set_limit_with_ctx_func_data = data;
}

grn_rc
grn_thread_dump(grn_ctx *ctx)
{
  GRN_API_ENTER;
#ifdef _WIN32
  grn_log_level level = GRN_LOG_NOTICE;
  HANDLE thread_snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
  if (thread_snapshot == INVALID_HANDLE_VALUE) {
    SERR("[thread][dump] failed to create thread snapshot");
    GRN_API_RETURN(ctx->rc);
  }
  HANDLE process = GetCurrentProcess();
  DWORD process_id = GetCurrentProcessId();
  THREADENTRY32 entry;
  entry.dwSize = sizeof(entry);
  if (!Thread32First(thread_snapshot, &entry)) {
    SERR("[thread][dump] failed to retrieve the first thread");
  }
  if (ctx->rc == GRN_SUCCESS) {
    do {
      if (entry.th32OwnerProcessID != process_id) {
        continue;
      }

      HANDLE thread = OpenThread(THREAD_GET_CONTEXT,
                                 FALSE,
                                 entry.th32ThreadID);
      if (thread == INVALID_HANDLE_VALUE) {
        continue;
      }
      CONTEXT context;
      context.ContextFlags = CONTEXT_FULL;
      if (GetThreadContext(thread, &context)) {
        GRN_LOG(ctx, level, "-- Thread %08" GRN_FMT_DWORD " --",
                entry.th32ThreadID);
        grn_windows_log_back_trace(ctx,
                                   level,
                                   process,
                                   thread,
                                   &context);
        GRN_LOG(ctx, level, "---------------------");
      }
      CloseHandle(thread);
    } while (Thread32Next(thread_snapshot, &entry));
  }
  CloseHandle(thread_snapshot);
#else
#endif
  GRN_API_RETURN(ctx->rc);
}