annotate openmp/runtime/src/kmp_io.cpp @ 152:e8a9b4f4d755

pull from 146
author anatofuz
date Wed, 11 Mar 2020 18:29:16 +0900
parents 1d019706d866
children 2e18cbf3894f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 /*
anatofuz
parents:
diff changeset
2 * kmp_io.cpp -- RTL IO
anatofuz
parents:
diff changeset
3 */
anatofuz
parents:
diff changeset
4
anatofuz
parents:
diff changeset
5 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
6 //
anatofuz
parents:
diff changeset
7 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
anatofuz
parents:
diff changeset
8 // See https://llvm.org/LICENSE.txt for license information.
anatofuz
parents:
diff changeset
9 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
anatofuz
parents:
diff changeset
10 //
anatofuz
parents:
diff changeset
11 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
12
anatofuz
parents:
diff changeset
13 #include <stdarg.h>
anatofuz
parents:
diff changeset
14 #include <stddef.h>
anatofuz
parents:
diff changeset
15 #include <stdio.h>
anatofuz
parents:
diff changeset
16 #include <stdlib.h>
anatofuz
parents:
diff changeset
17 #include <string.h>
anatofuz
parents:
diff changeset
18 #ifndef __ABSOFT_WIN
anatofuz
parents:
diff changeset
19 #include <sys/types.h>
anatofuz
parents:
diff changeset
20 #endif
anatofuz
parents:
diff changeset
21
anatofuz
parents:
diff changeset
22 #include "kmp.h" // KMP_GTID_DNE, __kmp_debug_buf, etc
anatofuz
parents:
diff changeset
23 #include "kmp_io.h"
anatofuz
parents:
diff changeset
24 #include "kmp_lock.h"
anatofuz
parents:
diff changeset
25 #include "kmp_os.h"
anatofuz
parents:
diff changeset
26 #include "kmp_str.h"
anatofuz
parents:
diff changeset
27
anatofuz
parents:
diff changeset
28 #if KMP_OS_WINDOWS
anatofuz
parents:
diff changeset
29 #if KMP_MSVC_COMPAT
anatofuz
parents:
diff changeset
30 #pragma warning(push)
anatofuz
parents:
diff changeset
31 #pragma warning(disable : 271 310)
anatofuz
parents:
diff changeset
32 #endif
anatofuz
parents:
diff changeset
33 #include <windows.h>
anatofuz
parents:
diff changeset
34 #if KMP_MSVC_COMPAT
anatofuz
parents:
diff changeset
35 #pragma warning(pop)
anatofuz
parents:
diff changeset
36 #endif
anatofuz
parents:
diff changeset
37 #endif
anatofuz
parents:
diff changeset
38
anatofuz
parents:
diff changeset
39 /* ------------------------------------------------------------------------ */
anatofuz
parents:
diff changeset
40
anatofuz
parents:
diff changeset
41 kmp_bootstrap_lock_t __kmp_stdio_lock = KMP_BOOTSTRAP_LOCK_INITIALIZER(
anatofuz
parents:
diff changeset
42 __kmp_stdio_lock); /* Control stdio functions */
anatofuz
parents:
diff changeset
43 kmp_bootstrap_lock_t __kmp_console_lock = KMP_BOOTSTRAP_LOCK_INITIALIZER(
anatofuz
parents:
diff changeset
44 __kmp_console_lock); /* Control console initialization */
anatofuz
parents:
diff changeset
45
anatofuz
parents:
diff changeset
46 #if KMP_OS_WINDOWS
anatofuz
parents:
diff changeset
47
anatofuz
parents:
diff changeset
48 static HANDLE __kmp_stdout = NULL;
anatofuz
parents:
diff changeset
49 static HANDLE __kmp_stderr = NULL;
anatofuz
parents:
diff changeset
50 static int __kmp_console_exists = FALSE;
anatofuz
parents:
diff changeset
51 static kmp_str_buf_t __kmp_console_buf;
anatofuz
parents:
diff changeset
52
anatofuz
parents:
diff changeset
53 static int is_console(void) {
anatofuz
parents:
diff changeset
54 char buffer[128];
anatofuz
parents:
diff changeset
55 DWORD rc = 0;
anatofuz
parents:
diff changeset
56 DWORD err = 0;
anatofuz
parents:
diff changeset
57 // Try to get console title.
anatofuz
parents:
diff changeset
58 SetLastError(0);
anatofuz
parents:
diff changeset
59 // GetConsoleTitle does not reset last error in case of success or short
anatofuz
parents:
diff changeset
60 // buffer, so we need to clear it explicitly.
anatofuz
parents:
diff changeset
61 rc = GetConsoleTitle(buffer, sizeof(buffer));
anatofuz
parents:
diff changeset
62 if (rc == 0) {
anatofuz
parents:
diff changeset
63 // rc == 0 means getting console title failed. Let us find out why.
anatofuz
parents:
diff changeset
64 err = GetLastError();
anatofuz
parents:
diff changeset
65 // err == 0 means buffer too short (we suppose console exists).
anatofuz
parents:
diff changeset
66 // In Window applications we usually have err == 6 (invalid handle).
anatofuz
parents:
diff changeset
67 }
anatofuz
parents:
diff changeset
68 return rc > 0 || err == 0;
anatofuz
parents:
diff changeset
69 }
anatofuz
parents:
diff changeset
70
anatofuz
parents:
diff changeset
71 void __kmp_close_console(void) {
anatofuz
parents:
diff changeset
72 /* wait until user presses return before closing window */
anatofuz
parents:
diff changeset
73 /* TODO only close if a window was opened */
anatofuz
parents:
diff changeset
74 if (__kmp_console_exists) {
anatofuz
parents:
diff changeset
75 __kmp_stdout = NULL;
anatofuz
parents:
diff changeset
76 __kmp_stderr = NULL;
anatofuz
parents:
diff changeset
77 __kmp_str_buf_free(&__kmp_console_buf);
anatofuz
parents:
diff changeset
78 __kmp_console_exists = FALSE;
anatofuz
parents:
diff changeset
79 }
anatofuz
parents:
diff changeset
80 }
anatofuz
parents:
diff changeset
81
anatofuz
parents:
diff changeset
82 /* For windows, call this before stdout, stderr, or stdin are used.
anatofuz
parents:
diff changeset
83 It opens a console window and starts processing */
anatofuz
parents:
diff changeset
84 static void __kmp_redirect_output(void) {
anatofuz
parents:
diff changeset
85 __kmp_acquire_bootstrap_lock(&__kmp_console_lock);
anatofuz
parents:
diff changeset
86
anatofuz
parents:
diff changeset
87 if (!__kmp_console_exists) {
anatofuz
parents:
diff changeset
88 HANDLE ho;
anatofuz
parents:
diff changeset
89 HANDLE he;
anatofuz
parents:
diff changeset
90
anatofuz
parents:
diff changeset
91 __kmp_str_buf_init(&__kmp_console_buf);
anatofuz
parents:
diff changeset
92
anatofuz
parents:
diff changeset
93 AllocConsole();
anatofuz
parents:
diff changeset
94 // We do not check the result of AllocConsole because
anatofuz
parents:
diff changeset
95 // 1. the call is harmless
anatofuz
parents:
diff changeset
96 // 2. it is not clear how to communicate failue
anatofuz
parents:
diff changeset
97 // 3. we will detect failure later when we get handle(s)
anatofuz
parents:
diff changeset
98
anatofuz
parents:
diff changeset
99 ho = GetStdHandle(STD_OUTPUT_HANDLE);
anatofuz
parents:
diff changeset
100 if (ho == INVALID_HANDLE_VALUE || ho == NULL) {
anatofuz
parents:
diff changeset
101
anatofuz
parents:
diff changeset
102 DWORD err = GetLastError();
anatofuz
parents:
diff changeset
103 // TODO: output error somehow (maybe message box)
anatofuz
parents:
diff changeset
104 __kmp_stdout = NULL;
anatofuz
parents:
diff changeset
105
anatofuz
parents:
diff changeset
106 } else {
anatofuz
parents:
diff changeset
107
anatofuz
parents:
diff changeset
108 __kmp_stdout = ho; // temporary code, need new global for ho
anatofuz
parents:
diff changeset
109 }
anatofuz
parents:
diff changeset
110 he = GetStdHandle(STD_ERROR_HANDLE);
anatofuz
parents:
diff changeset
111 if (he == INVALID_HANDLE_VALUE || he == NULL) {
anatofuz
parents:
diff changeset
112
anatofuz
parents:
diff changeset
113 DWORD err = GetLastError();
anatofuz
parents:
diff changeset
114 // TODO: output error somehow (maybe message box)
anatofuz
parents:
diff changeset
115 __kmp_stderr = NULL;
anatofuz
parents:
diff changeset
116
anatofuz
parents:
diff changeset
117 } else {
anatofuz
parents:
diff changeset
118
anatofuz
parents:
diff changeset
119 __kmp_stderr = he; // temporary code, need new global
anatofuz
parents:
diff changeset
120 }
anatofuz
parents:
diff changeset
121 __kmp_console_exists = TRUE;
anatofuz
parents:
diff changeset
122 }
anatofuz
parents:
diff changeset
123 __kmp_release_bootstrap_lock(&__kmp_console_lock);
anatofuz
parents:
diff changeset
124 }
anatofuz
parents:
diff changeset
125
anatofuz
parents:
diff changeset
126 #else
anatofuz
parents:
diff changeset
127 #define __kmp_stderr (stderr)
anatofuz
parents:
diff changeset
128 #define __kmp_stdout (stdout)
anatofuz
parents:
diff changeset
129 #endif /* KMP_OS_WINDOWS */
anatofuz
parents:
diff changeset
130
anatofuz
parents:
diff changeset
131 void __kmp_vprintf(enum kmp_io out_stream, char const *format, va_list ap) {
anatofuz
parents:
diff changeset
132 #if KMP_OS_WINDOWS
anatofuz
parents:
diff changeset
133 if (!__kmp_console_exists) {
anatofuz
parents:
diff changeset
134 __kmp_redirect_output();
anatofuz
parents:
diff changeset
135 }
anatofuz
parents:
diff changeset
136 if (!__kmp_stderr && out_stream == kmp_err) {
anatofuz
parents:
diff changeset
137 return;
anatofuz
parents:
diff changeset
138 }
anatofuz
parents:
diff changeset
139 if (!__kmp_stdout && out_stream == kmp_out) {
anatofuz
parents:
diff changeset
140 return;
anatofuz
parents:
diff changeset
141 }
anatofuz
parents:
diff changeset
142 #endif /* KMP_OS_WINDOWS */
anatofuz
parents:
diff changeset
143 auto stream = ((out_stream == kmp_out) ? __kmp_stdout : __kmp_stderr);
anatofuz
parents:
diff changeset
144
anatofuz
parents:
diff changeset
145 if (__kmp_debug_buf && __kmp_debug_buffer != NULL) {
anatofuz
parents:
diff changeset
146
anatofuz
parents:
diff changeset
147 int dc = __kmp_debug_count++ % __kmp_debug_buf_lines;
anatofuz
parents:
diff changeset
148 char *db = &__kmp_debug_buffer[dc * __kmp_debug_buf_chars];
anatofuz
parents:
diff changeset
149 int chars = 0;
anatofuz
parents:
diff changeset
150
anatofuz
parents:
diff changeset
151 #ifdef KMP_DEBUG_PIDS
anatofuz
parents:
diff changeset
152 chars = KMP_SNPRINTF(db, __kmp_debug_buf_chars, "pid=%d: ",
anatofuz
parents:
diff changeset
153 (kmp_int32)getpid());
anatofuz
parents:
diff changeset
154 #endif
anatofuz
parents:
diff changeset
155 chars += KMP_VSNPRINTF(db, __kmp_debug_buf_chars, format, ap);
anatofuz
parents:
diff changeset
156
anatofuz
parents:
diff changeset
157 if (chars + 1 > __kmp_debug_buf_chars) {
anatofuz
parents:
diff changeset
158 if (chars + 1 > __kmp_debug_buf_warn_chars) {
anatofuz
parents:
diff changeset
159 #if KMP_OS_WINDOWS
anatofuz
parents:
diff changeset
160 DWORD count;
anatofuz
parents:
diff changeset
161 __kmp_str_buf_print(&__kmp_console_buf, "OMP warning: Debugging buffer "
anatofuz
parents:
diff changeset
162 "overflow; increase "
anatofuz
parents:
diff changeset
163 "KMP_DEBUG_BUF_CHARS to %d\n",
anatofuz
parents:
diff changeset
164 chars + 1);
anatofuz
parents:
diff changeset
165 WriteFile(stream, __kmp_console_buf.str, __kmp_console_buf.used, &count,
anatofuz
parents:
diff changeset
166 NULL);
anatofuz
parents:
diff changeset
167 __kmp_str_buf_clear(&__kmp_console_buf);
anatofuz
parents:
diff changeset
168 #else
anatofuz
parents:
diff changeset
169 fprintf(stream, "OMP warning: Debugging buffer overflow; "
anatofuz
parents:
diff changeset
170 "increase KMP_DEBUG_BUF_CHARS to %d\n",
anatofuz
parents:
diff changeset
171 chars + 1);
anatofuz
parents:
diff changeset
172 fflush(stream);
anatofuz
parents:
diff changeset
173 #endif
anatofuz
parents:
diff changeset
174 __kmp_debug_buf_warn_chars = chars + 1;
anatofuz
parents:
diff changeset
175 }
anatofuz
parents:
diff changeset
176 /* terminate string if overflow occurred */
anatofuz
parents:
diff changeset
177 db[__kmp_debug_buf_chars - 2] = '\n';
anatofuz
parents:
diff changeset
178 db[__kmp_debug_buf_chars - 1] = '\0';
anatofuz
parents:
diff changeset
179 }
anatofuz
parents:
diff changeset
180 } else {
anatofuz
parents:
diff changeset
181 #if KMP_OS_WINDOWS
anatofuz
parents:
diff changeset
182 DWORD count;
anatofuz
parents:
diff changeset
183 #ifdef KMP_DEBUG_PIDS
anatofuz
parents:
diff changeset
184 __kmp_str_buf_print(&__kmp_console_buf, "pid=%d: ", (kmp_int32)getpid());
anatofuz
parents:
diff changeset
185 #endif
anatofuz
parents:
diff changeset
186 __kmp_str_buf_vprint(&__kmp_console_buf, format, ap);
anatofuz
parents:
diff changeset
187 WriteFile(stream, __kmp_console_buf.str, __kmp_console_buf.used, &count,
anatofuz
parents:
diff changeset
188 NULL);
anatofuz
parents:
diff changeset
189 __kmp_str_buf_clear(&__kmp_console_buf);
anatofuz
parents:
diff changeset
190 #else
anatofuz
parents:
diff changeset
191 #ifdef KMP_DEBUG_PIDS
anatofuz
parents:
diff changeset
192 fprintf(stream, "pid=%d: ", (kmp_int32)getpid());
anatofuz
parents:
diff changeset
193 #endif
anatofuz
parents:
diff changeset
194 vfprintf(stream, format, ap);
anatofuz
parents:
diff changeset
195 fflush(stream);
anatofuz
parents:
diff changeset
196 #endif
anatofuz
parents:
diff changeset
197 }
anatofuz
parents:
diff changeset
198 }
anatofuz
parents:
diff changeset
199
anatofuz
parents:
diff changeset
200 void __kmp_printf(char const *format, ...) {
anatofuz
parents:
diff changeset
201 va_list ap;
anatofuz
parents:
diff changeset
202 va_start(ap, format);
anatofuz
parents:
diff changeset
203
anatofuz
parents:
diff changeset
204 __kmp_acquire_bootstrap_lock(&__kmp_stdio_lock);
anatofuz
parents:
diff changeset
205 __kmp_vprintf(kmp_err, format, ap);
anatofuz
parents:
diff changeset
206 __kmp_release_bootstrap_lock(&__kmp_stdio_lock);
anatofuz
parents:
diff changeset
207
anatofuz
parents:
diff changeset
208 va_end(ap);
anatofuz
parents:
diff changeset
209 }
anatofuz
parents:
diff changeset
210
anatofuz
parents:
diff changeset
211 void __kmp_printf_no_lock(char const *format, ...) {
anatofuz
parents:
diff changeset
212 va_list ap;
anatofuz
parents:
diff changeset
213 va_start(ap, format);
anatofuz
parents:
diff changeset
214
anatofuz
parents:
diff changeset
215 __kmp_vprintf(kmp_err, format, ap);
anatofuz
parents:
diff changeset
216
anatofuz
parents:
diff changeset
217 va_end(ap);
anatofuz
parents:
diff changeset
218 }
anatofuz
parents:
diff changeset
219
anatofuz
parents:
diff changeset
220 void __kmp_fprintf(enum kmp_io stream, char const *format, ...) {
anatofuz
parents:
diff changeset
221 va_list ap;
anatofuz
parents:
diff changeset
222 va_start(ap, format);
anatofuz
parents:
diff changeset
223
anatofuz
parents:
diff changeset
224 __kmp_acquire_bootstrap_lock(&__kmp_stdio_lock);
anatofuz
parents:
diff changeset
225 __kmp_vprintf(stream, format, ap);
anatofuz
parents:
diff changeset
226 __kmp_release_bootstrap_lock(&__kmp_stdio_lock);
anatofuz
parents:
diff changeset
227
anatofuz
parents:
diff changeset
228 va_end(ap);
anatofuz
parents:
diff changeset
229 }