Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / mgr / Gil.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4  * Ceph - scalable distributed file system
5  *
6  * Copyright (C) 2017 SUSE LLC
7  *
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.
12  *
13  */
14
15 #pragma once
16
17 struct _ts;
18 typedef struct _ts PyThreadState;
19
20 #include <pthread.h>
21
22
23 /**
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.
27  */
28 class SafeThreadState
29 {
30   public:
31   SafeThreadState(PyThreadState *ts_);
32
33   SafeThreadState()
34     : ts(nullptr), thread(0)
35   {
36   }
37
38   PyThreadState *ts;
39   pthread_t thread;
40
41   void set(PyThreadState *ts_)
42   {
43     ts = ts_;
44     thread = pthread_self();
45   }
46 };
47
48 //
49 // Use one of these in any scope in which you need to hold Python's
50 // Global Interpreter Lock.
51 //
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)
54 //
55 // If in doubt, explicitly put a scope around the block of code you
56 // know you need the GIL in.
57 //
58 // See the comment in Gil::Gil for when to set new_thread == true
59 //
60 class Gil {
61 public:
62   Gil(const Gil&) = delete;
63   Gil& operator=(const Gil&) = delete;
64
65   Gil(SafeThreadState &ts, bool new_thread = false);
66   ~Gil();
67
68 private:
69   SafeThreadState &pThreadState;
70   PyThreadState *pNewThreadState = nullptr;
71 };
72