10 ABTD_xstream_context *p_ctx = (ABTD_xstream_context *)arg;
11 void *(*thread_f)(
void *) = p_ctx->thread_f;
12 void *p_arg = p_ctx->p_arg;
13 ABTI_ASSERT(p_ctx->state == ABTD_XSTREAM_CONTEXT_STATE_RUNNING);
19 pthread_mutex_lock(&p_ctx->state_lock);
22 if (p_ctx->state == ABTD_XSTREAM_CONTEXT_STATE_REQ_JOIN) {
23 pthread_cond_signal(&p_ctx->state_cond);
25 p_ctx->state = ABTD_XSTREAM_CONTEXT_STATE_WAITING;
30 pthread_cond_wait(&p_ctx->state_cond, &p_ctx->state_lock);
31 }
while (p_ctx->state == ABTD_XSTREAM_CONTEXT_STATE_WAITING);
32 if (p_ctx->state == ABTD_XSTREAM_CONTEXT_STATE_REQ_TERMINATE) {
37 ABTI_ASSERT(p_ctx->state == ABTD_XSTREAM_CONTEXT_STATE_RUNNING ||
38 p_ctx->state == ABTD_XSTREAM_CONTEXT_STATE_REQ_JOIN);
41 pthread_mutex_unlock(&p_ctx->state_lock);
48 ABTU_ret_err int ABTD_xstream_context_create(
void *(*f_xstream)(
void *),
50 ABTD_xstream_context *p_ctx)
52 p_ctx->thread_f = f_xstream;
58 p_ctx->state = ABTD_XSTREAM_CONTEXT_STATE_RUNNING;
59 int ret, init_stage = 0;
60 ret = pthread_mutex_init(&p_ctx->state_lock, NULL);
65 ret = pthread_cond_init(&p_ctx->state_cond, NULL);
70 ret = pthread_create(&p_ctx->native_thread, NULL,
78 if (init_stage >= 2) {
79 ret = pthread_cond_destroy(&p_ctx->state_cond);
80 ABTI_ASSERT(ret == 0);
82 if (init_stage >= 1) {
83 ret = pthread_mutex_destroy(&p_ctx->state_lock);
84 ABTI_ASSERT(ret == 0);
86 p_ctx->state = ABTD_XSTREAM_CONTEXT_STATE_UNINIT;
90 void ABTD_xstream_context_free(ABTD_xstream_context *p_ctx)
93 if (p_ctx->state == ABTD_XSTREAM_CONTEXT_STATE_UNINIT) {
96 pthread_mutex_lock(&p_ctx->state_lock);
97 ABTI_ASSERT(p_ctx->state == ABTD_XSTREAM_CONTEXT_STATE_WAITING);
98 p_ctx->state = ABTD_XSTREAM_CONTEXT_STATE_REQ_TERMINATE;
99 pthread_cond_signal(&p_ctx->state_cond);
100 pthread_mutex_unlock(&p_ctx->state_lock);
103 ret = pthread_join(p_ctx->native_thread, NULL);
104 ABTI_ASSERT(ret == 0);
105 ret = pthread_cond_destroy(&p_ctx->state_cond);
106 ABTI_ASSERT(ret == 0);
107 ret = pthread_mutex_destroy(&p_ctx->state_lock);
108 ABTI_ASSERT(ret == 0);
112 void ABTD_xstream_context_join(ABTD_xstream_context *p_ctx)
115 pthread_mutex_lock(&p_ctx->state_lock);
116 if (p_ctx->state != ABTD_XSTREAM_CONTEXT_STATE_WAITING) {
117 ABTI_ASSERT(p_ctx->state == ABTD_XSTREAM_CONTEXT_STATE_RUNNING);
118 p_ctx->state = ABTD_XSTREAM_CONTEXT_STATE_REQ_JOIN;
121 pthread_cond_wait(&p_ctx->state_cond, &p_ctx->state_lock);
122 }
while (p_ctx->state == ABTD_XSTREAM_CONTEXT_STATE_REQ_JOIN);
124 ABTI_ASSERT(p_ctx->state == ABTD_XSTREAM_CONTEXT_STATE_WAITING);
125 pthread_mutex_unlock(&p_ctx->state_lock);
128 void ABTD_xstream_context_revive(ABTD_xstream_context *p_ctx)
131 pthread_mutex_lock(&p_ctx->state_lock);
132 ABTI_ASSERT(p_ctx->state == ABTD_XSTREAM_CONTEXT_STATE_WAITING);
133 p_ctx->state = ABTD_XSTREAM_CONTEXT_STATE_RUNNING;
134 pthread_cond_signal(&p_ctx->state_cond);
135 pthread_mutex_unlock(&p_ctx->state_lock);
138 void ABTD_xstream_context_set_self(ABTD_xstream_context *p_ctx)
140 p_ctx->native_thread = pthread_self();
143 void ABTD_xstream_context_print(ABTD_xstream_context *p_ctx, FILE *p_os,
147 fprintf(p_os,
"%*s== NULL XSTREAM CONTEXT ==\n", indent,
"");
150 if (p_ctx->state == ABTD_XSTREAM_CONTEXT_STATE_RUNNING) {
152 }
else if (p_ctx->state == ABTD_XSTREAM_CONTEXT_STATE_WAITING) {
154 }
else if (p_ctx->state == ABTD_XSTREAM_CONTEXT_STATE_REQ_JOIN) {
156 }
else if (p_ctx->state == ABTD_XSTREAM_CONTEXT_STATE_REQ_TERMINATE) {
157 state =
"REQ_TERMINATE";
158 }
else if (p_ctx->state == ABTD_XSTREAM_CONTEXT_STATE_UNINIT) {
164 "%*s== XSTREAM CONTEXT (%p) ==\n"
166 indent,
"", (
void *)p_ctx, indent,
"", state);