Qore Programming Language - C/C++ Library  0.8.13.2
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 - 2017 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 <stdio.h>
42 #include <pthread.h>
43 
44 class QoreProgram;
45 class AbstractQoreZoneInfo;
46 class ThreadCleanupNode;
48 
50 typedef void (*qtdest_t)(void *);
51 
53 typedef void (*qtrdest_t)(void *, ExceptionSink *);
54 
56 typedef void (*q_thread_t)(ExceptionSink* xsink, void* arg);
57 
59 
65 DLLEXPORT bool is_valid_qore_thread();
66 
68 DLLEXPORT int gettid();
69 
71 DLLEXPORT QoreProgram* getProgram();
72 
74 DLLEXPORT const AbstractQoreZoneInfo* currentTZ();
75 
77 
79 DLLEXPORT void set_thread_resource(AbstractThreadResource* atr);
80 
82 
86 
88 
92 
94 
96 DLLEXPORT void set_thread_resource(const ResolvedCallReferenceNode* rcr, QoreValue arg);
97 
99 
104 DLLEXPORT int remove_thread_resource(const ResolvedCallReferenceNode* rcr, ExceptionSink* xsink);
105 
106 #if 0
107 
114 DLLEXPORT void set_thread_resource_id(q_trid_t trid, AbstractThreadResource *atr);
115 
117 
122 DLLEXPORT int remove_thread_resource_id(q_trid_t trid);
123 
125 
128 DLLEXPORT bool check_thread_resource_id(q_trid_t trid);
129 #endif
130 
132 DLLEXPORT q_trid_t qore_get_trid();
133 
135 
143 private:
144  static ThreadCleanupNode *head;
145 
146 public:
147  DLLLOCAL ThreadCleanupList();
148  DLLLOCAL ~ThreadCleanupList();
149  DLLLOCAL void exec();
150 
152 
155  DLLEXPORT void push(qtdest_t func, void *arg);
156 
158 
160  DLLEXPORT void pop(bool exec = true);
161 };
162 
164 DLLEXPORT extern ThreadCleanupList tclist;
165 
169 #define QFT_OK 0
170 #define QFT_ERROR -1
171 #define QFT_REGISTERED -2
172 
173 
174 
186 DLLEXPORT int q_register_foreign_thread();
187 
189 
198 DLLEXPORT int q_deregister_foreign_thread();
199 
201 
205 DLLEXPORT int q_reserve_foreign_thread_id();
206 
208 
214 DLLEXPORT int q_release_reserved_foreign_thread_id(int tid);
215 
217 
223 DLLEXPORT int q_register_reserved_foreign_thread(int tid);
224 
226 
231 
233 
241 int q_start_thread(ExceptionSink* xsink, q_thread_t f, void* arg = 0);
242 
244 
247 private:
249  DLLLOCAL QoreForeignThreadHelper& operator=(const QoreForeignThreadHelper&);
251  DLLLOCAL void* operator new(size_t);
252 
253 protected:
254  class qore_foreign_thread_priv* priv;
255 
256 public:
258  DLLEXPORT QoreForeignThreadHelper();
259 
261  DLLEXPORT explicit QoreForeignThreadHelper(int tid);
262 
264  DLLEXPORT ~QoreForeignThreadHelper();
265 };
266 
267 #endif // ifndef _QORE_THREAD_H
void(* qtdest_t)(void *)
pointer to a qore thread destructor function
Definition: qore_thread.h:50
base class for saving data using Qore&#39;s thread resource management system
Definition: AbstractThreadResource.h:51
DLLEXPORT int q_reserve_foreign_thread_id()
reserves a thread ID for later registration and returns the TID reserved; use q_release_reserved_fore...
DLLEXPORT int gettid()
returns the current TID number
DLLEXPORT void pop(bool exec=true)
must only be called in the module destructor/deletion function
DLLEXPORT bool check_thread_resource(AbstractThreadResource *atr)
check if a thread resouce has been saved
DLLEXPORT ThreadCleanupList tclist
the interface to the thread cleanup list
DLLEXPORT void set_thread_resource(AbstractThreadResource *atr)
save a resource against a thread for thread resource handling
DLLEXPORT void push(qtdest_t func, void *arg)
must only be called in the module initialization function
DLLEXPORT int remove_thread_resource(AbstractThreadResource *atr)
remove the resource from the thread resource list for the current thread
DLLEXPORT QoreProgram * getProgram()
returns the current QoreProgram
DLLEXPORT int q_deregister_foreign_thread()
deregisters the current thread as a foreign thread
DLLEXPORT int q_release_reserved_foreign_thread_id(int tid)
releases a TID reserved with q_reserve_foreign_thread_id()
void(* qtrdest_t)(void *, ExceptionSink *)
pointer to a qore thread resource destructor function
Definition: qore_thread.h:53
unsigned q_trid_t
type for thread resource IDs (unique within a single running qore library process) ...
Definition: common.h:538
The main value class in Qore, designed to be passed by value.
Definition: QoreValue.h:112
supports parsing and executing Qore-language code, reference counted, dynamically-allocated only ...
Definition: QoreProgram.h:117
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:47
list of functions to be run when a thread ends; required for some external libraries that require exp...
Definition: qore_thread.h:142
void(* q_thread_t)(ExceptionSink *xsink, void *arg)
pointer to a function that can be started with q_start_thread()
Definition: qore_thread.h:56
base class for resolved call references
Definition: CallReferenceNode.h:130
DLLEXPORT int q_register_foreign_thread()
registers the current thread as a Qore thread; returns QFT_OK (0) if the thread was successfully regi...
use this class to temporarily register and deregister a foreign thread to allow Qore code to be execu...
Definition: qore_thread.h:246
int q_start_thread(ExceptionSink *xsink, q_thread_t f, void *arg=0)
starts a new thread with the given arguments, when the thread terminates, it deregisters itself ...
DLLEXPORT q_trid_t qore_get_trid()
get a thread resource ID
DLLEXPORT int q_deregister_reserved_foreign_thread()
deregisters a foreign qore thread but retains the TID as reserved
DLLEXPORT int q_register_reserved_foreign_thread(int tid)
registers a foreign qore thread as a Qore thread with a reserved TID
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...
DLLEXPORT const AbstractQoreZoneInfo * currentTZ()
returns the current local time zone, note that if 0 = UTC