Skip to content

Commit

Permalink
[WIP] 30% supporting windows
Browse files Browse the repository at this point in the history
  • Loading branch information
radkesvat committed May 29, 2024
1 parent 04aa5ec commit bbc2585
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 7 deletions.
5 changes: 5 additions & 0 deletions ww/buffer_pool.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "buffer_pool.h"
#ifdef OS_UNIX
#include <malloc.h>
#endif
#ifdef DEBUG
#include "loggers/network_logger.h"
#endif
Expand Down Expand Up @@ -61,7 +63,10 @@ static void giveMemBackToOs(buffer_pool_t *pool)
#ifdef DEBUG
LOGD("BufferPool: freed %d buffers, %zu are in use", decrease, pool->in_use);
#endif
#ifdef OS_UNIX
malloc_trim(0);
#endif

}

shift_buffer_t *popBuffer(buffer_pool_t *pool)
Expand Down
9 changes: 8 additions & 1 deletion ww/eventloop/base/hlog.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,11 @@ logger_t* logger_create(void) {
// init gmtoff here
time_t ts = time(NULL);
static _Thread_local struct tm local_tm;
#ifdef OS_UNIX
localtime_r(&ts,&local_tm);

#else
localtime_s(&local_tm,&ts);
#endif
int local_hour = local_tm.tm_hour;
struct tm* gmt_tm = gmtime(&ts);
int gmt_hour = gmt_tm->tm_hour;
Expand Down Expand Up @@ -206,7 +209,11 @@ const char* logger_get_cur_file(logger_t* logger) {

static void logfile_name(const char* filepath, time_t ts, char* buf, int len) {
static _Thread_local struct tm tm;
#ifdef OS_UNIX
localtime_r(&ts,&tm);
#else
localtime_s(&tm,&ts);
#endif
snprintf(buf, len, "%s.%04d%02d%02d.log",
filepath,
tm.tm_year+1900,
Expand Down
8 changes: 8 additions & 0 deletions ww/eventloop/base/hlsem.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,11 @@ static bool SemaSignal(hsem_t* sp, uint32_t count) {
#define LSEMA_MAX_SPINS 10000

static bool _LSemaWaitPartialSpin(hlsem_t* s, uint64_t timeout_usecs) {
#ifdef OS_UNIX
ssize_t oldCount;
#else
int oldCount;
#endif
int spin = LSEMA_MAX_SPINS;
while (--spin >= 0) {
oldCount = atomic_load_explicit(&s->count, memory_order_relaxed);
Expand Down Expand Up @@ -257,7 +261,11 @@ bool LSemaWait(hlsem_t* s) {
}

bool LSemaTryWait(hlsem_t* s) {
#ifdef OS_UNIX
ssize_t oldCount = atomic_load_explicit(&s->count, memory_order_relaxed);
#else
int oldCount = atomic_load_explicit(&s->count, memory_order_relaxed);
#endif
while (oldCount > 0) {
if (atomic_compare_exchange_weak_explicit(&s->count, &oldCount, oldCount - 1, memory_order_acquire, memory_order_relaxed)) {
return true;
Expand Down
9 changes: 6 additions & 3 deletions ww/eventloop/base/hmutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,20 @@ BEGIN_EXTERN_C
#define HONCE_INIT INIT_ONCE_STATIC_INIT
typedef void (*honce_fn)();
static inline BOOL WINAPI s_once_func(INIT_ONCE* once, PVOID arg, PVOID* _) {
honce_fn fn = (honce_fn)arg;
(void)once;
(void)_;
honce_fn fn = NULL;
*(void **) (&fn) = arg;
fn();
return TRUE;
}
static inline void honce(honce_t* once, honce_fn fn) {
PVOID dummy = NULL;
InitOnceExecuteOnce(once, s_once_func, (PVOID)fn, &dummy);
InitOnceExecuteOnce(once, s_once_func, *(void **) (&fn), &dummy);
}

#define hsem_t HANDLE
#define hsem_init(psem, value) *(psem) = CreateSemaphore(NULL, value, value+100000, NULL)
#define hsem_init(psem, value) (*(psem) = CreateSemaphore(NULL, value, value+100000, NULL))
#define hsem_destroy(psem) CloseHandle(*(psem))
#define hsem_wait(psem) WaitForSingleObject(*(psem), INFINITE)
#define hsem_post(psem) ReleaseSemaphore(*(psem), 1, NULL)
Expand Down
4 changes: 4 additions & 0 deletions ww/eventloop/base/hsocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ HV_INLINE int tcp_nopush(int sockfd, int on DEFAULT(1)) {
#elif defined(TCP_CORK)
return setsockopt(sockfd, IPPROTO_TCP, TCP_CORK, (const char*)&on, sizeof(int));
#else
(void)sockfd;
(void)on;
return 0;
#endif
}
Expand Down Expand Up @@ -259,6 +261,8 @@ HV_INLINE int so_reuseport(int sockfd, int on DEFAULT(1)) {
// NOTE: SO_REUSEPORT allow multiple sockets to bind same port
return setsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT, (const char*)&on, sizeof(int));
#else
(void) sockfd;
(void) on;
return 0;
#endif
}
Expand Down
14 changes: 13 additions & 1 deletion ww/eventloop/base/htime.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "htime.h"
#define __STDC_WANT_LIB_EXT1__ 1
#include <time.h>

static const char* s_weekdays[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
Expand Down Expand Up @@ -75,7 +76,11 @@ datetime_t datetime_now(void) {

datetime_t datetime_localtime(time_t seconds) {
static _Thread_local struct tm tm;
#ifdef OS_UNIX
localtime_r(&seconds,&tm);
#else
localtime_s(&tm,&seconds);
#endif
datetime_t dt;
dt.year = tm.tm_year + 1900;
dt.month = tm.tm_mon + 1;
Expand All @@ -91,8 +96,11 @@ time_t datetime_mktime(datetime_t* dt) {
time_t ts;
time(&ts);
static _Thread_local struct tm ptm;
#ifdef OS_UNIX
localtime_r(&ts,&ptm);

#else
localtime_s(&ptm,&ts);
#endif
memcpy(&tm, &ptm, sizeof(struct tm));
tm.tm_year = dt->year - 1900;
tm.tm_mon = dt->month - 1;
Expand Down Expand Up @@ -232,7 +240,11 @@ time_t cron_next_timeout(int minute, int hour, int day, int week, int month) {
time_t tt;
time(&tt);
static _Thread_local struct tm tm;
#ifdef OS_UNIX
localtime_r(&tt,&tm);
#else
localtime_s(&tm,&tt);
#endif
time_t tt_round = 0;

tm.tm_sec = 0;
Expand Down
5 changes: 5 additions & 0 deletions ww/managers/socket_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,8 @@ static void onAcceptTcpSinglePort(hio_t *io)

static void onAcceptTcpMultiPort(hio_t *io)
{
#ifdef OS_UNIX

unsigned char pbuf[28] = {0};
socklen_t size = 16; // todo ipv6 value is 28
if (getsockopt(hio_fd(io), SOL_IP, kSoOriginalDest, &(pbuf[0]), &size) < 0)
Expand All @@ -390,6 +392,9 @@ static void onAcceptTcpMultiPort(hio_t *io)
}

distributeTcpSocket(io, (pbuf[2] << 8) | pbuf[3]);
#else
onAcceptTcpSinglePort(io);
#endif
}

static multiport_backend_t getDefaultMultiPortBackend(void)
Expand Down
4 changes: 2 additions & 2 deletions ww/utils/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -334,11 +334,11 @@ hash_t sockAddrCalcHash(const sockaddr_u *saddr)
// paddings are 0
if (saddr->sa.sa_family == AF_INET)
{
return CALC_HASH_BYTES(&(saddr->sin.sin_port), sizeof(struct sockaddr_in) + sizeof(in_port_t));
return CALC_HASH_BYTES(&(saddr->sin.sin_port), sizeof(struct sockaddr_in) + sizeof(uint16_t));
}
if (saddr->sa.sa_family == AF_INET6)
{
return CALC_HASH_BYTES(&(saddr->sin6.sin6_port), sizeof(struct sockaddr_in6) + sizeof(in_port_t));
return CALC_HASH_BYTES(&(saddr->sin6.sin6_port), sizeof(struct sockaddr_in6) + sizeof(uint16_t));
}
return CALC_HASH_BYTES(&(saddr->sa), (sockaddr_len((sockaddr_u *) saddr)));
}
Expand Down
7 changes: 7 additions & 0 deletions ww/ww.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,25 @@ struct ww_runtime_state_s *getWW(void)
return state;
}

// trimming should not be necessary, using it for test purposes
// todo (remove) should be removed ?
#ifdef OS_UNIX
void idleFreeMem(htimer_t *timer)
{
(void) timer;
malloc_trim(0);
}
#endif

// just because timer is considered "possibly lost" pointer
htimer_t *trim_timer = NULL;

_Noreturn void runMainThread(void)
{
#ifdef OS_UNIX
trim_timer = htimer_add_period(loops[0], idleFreeMem, 2, 0, 0, 0, 0, INFINITE);
#endif

hloop_run(loops[0]);
hloop_free(&loops[0]);
for (size_t i = 1; i < workers_count; i++)
Expand Down

0 comments on commit bbc2585

Please sign in to comment.