Skip to content

Commit 379ff4c

Browse files
committed
Optimize swoole.h, remove sched.h/<sys/time.h> includes. Remove gettimeofday and use C++ chrono instead
1 parent 6d32ce8 commit 379ff4c

File tree

7 files changed

+54
-77
lines changed

7 files changed

+54
-77
lines changed

‎ext-src/swoole_runtime.cc‎

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1501,7 +1501,7 @@ static void hook_pdo_driver(uint32_t flags) {
15011501
}
15021502
} else {
15031503
if (runtime_hook_flags & PHPCoroutine::HOOK_PDO_FIREBIRD) {
1504-
swoole_firebird_set_blocking(true);
1504+
swoole_firebird_set_blocking(true);
15051505
}
15061506
}
15071507
#endif
@@ -1809,9 +1809,11 @@ static PHP_FUNCTION(swoole_sleep) {
18091809

18101810
static PHP_FUNCTION(swoole_usleep) {
18111811
zend_long num;
1812-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &num) == FAILURE) {
1813-
RETURN_FALSE;
1814-
}
1812+
1813+
ZEND_PARSE_PARAMETERS_START(1, 1)
1814+
Z_PARAM_LONG(num)
1815+
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
1816+
18151817
if (num < 0) {
18161818
php_error_docref(nullptr, E_WARNING, "Number of seconds must be greater than or equal to 0");
18171819
RETURN_FALSE;
@@ -1826,9 +1828,11 @@ static PHP_FUNCTION(swoole_usleep) {
18261828

18271829
static PHP_FUNCTION(swoole_time_nanosleep) {
18281830
zend_long tv_sec, tv_nsec;
1829-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ll", &tv_sec, &tv_nsec) == FAILURE) {
1830-
RETURN_FALSE;
1831-
}
1831+
1832+
ZEND_PARSE_PARAMETERS_START(2, 2)
1833+
Z_PARAM_LONG(tv_sec)
1834+
Z_PARAM_LONG(tv_nsec)
1835+
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
18321836

18331837
if (tv_sec < 0) {
18341838
php_error_docref(nullptr, E_WARNING, "The seconds value must be greater than 0");
@@ -1861,41 +1865,34 @@ static PHP_FUNCTION(swoole_time_nanosleep) {
18611865

18621866
static PHP_FUNCTION(swoole_time_sleep_until) {
18631867
double d_ts;
1864-
timeval tm;
1865-
timespec php_req, php_rem;
18661868

1867-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "d", &d_ts) == FAILURE) {
1868-
RETURN_FALSE;
1869-
}
1869+
ZEND_PARSE_PARAMETERS_START(1, 1)
1870+
Z_PARAM_DOUBLE(d_ts)
1871+
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
18701872

1871-
if (gettimeofday(&tm, nullptr) != 0) {
1873+
if (!std::isfinite(d_ts) || d_ts < 0) {
1874+
php_error_docref(nullptr, E_WARNING, "Invalid timestamp: must be a positive finite number");
18721875
RETURN_FALSE;
18731876
}
18741877

1875-
double c_ts = d_ts - tm.tv_sec - tm.tv_usec / 1000000.00;
1876-
if (c_ts < 0) {
1877-
php_error_docref(nullptr, E_WARNING, "Sleep until to time is less than current time");
1878-
RETURN_FALSE;
1879-
}
1878+
using namespace std::chrono;
1879+
using dseconds = duration<double>;
1880+
1881+
const auto now = system_clock::now();
1882+
const auto target_duration = duration_cast<system_clock::duration>(dseconds(d_ts));
1883+
const auto target_time = system_clock::time_point(target_duration);
18801884

1881-
php_req.tv_sec = (time_t) c_ts;
1882-
if (php_req.tv_sec > c_ts) {
1883-
php_req.tv_sec--;
1885+
if (target_time <= now) {
1886+
php_error_docref(NULL, E_WARNING, "Argument #1 ($timestamp) must be greater than or equal to the current time");
1887+
RETURN_FALSE;
18841888
}
1885-
php_req.tv_nsec = (long) ((c_ts - php_req.tv_sec) * 1000000000.00);
18861889

1887-
double _time = (double) php_req.tv_sec + (double) php_req.tv_nsec / 1000000000.00;
1890+
const auto sleep_duration = target_time - now;
18881891
if (Coroutine::get_current()) {
1889-
System::sleep(_time);
1892+
const double sleep_seconds = duration_cast<dseconds>(sleep_duration).count();
1893+
System::sleep(sleep_seconds);
18901894
} else {
1891-
while (nanosleep(&php_req, &php_rem)) {
1892-
if (errno == EINTR) {
1893-
php_req.tv_sec = php_rem.tv_sec;
1894-
php_req.tv_nsec = php_rem.tv_nsec;
1895-
} else {
1896-
RETURN_FALSE;
1897-
}
1898-
}
1895+
std::this_thread::sleep_until(target_time);
18991896
}
19001897
RETURN_TRUE;
19011898
}

‎include/swoole.h‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@
3131
#define SW_EXTERN_C_END
3232
#endif
3333

34-
#ifndef _GNU_SOURCE
34+
#ifndef _POSIX_C_SOURCE
35+
#define _POSIX_C_SOURCE 200809L
36+
#endif
37+
38+
#if defined(__linux__) && !defined(_GNU_SOURCE)
3539
#define _GNU_SOURCE
3640
#endif
3741

@@ -49,13 +53,11 @@
4953
#include <cstring>
5054
#include <climits>
5155
#include <unistd.h>
52-
#include <sched.h> /* sched_yield() */
5356
#include <pthread.h>
5457
#include <inttypes.h>
5558

5659
#include <sys/uio.h>
5760
#include <sys/utsname.h>
58-
#include <sys/time.h>
5961

6062
#include <string>
6163
#include <memory>
@@ -492,9 +494,6 @@ enum swTraverseOperation {
492494
SW_TRAVERSE_STOP = 2,
493495
};
494496

495-
//-------------------------------------------------------------------------------
496-
#define sw_yield() sched_yield()
497-
498497
//------------------------------Base--------------------------------
499498
#ifndef uchar
500499
typedef unsigned char uchar;

‎include/swoole_util.h‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <cstdio>
2121
#include <cstdarg>
2222
#include <cassert>
23+
#include <ctime>
2324

2425
#include <string>
2526
#include <memory>
@@ -72,14 +73,11 @@ static inline long time(bool steady = false) {
7273
}
7374

7475
static inline long get_timezone() {
75-
#ifdef __linux__
76-
return timezone;
77-
#else
78-
struct timezone tz;
79-
struct timeval tv;
80-
gettimeofday(&tv, &tz);
81-
return tz.tz_minuteswest * 60;
82-
#endif
76+
using namespace std::chrono;
77+
auto now_time_t = system_clock::to_time_t(system_clock::now());
78+
std::tm local_tm{};
79+
localtime_r(&now_time_t, &local_tm);
80+
return local_tm.tm_gmtoff;
8381
}
8482

8583
class DeferTask {

‎src/core/base.cc‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#include <list>
3131
#include <set>
32+
#include <chrono>
3233
#include <random>
3334

3435
#include "swoole_string.h"
@@ -956,8 +957,7 @@ std::string intersection(const std::vector<std::string> &vec1, std::set<std::str
956957
}
957958

958959
double microtime() {
959-
timeval t;
960-
gettimeofday(&t, nullptr);
961-
return (double) t.tv_sec + ((double) t.tv_usec / 1000000);
960+
using namespace std::chrono;
961+
return duration_cast<duration<double>>(system_clock::now().time_since_epoch()).count();
962962
}
963963
}; // namespace swoole

‎src/core/misc.cc‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
#include "swoole.h"
1818

19+
#include <thread>
20+
1921
void sw_spinlock(sw_atomic_t *lock) {
2022
uint32_t i, n;
2123
while (true) {
@@ -33,7 +35,7 @@ void sw_spinlock(sw_atomic_t *lock) {
3335
}
3436
}
3537
}
36-
sw_yield();
38+
std::this_thread::yield();
3739
}
3840
}
3941

‎src/memory/table.cc‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include "swoole_hash.h"
1919
#include "swoole_util.h"
2020

21+
#include <thread>
22+
2123
namespace swoole {
2224

2325
Table *Table::make(uint32_t rows_size, float conflict_proportion) {
@@ -263,7 +265,7 @@ void TableRow::lock() {
263265
swoole_warning("timeout, force unlock");
264266
goto _success;
265267
}
266-
sw_yield();
268+
std::this_thread::yield();
267269
}
268270
}
269271

‎src/os/timer.cc‎

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "swoole_signal.h"
1919

2020
#include <csignal>
21+
#include <sys/time.h>
2122

2223
namespace swoole {
2324
static int SystemTimer_set(Timer *timer, long next_msec);
@@ -29,34 +30,12 @@ void Timer::init_with_system_timer() {
2930
}
3031

3132
static int SystemTimer_set(Timer *timer, long next_msec) {
32-
itimerval timer_set;
33-
timeval now;
34-
if (gettimeofday(&now, nullptr) < 0) {
35-
swoole_sys_warning("gettimeofday() failed");
36-
return SW_ERR;
37-
}
38-
33+
itimerval timer_set{};
3934
if (next_msec > 0) {
40-
int sec = next_msec / 1000;
41-
int msec = next_msec % 1000;
42-
timer_set.it_interval.tv_sec = sec;
43-
timer_set.it_interval.tv_usec = msec * 1000;
44-
timer_set.it_value.tv_sec = sec;
45-
timer_set.it_value.tv_usec = timer_set.it_interval.tv_usec;
46-
47-
if (timer_set.it_value.tv_usec > 1e6) {
48-
timer_set.it_value.tv_usec = timer_set.it_value.tv_usec - 1e6;
49-
timer_set.it_value.tv_sec += 1;
50-
}
51-
} else {
52-
timer_set = {};
53-
}
54-
55-
if (setitimer(ITIMER_REAL, &timer_set, nullptr) < 0) {
56-
swoole_sys_warning("setitimer() failed");
57-
return SW_ERR;
35+
timer_set.it_interval = {next_msec / 1000, (next_msec % 1000) * 1000};
36+
timer_set.it_value = timer_set.it_interval;
5837
}
59-
return SW_OK;
38+
return setitimer(ITIMER_REAL, &timer_set, nullptr) < 0 ? SW_ERR : SW_OK;
6039
}
6140

6241
void realtime_get(timespec *time) {

0 commit comments

Comments
 (0)