blob: e82f17d0f3fd307724a7f1b3ffb0454cd449219e [file] [log] [blame]
Matthew Treinish0db53772013-07-26 10:39:35 -04001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
3# Copyright 2011 OpenStack Foundation.
4# All Rights Reserved.
5#
6# Licensed under the Apache License, Version 2.0 (the "License"); you may
7# not use this file except in compliance with the License. You may obtain
8# a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15# License for the specific language governing permissions and limitations
16# under the License.
17
Matthew Treinishffa94d62013-09-11 18:09:17 +000018"""Local storage of variables using weak references"""
Matthew Treinish0db53772013-07-26 10:39:35 -040019
Matthew Treinishffa94d62013-09-11 18:09:17 +000020import threading
Matthew Treinish0db53772013-07-26 10:39:35 -040021import weakref
22
Matthew Treinish0db53772013-07-26 10:39:35 -040023
Matthew Treinishffa94d62013-09-11 18:09:17 +000024class WeakLocal(threading.local):
Matthew Treinish0db53772013-07-26 10:39:35 -040025 def __getattribute__(self, attr):
Matthew Treinishffa94d62013-09-11 18:09:17 +000026 rval = super(WeakLocal, self).__getattribute__(attr)
Matthew Treinish0db53772013-07-26 10:39:35 -040027 if rval:
28 # NOTE(mikal): this bit is confusing. What is stored is a weak
29 # reference, not the value itself. We therefore need to lookup
30 # the weak reference and return the inner value here.
31 rval = rval()
32 return rval
33
34 def __setattr__(self, attr, value):
35 value = weakref.ref(value)
Matthew Treinishffa94d62013-09-11 18:09:17 +000036 return super(WeakLocal, self).__setattr__(attr, value)
Matthew Treinish0db53772013-07-26 10:39:35 -040037
38
39# NOTE(mikal): the name "store" should be deprecated in the future
40store = WeakLocal()
41
42# A "weak" store uses weak references and allows an object to fall out of scope
43# when it falls out of scope in the code that uses the thread local storage. A
44# "strong" store will hold a reference to the object so that it never falls out
45# of scope.
46weak_store = WeakLocal()
Matthew Treinishffa94d62013-09-11 18:09:17 +000047strong_store = threading.local()