Qore Programming Language  0.9.16
StreamBase.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  StreamBase.h
4 
5  Qore Programming Language
6 
7  Copyright (C) 2016 - 2020 Qore Technologies, s.r.o.
8 
9  Permission is hereby granted, free of charge, to any person obtaining a
10  copy of this software and associated documentation files (the "Software"),
11  to deal in the Software without restriction, including without limitation
12  the rights to use, copy, modify, merge, publish, distribute, sublicense,
13  and/or sell copies of the Software, and to permit persons to whom the
14  Software is furnished to do so, subject to the following conditions:
15 
16  The above copyright notice and this permission notice shall be included in
17  all copies or substantial portions of the Software.
18 
19  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25  DEALINGS IN THE SOFTWARE.
26 
27  Note that the Qore library is released under a choice of three open-source
28  licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
29  information.
30 */
31 
32 #ifndef _QORE_STREAMBASE_H
33 #define _QORE_STREAMBASE_H
34 
35 #include "qore/AbstractPrivateData.h"
36 
37 #include <atomic>
38 
39 DLLEXPORT extern QoreClass* QC_STREAMBASE;
40 
45 public:
53  DLLLOCAL bool check(ExceptionSink *xsink) {
54  if (tid.load(std::memory_order_relaxed) != q_gettid()) {
55  xsink->raiseException("STREAM-THREAD-ERROR", "this %s object was created in TID %d; it is an error " \
56  "to access it from any other thread (accessed from TID %d)", getName(),
57  tid.load(std::memory_order_relaxed), q_gettid());
58  return false;
59  }
60  return true;
61  }
62 
68  DLLLOCAL void reassignThread(ExceptionSink *xsink) {
69  // use an atomic compare and exchange to ensure that we only update a stream where the thread is unassigned
70  int chktid = -1;
71  if (!tid.compare_exchange_strong(chktid, q_gettid(), std::memory_order_consume, std::memory_order_relaxed)) {
72  // do not raise an exception if reassigning to the same thread
73  if (chktid != q_gettid()) {
74  xsink->raiseException("STREAM-THREAD-ERROR", "this %s object is assigned to TID %d; it is an error " \
75  "to access it from any other thread (accessed from TID %d)", getName(), chktid, q_gettid());
76  }
77  }
78  }
79 
85  DLLLOCAL void unassignThread(ExceptionSink *xsink) {
86  // use an atomic compare and exchange to ensure that we only update a stream where the thread is assigned to this thread
87  int chktid = q_gettid();
88  if (!tid.compare_exchange_strong(chktid, -1, std::memory_order_release, std::memory_order_relaxed)) {
89  // do not raise an exception if already unassigned
90  if (chktid != -1) {
91  xsink->raiseException("STREAM-THREAD-ERROR", "this %s object is assigned to TID %d; unassignment may " \
92  "be processed from that thread (accessed from TID %d)", getName(), chktid, q_gettid());
93  }
94  }
95  }
96 
100  DLLLOCAL int getThreadId() {
101  return tid.load(std::memory_order_relaxed);
102  }
103 
108  DLLLOCAL virtual const char *getName() = 0;
109 
110 protected:
114  StreamBase() : tid(q_gettid()) {
115  }
116 
117 private:
119 
123  std::atomic<int> tid;
124 };
125 
126 #endif // _QORE_STREAMBASE_H
StreamBase::reassignThread
DLLLOCAL void reassignThread(ExceptionSink *xsink)
Reassigns current thread as thread used for stream manipulation, see check()
Definition: StreamBase.h:68
QoreClass
defines a Qore-language class
Definition: QoreClass.h:239
q_gettid
DLLEXPORT int q_gettid() noexcept
returns the current TID number
StreamBase::getName
virtual const DLLLOCAL char * getName()=0
Returns the name of the class.
StreamBase::unassignThread
DLLLOCAL void unassignThread(ExceptionSink *xsink)
Unassigns current thread as thread used for stream manipulation, see check()
Definition: StreamBase.h:85
StreamBase
Base class for private data of stream implementations in C++.
Definition: StreamBase.h:44
ExceptionSink::raiseException
DLLEXPORT AbstractQoreNode * raiseException(const char *err, const char *fmt,...)
appends a Qore-language exception to the list
ExceptionSink
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:48
StreamBase::getThreadId
DLLLOCAL int getThreadId()
Get currently assigned thread id.
Definition: StreamBase.h:100
AbstractPrivateData
the base class for all data to be used as private data of Qore objects
Definition: AbstractPrivateData.h:44
StreamBase::StreamBase
StreamBase()
Constructor.
Definition: StreamBase.h:114
StreamBase::check
DLLLOCAL bool check(ExceptionSink *xsink)
Checks that the current thread is the same as when the instance was created or assigned via unassignT...
Definition: StreamBase.h:53