UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
thread.c
Go to the documentation of this file.
1
5/*
6 * Copyright (c) 2019 CESNET, z. s. p. o.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, is permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * 3. Neither the name of CESNET nor the names of its contributors may be
21 * used to endorse or promote products derived from this software without
22 * specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
26 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
27 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
28 * EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
35 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#ifdef HAVE_CONFIG_H
39#include "config.h"
40#endif
41
42#include <libgen.h>
43#ifndef _WIN32
44#include <pthread.h>
45#endif
46#ifdef HAVE_SETTHREADDESCRIPTION
47#include <processthreadsapi.h>
48#endif
49#include <stdlib.h>
50#include <string.h>
51
52#include "debug.h"
53#include "host.h"
54#include "utils/thread.h"
55
56#if ! defined _WIN32 || defined HAVE_SETTHREADDESCRIPTION
57static inline char *get_argv_program_name(void) {
58#ifdef HAVE_CONFIG_H
59 if (uv_argv != NULL && uv_argv[0] != NULL) {
60 char *prog_name = (char *) malloc(strlen(uv_argv[0]) + 2);
61 strcpy(prog_name, uv_argv[0]);
62 char *name = basename(prog_name);
63 memmove(prog_name, name, strlen(name) + 1);
64 strcat(prog_name, "-");
65 return prog_name;
66 }
67#endif // defined HAVE_CONFIG_H
68 return strdup("uv-");
69}
70#endif
71
72void set_thread_name(const char *name) {
73#ifdef __linux__
74// thread name can have at most 16 chars (including terminating null char)
75 char *prog_name = get_argv_program_name();
76 char tmp[16];
77 tmp[sizeof tmp - 1] = '\0';
78 strncpy(tmp, prog_name, sizeof tmp - 1);
79 free(prog_name);
80 strncat(tmp, name, sizeof tmp - strlen(tmp) - 1);
81 pthread_setname_np(pthread_self(), tmp);
82#elif defined __APPLE__
83 char *prog_name = get_argv_program_name();
84 char *tmp = (char *) alloca(strlen(prog_name) + strlen(name) + 1);
85 strcpy(tmp, prog_name);
86 free(prog_name);
87 strcat(tmp, name);
88 pthread_setname_np(tmp);
89#elif defined _WIN32
90// supported from Windows 10, not yet in headers
91#ifdef HAVE_SETTHREADDESCRIPTION
92 const char *prog_name = get_argv_program_name();
93 size_t dst_len = (mbstowcs(NULL, prog_name, 0) + mbstowcs(NULL, name, 0) + 1) * sizeof(wchar_t);
94 wchar_t *tmp = (wchar_t *) alloca(dst_len);
95 mbstowcs(tmp, prog_name, dst_len / sizeof(wchar_t));
96 mbstowcs(tmp + wcslen(tmp), name, dst_len / sizeof(wchar_t) - wcslen(tmp));
97 SetThreadDescription(GetCurrentThread(), tmp);
98#else
99 UNUSED(name);
100#endif
101#endif
102}
103
#define UNUSED(x)
char ** uv_argv
Definition host.cpp:174
const char * name
Definition settings.cpp:193
static char * get_argv_program_name(void)
Definition thread.c:57
void set_thread_name(const char *name)
Definition thread.c:72