2018-08-21 05:27:52 +00:00
|
|
|
/* Copyright 2017-2018 ccls Authors
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
==============================================================================*/
|
|
|
|
|
2017-12-17 20:40:21 +00:00
|
|
|
#if defined(__unix__) || defined(__APPLE__)
|
2018-10-29 04:21:21 +00:00
|
|
|
#include "platform.hh"
|
2017-03-04 01:45:20 +00:00
|
|
|
|
2018-10-29 04:21:21 +00:00
|
|
|
#include "utils.hh"
|
2017-03-04 01:45:20 +00:00
|
|
|
|
|
|
|
#include <assert.h>
|
2017-09-22 01:14:57 +00:00
|
|
|
#include <limits.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
2017-04-18 17:21:53 +00:00
|
|
|
#include <dirent.h>
|
2018-05-13 16:52:19 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2018-10-02 16:34:55 +00:00
|
|
|
#include <pthread.h>
|
2018-05-13 16:52:19 +00:00
|
|
|
#include <signal.h>
|
2018-10-02 16:34:55 +00:00
|
|
|
#include <sys/resource.h>
|
2017-04-20 16:47:24 +00:00
|
|
|
#include <sys/stat.h>
|
2018-08-09 17:08:14 +00:00
|
|
|
#include <sys/types.h> // required for stat.h
|
2018-02-20 00:19:57 +00:00
|
|
|
#include <sys/wait.h>
|
2018-08-09 17:08:14 +00:00
|
|
|
#include <unistd.h>
|
2018-05-13 16:52:19 +00:00
|
|
|
#ifdef __GLIBC__
|
2017-12-23 16:01:43 +00:00
|
|
|
#include <malloc.h>
|
2017-10-31 22:43:07 +00:00
|
|
|
#endif
|
|
|
|
|
2018-10-15 08:26:13 +00:00
|
|
|
#include <llvm/ADT/SmallString.h>
|
|
|
|
#include <llvm/Support/Path.h>
|
2017-11-30 18:40:42 +00:00
|
|
|
|
2018-12-14 04:13:35 +00:00
|
|
|
#include <atomic>
|
|
|
|
#include <condition_variable>
|
|
|
|
#include <mutex>
|
2018-10-15 08:26:13 +00:00
|
|
|
#include <string>
|
2017-11-30 18:40:42 +00:00
|
|
|
|
2018-12-14 04:13:35 +00:00
|
|
|
namespace ccls {
|
|
|
|
namespace pipeline {
|
|
|
|
void ThreadEnter();
|
|
|
|
}
|
|
|
|
|
2018-08-09 17:08:14 +00:00
|
|
|
std::string NormalizePath(const std::string &path) {
|
2018-10-15 08:26:13 +00:00
|
|
|
llvm::SmallString<256> P(path);
|
|
|
|
llvm::sys::path::remove_dots(P, true);
|
|
|
|
return {P.data(), P.size()};
|
2017-03-28 01:47:12 +00:00
|
|
|
}
|
|
|
|
|
2017-08-17 18:02:47 +00:00
|
|
|
void FreeUnusedMemory() {
|
2018-05-13 16:52:19 +00:00
|
|
|
#ifdef __GLIBC__
|
2018-09-23 19:10:40 +00:00
|
|
|
malloc_trim(4 * 1024 * 1024);
|
2017-10-31 22:43:07 +00:00
|
|
|
#endif
|
2017-08-17 18:02:47 +00:00
|
|
|
}
|
|
|
|
|
2018-01-07 07:42:42 +00:00
|
|
|
void TraceMe() {
|
|
|
|
// If the environment variable is defined, wait for a debugger.
|
2018-03-31 03:16:33 +00:00
|
|
|
// In gdb, you need to invoke `signal SIGCONT` if you want ccls to continue
|
2018-01-07 07:42:42 +00:00
|
|
|
// after detaching.
|
2018-08-09 17:08:14 +00:00
|
|
|
const char *traceme = getenv("CCLS_TRACEME");
|
2018-04-30 04:49:03 +00:00
|
|
|
if (traceme)
|
|
|
|
raise(traceme[0] == 's' ? SIGSTOP : SIGTSTP);
|
2018-01-07 07:42:42 +00:00
|
|
|
}
|
|
|
|
|
2018-10-02 16:34:55 +00:00
|
|
|
void SpawnThread(void *(*fn)(void *), void *arg) {
|
|
|
|
pthread_t thd;
|
|
|
|
pthread_attr_t attr;
|
|
|
|
struct rlimit rlim;
|
|
|
|
size_t stack_size = 4 * 1024 * 1024;
|
2018-12-14 04:13:35 +00:00
|
|
|
if (getrlimit(RLIMIT_STACK, &rlim) == 0 && rlim.rlim_cur != RLIM_INFINITY)
|
2018-10-02 16:34:55 +00:00
|
|
|
stack_size = rlim.rlim_cur;
|
|
|
|
pthread_attr_init(&attr);
|
|
|
|
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
|
|
|
pthread_attr_setstacksize(&attr, stack_size);
|
2018-12-14 04:13:35 +00:00
|
|
|
pipeline::ThreadEnter();
|
2018-10-02 16:34:55 +00:00
|
|
|
pthread_create(&thd, &attr, fn, arg);
|
|
|
|
pthread_attr_destroy(&attr);
|
|
|
|
}
|
2018-12-14 04:13:35 +00:00
|
|
|
} // namespace ccls
|
2018-10-02 16:34:55 +00:00
|
|
|
|
2017-03-21 17:06:22 +00:00
|
|
|
#endif
|