Skip to content

Commit

Permalink
Fix getpagesize() when first used
Browse files Browse the repository at this point in the history
  • Loading branch information
beef9999 authored and lihuiba committed Sep 27, 2024
1 parent 1842e76 commit de6bf57
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 6 deletions.
1 change: 1 addition & 0 deletions include/photon/thread/arch.h
23 changes: 23 additions & 0 deletions thread/arch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
Copyright 2022 The Photon 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.
*/

#include "arch.h"

namespace photon {

uint32_t PAGE_SIZE = 0;

}
26 changes: 26 additions & 0 deletions thread/arch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
Copyright 2022 The Photon 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.
*/

#pragma once

#include <cstdint>

namespace photon {

// Only available after photon::init() or photon::vcpu_init()
extern uint32_t PAGE_SIZE;

}
3 changes: 1 addition & 2 deletions thread/stack-allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ limitations under the License.
#include <errno.h>
#include <photon/common/alog.h>
#include <photon/common/utility.h>
#include <photon/thread/arch.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <unistd.h>
Expand All @@ -28,8 +29,6 @@ limitations under the License.

namespace photon {

const static size_t PAGE_SIZE = getpagesize();

template <size_t MIN_ALLOCATION_SIZE = 4UL * 1024,
size_t MAX_ALLOCATION_SIZE = 64UL * 1024 * 1024>
class PooledStackAllocator {
Expand Down
11 changes: 7 additions & 4 deletions thread/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ inline int posix_memalign(void** memptr, size_t alignment, size_t size) {
#include <photon/common/alog.h>
#include <photon/common/alog-functionptr.h>
#include <photon/thread/thread-key.h>
#include <photon/thread/arch.h>

/* notes on the scheduler:
Expand Down Expand Up @@ -86,8 +87,6 @@ inline int posix_memalign(void** memptr, size_t alignment, size_t size) {
#name": "
#endif

static const size_t PAGE_SIZE = getpagesize();

namespace photon
{
inline uint64_t min(uint64_t a, uint64_t b) { return (a<b) ? a : b; }
Expand Down Expand Up @@ -929,6 +928,8 @@ R"(
stack_size = least_stack_size;
}
char* ptr = (char*)photon_thread_alloc(stack_size);
if (unlikely(!ptr))
return nullptr;
uint64_t p = (uint64_t)ptr + stack_size - sizeof(thread) - randomizer;
p = align_down(p, 64);
auto th = new ((char*)p) thread;
Expand Down Expand Up @@ -1660,8 +1661,8 @@ R"(
int ret = thread_usleep_defer(timeout, q, unlock, m);
auto en = ret < 0 ? errno : 0;
while (true) {
int ret = lock(m);
if (ret == 0) break;
int lock_ret = lock(m);
if (lock_ret == 0) break;
LOG_ERROR("failed to get mutex lock, ` `, try again", VALUE(ret), ERRNO());
thread_usleep(1000, nullptr);
}
Expand Down Expand Up @@ -1884,6 +1885,8 @@ R"(
}

int vcpu_init() {
if (unlikely(PAGE_SIZE == 0))
PAGE_SIZE = getpagesize();
RunQ rq;
if (rq.current) return -1; // re-init has no side-effect
char* ptr = nullptr;
Expand Down

0 comments on commit de6bf57

Please sign in to comment.