1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2017 SUSE LLC
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
18 typedef struct _ts PyThreadState;
24 * Wrap PyThreadState to carry a record of which POSIX thread
25 * the thread state relates to. This allows the Gil class to
26 * validate that we're being used from the right thread.
31 SafeThreadState(PyThreadState *ts_);
34 : ts(nullptr), thread(0)
41 void set(PyThreadState *ts_)
44 thread = pthread_self();
49 // Use one of these in any scope in which you need to hold Python's
50 // Global Interpreter Lock.
52 // Do *not* nest these, as a second GIL acquire will deadlock (see
53 // https://docs.python.org/2/c-api/init.html#c.PyEval_RestoreThread)
55 // If in doubt, explicitly put a scope around the block of code you
56 // know you need the GIL in.
58 // See the comment in Gil::Gil for when to set new_thread == true
62 Gil(const Gil&) = delete;
63 Gil& operator=(const Gil&) = delete;
65 Gil(SafeThreadState &ts, bool new_thread = false);
69 SafeThreadState &pThreadState;
70 PyThreadState *pNewThreadState = nullptr;