Skip to content

Commit 4a6cde0

Browse files
committed
vcap/testcard2: allow _done to be called from _init
Allow partial destroy - moved some inits at the beginning + join the thread only if started.
1 parent 2c9a500 commit 4a6cde0

1 file changed

Lines changed: 18 additions & 8 deletions

File tree

src/video_capture/testcard2.c

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,12 @@ enum {
108108
(struct video_desc){ 1920, 1080, UYVY, 24, PROGRESSIVE, 1 }
109109
#define MOD_NAME "[testcard2] "
110110

111+
// compat
112+
#ifndef PTHREAD_NULL // defined by POSIX v8
113+
#define PTHREAD_NULL ((pthread_t) { 0 })
114+
#endif
111115

116+
static void vidcap_testcard2_done(void *state);
112117
void * vidcap_testcard2_thread(void *args);
113118

114119
struct testcard_state2 {
@@ -323,10 +328,14 @@ static int vidcap_testcard2_init(struct vidcap_params *params, void **state)
323328
return VIDCAP_INIT_FAIL;
324329
}
325330
s->desc = DEFAULT_FORMAT;
331+
s->thread_id = PTHREAD_NULL;
332+
platform_sem_init(&s->semaphore, 0, 0);
333+
pthread_mutex_init(&s->lock, NULL);
334+
pthread_cond_init(&s->data_consumed_cv, NULL);
326335
#ifdef HAVE_LIBSDL_TTF
327336
s->sdl_font = get_sdl_render_font();
328337
if (s->sdl_font == NULL) {
329-
free(s);
338+
vidcap_testcard2_done(s);
330339
return VIDCAP_INIT_FAIL;
331340
}
332341
#endif
@@ -341,14 +350,14 @@ static int vidcap_testcard2_init(struct vidcap_params *params, void **state)
341350
}
342351
free(fmt);
343352
if (!ret) {
344-
free(s);
353+
vidcap_testcard2_done(s);
345354
return VIDCAP_INIT_FAIL;
346355
}
347356

348357
if (s->desc.width <= 0 || s->desc.height <= 0) {
349358
log_msg(LOG_LEVEL_ERROR, MOD_NAME "Wrong video size, given: %dx%d\n",
350359
s->desc.width, s->desc.height);
351-
free(s);
360+
vidcap_testcard2_done(s);
352361
return VIDCAP_INIT_FAIL;
353362
}
354363

@@ -386,13 +395,9 @@ static int vidcap_testcard2_init(struct vidcap_params *params, void **state)
386395
s->seconds_tone_played = 0.0;
387396
s->play_audio_frame = 0;
388397

389-
platform_sem_init(&s->semaphore, 0, 0);
390398
printf("Testcard capture set to %dx%d\n", s->desc.width, s->desc.height);
391399

392400
gettimeofday(&s->start_time, NULL);
393-
394-
pthread_mutex_init(&s->lock, NULL);
395-
pthread_cond_init(&s->data_consumed_cv, NULL);
396401

397402
pthread_create(&s->thread_id, NULL, vidcap_testcard2_thread, s);
398403

@@ -411,7 +416,12 @@ static void vidcap_testcard2_done(void *state)
411416
pthread_mutex_unlock(&s->lock);
412417
pthread_cond_signal(&s->data_consumed_cv);
413418

414-
pthread_join(s->thread_id, NULL);
419+
pthread_t null_thread_id;
420+
memset(&null_thread_id, 0, sizeof null_thread_id); // for safe memcmp
421+
null_thread_id = PTHREAD_NULL;
422+
if (memcmp(&s->thread_id, &null_thread_id, sizeof s->thread_id) != 0) {
423+
pthread_join(s->thread_id, NULL);
424+
}
415425

416426
pthread_mutex_destroy(&s->lock);
417427
pthread_cond_destroy(&s->data_consumed_cv);

0 commit comments

Comments
 (0)