Adaptive Framework  0.9.0
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
afw_thread.c
Go to the documentation of this file.
1 // See the 'COPYING' file in the project root for licensing information.
2 /*
3  * Adaptive Framework thread support.
4  *
5  * Copyright (c) 2010-2023 Clemson University
6  *
7  */
8 
14 #include "afw_internal.h"
15 
16 
17 
20  const afw_pool_t *p, afw_xctx_t *xctx)
21 {
22  apr_threadattr_t *attr;
23 
24  apr_threadattr_create(&attr, afw_pool_get_apr_pool(p));
25 
26  return (afw_thread_attr_t *)attr;
27 }
28 
29 
30 static void* APR_THREAD_FUNC
31 impl_thread_start(apr_thread_t *thd, void *data)
32 {
33  afw_thread_t *self = data;
34 
35  return self->start_function(self, self->start_function_arg);
36 }
37 
38 
39 
40 AFW_DEFINE(const afw_thread_t *)
42  afw_thread_attr_t *thread_attr,
43  afw_thread_function_t start_function,
44  void *start_function_arg,
45  const afw_utf8_t *name,
46  afw_integer_t thread_number,
47  afw_xctx_t *xctx)
48 {
49  afw_thread_t *self;
50  apr_status_t rv;
51 
52  self = afw_pool_internal_create_thread(-1, xctx);
53  self->thread_attr = thread_attr;
54  self->start_function = start_function;
55  self->start_function_arg = start_function_arg;
56  self->name = name;
57  self->thread_number = thread_number;
58  self->xctx = afw_xctx_internal_create_thread_xctx(self, xctx);
59 
60  rv = apr_thread_create(&self->apr_thread, (apr_threadattr_t *)thread_attr,
61  impl_thread_start, self, afw_pool_get_apr_pool(self->p));
62  if (rv != APR_SUCCESS) {
63  AFW_THROW_ERROR_RV_Z(general, apr, rv,
64  "apr_thread_create() failed", xctx);
65  }
66 
67  return self;
68 }
69 
70 AFW_DEFINE(void)
72  const afw_thread_t *thread,
73  afw_xctx_t *xctx)
74 {
75  apr_status_t rv, rv2;
76 
77  rv = apr_thread_join(&rv2, thread->apr_thread);
78  if (rv != APR_SUCCESS) {
79  AFW_THROW_ERROR_RV_Z(general, apr, rv,
80  "apr_thread_join() failed", xctx);
81  }
82  if (rv2 != APR_SUCCESS) {
83  AFW_THROW_ERROR_RV_Z(general, apr, rv2,
84  "apr_thread_join() dead thread error", xctx);
85  }
86 }
AFW_DEFINE(const afw_object_t *)
Adaptive Framework Core Internal.
void *(AFW_THREAD_FUNCTION * afw_thread_function_t)(const afw_thread_t *thread, void *arg)
Typedef for thread start function.
Definition: afw_common.h:1371
struct afw_thread_attr_s afw_thread_attr_t
Typedef for afw_thread_attr.
Definition: afw_common.h:1374
apr_int64_t afw_integer_t
typedef for big signed int.
Definition: afw_common.h:321
#define AFW_THROW_ERROR_RV_Z(code, rv_source_id, rv, message_z, xctx)
Macro used to set error and rv in xctx and throw it.
Definition: afw_error.h:301
#define afw_pool_get_apr_pool(instance)
Call method get_apr_pool of interface afw_pool.
afw_thread_join(const afw_thread_t *thread, afw_xctx_t *xctx)
Join a thread.
Definition: afw_thread.c:71
afw_thread_create(afw_thread_attr_t *thread_attr, afw_thread_function_t start_function, void *start_function_arg, const afw_utf8_t *name, afw_integer_t thread_number, afw_xctx_t *xctx)
Create a thread.
Definition: afw_thread.c:41
afw_thread_attr_create(const afw_pool_t *p, afw_xctx_t *xctx)
Create a thread attr.
Definition: afw_thread.c:19
Interface afw_pool public struct.
Struct for public part of afw_pool_t.
Definition: afw_thread.h:56
afw_thread_function_t start_function
The function called when the thread starts.
Definition: afw_thread.h:60
NFC normalized UTF-8 string.
Definition: afw_common.h:545
Interface afw_xctx public struct.