forked from MaskRay/ccls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
platform_posix.cc
80 lines (68 loc) · 1.82 KB
/
platform_posix.cc
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
// Copyright 2017-2018 ccls Authors
// SPDX-License-Identifier: Apache-2.0
#if defined(__unix__) || defined(__APPLE__) || defined(__HAIKU__)
#include "platform.hh"
#include "utils.hh"
#include <assert.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <signal.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/types.h> // required for stat.h
#include <sys/wait.h>
#include <unistd.h>
#ifdef __GLIBC__
#include <malloc.h>
#endif
#include <llvm/ADT/SmallString.h>
#include <llvm/Support/Path.h>
#include <atomic>
#include <condition_variable>
#include <mutex>
#include <string>
namespace ccls {
namespace pipeline {
void threadEnter();
}
std::string normalizePath(llvm::StringRef path) {
llvm::SmallString<256> p(path);
llvm::sys::path::remove_dots(p, true);
return {p.data(), p.size()};
}
void freeUnusedMemory() {
#ifdef __GLIBC__
malloc_trim(4 * 1024 * 1024);
#endif
}
void traceMe() {
// If the environment variable is defined, wait for a debugger.
// In gdb, you need to invoke `signal SIGCONT` if you want ccls to continue
// after detaching.
const char *traceme = getenv("CCLS_TRACEME");
if (traceme)
raise(traceme[0] == 's' ? SIGSTOP : SIGTSTP);
}
void spawnThread(void *(*fn)(void *), void *arg) {
pthread_t thd;
pthread_attr_t attr;
struct rlimit rlim;
size_t stack_size = 4 * 1024 * 1024;
if (getrlimit(RLIMIT_STACK, &rlim) == 0 && rlim.rlim_cur != RLIM_INFINITY)
stack_size = rlim.rlim_cur;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pthread_attr_setstacksize(&attr, stack_size);
pipeline::threadEnter();
pthread_create(&thd, &attr, fn, arg);
pthread_attr_destroy(&attr);
}
} // namespace ccls
#endif