mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
Support multiple threads by thread pool. v5.0.32
This commit is contained in:
parent
7c6bd0ce5c
commit
b2e083b00d
12 changed files with 479 additions and 104 deletions
|
@ -687,79 +687,6 @@ srs_error_t SrsServer::initialize_signal()
|
|||
return err;
|
||||
}
|
||||
|
||||
srs_error_t SrsServer::acquire_pid_file()
|
||||
{
|
||||
srs_error_t err = srs_success;
|
||||
|
||||
// when srs in dolphin mode, no need the pid file.
|
||||
if (_srs_config->is_dolphin()) {
|
||||
return srs_success;
|
||||
}
|
||||
|
||||
std::string pid_file = _srs_config->get_pid_file();
|
||||
|
||||
// Try to create dir for pid file.
|
||||
string pid_dir = srs_path_dirname(pid_file);
|
||||
if (!srs_path_exists(pid_dir)) {
|
||||
if ((err = srs_create_dir_recursively(pid_dir)) != srs_success) {
|
||||
return srs_error_wrap(err, "create %s", pid_dir.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
// -rw-r--r--
|
||||
// 644
|
||||
int mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
|
||||
|
||||
int fd;
|
||||
// open pid file
|
||||
if ((fd = ::open(pid_file.c_str(), O_WRONLY | O_CREAT, mode)) == -1) {
|
||||
return srs_error_new(ERROR_SYSTEM_PID_ACQUIRE, "open pid file=%s", pid_file.c_str());
|
||||
}
|
||||
|
||||
// require write lock
|
||||
struct flock lock;
|
||||
|
||||
lock.l_type = F_WRLCK; // F_RDLCK, F_WRLCK, F_UNLCK
|
||||
lock.l_start = 0; // type offset, relative to l_whence
|
||||
lock.l_whence = SEEK_SET; // SEEK_SET, SEEK_CUR, SEEK_END
|
||||
lock.l_len = 0;
|
||||
|
||||
if (fcntl(fd, F_SETLK, &lock) == -1) {
|
||||
if(errno == EACCES || errno == EAGAIN) {
|
||||
::close(fd);
|
||||
srs_error("srs is already running!");
|
||||
return srs_error_new(ERROR_SYSTEM_PID_ALREADY_RUNNING, "srs is already running");
|
||||
}
|
||||
return srs_error_new(ERROR_SYSTEM_PID_LOCK, "access to pid=%s", pid_file.c_str());
|
||||
}
|
||||
|
||||
// truncate file
|
||||
if (ftruncate(fd, 0) != 0) {
|
||||
return srs_error_new(ERROR_SYSTEM_PID_TRUNCATE_FILE, "truncate pid file=%s", pid_file.c_str());
|
||||
}
|
||||
|
||||
// write the pid
|
||||
string pid = srs_int2str(getpid());
|
||||
if (write(fd, pid.c_str(), pid.length()) != (int)pid.length()) {
|
||||
return srs_error_new(ERROR_SYSTEM_PID_WRITE_FILE, "write pid=%s to file=%s", pid.c_str(), pid_file.c_str());
|
||||
}
|
||||
|
||||
// auto close when fork child process.
|
||||
int val;
|
||||
if ((val = fcntl(fd, F_GETFD, 0)) < 0) {
|
||||
return srs_error_new(ERROR_SYSTEM_PID_GET_FILE_INFO, "fcntl fd=%d", fd);
|
||||
}
|
||||
val |= FD_CLOEXEC;
|
||||
if (fcntl(fd, F_SETFD, val) < 0) {
|
||||
return srs_error_new(ERROR_SYSTEM_PID_SET_FILE_INFO, "lock file=%s fd=%d", pid_file.c_str(), fd);
|
||||
}
|
||||
|
||||
srs_trace("write pid=%s to %s success!", pid.c_str(), pid_file.c_str());
|
||||
pid_fd = fd;
|
||||
|
||||
return srs_success;
|
||||
}
|
||||
|
||||
srs_error_t SrsServer::listen()
|
||||
{
|
||||
srs_error_t err = srs_success;
|
||||
|
@ -1637,10 +1564,6 @@ srs_error_t SrsServerAdapter::run(SrsWaitGroup* wg)
|
|||
return srs_error_wrap(err, "initialize st");
|
||||
}
|
||||
|
||||
if ((err = srs->acquire_pid_file()) != srs_success) {
|
||||
return srs_error_wrap(err, "acquire pid file");
|
||||
}
|
||||
|
||||
if ((err = srs->initialize_signal()) != srs_success) {
|
||||
return srs_error_wrap(err, "initialize signal");
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue