Skip to content

Commit de6bf57

Browse files
beef9999lihuiba
authored andcommitted
Fix getpagesize() when first used
1 parent 1842e76 commit de6bf57

File tree

5 files changed

+58
-6
lines changed

5 files changed

+58
-6
lines changed

include/photon/thread/arch.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../thread/arch.h

thread/arch.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
Copyright 2022 The Photon Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
#include "arch.h"
18+
19+
namespace photon {
20+
21+
uint32_t PAGE_SIZE = 0;
22+
23+
}

thread/arch.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
Copyright 2022 The Photon Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
#pragma once
18+
19+
#include <cstdint>
20+
21+
namespace photon {
22+
23+
// Only available after photon::init() or photon::vcpu_init()
24+
extern uint32_t PAGE_SIZE;
25+
26+
}

thread/stack-allocator.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ limitations under the License.
2020
#include <errno.h>
2121
#include <photon/common/alog.h>
2222
#include <photon/common/utility.h>
23+
#include <photon/thread/arch.h>
2324
#include <stdlib.h>
2425
#include <sys/mman.h>
2526
#include <unistd.h>
@@ -28,8 +29,6 @@ limitations under the License.
2829

2930
namespace photon {
3031

31-
const static size_t PAGE_SIZE = getpagesize();
32-
3332
template <size_t MIN_ALLOCATION_SIZE = 4UL * 1024,
3433
size_t MAX_ALLOCATION_SIZE = 64UL * 1024 * 1024>
3534
class PooledStackAllocator {

thread/thread.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ inline int posix_memalign(void** memptr, size_t alignment, size_t size) {
5151
#include <photon/common/alog.h>
5252
#include <photon/common/alog-functionptr.h>
5353
#include <photon/thread/thread-key.h>
54+
#include <photon/thread/arch.h>
5455

5556
/* notes on the scheduler:
5657
@@ -86,8 +87,6 @@ inline int posix_memalign(void** memptr, size_t alignment, size_t size) {
8687
#name": "
8788
#endif
8889

89-
static const size_t PAGE_SIZE = getpagesize();
90-
9190
namespace photon
9291
{
9392
inline uint64_t min(uint64_t a, uint64_t b) { return (a<b) ? a : b; }
@@ -929,6 +928,8 @@ R"(
929928
stack_size = least_stack_size;
930929
}
931930
char* ptr = (char*)photon_thread_alloc(stack_size);
931+
if (unlikely(!ptr))
932+
return nullptr;
932933
uint64_t p = (uint64_t)ptr + stack_size - sizeof(thread) - randomizer;
933934
p = align_down(p, 64);
934935
auto th = new ((char*)p) thread;
@@ -1660,8 +1661,8 @@ R"(
16601661
int ret = thread_usleep_defer(timeout, q, unlock, m);
16611662
auto en = ret < 0 ? errno : 0;
16621663
while (true) {
1663-
int ret = lock(m);
1664-
if (ret == 0) break;
1664+
int lock_ret = lock(m);
1665+
if (lock_ret == 0) break;
16651666
LOG_ERROR("failed to get mutex lock, ` `, try again", VALUE(ret), ERRNO());
16661667
thread_usleep(1000, nullptr);
16671668
}
@@ -1884,6 +1885,8 @@ R"(
18841885
}
18851886

18861887
int vcpu_init() {
1888+
if (unlikely(PAGE_SIZE == 0))
1889+
PAGE_SIZE = getpagesize();
18871890
RunQ rq;
18881891
if (rq.current) return -1; // re-init has no side-effect
18891892
char* ptr = nullptr;

0 commit comments

Comments
 (0)