|
| 1 | +/* Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. |
| 2 | + * |
| 3 | + * Redistribution and use in source and binary forms, with or without |
| 4 | + * modification, are permitted provided that the following conditions |
| 5 | + * are met: |
| 6 | + * * Redistributions of source code must retain the above copyright |
| 7 | + * notice, this list of conditions and the following disclaimer. |
| 8 | + * * Redistributions in binary form must reproduce the above copyright |
| 9 | + * notice, this list of conditions and the following disclaimer in the |
| 10 | + * documentation and/or other materials provided with the distribution. |
| 11 | + * * Neither the name of NVIDIA CORPORATION nor the names of its |
| 12 | + * contributors may be used to endorse or promote products derived |
| 13 | + * from this software without specific prior written permission. |
| 14 | + * |
| 15 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY |
| 16 | + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 18 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 19 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 20 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 22 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 23 | + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 25 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | + */ |
| 27 | + |
| 28 | +#include "helper_multiprocess.h" |
| 29 | +#include <cstdlib> |
| 30 | +#include <string> |
| 31 | + |
| 32 | +int sharedMemoryCreate(const char *name, size_t sz, sharedMemoryInfo *info) |
| 33 | +{ |
| 34 | +#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64) |
| 35 | + info->size = sz; |
| 36 | + info->shmHandle = CreateFileMapping(INVALID_HANDLE_VALUE, |
| 37 | + NULL, |
| 38 | + PAGE_READWRITE, |
| 39 | + 0, |
| 40 | + (DWORD)sz, |
| 41 | + name); |
| 42 | + if (info->shmHandle == 0) { |
| 43 | + return GetLastError(); |
| 44 | + } |
| 45 | + |
| 46 | + info->addr = MapViewOfFile(info->shmHandle, FILE_MAP_ALL_ACCESS, 0, 0, sz); |
| 47 | + if (info->addr == NULL) { |
| 48 | + return GetLastError(); |
| 49 | + } |
| 50 | + |
| 51 | + return 0; |
| 52 | +#else |
| 53 | + int status = 0; |
| 54 | + |
| 55 | + info->size = sz; |
| 56 | + |
| 57 | + info->shmFd = shm_open(name, O_RDWR | O_CREAT, 0777); |
| 58 | + if (info->shmFd < 0) { |
| 59 | + return errno; |
| 60 | + } |
| 61 | + |
| 62 | + status = ftruncate(info->shmFd, sz); |
| 63 | + if (status != 0) { |
| 64 | + return status; |
| 65 | + } |
| 66 | + |
| 67 | + info->addr = mmap(0, sz, PROT_READ | PROT_WRITE, MAP_SHARED, info->shmFd, 0); |
| 68 | + if (info->addr == NULL) { |
| 69 | + return errno; |
| 70 | + } |
| 71 | + |
| 72 | + return 0; |
| 73 | +#endif |
| 74 | +} |
| 75 | + |
| 76 | +int sharedMemoryOpen(const char *name, size_t sz, sharedMemoryInfo *info) |
| 77 | +{ |
| 78 | +#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64) |
| 79 | + info->size = sz; |
| 80 | + |
| 81 | + info->shmHandle = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, name); |
| 82 | + if (info->shmHandle == 0) { |
| 83 | + return GetLastError(); |
| 84 | + } |
| 85 | + |
| 86 | + info->addr = MapViewOfFile(info->shmHandle, FILE_MAP_ALL_ACCESS, 0, 0, sz); |
| 87 | + if (info->addr == NULL) { |
| 88 | + return GetLastError(); |
| 89 | + } |
| 90 | + |
| 91 | + return 0; |
| 92 | +#else |
| 93 | + info->size = sz; |
| 94 | + |
| 95 | + info->shmFd = shm_open(name, O_RDWR, 0777); |
| 96 | + if (info->shmFd < 0) { |
| 97 | + return errno; |
| 98 | + } |
| 99 | + |
| 100 | + info->addr = mmap(0, sz, PROT_READ | PROT_WRITE, MAP_SHARED, info->shmFd, 0); |
| 101 | + if (info->addr == NULL) { |
| 102 | + return errno; |
| 103 | + } |
| 104 | + |
| 105 | + return 0; |
| 106 | +#endif |
| 107 | +} |
| 108 | + |
| 109 | +void sharedMemoryClose(sharedMemoryInfo *info) |
| 110 | +{ |
| 111 | +#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64) |
| 112 | + if (info->addr) { |
| 113 | + UnmapViewOfFile(info->addr); |
| 114 | + } |
| 115 | + if (info->shmHandle) { |
| 116 | + CloseHandle(info->shmHandle); |
| 117 | + } |
| 118 | +#else |
| 119 | + if (info->addr) { |
| 120 | + munmap(info->addr, info->size); |
| 121 | + } |
| 122 | + if (info->shmFd) { |
| 123 | + close(info->shmFd); |
| 124 | + } |
| 125 | +#endif |
| 126 | +} |
| 127 | + |
| 128 | +int spawnProcess(Process *process, const char *app, char * const *args) |
| 129 | +{ |
| 130 | +#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64) |
| 131 | + STARTUPINFO si = {0}; |
| 132 | + BOOL status; |
| 133 | + size_t arglen = 0; |
| 134 | + size_t argIdx = 0; |
| 135 | + std::string arg_string; |
| 136 | + memset(process, 0, sizeof(*process)); |
| 137 | + |
| 138 | + while (*args) { |
| 139 | + arg_string.append(*args).append(1, ' '); |
| 140 | + args++; |
| 141 | + } |
| 142 | + |
| 143 | + status = CreateProcess(app, LPSTR(arg_string.c_str()), NULL, NULL, FALSE, 0, NULL, NULL, &si, process); |
| 144 | + |
| 145 | + return status ? 0 : GetLastError(); |
| 146 | +#else |
| 147 | + *process = fork(); |
| 148 | + if (*process == 0) { |
| 149 | + if (0 > execvp(app, args)) { |
| 150 | + return errno; |
| 151 | + } |
| 152 | + } |
| 153 | + else if (*process < 0) { |
| 154 | + return errno; |
| 155 | + } |
| 156 | + return 0; |
| 157 | +#endif |
| 158 | +} |
| 159 | + |
| 160 | +int waitProcess(Process *process) |
| 161 | +{ |
| 162 | +#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64) |
| 163 | + DWORD exitCode; |
| 164 | + WaitForSingleObject(process->hProcess, INFINITE); |
| 165 | + GetExitCodeProcess(process->hProcess, &exitCode); |
| 166 | + CloseHandle(process->hProcess); |
| 167 | + CloseHandle(process->hThread); |
| 168 | + return (int)exitCode; |
| 169 | +#else |
| 170 | + int status = 0; |
| 171 | + do { |
| 172 | + if (0 > waitpid(*process, &status, 0)) { |
| 173 | + return errno; |
| 174 | + } |
| 175 | + } while (!WIFEXITED(status)); |
| 176 | + return WEXITSTATUS(status); |
| 177 | +#endif |
| 178 | +} |
0 commit comments