LLVM OpenMP* Runtime Library
Loading...
Searching...
No Matches
kmp_io.cpp
1/*
2 * kmp_io.cpp -- RTL IO
3 */
4
5//===----------------------------------------------------------------------===//
6//
7// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
8// See https://llvm.org/LICENSE.txt for license information.
9// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
10//
11//===----------------------------------------------------------------------===//
12
13#include <stdarg.h>
14#include <stddef.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18#ifndef __ABSOFT_WIN
19#include <sys/types.h>
20#endif
21
22#include "kmp.h" // KMP_GTID_DNE, __kmp_debug_buf, etc
23#include "kmp_io.h"
24#include "kmp_lock.h"
25#include "kmp_os.h"
26#include "kmp_str.h"
27
28#if KMP_OS_WINDOWS
29#if KMP_MSVC_COMPAT
30#pragma warning(push)
31#pragma warning(disable : 271 310)
32#endif
33#include <windows.h>
34#if KMP_MSVC_COMPAT
35#pragma warning(pop)
36#endif
37#endif
38
39/* ------------------------------------------------------------------------ */
40
41kmp_bootstrap_lock_t __kmp_stdio_lock = KMP_BOOTSTRAP_LOCK_INITIALIZER(
42 __kmp_stdio_lock); /* Control stdio functions */
43kmp_bootstrap_lock_t __kmp_console_lock = KMP_BOOTSTRAP_LOCK_INITIALIZER(
44 __kmp_console_lock); /* Control console initialization */
45
46#if KMP_OS_WINDOWS
47
48static HANDLE __kmp_stdout = NULL;
49static HANDLE __kmp_stderr = NULL;
50static int __kmp_console_exists = FALSE;
51static kmp_str_buf_t __kmp_console_buf;
52
53void __kmp_close_console(void) {
54 /* wait until user presses return before closing window */
55 /* TODO only close if a window was opened */
56 if (__kmp_console_exists) {
57 __kmp_stdout = NULL;
58 __kmp_stderr = NULL;
59 __kmp_str_buf_free(&__kmp_console_buf);
60 __kmp_console_exists = FALSE;
61 }
62}
63
64/* For windows, call this before stdout, stderr, or stdin are used.
65 It opens a console window and starts processing */
66static void __kmp_redirect_output(void) {
67 __kmp_acquire_bootstrap_lock(&__kmp_console_lock);
68
69 if (!__kmp_console_exists) {
70 HANDLE ho;
71 HANDLE he;
72
73 __kmp_str_buf_init(&__kmp_console_buf);
74
75 AllocConsole();
76 // We do not check the result of AllocConsole because
77 // 1. the call is harmless
78 // 2. it is not clear how to communicate failue
79 // 3. we will detect failure later when we get handle(s)
80
81 ho = GetStdHandle(STD_OUTPUT_HANDLE);
82 if (ho == INVALID_HANDLE_VALUE || ho == NULL) {
83
84 DWORD err = GetLastError();
85 // TODO: output error somehow (maybe message box)
86 (void)err;
87 __kmp_stdout = NULL;
88
89 } else {
90
91 __kmp_stdout = ho; // temporary code, need new global for ho
92 }
93 he = GetStdHandle(STD_ERROR_HANDLE);
94 if (he == INVALID_HANDLE_VALUE || he == NULL) {
95
96 DWORD err = GetLastError();
97 // TODO: output error somehow (maybe message box)
98 (void)err;
99 __kmp_stderr = NULL;
100
101 } else {
102
103 __kmp_stderr = he; // temporary code, need new global
104 }
105 __kmp_console_exists = TRUE;
106 }
107 __kmp_release_bootstrap_lock(&__kmp_console_lock);
108}
109
110#else
111#define __kmp_stderr (stderr)
112#define __kmp_stdout (stdout)
113#endif /* KMP_OS_WINDOWS */