don't use local-file-change context for mapreduce
[hiphop-php.git] / hphp / util / rds-local.cpp
blob2a90d0bbd98690dfb957c9c33497139a894df9e4
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #include "hphp/util/rds-local.h"
19 #include "hphp/util/alloc.h"
21 namespace HPHP {
22 namespace rds {
23 namespace local {
24 namespace detail {
26 RDSLocalNode* head = nullptr;
28 // Hot rds local storage
30 // TODO(T32634221): Add copying logic. During request swap.
31 // This rds local holds the saved hot rds locals. Hot rds locals
32 // are stored directly in thread local storage, and are copied in and out of
33 // this rds local as the request becomes active, or inactive.
35 static RDS_LOCAL(HotRDSLocals, rl_hotBackingStore);
36 alignas(64) __thread HotRDSLocals rl_hotSection = {};
37 uint32_t s_usedbytes = 0;
39 uint32_t RDSLocalNode::s_RDSLocalsBase = 0;
41 Configuration g_config;
42 ///////////////////////////////////////////////////////////////////////////////
45 void RDSInit() {
46 assertx(detail::g_config.rdsInitFunc);
47 detail::RDSLocalNode::s_RDSLocalsBase =
48 detail::g_config.rdsInitFunc(detail::s_usedbytes);
51 void init() {
52 // We may have already been initialized in the case of a thread creating an
53 // RDS segment after we already decided to malloc some memory for RDS locals.
54 // This should not happen on request threads or RDS locals might not be
55 // accessible from the JIT.
56 if (detail::rl_hotSection.rdslocal_base != nullptr) return;
58 assertx(detail::g_config.initFunc);
59 detail::rl_hotSection.rdslocal_base =
60 detail::g_config.initFunc(detail::s_usedbytes,
61 detail::RDSLocalNode::s_RDSLocalsBase);
63 always_assert(detail::rl_hotSection.rdslocal_base);
64 always_assert((uintptr_t)detail::rl_hotSection.rdslocal_base % 16 == 0);
65 detail::iterate([](detail::RDSLocalNode* p) { p->init(); });
68 void fini(bool inrds) {
69 // This may be called twice on threads that create a malloced rdslocal area,
70 // and then initialize a full RDS segment. As the RDS segment is detroyed
71 // Fini is called, and it is called again from the context that created the
72 // malloced area.
73 if (!detail::rl_hotSection.rdslocal_base) return;
75 if (detail::g_config.inRdsFunc &&
76 inrds != detail::g_config.inRdsFunc(detail::rl_hotSection.rdslocal_base,
77 detail::s_usedbytes)) {
78 // There will be another call to deallocate the rds local section.
79 return;
81 detail::iterate([](detail::RDSLocalNode* p) { p->fini(); });
82 if (!inrds) {
83 assertx(detail::g_config.finiFunc);
84 detail::g_config.finiFunc(detail::rl_hotSection.rdslocal_base);
86 detail::rl_hotSection.rdslocal_base = nullptr;
89 }}}