2 STATIC
void thdsws_run(struct thdsws_ctx_t
*ctx
)
5 struct thdsws_ctx_private_t
*ctx_priv
;
7 ctx_priv
= ctx
->private;
10 if (ctx_priv
->state
== RUNNING
)
11 fatal("the worker thread is already running, did you forget to wait for it to idle\n");
12 ctx_priv
->state
= RUNNING
;
13 state_unlock(ctx_priv
);
14 r
= pthread_cond_signal(&ctx_priv
->have_fr_to_scale
);
16 fatal("unable to signal the worker thread a frame is to be scaled\n");
20 STATIC
bool thdsws_is_busy(struct thdsws_ctx_t
*ctx
)
22 struct thdsws_ctx_private_t
*ctx_priv
;
25 ctx_priv
= ctx
->private;
27 if (ctx_priv
->state
== RUNNING
)
31 state_unlock(ctx_priv
);
36 STATIC
struct thdsws_ctx_t
*thdsws_init_once(void)
41 struct thdsws_ctx_t
*ctx
;
42 struct thdsws_ctx_private_t
*ctx_priv
;
44 ctx
= calloc(1, sizeof(*ctx
));
46 fatal("unable to allocate memory for context\n");
47 ctx_priv
= calloc(1, sizeof(*ctx_priv
));
49 fatal("unable to allocate memory for private context\n");
50 ctx
->private = ctx_priv
;
52 r
= pthread_mutex_init(&ctx_priv
->mutex
, 0);
54 fatal("unable to create the state mutex\n");
55 r
= pthread_cond_init(&ctx_priv
->have_fr_to_scale
, 0);
57 fatal("unable to create waiting condition\n");
59 r
= pthread_attr_init(&attr
);
61 fatal("unable to initialize a worker thread attribute\n");
63 r
= pthread_attr_setdetachstate(&attr
, PTHREAD_CREATE_DETACHED
);
65 fatal("unable to set the worker thread attribute to detach mode\n");
67 /* be really sure the entry state is correct _before_ thd creation */
68 ctx_priv
->state
= IDLE
;
69 r
= pthread_create(&worker
, &attr
, &worker_entry
, ctx
);
71 fatal("unable to create the worker thread\n");
72 pthread_attr_destroy(&attr
);
76 #define TIME_UNIT_NS 1000000 /* 1ms */
77 #define TIMEOUT_UNITS_N 100 /* 100 * 1 ms = 100 ms */
78 STATIC
void thdsws_wait_for_idle(struct thdsws_ctx_t
*ctx
)
86 if (!thdsws_is_busy(ctx
))
89 ts
.tv_nsec
= TIME_UNIT_NS
;
94 memset(&rem
, 0, sizeof(rem
));
96 r
= nanosleep(&ts
, &rem
);
100 if (errno
== EINTR
) {
101 memcpy(&ts
, &rem
, sizeof(ts
));
104 fatal("unable to sleep to wait for idle\n");
107 if (nanoloops_n
== TIMEOUT_UNITS_N
)
108 fatal("wait for idle timeout\n");
112 #undef TIMEOUT_UNITS_N