Qore Programming Language - C/C++ Library  0.9.5
qore_thread.h
Go to the documentation of this file.
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  qore_thread.h
4 
5  POSIX thread library for Qore
6 
7  Qore Programming Language
8 
9  Copyright (C) 2003 - 2020 Qore Technologies, s.r.o.
10 
11  Permission is hereby granted, free of charge, to any person obtaining a
12  copy of this software and associated documentation files (the "Software"),
13  to deal in the Software without restriction, including without limitation
14  the rights to use, copy, modify, merge, publish, distribute, sublicense,
15  and/or sell copies of the Software, and to permit persons to whom the
16  Software is furnished to do so, subject to the following conditions:
17 
18  The above copyright notice and this permission notice shall be included in
19  all copies or substantial portions of the Software.
20 
21  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  DEALINGS IN THE SOFTWARE.
28 
29  Note that the Qore library is released under a choice of three open-source
30  licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
31  information.
32 */
33 
34 #ifndef _QORE_QORE_THREAD_H
35 #define _QORE_QORE_THREAD_H
36 
41 #include <cstdio>
42 #include <pthread.h>
43 
44 #include <functional>
45 
46 class QoreProgram;
47 class AbstractQoreZoneInfo;
48 class ThreadCleanupNode;
50 
52 typedef void (*qtdest_t)(void *);
53 
55 typedef void (*qtrdest_t)(void *, ExceptionSink *);
56 
58 typedef void (*q_thread_t)(ExceptionSink* xsink, void* arg);
59 
61 
67 DLLEXPORT bool is_valid_qore_thread();
68 
70 DLLEXPORT int gettid() noexcept;
71 
73 DLLEXPORT QoreProgram* getProgram();
74 
76 DLLEXPORT const AbstractQoreZoneInfo* currentTZ();
77 
79 
82 
84 
88 
90 
94 
96 
99 DLLEXPORT void set_thread_resource(const ResolvedCallReferenceNode* rcr, const QoreValue arg);
100 
102 
108 
109 #if 0
110 
117 DLLEXPORT void set_thread_resource_id(q_trid_t trid, AbstractThreadResource *atr);
118 
120 
125 DLLEXPORT int remove_thread_resource_id(q_trid_t trid);
126 
128 
131 DLLEXPORT bool check_thread_resource_id(q_trid_t trid);
132 #endif
133 
136 
138 
146 private:
147  static ThreadCleanupNode *head;
148 
149 public:
150  DLLLOCAL ThreadCleanupList();
151  DLLLOCAL ~ThreadCleanupList();
152  DLLLOCAL void exec();
153 
155 
158  DLLEXPORT void push(qtdest_t func, void *arg);
159 
161 
163  DLLEXPORT void pop(bool exec = true);
164 };
165 
167 DLLEXPORT extern ThreadCleanupList tclist;
168 
172 #define QFT_OK 0
173 #define QFT_ERROR -1
174 #define QFT_REGISTERED -2
175 
176 
177 
190 
192 
202 
204 
209 
211 
218 
220 
226 DLLEXPORT int q_register_reserved_foreign_thread(int tid);
227 
229 
234 
236 
244 int q_start_thread(ExceptionSink* xsink, q_thread_t f, void* arg = nullptr);
245 
247 
250 private:
252  DLLLOCAL QoreForeignThreadHelper& operator=(const QoreForeignThreadHelper&);
254  DLLLOCAL void* operator new(size_t);
255 
256 protected:
257  class qore_foreign_thread_priv* priv;
258 
259 public:
262 
264  DLLEXPORT explicit QoreForeignThreadHelper(int tid);
265 
268 };
269 
270 typedef std::function<void(void*)> q_thread_local_destructor;
271 
273 
275 struct q_user_tld {
276  void* data;
277  q_thread_local_destructor destructor;
278 
279  DLLLOCAL q_user_tld(void* data, q_thread_local_destructor destructor);
280  DLLLOCAL q_user_tld(q_user_tld&& old) = default;
281  DLLLOCAL q_user_tld(const q_user_tld& old) = default;
282  DLLLOCAL q_user_tld& operator=(const q_user_tld& other) = default;
283 };
284 
286 
289 
291 
295 DLLEXPORT void q_save_thread_local_data(int key, void* data, q_thread_local_destructor destructor = nullptr);
296 
298 
302 DLLEXPORT void* q_swap_thread_local_data(int key, void* new_data, q_thread_local_destructor destructor = nullptr, bool run_destructor = true);
303 
305 
309 DLLEXPORT void* q_get_thread_local_data(int key);
310 
312 
321 
323 
333 DLLEXPORT int q_remove_thread_local_data(int key, q_user_tld& data, bool run_destructor = true);
334 
335 #endif // ifndef _QORE_THREAD_H
q_swap_thread_local_data
DLLEXPORT void * q_swap_thread_local_data(int key, void *new_data, q_thread_local_destructor destructor=nullptr, bool run_destructor=true)
swaps thread-local data
q_start_thread
int q_start_thread(ExceptionSink *xsink, q_thread_t f, void *arg=nullptr)
starts a new thread with the given arguments, when the thread terminates, it deregisters itself
QoreForeignThreadHelper
use this class to temporarily register and deregister a foreign thread to allow Qore code to be execu...
Definition: qore_thread.h:249
check_thread_resource
DLLEXPORT bool check_thread_resource(AbstractThreadResource *atr)
check if a thread resouce has been saved
q_register_foreign_thread
DLLEXPORT int q_register_foreign_thread()
registers the current thread as a Qore thread; returns QFT_OK (0) if the thread was successfully regi...
QoreProgram
supports parsing and executing Qore-language code, reference counted, dynamically-allocated only
Definition: QoreProgram.h:126
QoreValue
The main value class in Qore, designed to be passed by value.
Definition: QoreValue.h:262
ThreadCleanupList
list of functions to be run when a thread ends; required for some external libraries that require exp...
Definition: qore_thread.h:145
qtdest_t
void(* qtdest_t)(void *)
pointer to a qore thread destructor function
Definition: qore_thread.h:52
q_get_thread_local_data_all
DLLEXPORT q_user_tld * q_get_thread_local_data_all(int key)
returns thread-local data
q_release_reserved_foreign_thread_id
DLLEXPORT int q_release_reserved_foreign_thread_id(int tid)
releases a TID reserved with q_reserve_foreign_thread_id()
qtrdest_t
void(* qtrdest_t)(void *, ExceptionSink *)
pointer to a qore thread resource destructor function
Definition: qore_thread.h:55
q_save_thread_local_data
DLLEXPORT void q_save_thread_local_data(int key, void *data, q_thread_local_destructor destructor=nullptr)
saves thread-local data
q_thread_t
void(* q_thread_t)(ExceptionSink *xsink, void *arg)
pointer to a function that can be started with q_start_thread()
Definition: qore_thread.h:58
ThreadCleanupList::pop
DLLEXPORT void pop(bool exec=true)
must only be called in the module destructor/deletion function
q_get_unique_thread_local_data_key
DLLEXPORT int q_get_unique_thread_local_data_key()
returns a unique ID to use with thread_local_data APIs
gettid
DLLEXPORT int gettid() noexcept
returns the current TID number
q_register_reserved_foreign_thread
DLLEXPORT int q_register_reserved_foreign_thread(int tid)
registers a foreign qore thread as a Qore thread with a reserved TID
q_get_thread_local_data
DLLEXPORT void * q_get_thread_local_data(int key)
returns thread-local data
AbstractThreadResource
base class for saving data using Qore's thread resource management system
Definition: AbstractThreadResource.h:51
q_deregister_reserved_foreign_thread
DLLEXPORT int q_deregister_reserved_foreign_thread()
deregisters a foreign qore thread but retains the TID as reserved
remove_thread_resource
DLLEXPORT int remove_thread_resource(AbstractThreadResource *atr)
remove the resource from the thread resource list for the current thread
tclist
DLLEXPORT ThreadCleanupList tclist
the interface to the thread cleanup list
ExceptionSink
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:48
q_reserve_foreign_thread_id
DLLEXPORT int q_reserve_foreign_thread_id()
reserves a thread ID for later registration and returns the TID reserved; use q_release_reserved_fore...
QoreForeignThreadHelper::~QoreForeignThreadHelper
DLLEXPORT ~QoreForeignThreadHelper()
deregisters the current thread if the registration was successful using either q_deregister_foreign_t...
currentTZ
DLLEXPORT const AbstractQoreZoneInfo * currentTZ()
returns the current local time zone, note that if 0 = UTC
is_valid_qore_thread
DLLEXPORT bool is_valid_qore_thread()
returns true if the current thread is a valid qore thread; it is not safe to call most Qore functions...
q_user_tld
data structure for user thread-local data
Definition: qore_thread.h:275
ThreadCleanupList::push
DLLEXPORT void push(qtdest_t func, void *arg)
must only be called in the module initialization function
getProgram
DLLEXPORT QoreProgram * getProgram()
returns the current QoreProgram
ResolvedCallReferenceNode
base class for resolved call references
Definition: CallReferenceNode.h:105
q_deregister_foreign_thread
DLLEXPORT int q_deregister_foreign_thread()
deregisters the current thread as a foreign thread
set_thread_resource
DLLEXPORT void set_thread_resource(AbstractThreadResource *atr)
save a resource against a thread for thread resource handling
QoreForeignThreadHelper::QoreForeignThreadHelper
DLLEXPORT QoreForeignThreadHelper()
registers the current thread as a foreign thread; the thread will be deregistered with q_deregister_f...
q_trid_t
unsigned q_trid_t
type for thread resource IDs (unique within a single running qore library process)
Definition: common.h:463
q_remove_thread_local_data
DLLEXPORT int q_remove_thread_local_data(int key, q_user_tld &data, bool run_destructor=true)
returns and removes thread-local data
qore_get_trid
DLLEXPORT q_trid_t qore_get_trid()
get a thread resource ID